KickJava   Java API By Example, From Geeks To Geeks.

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


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 /*
25  * @(#) StandAloneEJBModulesManager.java
26  *
27  * Copyright 2000-2001 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  *
31  * This software is the confidential and proprietary information
32  * of iPlanet/Sun Microsystems, Inc. ("Confidential Information").
33  * You shall not disclose such Confidential Information and shall
34  * use it only in accordance with the terms of the license
35  * agreement you entered into with iPlanet/Sun Microsystems.
36  */

37 package com.sun.enterprise.server;
38
39 import com.sun.enterprise.admin.event.AdminEventListenerException;
40 import com.sun.enterprise.admin.event.AdminEventListenerRegistry;
41 import com.sun.enterprise.admin.event.BaseDeployEvent;
42 import com.sun.enterprise.admin.event.DeployEventListenerHelper;
43 import com.sun.enterprise.admin.event.ModuleDeployEvent;
44 import com.sun.enterprise.admin.event.ModuleDeployEventListener;
45 import com.sun.enterprise.config.ConfigException;
46 import com.sun.enterprise.instance.EjbModulesManager;
47
48 import com.sun.enterprise.security.acl.RoleMapper;
49 import com.sun.enterprise.deployment.Application;
50
51 import com.sun.enterprise.util.i18n.StringManager;
52 import com.sun.enterprise.deployment.backend.Deployer;
53 import com.sun.enterprise.deployment.backend.DeployerFactory;
54 import com.sun.enterprise.deployment.backend.DeploymentRequest;
55 import com.sun.enterprise.deployment.backend.DeploymentCommand;
56 import com.sun.enterprise.deployment.backend.DeployableObjectType;
57 import com.sun.enterprise.deployment.backend.IASDeploymentException;
58 import com.sun.enterprise.config.ConfigContext;
59
60 import java.util.logging.Logger JavaDoc;
61 import java.util.logging.Level JavaDoc;
62 import com.sun.logging.LogDomains;
63
64 //for jsr77
65
import com.sun.enterprise.deployment.io.ApplicationDeploymentDescriptorFile;
66 import com.sun.enterprise.deployment.io.EjbDeploymentDescriptorFile;
67 import com.sun.enterprise.deployment.node.J2EEDocumentBuilder;
68 import java.util.Iterator JavaDoc;
69 import com.sun.enterprise.deployment.EjbBundleDescriptor;
70 import javax.management.MBeanException JavaDoc;
71 import com.sun.enterprise.management.StateManageable;
72 import com.sun.enterprise.deployment.autodeploy.AutoDirReDeployer;
73
74 /**
75  * Manages all stand alone EJB modules. It also acts as an listener for
76  * the deployment events.
77  *
78  * @author Mahesh Kannan
79  * @author Nazrul Islam
80  * @since JDK1.4
81  */

82 public class StandAloneEJBModulesManager extends AbstractManager
83         implements ModuleDeployEventListener {
84
85     /** logger to log core messages */
86     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.CORE_LOGGER);
87
88     /** local string manager */
89     private static StringManager localStrings =
90                 StringManager.getManager(StandAloneEJBModulesManager.class);
91
92     /**
93      * This manager is responsible for loading all stand alone ejb modules
94      * in an instance. It creates EJBModuleLoader for each deployed ejb module.
95      * EJBModuleLoader is responsible for further loading ejb containers for
96      * each module.
97      *
98      * <p>
99      * It is assumed that the next two params are set by the startup Code.
100      *
101      * @param ejbManager encapsulates application related info
102      * @param commonclassLoader the top level class loader. For now this
103      * can be the MainThread's ClassLoader.
104      */

105     StandAloneEJBModulesManager(EjbModulesManager ejbManager,
106             ClassLoader JavaDoc parentClassLoader) {
107
108         super(parentClassLoader, ejbManager);
109
110         AdminEventListenerRegistry.addModuleDeployEventListener(this);
111     }
112
113     /**
114      * Returns a loader for stand alone ejb module.
115      *
116      * @param moduleId registration name of the ejb module
117      *
118      * @return a loader for the stand alone ejb module
119      */

120     protected AbstractLoader getLoader(String JavaDoc moduleId) {
121         return new EJBModuleLoader(moduleId, this.parentClassLoader,
122             (EjbModulesManager)this.configManager);
123     }
124
125     private void holdRequest() { }
126
127     private void holdRequest(String JavaDoc appID) { }
128
129     // ---- START OF MonitorListener METHODS ----------------------------------
130

