View Javadoc

1   /*
2    * $Id: RemoveCachedMessages.java 471754 2006-11-06 14:55:09Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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)
48          throws Exception {
49  
50          // Get session scope
51          Map session = actionCtx.getSessionScope();
52  
53          // Remove messages as needed
54          removeAccessedMessages(session, Globals.MESSAGE_KEY);
55  
56          // Remove error messages as needed
57          removeAccessedMessages(session, Globals.ERROR_KEY);
58  
59          return false;
60      }
61  
62      /**
63       * <p>Removes any <code>ActionMessages</code> object from the specified
64       * scope stored under the specified key if the messages'
65       * <code>isAccessed</code> method returns true.
66       *
67       * @param scope The scope to check for messages in.
68       * @param key The key the messages are stored under.
69       */
70      private void removeAccessedMessages(Map scope, String key) {
71          ActionMessages messages = (ActionMessages)scope.get(key);
72          if (messages != null && messages.isAccessed()) {
73              scope.remove(key);
74          }
75      }
76  }