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