View Javadoc

1   /*
2    * $Id$
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.listener;
22  
23  import org.apache.struts2.ServletActionContext;
24  import org.apache.struts2.dispatcher.Dispatcher;
25  import org.apache.struts2.dispatcher.ng.InitOperations;
26  import org.apache.struts2.dispatcher.ng.PrepareOperations;
27  
28  import javax.servlet.ServletContextEvent;
29  import javax.servlet.ServletContextListener;
30  
31  /***
32   * Servlet listener for Struts.  The preferred way to use Struts is as a filter via the
33   * {@link org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter} and its variants.  This servlet dispatcher
34   * is only for those that really know what they are doing as it may not support every feature of Struts, particularly
35   * static resource serving.
36   */
37  public class StrutsListener implements ServletContextListener {
38      private PrepareOperations prepare;
39  
40      public void contextInitialized(ServletContextEvent sce) {
41         InitOperations init = new InitOperations();
42          try {
43              ListenerHostConfig config = new ListenerHostConfig(sce.getServletContext());
44              init.initLogging(config);
45              Dispatcher dispatcher = init.initDispatcher(config);
46              init.initStaticContentLoader(config, dispatcher);
47  
48              prepare = new PrepareOperations(config.getServletContext(), dispatcher);
49          } finally {
50              init.cleanup();
51          }
52      }
53  
54      public void contextDestroyed(ServletContextEvent sce) {
55          prepare.cleanupDispatcher();
56      }
57  }