131     /**
132      * This is a callback from reload monitor when a change in time stamp
133      * for a stand alone ejb module is detected.
134      *
135      * @param entry monitored entry with a change in time stamp
136      * @return if stand alone ejb module was reloaded successfully
137      */

138     public synchronized boolean reload(MonitorableEntry entry) {
139
140         String JavaDoc moduleName = entry.getId();
141         boolean status = false;
142
143         // redeploys the stand alone ejb module
144
try {
145
146             DeploymentRequest req = new DeploymentRequest(
147                             this.configManager.getInstanceEnvironment(),
148                             DeployableObjectType.EJB,
149                             DeploymentCommand.DEPLOY);
150
151             // monitored file points to $APP_ROOT/.reload
152
req.setFileSource(entry.getMonitoredFile().getParentFile());
153
154             // application registration name
155
req.setName(moduleName);
156
157             // we are always trying a redeployment
158
req.setForced(true);
159
160        AutoDirReDeployer deployer = new AutoDirReDeployer(req);
161        status = deployer.redeploy();
162
163         } catch (IASDeploymentException de) {
164             _logger.log(Level.WARNING,"core.error_in_reload_ejb_module",de);
165             return false;
166         }
167         return status;
168     }
169     // ---- END OF MonitorListener METHODS ------------------------------------
170

171     private boolean moduleDeployed(boolean jsr77, String JavaDoc moduleName,
172         ConfigContext dynamicConfigContext)
173     {
174     return moduleDeployed(jsr77, moduleName, true, dynamicConfigContext);
175     }
176
177     private boolean moduleDeployed(boolean jsr77, String JavaDoc moduleName,
178         boolean addToRM, ConfigContext dynamicConfigContext)
179     {
180         boolean result = false;
181         boolean loadJSR77 = jsr77 || loadJSR77(moduleName, DeployableObjectType.EJB);
182
183         AbstractLoader modLoader = null;
184         try {
185
186             modLoader = getLoader(moduleName);
187         modLoader.setConfigContext(dynamicConfigContext);
188
189             // create root mBean for this module
190
if (loadJSR77) {
191                 try {
192                     modLoader.createRootMBean();
193                 } catch (MBeanException JavaDoc mbe) {
194                     _logger.log(Level.WARNING,
195                         "core.error_while_creating_jsr77_root_mbean",mbe);
196                 }
197             }
198
199             if (isEnabled(dynamicConfigContext, moduleName)) {
200
201                 if (_logger.isLoggable(Level.FINEST)) {
202                    _logger.log(Level.FINEST,
203                            "Loading enabled ejb module: " + moduleName);
204                 }
205
206                 //Check to see if the app is already loaded. If yes, do not reload.
207
if (id2loader.get(moduleName) != null) {
208                     return true;
209                 }
210
211                 // set jsr77 state STARTING
212
try {
213                     modLoader.setState(StateManageable.STARTING_STATE);
214                 } catch (MBeanException JavaDoc mbe) {
215                     _logger.log(Level.WARNING,
216                             "core.error_while_setting_jsr77_state",mbe);
217                 }
218
219                 boolean retSts = modLoader.load(jsr77);
220
221                 if (retSts) {
222                     this.id2loader.put(moduleName, modLoader);
223
224                     // adds the ejb module to be monitored if ON
225
if (addToRM) {
226                         addToReloadMonitor(moduleName);
227                     }
228
229                     // set jsr77 state to RUNNING
230
try {
231                         modLoader.setState(StateManageable.RUNNING_STATE);
232                     } catch (MBeanException JavaDoc mbe) {
233                         _logger.log(Level.WARNING,
234                             "core.error_while_setting_jsr77_state",mbe);
235                     }
236
237                     result = true;
238                 } else {
239                     if (loadJSR77) {
240                         // delete root and leaf mBeans for this module
241
try {
242                             modLoader.deleteLeafAndRootMBeans();
243                         } catch (MBeanException JavaDoc mbe) {
244                             _logger.log(Level.WARNING,
245                             "core.error_while_deleting_jsr77_root_and_leaf_mbeans",mbe);
246                         }
247                     } else {
248                         // set jsr77 state FAILED
249
try {
250                             modLoader.setState(StateManageable.FAILED_STATE);
251                         } catch (MBeanException JavaDoc mbe) {
252                             _logger.log(Level.WARNING,
253                                 "core.error_while_setting_jsr77_state",mbe);
254                         }
255                     }
256
257                     _logger.log(Level.WARNING,
258                                 "core.error_while_loading_ejb_module");
259                 }
260             } else {
261                 _logger.log(Level.INFO, "core.ejb_module_disabled", moduleName);
262                 //setting result to false since module was disabled
263
result = false;
264             }
265         } catch (ConfigException ce) {
266                         _logger.log(Level.WARNING,"core.error_while_loading_ejb_module",ce);
267             result = false;
268         } finally {
269         //To ensure that the config context is not reused...
270
if (modLoader != null) {
271         modLoader.setConfigContext(null);
272         }
273     }
274         return result;
275     }
276
277     /**
278      * Unloads the given stand alone ejb module.
279      * This removes this ejb module from reload monitor list.
280      * This gets called when the ejb module is undeployed.
281      *
282      * @param moduleName name of the stand alone ejb module
283      * @return true if module was unloaded successfully
284      *
285      * @throws ConfigException if an error while reading data from config
286      */

