View Javadoc

1   /*
2    * $Id: PlexusFilter.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  
22  package org.apache.struts2.plexus;
23  
24  import java.io.IOException;
25  import java.util.Collections;
26  
27  import javax.servlet.Filter;
28  import javax.servlet.FilterChain;
29  import javax.servlet.FilterConfig;
30  import javax.servlet.ServletContext;
31  import javax.servlet.ServletException;
32  import javax.servlet.ServletRequest;
33  import javax.servlet.ServletResponse;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpSession;
36  
37  import org.codehaus.plexus.PlexusContainer;
38  
39  import com.opensymphony.xwork2.util.logging.Logger;
40  import com.opensymphony.xwork2.util.logging.LoggerFactory;
41  
42  /***
43   * Creates a plexus container for the application, session, and request
44   */
45  public class PlexusFilter implements Filter {
46      private static final Logger LOG = LoggerFactory.getLogger(PlexusObjectFactory.class);
47      private static final String CHILD_CONTAINER_NAME = "request";
48  
49      private static boolean loaded = false;
50  
51      private ServletContext ctx;
52  
53      /***
54       * @return Returns if the container is loaded.
55       */
56      public static boolean isLoaded() {
57          return loaded;
58      }
59  
60      /* (non-Javadoc)
61       * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
62       */
63      public void init(FilterConfig filterConfig) throws ServletException {
64          ctx = filterConfig.getServletContext();
65          loaded = true;
66      }
67  
68      /* (non-Javadoc)
69       * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
70       */
71      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
72          PlexusContainer child = null;
73          try {
74              try {
75                  HttpServletRequest request = (HttpServletRequest) req;
76                  HttpSession session = request.getSession(false);
77                  PlexusContainer parent;
78                  if (session != null) {
79                      parent = (PlexusContainer) session.getAttribute(PlexusLifecycleListener.KEY);
80                  } else {
81                      parent = (PlexusContainer) ctx.getAttribute(PlexusLifecycleListener.KEY);
82                  }
83  
84                  if (parent.hasChildContainer(CHILD_CONTAINER_NAME)) {
85                      LOG.warn("Plexus container (scope: request) alredy exist.");
86                      child = parent.getChildContainer(CHILD_CONTAINER_NAME);
87                  } else {
88                      child = parent.createChildContainer(CHILD_CONTAINER_NAME, Collections.EMPTY_LIST, Collections.EMPTY_MAP);
89                      PlexusUtils.configure(child, "plexus-request.xml");
90                      child.initialize();
91                      child.start();
92                  }
93                  PlexusThreadLocal.setPlexusContainer(child);
94              } catch (Exception e) {
95                  LOG.error("Error initializing plexus container (scope: request)", e);
96              }
97  
98              chain.doFilter(req, res);
99          }
100         finally {
101             try {
102                 if (child != null) {
103                     child.dispose();
104                 }
105                 PlexusThreadLocal.setPlexusContainer(null);
106             } catch (Exception e) {
107                 LOG.error("Error disposing plexus container (scope: request)", e);
108             }
109         }
110     }
111 
112     /* (non-Javadoc)
113      * @see javax.servlet.Filter#destroy()
114      */
115     public void destroy() {
116     }
117 }