View Javadoc

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