287     private boolean moduleUnDeployed(String JavaDoc moduleName) {
288         return moduleUnDeployed(false, moduleName);
289     }
290
291     /**
292      * Unloads the given stand alone ejb module.
293      * This removes this ejb module from reload monitor list.
294      * This gets called when the ejb module is undeployed.
295      *
296      * @param jsr77 delete jsr77 mBeans if true
297      * @param moduleName name of the stand alone ejb module
298      * @return true if module was unloaded successfully
299      *
300      * @throws ConfigException if an error while reading data from config
301      */

302     private boolean moduleUnDeployed(boolean jsr77, String JavaDoc moduleName) {
303         return moduleUnDeployed(jsr77, moduleName, true);
304     }
305
306     /**
307      * Unloads the given ejb module.
308      *
309      * @param jsr77 delete jsr77 mBeans if true
310      * @param moduleName name of the stand alone ejb module
311      * @param clearRM reloadMonitor, reload if true
312      * @return true if module was unloaded successfully
313      *
314      * @throws ConfigException if an error while reading data from config
315      */

316     private boolean moduleUnDeployed(boolean jsr77, String JavaDoc moduleName, boolean clearRM) {
317
318         EJBModuleLoader modLoader =
319             (EJBModuleLoader) this.id2loader.remove(moduleName);
320
321         // removes this ejb module from reload monitor
322
if (clearRM) {
323             removeFromReloadMonitor(moduleName);
324         }
325
326         // module is not in the registry - must have been undeployed
327
if (modLoader == null) {
328             return true;
329         }
330
331         // set jsr77 state to STOPPING
332
try {
333             modLoader.setState(StateManageable.STOPPING_STATE);
334         } catch (MBeanException JavaDoc mbe) {
335             _logger.log(Level.WARNING,
336                 "core.error_while_setting_jsr77_state",mbe);
337         }
338
339         Application app = modLoader.getApplication();
340         RoleMapper.removeRoleMapper(app.getRoleMapper().getName());
341
342         if (jsr77) {
343             // delete jsr77 mBean for this ejbModule
344
try {
345                 modLoader.deleteRootMBean();
346             } catch (MBeanException JavaDoc mbe) {
347                 _logger.log(Level.WARNING,
348                         "core.error_while_deleting_jsr77_root_mbean",mbe);
349             }
350         } else {
351             // set jsr77 state to STOPPED
352
try {
353                 modLoader.setState(StateManageable.STOPPED_STATE);
354             } catch (MBeanException JavaDoc mbe) {
355                 _logger.log(Level.WARNING,
356                         "core.error_while_setting_jsr77_state",mbe);
357             }
358         }
359
360         boolean undeployed = modLoader.unload(jsr77);
361
362         if (undeployed) {
363             _logger.log(Level.INFO,
364                         "core.ejb_module_unload_successful", moduleName);
365         } else {
366             _logger.log(Level.INFO,
367                         "core.ejb_module_not_unloaded", moduleName);
368         }
369
370         return undeployed;
371     }
372
373     // ---- START OF ModuleDeployEventListener METHODS ------------------------
374

375     /**
376      * Invoked when a standalone J2EE module is deployed.
377      */

