1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.chain.commands.servlet;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
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.commands.util.ClassUtils;
29 import org.apache.struts.chain.contexts.ActionContext;
30 import org.apache.struts.chain.contexts.ServletActionContext;
31 import org.apache.struts.config.ActionConfig;
32 import org.apache.struts.config.ModuleConfig;
33
34 import java.util.HashMap;
35 import java.util.Map;
36
37 /**
38 * <p>Concrete implementation of <code>AbstractCreateAction</code> for use in
39 * a Servlet API chain. Expects that the ActionContext passed into it can
40 * safely be cast to <code>ServletActionContext</code>.</p>
41 */
42 public class CreateAction
43 extends org.apache.struts.chain.commands.AbstractCreateAction {
44
45 private static final Log log = LogFactory.getLog(CreateAction.class);
46
47
48
49
50 protected synchronized Action getAction(ActionContext context, String type,
51 ActionConfig actionConfig)
52 throws Exception {
53 ModuleConfig moduleConfig = actionConfig.getModuleConfig();
54 String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
55 Map actions = (Map) context.getApplicationScope().get(actionsKey);
56
57 if (actions == null) {
58 actions = new HashMap();
59 context.getApplicationScope().put(actionsKey, actions);
60 }
61
62 Action action = null;
63
64 synchronized (actions) {
65 action = (Action) actions.get(type);
66
67 if (action == null) {
68 action = createAction(context, type);
69 actions.put(type, action);
70 }
71 }
72
73 if (action.getServlet() == null) {
74 ServletActionContext saContext = (ServletActionContext) context;
75 ActionServlet actionServlet = saContext.getActionServlet();
76
77 action.setServlet(actionServlet);
78 }
79
80 return (action);
81 }
82
83
84 /**
85 * <p>Invoked by <code>getAction</code> when the <code>Action</code>
86 * actually has to be created. If the instance is already created and
87 * cached, this method will not be called. </p>
88 *
89 * @param context The <code>Context</code> for this request
90 * @param type Name of class to instantiate
91 * @return Instantiated Action class
92 * @throws Exception if there are any problems instantiating the Action
93 * class.
94 * @since Struts 1.3.7
95 */
96 protected Action createAction(ActionContext context, String type) throws Exception {
97 log.info("Initialize action of type: " + type);
98 return (Action) ClassUtils.getApplicationInstance(type);
99 }
100 }