1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.spring.context;
21  
22  import com.liferay.portal.bean.BeanLocatorImpl;
23  import com.liferay.portal.kernel.bean.BeanLocator;
24  import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.PortletClassLoaderUtil;
28  
29  import java.lang.reflect.Method;
30  
31  import javax.servlet.ServletContext;
32  import javax.servlet.ServletContextEvent;
33  
34  import org.springframework.context.ApplicationContext;
35  import org.springframework.web.context.ContextLoader;
36  import org.springframework.web.context.ContextLoaderListener;
37  import org.springframework.web.context.support.WebApplicationContextUtils;
38  
39  /**
40   * <a href="PortletContextLoaderListener.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PortletContextLoaderListener extends ContextLoaderListener {
47  
48      public void contextDestroyed(ServletContextEvent event) {
49          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
50  
51          ServletContext servletContext = event.getServletContext();
52  
53          try {
54              Class<?> beanLocatorUtilClass = Class.forName(
55                  "com.liferay.util.bean.PortletBeanLocatorUtil", true,
56                  classLoader);
57  
58              Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
59                  "setBeanLocator", new Class[] {BeanLocator.class});
60  
61              setBeanLocatorMethod.invoke(
62                  beanLocatorUtilClass, new Object[] {null});
63  
64              PortletBeanLocatorUtil.setBeanLocator(
65                  servletContext.getServletContextName(), null);
66          }
67          catch (Exception e) {
68              _log.error(e, e);
69          }
70  
71          super.contextDestroyed(event);
72      }
73  
74      public void contextInitialized(ServletContextEvent event) {
75          super.contextInitialized(event);
76  
77          ClassLoader classLoader = PortletClassLoaderUtil.getClassLoader();
78  
79          ServletContext servletContext = event.getServletContext();
80  
81          ApplicationContext applicationContext =
82              WebApplicationContextUtils.getWebApplicationContext(servletContext);
83  
84          BeanLocator beanLocator = new BeanLocatorImpl(
85              classLoader, applicationContext);
86  
87          try {
88              Class<?> beanLocatorUtilClass = Class.forName(
89                  "com.liferay.util.bean.PortletBeanLocatorUtil", true,
90                  classLoader);
91  
92              Method setBeanLocatorMethod = beanLocatorUtilClass.getMethod(
93                  "setBeanLocator", new Class[] {BeanLocator.class});
94  
95              setBeanLocatorMethod.invoke(
96                  beanLocatorUtilClass, new Object[] {beanLocator});
97  
98              PortletBeanLocatorUtil.setBeanLocator(
99                  servletContext.getServletContextName(), beanLocator);
100         }
101         catch (Exception e) {
102             _log.error(e, e);
103         }
104     }
105 
106     protected ContextLoader createContextLoader() {
107         return new PortletContextLoader();
108     }
109 
110     private static Log _log =
111         LogFactoryUtil.getLog(PortletContextLoaderListener.class);
112 
113 }