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