1 | /* |
2 | * Copyright 2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.apache.pluto.driver; |
17 | |
18 | import javax.servlet.ServletContext; |
19 | import javax.servlet.ServletContextEvent; |
20 | import javax.servlet.ServletContextListener; |
21 | |
22 | import org.apache.commons.logging.Log; |
23 | import org.apache.commons.logging.LogFactory; |
24 | import org.apache.pluto.PortletContainer; |
25 | import org.apache.pluto.PortletContainerException; |
26 | import org.apache.pluto.PortletContainerFactory; |
27 | import org.apache.pluto.driver.config.AdminConfiguration; |
28 | import org.apache.pluto.driver.config.DriverConfiguration; |
29 | import org.apache.pluto.driver.config.DriverConfigurationException; |
30 | import org.apache.pluto.driver.config.DriverConfigurationFactory; |
31 | import org.apache.pluto.driver.services.container.ContainerServicesImpl; |
32 | import org.apache.pluto.driver.services.container.PortalContextImpl; |
33 | |
34 | /** |
35 | * Listener used to start up / shut down the Pluto Portal Driver upon startup / |
36 | * showdown of the servlet context in which it resides. |
37 | * |
38 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
39 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
40 | * @version $Revision: 1.5 $ $Date: 2006/08/29 19:14:32 $ |
41 | * @since Sep 22, 2004 |
42 | */ |
43 | public class PortalStartupListener implements ServletContextListener { |
44 | |
45 | /** Internal logger. */ |
46 | private static final Log LOG = LogFactory.getLog(PortalStartupListener.class); |
47 | |
48 | /** The KEY with which the container is bound to the context. */ |
49 | private static final String CONTAINER_KEY = AttributeKeys.PORTLET_CONTAINER; |
50 | |
51 | /** The KEY with which the driver configuration is bound to the context. */ |
52 | private static final String DRIVER_CONFIG_KEY = AttributeKeys.DRIVER_CONFIG; |
53 | |
54 | /** The KEY with which the admin configuration is bound to the context. */ |
55 | private static final String ADMIN_CONFIG_KEY = AttributeKeys.DRIVER_ADMIN_CONFIG; |
56 | |
57 | // ServletContextListener Impl --------------------------------------------- |
58 | |
59 | /** |
60 | * Receives the startup notification and subsequently starts up the portal |
61 | * driver. The following are done in this order: |
62 | * <ol> |
63 | * <li>Retrieve the ResourceConfig File</li> |
64 | * <li>Parse the ResourceConfig File into ResourceConfig Objects</li> |
65 | * <li>Create a Portal Context</li> |
66 | * <li>Create the ContainerServices implementation</li> |
67 | * <li>Create the Portlet Container</li> |
68 | * <li>Initialize the Container</li> |
69 | * <li>Bind the configuration to the ServletContext</li> |
70 | * <li>Bind the container to the ServletContext</li> |
71 | * <ol> |
72 | * |
73 | * @param event |
74 | * the servlet context event. |
75 | */ |
76 | public void contextInitialized(ServletContextEvent event) { |
77 | ServletContext servletContext = event.getServletContext(); |
78 | if (LOG.isInfoEnabled()) { |
79 | LOG.info("Starting up Pluto Portal Driver..."); |
80 | } |
81 | |
82 | initDriverConfiguration(servletContext); |
83 | initAdminConfiguration(servletContext); |
84 | initContainer(servletContext); |
85 | |
86 | if (LOG.isInfoEnabled()) { |
87 | LOG.info("********** Pluto Portal Driver Started **********\n\n"); |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * Recieve notification that the context is being shut down and subsequently |
93 | * destroy the container. |
94 | * |
95 | * @param event |
96 | * the destrubtion event. |
97 | */ |
98 | public void contextDestroyed(ServletContextEvent event) { |
99 | ServletContext servletContext = event.getServletContext(); |
100 | if (LOG.isInfoEnabled()) { |
101 | LOG.info("Shutting down Pluto Portal Driver..."); |
102 | } |
103 | destroyContainer(servletContext); |
104 | destroyAdminConfiguration(servletContext); |
105 | destroyDriverConfiguration(servletContext); |
106 | if (LOG.isInfoEnabled()) { |
107 | LOG.info("********** Pluto Portal Driver Shut Down **********\n\n"); |
108 | } |
109 | } |
110 | |
111 | // Private Initialization Methods ------------------------------------------ |
112 | |
113 | /** |
114 | * Initializes the Portal Driver Configuration. This method loads the driver |
115 | * configuration object and saves it to the servlet context scope. |
116 | * |
117 | * @param servletContext |
118 | * the servlet context. |
119 | */ |
120 | private void initDriverConfiguration(ServletContext servletContext) { |
121 | if (LOG.isDebugEnabled()) { |
122 | LOG.debug("Initializing Portal Driver Configuration..."); |
123 | } |
124 | DriverConfiguration driverConfig = DriverConfigurationFactory.getFactory().getConfig(servletContext); |
125 | if (driverConfig == null) { |
126 | LOG.error("Unable to locate Portal Driver Configuration."); |
127 | } else { |
128 | servletContext.setAttribute(DRIVER_CONFIG_KEY, driverConfig); |
129 | if (LOG.isInfoEnabled()) { |
130 | LOG.info("Driver Configuration initialized to: " + driverConfig.getClass().getName()); |
131 | } |
132 | } |
133 | } |
134 | |
135 | /** |
136 | * Initializes the Admin Configuration if available. This method tries to |
137 | * load the admin configuration object. If it is not available, |
138 | * administration will not be allowed. Otherwise, saves it to the servlet |
139 | * context scope. |
140 | * |
141 | * @param servletContext |
142 | * the servlet context. |
143 | */ |
144 | private void initAdminConfiguration(ServletContext servletContext) { |
145 | if (LOG.isDebugEnabled()) { |
146 | LOG.debug("Initializing Portal Admin Configuration..."); |
147 | } |
148 | AdminConfiguration adminConfig = DriverConfigurationFactory.getFactory().getAdminConfig(servletContext); |
149 | if (adminConfig == null) { |
150 | LOG.warn("Admin Configuration not available. " + "Administration will not be allowed."); |
151 | } else { |
152 | servletContext.setAttribute(ADMIN_CONFIG_KEY, adminConfig); |
153 | if (LOG.isInfoEnabled()) { |
154 | LOG.info("Admin Configuration initialized to: " + adminConfig.getClass().getName()); |
155 | } |
156 | } |
157 | } |
158 | |
159 | /** |
160 | * Initializes the portlet container. This method constructs and initializes |
161 | * the portlet container, and saves it to the servlet context scope. |
162 | * |
163 | * @param servletContext |
164 | * the servlet context. |
165 | */ |
166 | private void initContainer(ServletContext servletContext) { |
167 | |
168 | // Retrieve the driver configuration from servlet context. |
169 | DriverConfiguration driverConfig = (DriverConfiguration) servletContext.getAttribute(DRIVER_CONFIG_KEY); |
170 | |
171 | try { |
172 | |
173 | // Create portal context. |
174 | if (LOG.isDebugEnabled()) { |
175 | LOG.debug("Creating portal context [" + driverConfig.getPortalName() + "/" + driverConfig.getPortalVersion() + "]..."); |
176 | } |
177 | PortalContextImpl portalContext = new PortalContextImpl(driverConfig); |
178 | |
179 | // Create container services. |
180 | if (LOG.isDebugEnabled()) { |
181 | LOG.debug("Creating container services..."); |
182 | } |
183 | ContainerServicesImpl containerServices = new ContainerServicesImpl(portalContext, driverConfig); |
184 | |
185 | // Create portlet container. |
186 | if (LOG.isDebugEnabled()) { |
187 | LOG.debug("Creating portlet container..."); |
188 | } |
189 | PortletContainerFactory factory = PortletContainerFactory.getInstance(); |
190 | PortletContainer container = factory.createContainer(driverConfig.getContainerName(), containerServices, containerServices); |
191 | |
192 | // Initialize portlet container. |
193 | if (LOG.isDebugEnabled()) { |
194 | LOG.debug("Initializing portlet container..."); |
195 | } |
196 | container.init(servletContext); |
197 | |
198 | // Save portlet container to the servlet context scope. |
199 | servletContext.setAttribute(CONTAINER_KEY, container); |
200 | if (LOG.isInfoEnabled()) { |
201 | LOG.info("Pluto portlet container started."); |
202 | } |
203 | |
204 | } catch (DriverConfigurationException ex) { |
205 | LOG.error("Unable to retrieve driver configuration " + "due to configuration error: " + ex.getMessage(), ex); |
206 | } catch (PortletContainerException ex) { |
207 | LOG.error("Unable to start up portlet container: " + ex.getMessage(), ex); |
208 | } |
209 | } |
210 | |
211 | // Private Destruction Methods --------------------------------------------- |
212 | |
213 | /** |
214 | * Destroyes the portlet container and removes it from servlet context. |
215 | * |
216 | * @param servletContext |
217 | * the servlet context. |
218 | */ |
219 | private void destroyContainer(ServletContext servletContext) { |
220 | if (LOG.isInfoEnabled()) { |
221 | LOG.info("Shutting down Pluto Portal Driver..."); |
222 | } |
223 | PortletContainer container = (PortletContainer) servletContext.getAttribute(CONTAINER_KEY); |
224 | if (container != null) { |
225 | try { |
226 | container.destroy(); |
227 | if (LOG.isInfoEnabled()) { |
228 | LOG.info("Pluto Portal Driver shut down."); |
229 | } |
230 | } catch (PortletContainerException ex) { |
231 | LOG.error("Unable to shut down portlet container: " + ex.getMessage(), ex); |
232 | } finally { |
233 | servletContext.removeAttribute(CONTAINER_KEY); |
234 | } |
235 | } |
236 | } |
237 | |
238 | /** |
239 | * Destroyes the portal driver config and removes it from servlet context. |
240 | * |
241 | * @param servletContext |
242 | * the servlet context. |
243 | */ |
244 | private void destroyDriverConfiguration(ServletContext servletContext) { |
245 | DriverConfiguration driverConfig = (DriverConfiguration) servletContext.getAttribute(DRIVER_CONFIG_KEY); |
246 | if (driverConfig != null) { |
247 | try { |
248 | driverConfig.destroy(); |
249 | if (LOG.isInfoEnabled()) { |
250 | LOG.info("Pluto Portal Driver Config destroyed."); |
251 | } |
252 | } catch (DriverConfigurationException ex) { |
253 | LOG.error("Unable to destroy portal driver config: " + ex.getMessage(), ex); |
254 | } finally { |
255 | servletContext.removeAttribute(DRIVER_CONFIG_KEY); |
256 | } |
257 | } |
258 | } |
259 | |
260 | /** |
261 | * Destroyes the portal admin config and removes it from servlet context. |
262 | * |
263 | * @param servletContext |
264 | * the servlet context. |
265 | */ |
266 | private void destroyAdminConfiguration(ServletContext servletContext) { |
267 | AdminConfiguration adminConfig = (AdminConfiguration) servletContext.getAttribute(ADMIN_CONFIG_KEY); |
268 | if (adminConfig != null) { |
269 | try { |
270 | adminConfig.destroy(); |
271 | if (LOG.isInfoEnabled()) { |
272 | LOG.info("Pluto Portal Admin Config destroyed."); |
273 | } |
274 | } catch (DriverConfigurationException ex) { |
275 | LOG.error("Unable to destroy portal admin config: " + ex.getMessage(), ex); |
276 | } finally { |
277 | servletContext.removeAttribute(ADMIN_CONFIG_KEY); |
278 | } |
279 | } |
280 | } |
281 | |
282 | } |