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 javax.naming.Context;
34 import javax.naming.InitialContext;
35
36 import javax.servlet.ServletContext;
37 import javax.servlet.ServletContextEvent;
38 import javax.servlet.ServletContextListener;
39
40 import javax.sql.DataSource;
41
42
48 public class PortletContextListener
49 implements PortalInitable, ServletContextListener {
50
51 public void contextDestroyed(ServletContextEvent event) {
52 ServletContext servletContext = event.getServletContext();
53
54 Thread currentThread = Thread.currentThread();
55
56 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
57
58 HotDeployUtil.fireUndeployEvent(
59 new HotDeployEvent(servletContext, contextClassLoader));
60
61 try {
62 if (!_bindLiferayPool) {
63 return;
64 }
65
66 _bindLiferayPool = false;
67
68 if (_log.isDebugEnabled()) {
69 _log.debug("Dynamically unbinding the Liferay data source");
70 }
71
72 Context ctx = new InitialContext();
73
74 ctx.unbind(_JNDI_JDBC_LIFERAY_POOL);
75
76 ctx.destroySubcontext(_JNDI_JDBC);
77 }
78 catch (Exception e) {
79 if (_log.isWarnEnabled()) {
80 _log.warn(
81 "Unable to dynamically unbind the Liferay data source: "
82 + e.getMessage());
83 }
84 }
85 }
86
87 public void contextInitialized(ServletContextEvent event) {
88 _servletContext = event.getServletContext();
89
90 Thread currentThread = Thread.currentThread();
91
92 _classLoader = currentThread.getContextClassLoader();
93
94 PortalInitableUtil.init(this);
95 }
96
97 public void portalInit() {
98 HotDeployUtil.fireDeployEvent(
99 new HotDeployEvent(_servletContext, _classLoader));
100
101 try {
102 if (_log.isDebugEnabled()) {
103 _log.debug("Dynamically binding the Liferay data source");
104 }
105
106 DataSource dataSource = InfrastructureUtil.getDataSource();
107
108 if (dataSource == null) {
109 if (_log.isDebugEnabled()) {
110 _log.debug(
111 "Abort dynamically binding the Liferay data source " +
112 "because it is not available");
113 }
114
115 return;
116 }
117
118 Context ctx = new InitialContext();
119
120 ctx.createSubcontext(_JNDI_JDBC);
121
122 ctx.bind(
123 _JNDI_JDBC_LIFERAY_POOL, InfrastructureUtil.getDataSource());
124
125 _bindLiferayPool = true;
126 }
127 catch (Exception e) {
128 if (_log.isWarnEnabled()) {
129 _log.warn(
130 "Unable to dynamically bind the Liferay data source: "
131 + e.getMessage());
132 }
133 }
134 }
135
136 private static final String _JNDI_JDBC = "java_liferay:jdbc";
137
138 private static final String _JNDI_JDBC_LIFERAY_POOL =
139 _JNDI_JDBC + "/LiferayPool";
140
141 private static Log _log =
142 LogFactoryUtil.getLog(PortletContextListener.class);
143
144 private ServletContext _servletContext;
145 private ClassLoader _classLoader;
146 private boolean _bindLiferayPool;
147
148 }