View Javadoc

1   /*
2    * $Id: PlexusLifecycleListener.java 471756 2006-11-06 15:01:43Z husted $
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.plexus;
22  
23  import java.util.Collections;
24  
25  import javax.servlet.ServletContext;
26  import javax.servlet.ServletContextEvent;
27  import javax.servlet.ServletContextListener;
28  import javax.servlet.http.HttpSession;
29  import javax.servlet.http.HttpSessionEvent;
30  import javax.servlet.http.HttpSessionListener;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.codehaus.plexus.DefaultPlexusContainer;
35  import org.codehaus.plexus.PlexusContainer;
36  
37  /***
38   * Manages the Plexus lifecycle for the servlet and session contexts
39   */
40  public class PlexusLifecycleListener implements ServletContextListener, HttpSessionListener {
41      private static final Log log = LogFactory.getLog(PlexusObjectFactory.class);
42  
43      private static boolean loaded = false;
44      public static final String KEY = "struts.plexus.container";
45  
46      /***
47       * @return Returns if the container is loaded.
48       */
49      public static boolean isLoaded() {
50          return loaded;
51      }
52  
53      /* (non-Javadoc)
54       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
55       */
56      public void contextInitialized(ServletContextEvent servletContextEvent) {
57          loaded = true;
58  
59          try {
60              PlexusContainer pc = new DefaultPlexusContainer();
61              PlexusUtils.configure(pc, "plexus-application.xml");
62              ServletContext ctx = servletContextEvent.getServletContext();
63              ctx.setAttribute(KEY, pc);
64  
65              pc.initialize();
66              pc.start();
67          } catch (Exception e) {
68              log.error("Error initializing plexus container (scope: application)", e);
69          }
70      }
71  
72      /* (non-Javadoc)
73       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
74       */
75      public void contextDestroyed(ServletContextEvent servletContextEvent) {
76          try {
77              ServletContext ctx = servletContextEvent.getServletContext();
78              PlexusContainer pc = (PlexusContainer) ctx.getAttribute(KEY);
79              pc.dispose();
80          } catch (Exception e) {
81              log.error("Error disposing plexus container (scope: application)", e);
82          }
83      }
84  
85      /* (non-Javadoc)
86       * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
87       */
88      public void sessionCreated(HttpSessionEvent httpSessionEvent) {
89          try {
90              HttpSession session = httpSessionEvent.getSession();
91              ServletContext ctx = session.getServletContext();
92              PlexusContainer parent = (PlexusContainer) ctx.getAttribute(KEY);
93              PlexusContainer child = parent.createChildContainer("session", Collections.EMPTY_LIST, Collections.EMPTY_MAP);
94              session.setAttribute(KEY, child);
95              PlexusUtils.configure(child, "plexus-session.xml");
96              child.initialize();
97              child.start();
98          } catch (Exception e) {
99              log.error("Error initializing plexus container (scope: session)", e);
100         }
101     }
102 
103     /* (non-Javadoc)
104      * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
105      */
106     public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
107         try {
108             HttpSession session = httpSessionEvent.getSession();
109             PlexusContainer child = (PlexusContainer) session.getAttribute(KEY);
110             child.dispose();
111         } catch (Exception e) {
112             log.error("Error initializing plexus container (scope: session)", e);
113         }
114     }
115 }