1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
49  public class PortletContextListener
50      implements PortalInitable, ServletContextListener {
51  
52      public void contextDestroyed(ServletContextEvent event) {
53          ServletContext servletContext = event.getServletContext();
54  
55          Thread currentThread = Thread.currentThread();
56  
57          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
58  
59          HotDeployUtil.fireUndeployEvent(
60              new HotDeployEvent(servletContext, contextClassLoader));
61  
62          try {
63              if (!_bindLiferayPool) {
64                  return;
65              }
66  
67              _bindLiferayPool = false;
68  
69              if (_log.isDebugEnabled()) {
70                  _log.debug("Dynamically unbinding the Liferay data source");
71              }
72  
73              Context ctx = new InitialContext();
74  
75              ctx.unbind(_JNDI_JDBC_LIFERAY_POOL);
76  
77              ctx.destroySubcontext(_JNDI_JDBC);
78          }
79          catch (Exception e) {
80              if (_log.isWarnEnabled()) {
81                  _log.warn(
82                      "Unable to dynamically unbind the Liferay data source: "
83                          + e.getMessage());
84              }
85          }
86      }
87  
88      public void contextInitialized(ServletContextEvent event) {
89          _servletContext = event.getServletContext();
90  
91          Thread currentThread = Thread.currentThread();
92  
93          _classLoader = currentThread.getContextClassLoader();
94  
95          PortalInitableUtil.init(this);
96      }
97  
98      public void portalInit() {
99          HotDeployUtil.fireDeployEvent(
100             new HotDeployEvent(_servletContext, _classLoader));
101 
102         try {
103             if (_log.isDebugEnabled()) {
104                 _log.debug("Dynamically binding the Liferay data source");
105             }
106 
107             DataSource dataSource = InfrastructureUtil.getDataSource();
108 
109             if (dataSource == null) {
110                 if (_log.isDebugEnabled()) {
111                     _log.debug(
112                         "Abort dynamically binding the Liferay data source " +
113                             "because it is not available");
114                 }
115 
116                 return;
117             }
118 
119             Context ctx = new InitialContext();
120 
121             ctx.createSubcontext(_JNDI_JDBC);
122 
123             ctx.bind(
124                 _JNDI_JDBC_LIFERAY_POOL, InfrastructureUtil.getDataSource());
125 
126             _bindLiferayPool = true;
127         }
128         catch (Exception e) {
129             if (_log.isWarnEnabled()) {
130                 _log.warn(
131                     "Unable to dynamically bind the Liferay data source: "
132                         + e.getMessage());
133             }
134         }
135     }
136 
137     private static final String _JNDI_JDBC = "java_liferay:jdbc";
138 
139     private static final String _JNDI_JDBC_LIFERAY_POOL =
140         _JNDI_JDBC + "/LiferayPool";
141 
142     private static Log _log =
143         LogFactoryUtil.getLog(PortletContextListener.class);
144 
145     private ServletContext _servletContext;
146     private ClassLoader _classLoader;
147     private boolean _bindLiferayPool;
148 
149 }