1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.servlet;
24  
25  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
26  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.InfrastructureUtil;
30  import com.liferay.portal.kernel.util.PortalInitable;
31  import com.liferay.portal.kernel.util.PortalInitableUtil;
32  
33  import javax.naming.Context;
34  import javax.naming.InitialContext;
35  
36  import javax.servlet.ServletContext;
37  import javax.servlet.ServletContextEvent;
38  import javax.servlet.ServletContextListener;
39  
40  import javax.sql.DataSource;
41  
42  /**
43   * <a href="PortletContextListener.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Ivica Cardic
46   * @author Brian Wing Shun Chan
47   */
48  public class PortletContextListener
49      implements PortalInitable, ServletContextListener {
50  
51      public void contextDestroyed(ServletContextEvent event) {
52          ServletContext servletContext = event.getServletContext();
53  
54          Thread currentThread = Thread.currentThread();
55  
56          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
57  
58          HotDeployUtil.fireUndeployEvent(
59              new HotDeployEvent(servletContext, contextClassLoader));
60  
61          try {
62              if (!_bindLiferayPool) {
63                  return;
64              }
65  
66              _bindLiferayPool = false;
67  
68              if (_log.isDebugEnabled()) {
69                  _log.debug("Dynamically unbinding the Liferay data source");
70              }
71  
72              Context ctx = new InitialContext();
73  
74              ctx.unbind(_JNDI_JDBC_LIFERAY_POOL);
75  
76              ctx.destroySubcontext(_JNDI_JDBC);
77          }
78          catch (Exception e) {
79              if (_log.isWarnEnabled()) {
80                  _log.warn(
81                      "Unable to dynamically unbind the Liferay data source: "
82                          + e.getMessage());
83              }
84          }
85      }
86  
87      public void contextInitialized(ServletContextEvent event) {
88          _servletContext = event.getServletContext();
89  
90          Thread currentThread = Thread.currentThread();
91  
92          _classLoader = currentThread.getContextClassLoader();
93  
94          PortalInitableUtil.init(this);
95      }
96  
97      public void portalInit() {
98          HotDeployUtil.fireDeployEvent(
99              new HotDeployEvent(_servletContext, _classLoader));
100 
101         try {
102             if (_log.isDebugEnabled()) {
103                 _log.debug("Dynamically binding the Liferay data source");
104             }
105 
106             DataSource dataSource = InfrastructureUtil.getDataSource();
107 
108             if (dataSource == null) {
109                 if (_log.isDebugEnabled()) {
110                     _log.debug(
111                         "Abort dynamically binding the Liferay data source " +
112                             "because it is not available");
113                 }
114 
115                 return;
116             }
117 
118             Context ctx = new InitialContext();
119 
120             ctx.createSubcontext(_JNDI_JDBC);
121 
122             ctx.bind(
123                 _JNDI_JDBC_LIFERAY_POOL, InfrastructureUtil.getDataSource());
124 
125             _bindLiferayPool = true;
126         }
127         catch (Exception e) {
128             if (_log.isWarnEnabled()) {
129                 _log.warn(
130                     "Unable to dynamically bind the Liferay data source: "
131                         + e.getMessage());
132             }
133         }
134     }
135 
136     private static final String _JNDI_JDBC = "java_liferay:jdbc";
137 
138     private static final String _JNDI_JDBC_LIFERAY_POOL =
139         _JNDI_JDBC + "/LiferayPool";
140 
141     private static Log _log =
142         LogFactoryUtil.getLog(PortletContextListener.class);
143 
144     private ServletContext _servletContext;
145     private ClassLoader _classLoader;
146     private boolean _bindLiferayPool;
147 
148 }