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