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