KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > server > WebModuleDeployEventListener


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.server;
25
26 import com.sun.enterprise.admin.event.AdminEventListenerException;
27 import com.sun.enterprise.admin.event.AdminEventListenerRegistry;
28 import com.sun.enterprise.admin.event.DeployEventListenerHelper;
29 import com.sun.enterprise.admin.event.ModuleDeployEvent;
30 import com.sun.enterprise.admin.event.ModuleDeployEventListener;
31
32 import com.sun.enterprise.admin.common.MBeanServerFactory;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import com.sun.enterprise.config.ConfigBean;
37 import com.sun.enterprise.config.ConfigContext;
38 import com.sun.enterprise.config.ConfigException;
39 import com.sun.enterprise.config.serverbeans.ApplicationHelper;
40 import com.sun.enterprise.config.serverbeans.ApplicationRef;
41 import com.sun.enterprise.config.serverbeans.ApplicationRef;
42 import com.sun.enterprise.config.serverbeans.Server;
43 import com.sun.enterprise.config.serverbeans.Server;
44 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
45 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
46 import com.sun.enterprise.config.serverbeans.WebModule;
47 import com.sun.enterprise.deployment.Application;
48 import com.sun.enterprise.deployment.WebBundleDescriptor;
49 import com.sun.enterprise.deployment.WebServiceEndpoint;
50 import com.sun.enterprise.deployment.WebServicesDescriptor;
51 import com.sun.enterprise.instance.WebModulesManager;
52 import com.sun.enterprise.security.acl.RoleMapper;
53 import com.sun.enterprise.Switch;
54 import com.sun.enterprise.util.i18n.StringManager;
55 import com.sun.enterprise.util.StringUtils;
56 import com.sun.enterprise.web.VirtualServer;
57 import com.sun.enterprise.web.WebContainer;
58 import com.sun.enterprise.web.WebModuleConfig;
59 import com.sun.logging.LogDomains;
60
61 import java.io.File JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Vector JavaDoc;
64 import java.util.Iterator JavaDoc;
65 import java.util.Hashtable JavaDoc;
66 import java.util.logging.Logger JavaDoc;
67 import java.util.logging.Level JavaDoc;
68
69 import org.apache.catalina.Container;
70 import org.apache.catalina.Context;
71 import org.apache.catalina.Deployer;
72 import org.apache.catalina.Engine;
73 import org.apache.catalina.Host;
74 import org.apache.catalina.core.StandardHost;
75
76 /**
77  * A ModuleDeployEventListener for dynamic deployment support
78  * of web module.
79  *
80  * @author Amy Roh
81  */

