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
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
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 }