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
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 (ServerDetector.isGlassfish2()) {
117 return;
118 }
119
120 if (_log.isDebugEnabled()) {
121 _log.debug("Dynamically binding the Liferay data source");
122 }
123
124 DataSource dataSource = InfrastructureUtil.getDataSource();
125
126 if (dataSource == null) {
127 if (_log.isDebugEnabled()) {
128 _log.debug(
129 "Abort dynamically binding the Liferay data source " +
130 "because it is not available");
131 }
132
133 return;
134 }
135
136 Context context = new InitialContext();
137
138 try {
139 context.lookup(_JNDI_JDBC);
140 }
141 catch (NamingException ne) {
142 context.createSubcontext(_JNDI_JDBC);
143 }
144
145 try {
146 context.lookup(_JNDI_JDBC_LIFERAY_POOL);
147 }
148 catch (NamingException ne) {
149 try {
150 Method method = dataSource.getClass().getMethod(
151 "getTargetDataSource");
152
153 dataSource = (DataSource)method.invoke(dataSource);
154 }
155 catch (NoSuchMethodException nsme) {
156 }
157
158 context.bind(_JNDI_JDBC_LIFERAY_POOL, dataSource);
159 }
160
161 _bindLiferayPool = true;
162 }
163 catch (Exception e) {
164 if (_log.isWarnEnabled()) {
165 _log.warn(
166 "Unable to dynamically bind the Liferay data source: "
167 + e.getMessage());
168 }
169 }
170 }
171
172 private static final String _JNDI_JDBC = "java_liferay:jdbc";
173
174 private static final String _JNDI_JDBC_LIFERAY_POOL =
175 _JNDI_JDBC + "/LiferayPool";
176
177 private static Log _log =
178 LogFactoryUtil.getLog(PortletContextListener.class);
179
180 private ServletContext _servletContext;
181 private ClassLoader _classLoader;
182 private boolean _bindLiferayPool;
183
184 }