82  
83 public class WebModuleDeployEventListener extends DummyWebModuleManager
84                                           implements ModuleDeployEventListener {
85
86
87     /** logger to log core messages */
88     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
89
90     /** local string manager */
91     private static StringManager localStrings =
92                         StringManager.getManager(WebModuleDeployEventListener.class);
93                 
94     private Hashtable JavaDoc cachedWebModules = new Hashtable JavaDoc();
95
96
97     /**
98      * Standard constructor.
99      */

100     public WebModuleDeployEventListener(WebModulesManager manager, ClassLoader JavaDoc loader) {
101         super(manager, loader);
102         AdminEventListenerRegistry.addModuleDeployEventListener(this);
103     }
104     
105     
106     // ----------------------------------------------------- Instance Variables
107

108
109     /**
110      * The WebContainer instance.
111      */

112     protected WebContainer webContainer = null;
113     
114     
115     // --------------------------------------------------------- Private Methods
116

117  
118     /**
119      * Creates and returns an object that contains information about
120      * the web module's configuration such as the information specified
121      * in server.xml, the deployment descriptor objects etc.
122      *
123      * @return null if an error occured while reading/parsing the
124      * deployment descriptors.
125      */

126     private WebModuleConfig loadWebModuleConfig(WebModule wm) {
127
128         WebModuleConfig wmInfo = new WebModuleConfig();
129         wmInfo.setBean(wm);
130         String JavaDoc wmID = wm.getName();
131         String JavaDoc location = wm.getLocation();
132         try {
133         Application app = getWebModulesManager().getDescriptor(wmID,
134                 location);
135             WebBundleDescriptor wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
136             wmInfo.setDescriptor(wbd);
137             String JavaDoc vs = ServerBeansFactory.getVirtualServersByAppName(
138                     wm.getConfigContext(), wmID);
139             wmInfo.setVirtualServers(vs);
140         } catch (ConfigException ce) {
141             wmInfo = null;
142         }
143         return wmInfo;
144         
145     }
146  
147     private void moduleDeployed(ConfigContext config, String JavaDoc moduleName)
148             throws AdminEventListenerException {
149
150         WebModule webModule = (WebModule)cachedWebModules.get(moduleName);
151         if ( webModule == null ){
152             webModule = getWebModule(config, moduleName);
153             cachedWebModules.put(moduleName, webModule);
154         }
155
156         //Do not deploy if enable is false
157
if (!isEnabled(config, moduleName)) {
158             return;
159         }
160         String JavaDoc location = webModule.getLocation();
161         // If module root is relative then prefix it with the
162
// location of where all the standalone modules for
163
// this server instance are deployed
164
File JavaDoc moduleBase = new File JavaDoc(location);
165         String JavaDoc modulesRoot = getWebContainer().getModulesRoot();
166         if (!moduleBase.isAbsolute()) {
167             location = modulesRoot+File.separator+location;
168             webModule.setLocation(location);
169         }
170         WebModuleConfig wmInfo = loadWebModuleConfig(webModule);
171         List JavaDoc<Throwable JavaDoc> throwables =
172             getWebContainer().loadWebModule(wmInfo, "null");
173         if (throwables != null && !throwables.isEmpty()) {
174             //we don't have the ability to return all errors. so return the
175
//first one, enough to signal the problem.
176
String JavaDoc msg = throwables.get(0).getMessage();
177             AdminEventListenerException ex =
178                 new AdminEventListenerException(msg);
179             ex.initCause(throwables.get(0));
180             throw ex;
181         }
182     }
183      
184          
185     private void moduleUndeployed(ConfigContext config, String JavaDoc moduleName)
186             throws AdminEventListenerException {
187
188         WebModule webModule = (WebModule)cachedWebModules.get(moduleName);
189         if ( webModule == null ){
190             webModule = getWebModule(config, moduleName);
191         } else {
192             cachedWebModules.remove(moduleName);
193         }
194
195         String JavaDoc appName = null;
196         WebBundleDescriptor wbd;
197         try {
198         Application app = getWebModulesManager().getDescriptor(
199                 webModule.getName(), webModule.getLocation());
200             RoleMapper.removeRoleMapper(app.getRoleMapper().getName());
201             wbd = (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
202             appName = app.getRegistrationName();
203         } catch (ConfigException ce) {
204             throw new AdminEventListenerException(ce);
205         }
206         String JavaDoc contextRoot = webModule.getContextRoot();
207         String JavaDoc virtualServers = null;
208         try {
209             virtualServers = ServerBeansFactory
210                                 .getVirtualServersByAppName(config,moduleName);
211         } catch(ConfigException ce) {
212             if (_logger.isLoggable(Level.FINEST)) {
213                 _logger.log(Level.FINEST,
214                 "Exception getting virtual servers by app name "
215                     + moduleName, ce);
216             }
217         }
218         try {
219             getWebContainer().unloadWebModule(contextRoot, appName,
220                                          virtualServers, wbd);
221         } finally {
222             try {
223               Switch.getSwitch().getNamingManager().unbindObjects(wbd);
224             } catch (javax.naming.NamingException JavaDoc nameEx) {
225                 _logger.log(Level.FINEST, "[WebModuleDeployEventListener] "
226                 + " Exception during namingManager.unbindObject",
227                 nameEx);
228             }
229         }
230     }
231     
232     // --------------------------------------------------------- Public Methods
233

234     
235     // ---- START OF ModuleDeployEventListener METHODS ------------------------
236

237     
238     /**
239      * Invoked when a standalone J2EE module is deployed.
240      *
241      * @param event the module deployment event
242      *
243      * @throws AdminEventListenerException when the listener is unable to
244      * process the event.
245      */

246     public synchronized void moduleDeployed(ModuleDeployEvent event)
247             throws AdminEventListenerException {
248
249         if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
250             if (_logger.isLoggable(Level.FINEST)) {
251                 _logger.log(Level.FINEST,
252                     "[WebModuleDeployEventListener] Handling event " + event.toString());
253             }
254
255             DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);
256
257             ConfigContext config = event.getConfigContext();
258             String JavaDoc moduleName = event.getModuleName();
259
260             // refreshes the config context with the context from this event
261
try {
262                 getWebModulesManager().refreshConfigContext(config);
263             } catch (ConfigException ce) {
264                 throw new AdminEventListenerException(ce.getMessage());
265             }
266
267             // The only job of dummyLoader is to inform the ondemand framework
268
// about this module. Before that, we need to disable this module
269
// so that webcontainer does not start this module during it's startup.
270
setEnable( moduleName, false);
271             AbstractLoader dummyLoader = getLoader(moduleName);
272             dummyLoader.load(false);
273             setEnable( moduleName, true);
274
275             moduleDeployed(config, moduleName);
276         }
277         
278     }
279
280     private void setEnable(String JavaDoc moduleName, boolean flag) {
281         try {
282             ConfigContext ctx = ApplicationServer.getServerContext().getConfigContext();
283             ConfigBean app = ApplicationHelper.findApplication(ctx, moduleName);
284             app.setEnabled(flag);
285             Server JavaDoc server = ServerBeansFactory.getServerBean(ctx);
286             ApplicationRef appRef = server.getApplicationRefByRef(moduleName);
287             appRef.setEnabled(flag);
288         } catch (ConfigException ce) {
289             if (_logger.isLoggable(Level.FINER)) {
290                 _logger.log(Level.FINER,
291                 "[WebModuleDeployEventListener] setEnable failed for " + moduleName);
292             }
293         }
294     }
295     
296     
297     /**
298      * Invoked when a standalone J2EE module is undeployed.
299      *
300      * @param event the module deployment event
301      *
302      * @throws AdminEventListenerException when the listener is unable to
303      * process the event.
304      */

