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