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;
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.ActionMapping;
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.config.ActionConfig;
32 import org.apache.struts.config.ForwardConfig;
33 import org.apache.struts.dispatcher.Dispatcher;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class ExecuteDispatcher extends ActionCommandBase {
38
39
40
41
42 private final Logger log =
43 LoggerFactory.getLogger(ExecuteDispatcher.class);
44
45 private String defaultDispatcherType;
46
47
48
49
50
51
52
53
54
55
56 protected Dispatcher createDispatcher(String type, ActionContext context) throws Exception {
57 log.info("Initializing dispatcher of type: {}", type);
58 return (Dispatcher) ClassUtils.getApplicationInstance(type);
59 }
60
61 @Override
62 protected boolean execute_(ActionContext context) throws Exception {
63
64 Boolean valid = context.getFormValid();
65 if ((valid == null) || !valid.booleanValue()) {
66 return CONTINUE_PROCESSING;
67 }
68
69
70 if (context.getAction() == null) {
71 return CONTINUE_PROCESSING;
72 }
73
74
75 String dispatcherType = getDispatcherType(context);
76 if (dispatcherType == null) {
77 dispatcherType = defaultDispatcherType;
78 if (dispatcherType == null) {
79 return CONTINUE_PROCESSING;
80 }
81 }
82
83
84 String cacheKey = Constants.DISPATCHERS_KEY + context.getModuleConfig().getPrefix();
85 @SuppressWarnings("unchecked")
86 Map<String, Dispatcher> dispatchers = (Map<String, Dispatcher>) context.getApplicationScope().get(cacheKey);
87 if (dispatchers == null) {
88 dispatchers = new HashMap<>();
89 context.getApplicationScope().put(cacheKey, dispatchers);
90 }
91
92
93 Dispatcher dispatcher = null;
94 synchronized (dispatchers) {
95 ActionConfig actionConfig = context.getActionConfig();
96 String actionType = actionConfig.getType();
97 dispatcher = dispatchers.get(actionType);
98
99 if (dispatcher == null) {
100 dispatcher = createDispatcher(dispatcherType, context);
101 dispatchers.put(actionType, dispatcher);
102 }
103 }
104
105
106 Object result = dispatcher.dispatch(context);
107 processDispatchResult(result, context);
108
109 return CONTINUE_PROCESSING;
110 }
111
112
113
114
115
116
117
118
119
120 public final String getDefaultDispatcherType() {
121 return defaultDispatcherType;
122 }
123
124
125
126
127
128
129
130
131
132 protected String getDispatcherType(ActionContext context) {
133 String dispatcherType = null;
134 if (context != null) {
135 dispatcherType = context.getActionConfig().getDispatcher();
136 }
137 return dispatcherType;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 protected void processDispatchResult(Object result, ActionContext context) {
162
163 if (result == null) {
164 return;
165 }
166
167
168 if (result instanceof ForwardConfig) {
169 context.setForwardConfig((ForwardConfig) result);
170 return;
171 }
172
173
174 ActionConfig actionConfig = context.getActionConfig();
175 ActionMapping mapping = ((ActionMapping) actionConfig);
176 if (result instanceof String) {
177 context.setForwardConfig(mapping.findRequiredForward((String) result));
178 return;
179 }
180
181
182 if (result == void.class) {
183 context.setForwardConfig(mapping.findRequiredForward(Action.SUCCESS));
184 return;
185 }
186
187
188 throw new IllegalStateException("Unknown dispatch return type: " + result.getClass().getName());
189 }
190
191
192
193
194
195
196
197 public final void setDefaultDispatcherType(String defaultDispatcherType) {
198 this.defaultDispatcherType = defaultDispatcherType;
199 }
200 }