305     public synchronized void moduleUndeployed(ModuleDeployEvent event)
306             throws AdminEventListenerException {
307
308         if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
309             if (_logger.isLoggable(Level.FINEST)) {
310                 _logger.log(Level.FINEST,
311                     "[WebModuleDeployEventListener] Handling event " + event.toString());
312             }
313             ConfigContext config = event.getOldConfigContext();
314             String JavaDoc moduleName = event.getModuleName();
315
316             // refreshes the config context with the context from this event
317
try {
318                 getWebModulesManager().refreshConfigContext(config);
319             } catch (ConfigException ce) {
320                 throw new AdminEventListenerException(ce.getMessage());
321             }
322
323             moduleUndeployed(config, moduleName);
324         }
325
326     }
327
328
329     /**
330      * Invoked when a standalone J2EE module is redeployed.
331      *
332      * @param event the module deployment event
333      *
334      * @throws AdminEventListenerException when the listener is unable to
335      * process the event.
336      */

337     public synchronized void moduleRedeployed(ModuleDeployEvent event)
338             throws AdminEventListenerException {
339
340         if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
341             if (_logger.isLoggable(Level.FINEST)) {
342                 _logger.log(Level.FINEST,
343                     "[WebModuleDeployEventListener] Handling event " + event.toString());
344             }
345             String JavaDoc moduleName = event.getModuleName();
346                 
347             // unload from old location
348
ConfigContext oldConfig = event.getOldConfigContext();
349             moduleUndeployed(oldConfig, moduleName);
350                
351             // load to new location
352
ConfigContext config = event.getConfigContext();
353             moduleDeployed(config, moduleName);
354         }
355         
356     }
357
358
359     /**
360      * Invoked when a standalone J2EE module is enabled.
361      *
362      * @param event the module deployment event
363      *
364      * @throws AdminEventListenerException when the listener is unable to
365      * process the event.
366      */

367     public synchronized void moduleEnabled(ModuleDeployEvent event)
368             throws AdminEventListenerException {
369
370         if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
371             if (_logger.isLoggable(Level.FINEST)) {
372                 _logger.log(Level.FINEST,
373                     "[WebModuleDeployEventListener] Handling event " + event.toString());
374             }
375             ConfigContext config = event.getConfigContext();
376             String JavaDoc moduleName = event.getModuleName();
377             WebModule webModule = (WebModule)cachedWebModules.get(moduleName);
378             if ( webModule == null ){
379                 webModule = getWebModule(config, moduleName);
380                 cachedWebModules.put(moduleName, webModule);
381             }
382
383             //Do not deploy if enable is false
384
if (!isEnabled(event.getConfigContext(), moduleName)) {
385                 return;
386             }
387
388             String JavaDoc location = webModule.getLocation();
389             // If module root is relative then prefix it with the
390
// location of where all the standalone modules for
391
// this server instance are deployed
392
File JavaDoc moduleBase = new File JavaDoc(location);
393             String JavaDoc modulesRoot = getWebContainer().getModulesRoot();
394             if (!moduleBase.isAbsolute()) {
395                 location = modulesRoot+File.separator+location;
396                 webModule.setLocation(location);
397             }
398             WebModuleConfig wmInfo = loadWebModuleConfig(webModule);
399             getWebContainer().enableWebModule(wmInfo, "null");
400         }
401         
402     }
403
404
405     /**
406      * Invoked when a standalone J2EE module is disabled.
407      *
408      * @param event the module deployment event
409      *
410      * @throws AdminEventListenerException when the listener is unable to
411      * process the event.
412      */