378         public synchronized void moduleDeployed(ModuleDeployEvent event)
379             throws AdminEventListenerException {
380
381         boolean jsr77 = false;
382
383         if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
384             
385             DeployEventListenerHelper.getDeployEventListenerHelper().synchronize(event);
386
387             String JavaDoc modID = event.getModuleName();
388
389             if (_logger.isLoggable(Level.FINEST)) {
390                 _logger.log(Level.FINEST,
391                             "Deploying EJB Module: " + modID);
392             }
393
394             try {
395                 // refreshes the config context with the context from this event
396
this.configManager.refreshConfigContext(
397                                         event.getConfigContext() );
398
399                 // set jsr77 flag
400
String JavaDoc action = event.getAction();
401                 if ((action.equals(BaseDeployEvent.DEPLOY)) ||
402                      (action.equals(BaseDeployEvent.REDEPLOY))) {
403                     jsr77 = true;
404                 }
405
406                 if (isEnabled(event.getConfigContext(), modID) &&
407                       !moduleDeployed(jsr77, modID, event.getConfigContext())) {
408                           
409                     String JavaDoc msg = localStrings.getString(
410                         "standaloneejbmodulesmgr.ejbmodule_deployed_failed",
411                         modID);
412                     registerException(event, msg);
413                 }
414             } catch (ConfigException ce) {
415                 throw new AdminEventListenerException(ce.getMessage());
416             }
417         }
418     }
419
420    /**
421     * Invoked when a standalone J2EE module is undeployed.
422     */

423     public synchronized void moduleUndeployed(ModuleDeployEvent event)
424             throws AdminEventListenerException {
425
426         boolean jsr77 = false;
427
428         String JavaDoc action = event.getAction();
429         if ((action.equals(BaseDeployEvent.UNDEPLOY)) ||
430             (action.equals(BaseDeployEvent.REDEPLOY))) {
431             jsr77 = true;
432         }
433
434         try {
435             if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
436
437                 String JavaDoc modID = event.getModuleName();
438
439                 if (_logger.isLoggable(Level.FINEST)) {
440                     _logger.log(Level.FINEST,
441                                 "Undeploying EJB Module: " + modID);
442                 }
443
444                 // refreshes the config context with the context from this event
445
this.configManager.refreshConfigContext(
446                                 event.getOldConfigContext() );
447
448                 boolean undeployed = moduleUnDeployed(jsr77, modID);
449
450                 if (!undeployed) {
451                     String JavaDoc msg = localStrings.getString(
452                         "standaloneejbmodulesmgr.ejbmodule_undeployed_failed",
453                         modID);
454                     registerException(event, msg);
455                 }
456             }
457         } catch (ConfigException ce) {
458             throw new AdminEventListenerException(ce.getMessage());
459         }
460     }
461
462    /**
463     * Invoked when a standalone J2EE module is redeployed.
464     */

465     public synchronized void moduleRedeployed(ModuleDeployEvent event)
466             throws AdminEventListenerException {
467
468         if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
469
470             String JavaDoc modID = event.getModuleName();
471
472                         if (_logger.isLoggable(Level.FINEST)) {
473                     _logger.log(Level.FINEST,
474                             "Redeploying EJB Module: " + modID);
475             }
476
477             moduleUndeployed(event);
478             moduleDeployed(event);
479         }
480     }
481
482    /**
483     * Invoked when a standalone J2EE module is enabled.
484     */

485     public synchronized void moduleEnabled(ModuleDeployEvent event)
486             throws AdminEventListenerException {
487
488         if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
489
490             String JavaDoc modID = event.getModuleName();
491
492             if (!isEnabled(event.getConfigContext(), modID)) {
493                 return;
494             }
495
496             if (_logger.isLoggable(Level.FINEST)) {
497                 _logger.log(Level.FINEST, "Enabling EJB Module: " + modID);
498             }
499
500             moduleDeployed(event);
501         }
502     }
503
504    /**
505     * Invoked when a standalone J2EE module is disabled.
506     */

507     public synchronized void moduleDisabled(ModuleDeployEvent event)
508             throws AdminEventListenerException {
509
510         if (event.getModuleTypeCode() == event.TYPE_EJBMODULE_CODE) {
511
512             String JavaDoc modID = event.getModuleName();
513
514                         if (_logger.isLoggable(Level.FINEST)) {
515                     _logger.log(Level.FINEST,
516                             "Disabling EJB Module: " + modID);
517             }
518
519             moduleUndeployed(event);
520         }
521     }
522     
523     
524 /**
525      * Invoked when a reference is created from a
526      * server instance (or cluster) to a particular module.
527      *
528      * @throws AdminEventListenerException when the listener is unable to
529      * process the event.
530      */

531     public void moduleReferenceAdded(ModuleDeployEvent event)
532             throws AdminEventListenerException {
533                 
534 }
535
536     /**
537      * Invoked when a reference is removed from a
538      * server instance (or cluster) to a particular module.
539      *
540      * @throws AdminEventListenerException when the listener is unable to
541      * process the event.
542      */

543     public void moduleReferenceRemoved(ModuleDeployEvent event)
544             throws AdminEventListenerException {
545                 
546     }
547     
548     
549
550     // ---- END OF ModuleDeployEventListener METHODS ------------------------
551

552
553 }
554
Popular Tags