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 import org.apache.struts2.StrutsConstants;
29
30 import java.util.*;
31 import java.util.regex.Pattern;
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 *
85 * @throws IllegalStateException If there is no dispatcher available
86 */
87 public Dispatcher findDispatcherOnThread() {
88 Dispatcher dispatcher = Dispatcher.getInstance();
89 if (dispatcher == null) {
90 throw new IllegalStateException("Must have the StrutsPrepareFilter execute before this one");
91 }
92 return dispatcher;
93 }
94
95 /***
96 * Create a {@link Dispatcher}
97 */
98 private Dispatcher createDispatcher( HostConfig filterConfig ) {
99 Map<String, String> params = new HashMap<String, String>();
100 for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
101 String name = (String) e.next();
102 String value = filterConfig.getInitParameter(name);
103 params.put(name, value);
104 }
105 return new Dispatcher(filterConfig.getServletContext(), params);
106 }
107
108 public void cleanup() {
109 ActionContext.setContext(null);
110 }
111
112 /***
113 * Extract a list of patterns to exclude from request filtering
114 *
115 * @param dispatcher The dispatcher to check for exclude pattern configuration
116 *
117 * @return a List of Patterns for request to exclude if apply, or <tt>null</tt>
118 *
119 * @see org.apache.struts2.StrutsConstants#STRUTS_ACTION_EXCLUDE_PATTERN
120 */
121 public List<Pattern> buildExcludedPatternsList( Dispatcher dispatcher ) {
122 return buildExcludedPatternsList(dispatcher.getContainer().getInstance(String.class, StrutsConstants.STRUTS_ACTION_EXCLUDE_PATTERN));
123 }
124
125 private List<Pattern> buildExcludedPatternsList( String patterns ) {
126 if (null != patterns && patterns.trim().length() != 0) {
127 List<Pattern> list = new ArrayList<Pattern>();
128 String[] tokens = patterns.split(",");
129 for ( String token : tokens ) {
130 list.add(Pattern.compile(token.trim()));
131 }
132 return Collections.unmodifiableList(list);
133 } else {
134 return null;
135 }
136 }
137
138 }