1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts.chain.commands.servlet;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.apache.struts.action.Action;
23 import org.apache.struts.action.ActionServlet;
24 import org.apache.struts.chain.Constants;
25 import org.apache.struts.chain.commands.util.ClassUtils;
26 import org.apache.struts.chain.contexts.ActionContext;
27 import org.apache.struts.chain.contexts.ServletActionContext;
28 import org.apache.struts.config.ActionConfig;
29 import org.apache.struts.config.ModuleConfig;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 /***
35 * <p>Concrete implementation of <code>AbstractCreateAction</code> for use in
36 * a Servlet API chain. Expects that the ActionContext passed into it can
37 * safely be cast to <code>ServletActionContext</code>.</p>
38 */
39 public class CreateAction
40 extends org.apache.struts.chain.commands.AbstractCreateAction {
41
42 private static final Log log = LogFactory.getLog(CreateAction.class);
43
44
45
46
47 protected synchronized Action getAction(ActionContext context, String type,
48 ActionConfig actionConfig)
49 throws Exception {
50 ModuleConfig moduleConfig = actionConfig.getModuleConfig();
51 String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
52 Map actions = (Map) context.getApplicationScope().get(actionsKey);
53
54 if (actions == null) {
55 actions = new HashMap();
56 context.getApplicationScope().put(actionsKey, actions);
57 }
58
59 Action action = null;
60
61 synchronized (actions) {
62 action = (Action) actions.get(type);
63
64 if (action == null) {
65 log.info("Initialize action of type: " + type);
66 action = (Action) ClassUtils.getApplicationInstance(type);
67 actions.put(type, action);
68 }
69 }
70
71 if (action.getServlet() == null) {
72 ServletActionContext saContext = (ServletActionContext) context;
73 ActionServlet actionServlet = saContext.getActionServlet();
74
75 action.setServlet(actionServlet);
76 }
77
78 return (action);
79 }
80 }