01: /*
02: * $Id: RemoveCachedMessages.java 471754 2006-11-06 14:55:09Z husted $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21: package org.apache.struts.chain.commands;
22:
23: import java.util.Map;
24: import org.apache.struts.Globals;
25: import org.apache.struts.chain.contexts.ActionContext;
26: import org.apache.struts.action.ActionMessages;
27:
28: /**
29: * <p>Remove cached messages stored in the session.</p>
30: *
31: * @version $Id: RemoveCachedMessages.java 471754 2006-11-06 14:55:09Z husted $
32: * @since Struts 1.3.5
33: */
34: public class RemoveCachedMessages extends ActionCommandBase {
35:
36: /**
37: * <p>Removes any <code>ActionMessages</code> object stored in the session
38: * under <code>Globals.MESSAGE_KEY</code> and <code>Globals.ERROR_KEY</code>
39: * if the messages' <code>isAccessed</code> method returns true. This
40: * allows messages to be stored in the session, displayed one time, and be
41: * released here.</p>
42: *
43: * @param actionCtx The <code>Context</code> for the current request
44: * @return <code>false</code> so that processing continues
45: * @throws Exception on any error
46: */
47: public boolean execute(ActionContext actionCtx) throws Exception {
48:
49: // Get session scope
50: Map session = actionCtx.getSessionScope();
51:
52: // Remove messages as needed
53: removeAccessedMessages(session, Globals.MESSAGE_KEY);
54:
55: // Remove error messages as needed
56: removeAccessedMessages(session, Globals.ERROR_KEY);
57:
58: return false;
59: }
60:
61: /**
62: * <p>Removes any <code>ActionMessages</code> object from the specified
63: * scope stored under the specified key if the messages'
64: * <code>isAccessed</code> method returns true.
65: *
66: * @param scope The scope to check for messages in.
67: * @param key The key the messages are stored under.
68: */
69: private void removeAccessedMessages(Map scope, String key) {
70: ActionMessages messages = (ActionMessages) scope.get(key);
71: if (messages != null && messages.isAccessed()) {
72: scope.remove(key);
73: }
74: }
75: }
|