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  
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 }