1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.dispatcher.ng;
22
23 import com.opensymphony.xwork2.util.logging.LoggerFactory;
24 import com.opensymphony.xwork2.ActionContext;
25 import org.apache.struts2.dispatcher.Dispatcher;
26 import org.apache.struts2.dispatcher.StaticContentLoader;
27 import org.apache.struts2.util.ClassLoaderUtils;
28
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Iterator;
32
33 /***
34 * Contains initialization operations
35 */
36 public class InitOperations {
37
38 public InitOperations() {
39 }
40
41 /***
42 * Initializes the internal Struts logging
43 */
44 public void initLogging(HostConfig filterConfig) {
45 String factoryName = filterConfig.getInitParameter("loggerFactory");
46 if (factoryName != null) {
47 try {
48 Class cls = ClassLoaderUtils.loadClass(factoryName, this.getClass());
49 LoggerFactory fac = (LoggerFactory) cls.newInstance();
50 LoggerFactory.setLoggerFactory(fac);
51 } catch (InstantiationException e) {
52 System.err.println("Unable to instantiate logger factory: " + factoryName + ", using default");
53 e.printStackTrace();
54 } catch (IllegalAccessException e) {
55 System.err.println("Unable to access logger factory: " + factoryName + ", using default");
56 e.printStackTrace();
57 } catch (ClassNotFoundException e) {
58 System.err.println("Unable to locate logger factory class: " + factoryName + ", using default");
59 e.printStackTrace();
60 }
61 }
62 }
63
64 /***
65 * Creates and initializes the dispatcher
66 */
67 public Dispatcher initDispatcher(HostConfig filterConfig) {
68 Dispatcher dispatcher = createDispatcher(filterConfig);
69 dispatcher.init();
70 return dispatcher;
71 }
72
73 /***
74 * Initializes the static content loader with the filter configuration
75 */
76 public StaticContentLoader initStaticContentLoader(HostConfig filterConfig, Dispatcher dispatcher) {
77 StaticContentLoader loader = dispatcher.getContainer().getInstance(StaticContentLoader.class);
78 loader.setHostConfig(filterConfig);
79 return loader;
80 }
81
82 /***
83 * @return The dispatcher on the thread.
84 * @throws IllegalStateException If there is no dispatcher available
85 */
86 public Dispatcher findDispatcherOnThread() {
87 Dispatcher dispatcher = Dispatcher.getInstance();
88 if (dispatcher == null) {
89 throw new IllegalStateException("Must have the StrutsPrepareFilter execute before this one");
90 }
91 return dispatcher;
92 }
93
94 /***
95 * Create a {@link Dispatcher}
96 */
97 private Dispatcher createDispatcher(HostConfig filterConfig) {
98 Map<String, String> params = new HashMap<String, String>();
99 for (Iterator e = filterConfig.getInitParameterNames(); e.hasNext();) {
100 String name = (String) e.next();
101 String value = filterConfig.getInitParameter(name);
102 params.put(name, value);
103 }
104 return new Dispatcher(filterConfig.getServletContext(), params);
105 }
106
107 public void cleanup() {
108 ActionContext.setContext(null);
109 }
110 }