View Javadoc

1   /*
2    * $Id: DefaultActionSupport.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }