1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles2;
23
24 import jakarta.servlet.ServletException;
25
26 import org.apache.struts.action.ActionServlet;
27 import org.apache.struts.action.PlugIn;
28 import org.apache.struts.action.RequestProcessor;
29 import org.apache.struts.chain.ComposableRequestProcessor;
30 import org.apache.struts.config.ControllerConfig;
31 import org.apache.struts.config.ModuleConfig;
32 import org.apache.struts.config.PlugInConfig;
33 import org.apache.struts.tiles2.util.PlugInConfigContextAdapter;
34 import org.apache.struts.util.RequestUtils;
35 import org.apache.tiles.TilesContainer;
36 import org.apache.tiles.TilesException;
37 import org.apache.tiles.access.TilesAccess;
38 import org.apache.tiles.definition.DefinitionsFactory;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.slf4j.Marker;
42 import org.slf4j.MarkerFactory;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 public class TilesPlugin implements PlugIn {
71
72
73
74
75 private final static Marker FATAL = MarkerFactory.getMarker("FATAL");
76
77
78
79
80 private final Logger log =
81 LoggerFactory.getLogger(TilesPlugin.class);
82
83
84
85
86 protected boolean moduleAware = false;
87
88
89
90
91
92 protected PlugInConfig currentPlugInConfigObject = null;
93
94
95
96
97
98 protected PlugInConfigContextAdapter currentPlugInConfigContextAdapter = null;
99
100
101
102
103
104
105 public boolean isModuleAware() {
106 return moduleAware;
107 }
108
109
110
111
112
113
114
115
116 public void setModuleAware(boolean moduleAware) {
117 this.moduleAware = moduleAware;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public void init(ActionServlet servlet, ModuleConfig moduleConfig)
133 throws ServletException {
134
135 currentPlugInConfigContextAdapter = new PlugInConfigContextAdapter(
136 this.currentPlugInConfigObject, servlet.getServletContext());
137
138
139 this.initRequestProcessorClass(moduleConfig);
140
141
142 try {
143 TilesPluginContainerFactory factory;
144 TilesContainer container;
145 if (moduleAware) {
146 factory = new TilesPluginContainerFactory();
147 container = TilesAccess
148 .getContainer(currentPlugInConfigContextAdapter);
149 if (container == null) {
150 container = factory.createContainer(
151 currentPlugInConfigContextAdapter);
152 TilesAccess.setContainer(currentPlugInConfigContextAdapter,
153 container);
154 }
155 if (container instanceof TilesPluginContainer) {
156 TilesPluginContainer pluginContainer =
157 (TilesPluginContainer) container;
158
159
160
161 if (pluginContainer.getProperDefinitionsFactory(moduleConfig
162 .getPrefix()) != null) {
163 throw new ServletException("Tiles definitions factory for module '"
164 + moduleConfig.getPrefix()
165 + "' has already been configured");
166 }
167
168 DefinitionsFactory defsFactory = factory
169 .createDefinitionsFactory(pluginContainer, currentPlugInConfigContextAdapter);
170 pluginContainer.setDefinitionsFactory(moduleConfig.getPrefix(), defsFactory);
171 } else {
172 log.warn("The created container is not instance of "
173 + "TilesPluginContainer"
174 + " and cannot be configured correctly");
175 }
176 } else {
177 if (currentPlugInConfigContextAdapter.getApplicationScope()
178 .containsKey(TilesAccess.CONTAINER_ATTRIBUTE)) {
179 throw new ServletException(
180 "Tiles container has already been configured");
181 }
182 factory = new TilesPluginContainerFactory();
183 container = factory.createContainer(
184 currentPlugInConfigContextAdapter);
185 TilesAccess.setContainer(currentPlugInConfigContextAdapter,
186 container);
187 }
188 } catch (TilesException e) {
189 log.error(FATAL, "Unable to retrieve tiles factory.", e);
190 throw new IllegalStateException("Unable to instantiate container.");
191 }
192 }
193
194
195
196
197 public void destroy() {
198 try {
199 TilesAccess.setContainer(currentPlugInConfigContextAdapter, null);
200 } catch (TilesException e) {
201 log.warn("Unable to remove tiles container from service.");
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215 protected void initRequestProcessorClass(ModuleConfig config)
216 throws ServletException {
217
218 String tilesProcessorClassname = TilesRequestProcessor.class.getName();
219 ControllerConfig ctrlConfig = config.getControllerConfig();
220 String configProcessorClassname = ctrlConfig.getProcessorClass();
221
222
223 Class<?> configProcessorClass;
224 try {
225 configProcessorClass =
226 RequestUtils.applicationClass(configProcessorClassname);
227
228 } catch (ClassNotFoundException ex) {
229 log.error(FATAL, "Can't set TilesRequestProcessor: bad class name '{}'.",
230 configProcessorClassname);
231 throw new ServletException(ex);
232 }
233
234
235
236 if (ComposableRequestProcessor.class.isAssignableFrom(configProcessorClass)) {
237 return;
238 }
239
240
241
242 if (configProcessorClassname.equals(RequestProcessor.class.getName())
243 || configProcessorClassname.endsWith(tilesProcessorClassname)) {
244
245 ctrlConfig.setProcessorClass(tilesProcessorClassname);
246 return;
247 }
248
249
250 Class<TilesRequestProcessor> tilesProcessorClass = TilesRequestProcessor.class;
251 if (!tilesProcessorClass.isAssignableFrom(configProcessorClass)) {
252
253 String msg =
254 "TilesPlugin : Specified RequestProcessor not compatible with TilesRequestProcessor";
255 log.error(FATAL, msg);
256 throw new ServletException(msg);
257 }
258 }
259
260
261
262
263
264
265 public void setCurrentPlugInConfigObject(PlugInConfig plugInConfigObject) {
266 this.currentPlugInConfigObject = plugInConfigObject;
267 }
268 }