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.kernel.servlet;
21  
22  import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
23  import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.InfrastructureUtil;
27  import com.liferay.portal.kernel.util.PortalInitable;
28  import com.liferay.portal.kernel.util.PortalInitableUtil;
29  
30  import javax.naming.Context;
31  import javax.naming.InitialContext;
32  
33  import javax.servlet.ServletContext;
34  import javax.servlet.ServletContextEvent;
35  import javax.servlet.ServletContextListener;
36  
37  import javax.sql.DataSource;
38  
39  /**
40   * <a href="PortletContextListener.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Ivica Cardic
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class PortletContextListener
47      implements PortalInitable, ServletContextListener {
48  
49      public void contextDestroyed(ServletContextEvent event) {
50          ServletContext servletContext = event.getServletContext();
51  
52          Thread currentThread = Thread.currentThread();
53  
54          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
55  
56          HotDeployUtil.fireUndeployEvent(
57              new HotDeployEvent(servletContext, contextClassLoader));
58  
59          try {
60              if (!_bindLiferayPool) {
61                  return;
62              }
63  
64              _bindLiferayPool = false;
65  
66              if (_log.isDebugEnabled()) {
67                  _log.debug("Dynamically unbinding the Liferay data source");
68              }
69  
70              Context ctx = new InitialContext();
71  
72              ctx.unbind(_JNDI_JDBC_LIFERAY_POOL);
73  
74              ctx.destroySubcontext(_JNDI_JDBC);
75          }
76          catch (Exception e) {
77              if (_log.isWarnEnabled()) {
78                  _log.warn(
79                      "Unable to dynamically unbind the Liferay data source: "
80                          + e.getMessage());
81              }
82          }
83      }
84  
85      public void contextInitialized(ServletContextEvent event) {
86          _servletContext = event.getServletContext();
87  
88          Thread currentThread = Thread.currentThread();
89  
90          _classLoader = currentThread.getContextClassLoader();
91  
92          PortalInitableUtil.init(this);
93      }
94  
95      public void portalInit() {
96          HotDeployUtil.fireDeployEvent(
97              new HotDeployEvent(_servletContext, _classLoader));
98  
99          try {
100             if (_log.isDebugEnabled()) {
101                 _log.debug("Dynamically binding the Liferay data source");
102             }
103 
104             DataSource dataSource = InfrastructureUtil.getDataSource();
105 
106             if (dataSource == null) {
107                 if (_log.isDebugEnabled()) {
108                     _log.debug(
109                         "Abort dynamically binding the Liferay data source " +
110                             "because it is not available");
111                 }
112 
113                 return;
114             }
115 
116             Context ctx = new InitialContext();
117 
118             ctx.createSubcontext(_JNDI_JDBC);
119 
120             ctx.bind(
121                 _JNDI_JDBC_LIFERAY_POOL, InfrastructureUtil.getDataSource());
122 
123             _bindLiferayPool = true;
124         }
125         catch (Exception e) {
126             if (_log.isWarnEnabled()) {
127                 _log.warn(
128                     "Unable to dynamically bind the Liferay data source: "
129                         + e.getMessage());
130             }
131         }
132     }
133 
134     private static final String _JNDI_JDBC = "java_liferay:jdbc";
135 
136     private static final String _JNDI_JDBC_LIFERAY_POOL =
137         _JNDI_JDBC + "/LiferayPool";
138 
139     private static Log _log =
140         LogFactoryUtil.getLog(PortletContextListener.class);
141 
142     private ServletContext _servletContext;
143     private ClassLoader _classLoader;
144     private boolean _bindLiferayPool;
145 
146 }