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 java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.struts.action.Action;
27 import org.apache.struts.action.ActionServlet;
28 import org.apache.struts.chain.Constants;
29 import org.apache.struts.chain.commands.util.ClassUtils;
30 import org.apache.struts.chain.contexts.ActionContext;
31 import org.apache.struts.chain.contexts.ServletActionContext;
32 import org.apache.struts.config.ActionConfig;
33 import org.apache.struts.config.ModuleConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42 public class CreateAction
43 extends org.apache.struts.chain.commands.AbstractCreateAction {
44
45
46
47
48
49 private final Logger log =
50 LoggerFactory.getLogger(CreateAction.class);
51
52
53
54
55 protected synchronized Action getAction(ActionContext context, String type,
56 ActionConfig actionConfig)
57 throws Exception {
58
59 ServletActionContext saContext = (ServletActionContext) context;
60 ActionServlet actionServlet = saContext.getActionServlet();
61
62 ModuleConfig moduleConfig = actionConfig.getModuleConfig();
63 String actionsKey = Constants.ACTIONS_KEY + moduleConfig.getPrefix();
64 @SuppressWarnings("unchecked")
65 Map<String, Action> actions = (Map<String, Action>) context.getApplicationScope().get(actionsKey);
66
67 if (actions == null) {
68 actions = new HashMap<>();
69 context.getApplicationScope().put(actionsKey, actions);
70 }
71
72 Action action = null;
73
74 try {
75 if (actionConfig.isSingleton()) {
76 synchronized (actions) {
77 action = actions.get(type);
78 if (action == null) {
79 action = createAction(context, type);
80 actions.put(type, action);
81 }
82 }
83 } else {
84 action = createAction(context, type);
85 }
86 } catch (Exception e) {
87 log.atError()
88 .setMessage(() -> actionServlet.getInternal().getMessage(
89 "actionCreate", actionConfig.getPath(),
90 actionConfig.toString()))
91 .setCause(e).log();
92 throw e;
93 }
94
95 if (action.getServlet() == null) {
96 action.setServlet(actionServlet);
97 }
98
99 return (action);
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114 protected Action createAction(ActionContext context, String type) throws Exception {
115 log.info("Initialize action of type: {}", type);
116 return (Action) ClassUtils.getApplicationInstance(type);
117 }
118 }