1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.servlet;
16  
17  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
18  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.BasePortalLifecycle;
22  import com.liferay.portal.kernel.util.InfrastructureUtil;
23  import com.liferay.portal.kernel.util.ServerDetector;
24  
25  import java.lang.reflect.Method;
26  
27  import javax.naming.Context;
28  import javax.naming.InitialContext;
29  import javax.naming.NamingException;
30  
31  import javax.servlet.ServletContext;
32  import javax.servlet.ServletContextEvent;
33  import javax.servlet.ServletContextListener;
34  
35  import javax.sql.DataSource;
36  
37  /**
38   * <a href="PortletContextListener.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Ivica Cardic
41   * @author Brian Wing Shun Chan
42   */
43  public class PortletContextListener
44      extends BasePortalLifecycle implements ServletContextListener {
45  
46      public void contextDestroyed(ServletContextEvent servletContextEvent) {
47          portalDestroy();
48      }
49  
50      public void contextInitialized(ServletContextEvent servletContextEvent) {
51          _servletContext = servletContextEvent.getServletContext();
52  
53          Thread currentThread = Thread.currentThread();
54  
55          _portletClassLoader = currentThread.getContextClassLoader();
56  
57          registerPortalLifecycle();
58      }
59  
60      protected void doPortalDestroy() {
61          HotDeployUtil.fireUndeployEvent(
62              new HotDeployEvent(_servletContext, _portletClassLoader));
63  
64          try {
65              if (!_bindLiferayPool) {
66                  return;
67              }
68  
69              _bindLiferayPool = false;
70  
71              if (_log.isDebugEnabled()) {
72                  _log.debug("Dynamically unbinding the Liferay data source");
73              }
74  
75              Context context = new InitialContext();
76  
77              try {
78                  context.lookup(_JNDI_JDBC_LIFERAY_POOL);
79              }
80              catch (NamingException ne) {
81                  context.unbind(_JNDI_JDBC_LIFERAY_POOL);
82              }
83  
84              try {
85                  context.lookup(_JNDI_JDBC);
86              }
87              catch (NamingException ne) {
88                  context.destroySubcontext(_JNDI_JDBC);
89              }
90          }
91          catch (Exception e) {
92              if (_log.isWarnEnabled()) {
93                  _log.warn(
94                      "Unable to dynamically unbind the Liferay data source: "
95                          + e.getMessage());
96              }
97          }
98      }
99  
100     protected void doPortalInit() {
101         HotDeployUtil.fireDeployEvent(
102             new HotDeployEvent(_servletContext, _portletClassLoader));
103 
104         try {
105             if (ServerDetector.isGlassfish2()) {
106                 return;
107             }
108 
109             if (_log.isDebugEnabled()) {
110                 _log.debug("Dynamically binding the Liferay data source");
111             }
112 
113             DataSource dataSource = InfrastructureUtil.getDataSource();
114 
115             if (dataSource == null) {
116                 if (_log.isDebugEnabled()) {
117                     _log.debug(
118                         "Abort dynamically binding the Liferay data source " +
119                             "because it is not available");
120                 }
121 
122                 return;
123             }
124 
125             Context context = new InitialContext();
126 
127             try {
128                 context.lookup(_JNDI_JDBC);
129             }
130             catch (NamingException ne) {
131                 context.createSubcontext(_JNDI_JDBC);
132             }
133 
134             try {
135                 context.lookup(_JNDI_JDBC_LIFERAY_POOL);
136             }
137             catch (NamingException ne) {
138                 try {
139                     Method method = dataSource.getClass().getMethod(
140                         "getTargetDataSource");
141 
142                     dataSource = (DataSource)method.invoke(dataSource);
143                 }
144                 catch (NoSuchMethodException nsme) {
145                 }
146 
147                 context.bind(_JNDI_JDBC_LIFERAY_POOL, dataSource);
148             }
149 
150             _bindLiferayPool = true;
151         }
152         catch (Exception e) {
153             if (_log.isWarnEnabled()) {
154                 _log.warn(
155                     "Unable to dynamically bind the Liferay data source: "
156                         + e.getMessage());
157             }
158         }
159     }
160 
161     private static final String _JNDI_JDBC = "java_liferay:jdbc";
162 
163     private static final String _JNDI_JDBC_LIFERAY_POOL =
164         _JNDI_JDBC + "/LiferayPool";
165 
166     private static Log _log = LogFactoryUtil.getLog(
167         PortletContextListener.class);
168 
169     private boolean _bindLiferayPool;
170     private ClassLoader _portletClassLoader;
171     private ServletContext _servletContext;
172 
173 }