413     public synchronized void moduleDisabled(ModuleDeployEvent event)
414             throws AdminEventListenerException {
415
416         if (event.getModuleType().equals(ModuleDeployEvent.TYPE_WEBMODULE)) {
417             if (_logger.isLoggable(Level.FINEST)) {
418                 _logger.log(Level.FINEST,
419                     "[WebModuleDeployEventListener] Handling event " + event.toString());
420             }
421             ConfigContext config = event.getConfigContext();
422             String JavaDoc moduleName = event.getModuleName();
423             WebModule webModule = (WebModule)cachedWebModules.get(moduleName);
424             if ( webModule == null ){
425                 webModule = getWebModule(config, moduleName);
426             }
427
428             String JavaDoc appName = null;
429             WebBundleDescriptor wbd;
430             try {
431                 Application app = getWebModulesManager().getDescriptor(
432                     webModule.getName(), webModule.getLocation());
433                 wbd =
434                     (WebBundleDescriptor) app.getStandaloneBundleDescriptor();
435                 appName = app.getRegistrationName();
436             } catch (ConfigException ce) {
437                 throw new AdminEventListenerException(ce);
438             }
439             String JavaDoc contextRoot = webModule.getContextRoot();
440             String JavaDoc virtualServers = null;
441             try {
442                 virtualServers = ServerBeansFactory
443                                 .getVirtualServersByAppName(config,moduleName);
444             } catch(ConfigException ce) {
445                 if (_logger.isLoggable(Level.FINEST)) {
446                     _logger.log(Level.FINEST,
447                     "Exception getting virtual servers by app name "
448                     + moduleName, ce);
449                 }
450             }
451             getWebContainer().disableWebModule(contextRoot,appName,
452                 virtualServers);
453         }
454         
455     }
456
457     private WebModule getWebModule(ConfigContext config, String JavaDoc moduleName)
458         throws AdminEventListenerException {
459         WebModule webModule = null;
460         try {
461           webModule = (WebModule) ApplicationHelper.findApplication(
462               config, moduleName);
463         } catch (ConfigException ce) {
464             throw new AdminEventListenerException(ce);
465         }
466  
467         if (webModule == null) {
468             String JavaDoc msg = localStrings.getString(
469                 "webmodule.module_not_found",
470                 moduleName);
471             throw new AdminEventListenerException(msg);
472         }
473         return webModule;
474     }
475
476     /**
477      * Invoked when a reference is created from a
478      * server instance (or cluster) to a particular module.
479      *
480      * @throws AdminEventListenerException when the listener is unable to
481      * process the event.
482      */

483     public void moduleReferenceAdded(ModuleDeployEvent event)
484             throws AdminEventListenerException {
485     }
486
487     /**
488      * Invoked when a reference is removed from a
489      * server instance (or cluster) to a particular module.
490      *
491      * @throws AdminEventListenerException when the listener is unable to
492      * process the event.
493      */

494     public void moduleReferenceRemoved(ModuleDeployEvent event)
495             throws AdminEventListenerException {
496     }
497     
498     
499     // ---- END OF ModuleDeployEventListener METHODS --------------------------
500

501     /**
502      * Whether or not a web module should be
503      * enabled is defined by the "enable" attribute on both the
504      * module element and the application-ref element.
505      *
506      * @param config The dynamic ConfigContext
507      * @param moduleName The name of the component (application or module)
508      * @return boolean
509      */

510     protected boolean isEnabled(ConfigContext config, String JavaDoc moduleName) {
511         try {
512             ConfigBean app = ApplicationHelper.findApplication(config, moduleName);
513
514             Server JavaDoc server = ServerBeansFactory.getServerBean(config);
515             ApplicationRef appRef = server.getApplicationRefByRef(moduleName);
516
517             return ((app != null && app.isEnabled()) &&
518                         (appRef != null && appRef.isEnabled()));
519         } catch (ConfigException e) {
520             AdminEventListenerException ex = new AdminEventListenerException();
521             ex.initCause(e);
522             _logger.log(Level.FINE, "Error in finding " + moduleName, e);
523
524             //If there is anything wrong, do not enable the module
525
return false;
526         }
527     }
528
529     private WebModulesManager getWebModulesManager() throws ConfigException {
530         return (WebModulesManager) this.configManager;
531     }
532
533     private WebContainer getWebContainer() {
534         if (webContainer == null) {
535             this.webContainer = WebContainer.getInstance();
536         }
537         return this.webContainer;
538     }
539
540 }
541
Popular Tags