001
014
015 package com.liferay.portal.kernel.servlet;
016
017 import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
018 import com.liferay.portal.kernel.deploy.hot.HotDeployUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.BasePortalLifecycle;
022 import com.liferay.portal.kernel.util.InfrastructureUtil;
023 import com.liferay.portal.kernel.util.ServerDetector;
024
025 import java.lang.reflect.Method;
026
027 import javax.naming.Context;
028 import javax.naming.InitialContext;
029 import javax.naming.NamingException;
030
031 import javax.servlet.ServletContext;
032 import javax.servlet.ServletContextEvent;
033 import javax.servlet.ServletContextListener;
034
035 import javax.sql.DataSource;
036
037
041 public class PortletContextListener
042 extends BasePortalLifecycle implements ServletContextListener {
043
044 public void contextDestroyed(ServletContextEvent servletContextEvent) {
045 portalDestroy();
046 }
047
048 public void contextInitialized(ServletContextEvent servletContextEvent) {
049 _servletContext = servletContextEvent.getServletContext();
050
051 Thread currentThread = Thread.currentThread();
052
053 _portletClassLoader = currentThread.getContextClassLoader();
054
055 registerPortalLifecycle();
056 }
057
058 protected void doPortalDestroy() {
059 HotDeployUtil.fireUndeployEvent(
060 new HotDeployEvent(_servletContext, _portletClassLoader));
061
062 try {
063 if (!_bindLiferayPool) {
064 return;
065 }
066
067 _bindLiferayPool = false;
068
069 if (_log.isDebugEnabled()) {
070 _log.debug("Dynamically unbinding the Liferay data source");
071 }
072
073 Context context = new InitialContext();
074
075 try {
076 context.lookup(_JNDI_JDBC_LIFERAY_POOL);
077 }
078 catch (NamingException ne) {
079 context.unbind(_JNDI_JDBC_LIFERAY_POOL);
080 }
081
082 try {
083 context.lookup(_JNDI_JDBC);
084 }
085 catch (NamingException ne) {
086 context.destroySubcontext(_JNDI_JDBC);
087 }
088 }
089 catch (Exception e) {
090 if (_log.isWarnEnabled()) {
091 _log.warn(
092 "Unable to dynamically unbind the Liferay data source: "
093 + e.getMessage());
094 }
095 }
096 }
097
098 protected void doPortalInit() {
099 HotDeployUtil.fireDeployEvent(
100 new HotDeployEvent(_servletContext, _portletClassLoader));
101
102 try {
103 if (ServerDetector.isGlassfish()) {
104 return;
105 }
106
107 if (_log.isDebugEnabled()) {
108 _log.debug("Dynamically binding the Liferay data source");
109 }
110
111 DataSource dataSource = InfrastructureUtil.getDataSource();
112
113 if (dataSource == null) {
114 if (_log.isDebugEnabled()) {
115 _log.debug(
116 "Abort dynamically binding the Liferay data source " +
117 "because it is not available");
118 }
119
120 return;
121 }
122
123 Context context = new InitialContext();
124
125 try {
126 context.lookup(_JNDI_JDBC);
127 }
128 catch (NamingException ne) {
129 context.createSubcontext(_JNDI_JDBC);
130 }
131
132 try {
133 context.lookup(_JNDI_JDBC_LIFERAY_POOL);
134 }
135 catch (NamingException ne) {
136 try {
137 Method method = dataSource.getClass().getMethod(
138 "getTargetDataSource");
139
140 dataSource = (DataSource)method.invoke(dataSource);
141 }
142 catch (NoSuchMethodException nsme) {
143 }
144
145 context.bind(_JNDI_JDBC_LIFERAY_POOL, dataSource);
146 }
147
148 _bindLiferayPool = true;
149 }
150 catch (Exception e) {
151 if (_log.isWarnEnabled()) {
152 _log.warn(
153 "Unable to dynamically bind the Liferay data source: "
154 + e.getMessage());
155 }
156 }
157 }
158
159 private static final String _JNDI_JDBC = "java_liferay:jdbc";
160
161 private static final String _JNDI_JDBC_LIFERAY_POOL =
162 _JNDI_JDBC + "/LiferayPool";
163
164 private static Log _log = LogFactoryUtil.getLog(
165 PortletContextListener.class);
166
167 private boolean _bindLiferayPool;
168 private ClassLoader _portletClassLoader;
169 private ServletContext _servletContext;
170
171 }