KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > chain > CreateAction


1 /*
2  * Copyright 2003,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.struts.chain;
18
19
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import org.apache.commons.chain.Command;
23 import org.apache.commons.chain.Context;
24 import org.apache.commons.chain.web.WebContext;
25 import org.apache.struts.action.Action;
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.chain.Constants;
28 import org.apache.struts.chain.util.ClassUtils;
29 import org.apache.struts.config.ActionConfig;
30 import org.apache.struts.config.ModuleConfig;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34
35 /**
36  * <p>Create (if necessary) and cache an <code>Action</code> for this request.
37  * </p>
38  *
39  * @author Craig R. McClanahan
40  * @version $Rev: 54933 $ $Date: 2004-10-16 18:04:52 +0100 (Sat, 16 Oct 2004) $
41  */

42
43 public class CreateAction implements Command {
44
45
46     // ------------------------------------------------------ Instance Variables
47
private static final Log log = LogFactory.getLog(CreateAction.class);
48
49     private String JavaDoc actionKey = Constants.ACTION_KEY;
50     private String JavaDoc actionConfigKey = Constants.ACTION_CONFIG_KEY;
51     private String JavaDoc actionServletKey = Constants.ACTION_SERVLET_KEY;
52     private String JavaDoc validKey = Constants.VALID_KEY;
53
54
55     // -------------------------------------------------------------- Properties
56

57
58     /**
59      * <p>Return the context attribute key under which the
60      * <code>Action</code> for the currently selected application
61      * action is stored.</p>
62      */

63     public String JavaDoc getActionKey() {
64
65         return (this.actionKey);
66
67     }
68
69
70     /**
71      * <p>Set the context attribute key under which the
72      * <code>Action</code> for the currently selected application
73      * action is stored.</p>
74      *
75      * @param actionKey The new context attribute key
76      */

77     public void setActionKey(String JavaDoc actionKey) {
78
79         this.actionKey = actionKey;
80
81     }
82
83
84     /**
85      * <p>Return the context attribute key under which the
86      * <code>ActionConfig</code> for the currently selected application
87      * action is stored.</p>
88      */

89     public String JavaDoc getActionConfigKey() {
90
91         return (this.actionConfigKey);
92
93     }
94
95
96     /**
97      * <p>Set the context attribute key under which the
98      * <code>ActionConfig</code> for the currently selected application
99      * action is stored.</p>
100      *
101      * @param actionConfigKey The new context attribute key
102      */

103     public void setActionConfigKey(String JavaDoc actionConfigKey) {
104
105         this.actionConfigKey = actionConfigKey;
106
107     }
108
109
110     /**
111      * <p>Return the context attribute key under which the
112      * <code>ActionServlet</code> for the current web application
113      * is stored.</p>
114      */

115     public String JavaDoc getActionServletKey() {
116
117         return (this.actionServletKey);
118
119     }
120
121
122     /**
123      * <p>Set the context attribute key under which the
124      * <code>ActionServlet</code> for the current web application
125      * is stored.</p>
126      *
127      * @param actionServletKey The new context attribute key
128      */

129     public void setActionServletKey(String JavaDoc actionServletKey) {
130
131         this.actionServletKey = actionServletKey;
132
133     }
134
135
136     /**
137      * <p>Return the context attribute key under which the
138      * validity flag for this request is stored.</p>
139      */

140     public String JavaDoc getValidKey() {
141
142         return (this.validKey);
143
144     }
145
146
147     /**
148      * <p>Set the context attribute key under which the
149      * validity flag for this request is stored.</p>
150      *
151      * @param validKey The new context attribute key
152      */

153     public void setValidKey(String JavaDoc validKey) {
154
155         this.validKey = validKey;
156
157     }
158
159
160     // ---------------------------------------------------------- Public Methods
161

162
163     /**
164      * <p>Create (if necessary) and cache an <code>Action</code> for this
165      * request.</p>
166      *
167      * @param context The <code>Context</code> for the current request
168      *
169      * @return <code>false</code> so that processing continues
170      */

171     public boolean execute(Context context) throws Exception JavaDoc {
172
173         // Skip processing if the current request is not valid
174
Boolean JavaDoc valid = (Boolean JavaDoc) context.get(getValidKey());
175         if ((valid == null) || !valid.booleanValue()) {
176             return (false);
177         }
178         
179         // Check to see if an action has already been created
180
if (context.get(getActionKey()) != null) {
181             return (false);
182         }
183
184         // Look up the class name for the desired Action
185
ActionConfig actionConfig = (ActionConfig)
186             context.get(getActionConfigKey());
187         String JavaDoc type = actionConfig.getType();
188
189         if (type == null) {
190             return (false);
191         }
192
193         // Create (if necessary) and cache an Action instance
194
Action action = null;
195         Map JavaDoc actions = getActions(context, actionConfig.getModuleConfig());
196         synchronized (actions) {
197             action = (Action) actions.get(type);
198             if (action == null) {
199                 log.info("Initialize action of type: " + type + " for actionConfig " + actionConfig);
200                 action = (Action) ClassUtils.getApplicationInstance(type);
201                 ActionServlet actionServlet = (ActionServlet)
202                     context.get(getActionServletKey());
203                 action.setServlet(actionServlet);
204                 actions.put(type, action);
205             }
206         }
207         context.put(getActionKey(), action);
208
209         return (false);
210
211     }
212
213
214     // ------------------------------------------------------- Protected Methods
215

216
217     /**
218      * <p>Create (if necessary) and return a <code>Map</code> containing the
219      * <code>Action</code> instances for the current application module.</p>
220      *
221      * @param context The context for this request
222      * @param moduleConfig The <code>ModuleConfig</code> for the current
223      * application module
224      */

225     protected synchronized Map JavaDoc getActions(Context context,
226                                           ModuleConfig moduleConfig) {
227
228         WebContext wcontext = (WebContext) context;
229         String JavaDoc actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
230         Map JavaDoc actions = (Map JavaDoc) wcontext.getApplicationScope().get(actionsKey);
231         if (actions == null) {
232             actions = new HashMap JavaDoc();
233             wcontext.getApplicationScope().put(actionKey, actions);
234         }
235         return (actions);
236
237     }
238
239
240
241 }
242
Popular Tags