KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#) EJBModuleLoader.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 java.util.Set JavaDoc;
40 import com.sun.enterprise.loader.EJBClassPathUtils;
41 import com.sun.enterprise.config.ConfigException;
42 import com.sun.enterprise.instance.EjbModulesManager;
43
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46 import com.sun.logging.LogDomains;
47 import com.sun.enterprise.loader.EJBClassLoader;
48
49 // for jsr77
50
import com.sun.enterprise.instance.InstanceEnvironment;
51 import com.sun.enterprise.deployment.io.EjbDeploymentDescriptorFile;
52 import com.sun.enterprise.deployment.node.J2EEDocumentBuilder;
53 import com.sun.enterprise.deployment.BundleDescriptor;
54 import com.sun.enterprise.deployment.Descriptor;
55 import com.sun.enterprise.deployment.EjbDescriptor;
56 import com.sun.enterprise.deployment.EjbBundleDescriptor;
57 import com.sun.enterprise.deployment.Application;
58 import java.util.Iterator JavaDoc;
59 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
60 import javax.management.MBeanException JavaDoc;
61 import com.sun.enterprise.Switch;
62
63 import com.sun.enterprise.server.event.ApplicationEvent;
64
65 /**
66  * EJB loader loads and unloads stand alone ejb module.
67  *
68  * @author Mahesh Kannan
69  * @author Nazrul Islam
70  * @since JDK1.4
71  */

72 class EJBModuleLoader extends AbstractLoader {
73  
74     static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.LOADER_LOGGER);
75
76     /**
77      * EJBModuleLoaderer loads one module.
78      *
79      * @param modID the name of the ejb module
80      * @param parentClassLoader the parent class loader
81      * @param ejbModulesManager the ejb module mgr for this VS
82      */

83     EJBModuleLoader(String JavaDoc modID, ClassLoader JavaDoc parentClassLoader,
84             EjbModulesManager ejbModulesManager) {
85
86         super(modID, parentClassLoader, ejbModulesManager);
87         boolean createClassLoader = true;
88         try {
89             application = ejbModulesManager.getRegisteredDescriptor(modID);
90
91             //application object would be null if this is during server
92
//startup or deployment to remote instance
93
if (application == null) {
94                 application= (Application)
95                     ejbModulesManager.getDescriptor(modID, parentClassLoader);
96                 createClassLoader = false;
97             }
98             //not null during deployment on DAS or in some case of
99
//remote serer start up where the clasloader is initialized
100
//by the ResourceUtil
101
else {
102                 try {
103                     //the following check is to make sure we do not create
104
//a new classloader if one is already initialized and
105
//not void. note that the classloader used in deployment
106
//is voided (done called) at this point.
107
ClassLoader JavaDoc clazzloader = application.getClassLoader();
108                     if (clazzloader != null
109                         && (clazzloader instanceof EJBClassLoader)
110                         && !((EJBClassLoader)clazzloader).isDone()) {
111                             createClassLoader = false;
112                     }
113                 } catch (Exception JavaDoc ex) {}
114             }
115
116             if (createClassLoader) {
117                 String JavaDoc[] classPath = (String JavaDoc[])
118                     EJBClassPathUtils.getModuleClasspath(
119                         modID, null, ejbModulesManager).toArray(new String JavaDoc[0]);
120                 initializeLoader(classPath, ejbModulesManager.getLocation(modID), ModuleType.EJB);
121                 application.setClassLoader(this.ejbClassLoader);
122                 if (application.isVirtual()) { //better be
123
BundleDescriptor bd =
124                         application.getStandaloneBundleDescriptor();
125                     bd.setClassLoader(ejbClassLoader);
126                 }
127             } else {
128                 initializeLoader(application.getClassLoader());
129             }
130         } catch (Exception JavaDoc confEx) {
131             //@@ i18n
132
_logger.log(Level.SEVERE, "ERROR while loading application " + modID);
133             _logger.log(Level.SEVERE,"loader.error_while_loading_app_desc",
134                         confEx);
135         }
136     }
137     
138     /**
139      * Loads all the beans in this stand alone ejb module.
140      * This routine creates the EJB and MDB container.
141      *
142      * @param jsr77 create jsr77 mBeans if true
143      * @return true if all ejbs loaded properly
144      */

145     boolean load(boolean jsr77) {
146     //Note: Application.isVirtual will be true for stand-alone module
147
notifyAppEvent(ApplicationEvent.BEFORE_APPLICATION_LOAD);
148         boolean pusLoaded = false;
149         if(application.isVirtual()) { // standalone ejb jar
150
// load persistence units for standalone ejb jars only
151
// because for embedded ejb-jars, AppliationLoader loads them.
152
if(!loadPersistenceUnits()) {
153                 return false; // abort loading at this point.
154
} else {
155                 pusLoaded = true;
156             }
157         }
158         boolean status = loadEjbs(jsr77);
159
160     if (status == true) {
161         notifyAppEvent(ApplicationEvent.AFTER_APPLICATION_LOAD);
162             loadWebserviceEndpoints(jsr77);
163     } else {
164             if(pusLoaded) { // unload iff we have loaded.
165
unloadPersistenceUnits();
166             }
167         }
168
169     return status;
170     }
171         
172     /**
173      * Unloads the beans in this stand alone ejb module.
174      *
175      * @param jsr77 delete jsr77 mBeans if true
176      * @return true if removed successful
177      */

178     boolean unload(boolean jsr77) {
179         // undeploy the ejb modules
180

181     //Note: Application.isVirtual will be true for stand-alone module
182
notifyAppEvent(ApplicationEvent.BEFORE_APPLICATION_UNLOAD);
183
184         boolean result = unloadEjbs(jsr77);
185         unloadWebserviceEndpoints(jsr77);
186
187         if(application.isVirtual()) { // standalone jar
188
// unload persistence units for standalone ejb jars only
189
// because for embedded ejb jar, AppliationLoader unloads them.
190
result &= unloadPersistenceUnits();
191         }
192
193         configManager.unregisterDescriptor(id);
194
195     notifyAppEvent(ApplicationEvent.AFTER_APPLICATION_UNLOAD);
196
197         // helps garbage collector
198
done();
199         return result;
200     }
201
202
203     /**
204      * Create jsr77 root mBean
205      */

206     void createRootMBean() throws MBeanException JavaDoc {
207
208         EjbDeploymentDescriptorFile eddf = null;
209
210         java.util.Set JavaDoc ejbBundles = this.application.getEjbBundleDescriptors();
211
212         for(Iterator JavaDoc it=ejbBundles.iterator(); it.hasNext(); ) {
213
214             EjbBundleDescriptor bundleDesc = (EjbBundleDescriptor)it.next();
215
216         try {
217                 Switch.getSwitch().getManagementObjectManager().createEJBModuleMBean(
218             bundleDesc,
219             this.configManager.getInstanceEnvironment().getName(),
220             this.configManager.getLocation(this.id));
221         } catch (Exception JavaDoc e) {
222             throw new MBeanException JavaDoc(e);
223         }
224         }
225     }
226
227
228     /**
229      * Delete jsr77 root mBean
230      */

231     void deleteRootMBean() throws MBeanException JavaDoc {
232
233         java.util.Set JavaDoc ejbBundles = this.application.getEjbBundleDescriptors();
234
235         for(Iterator JavaDoc it=ejbBundles.iterator(); it.hasNext(); ) {
236
237             EjbBundleDescriptor bundleDesc = (EjbBundleDescriptor)it.next();
238
239             Switch.getSwitch().getManagementObjectManager().deleteEJBModuleMBean(bundleDesc,
240                     this.configManager.getInstanceEnvironment().getName());
241         }
242     }
243
244
245     /**
246      * Create jsr77 mBeans for ejbs within this module
247      */

248     void createLeafMBeans() throws MBeanException JavaDoc {
249
250         java.util.Set JavaDoc ejbBundles = this.application.getEjbBundleDescriptors();
251
252         for(Iterator JavaDoc it=ejbBundles.iterator(); it.hasNext(); ) {
253
254             EjbBundleDescriptor bundleDesc = (EjbBundleDescriptor)it.next();
255
256             Switch.getSwitch().getManagementObjectManager().createEJBMBeans(bundleDesc,
257                     this.configManager.getInstanceEnvironment().getName());
258         }
259     }
260
261
262     /**
263      * Create jsr77 mBeans for ejbs within this module
264      */

265     void createLeafMBean(Descriptor descriptor) throws MBeanException JavaDoc {
266
267         EjbDescriptor ejbDescriptor = null;
268         try {
269             ejbDescriptor = (EjbDescriptor) descriptor;
270         } catch (Exception JavaDoc e) {
271             throw new MBeanException JavaDoc(e);
272         }
273
274         Switch.getSwitch().getManagementObjectManager().createEJBMBean(ejbDescriptor,
275                     this.configManager.getInstanceEnvironment().getName());
276     }
277
278
279     /**
280      * Delete jsr77 mBeans for ejbs within this module
281      */

282     void deleteLeafMBeans() throws MBeanException JavaDoc {
283
284         java.util.Set JavaDoc ejbBundles = this.application.getEjbBundleDescriptors();
285
286         for(Iterator JavaDoc it=ejbBundles.iterator(); it.hasNext(); ) {
287
288             EjbBundleDescriptor bundleDesc = (EjbBundleDescriptor)it.next();
289
290             Switch.getSwitch().getManagementObjectManager().deleteEJBMBeans(bundleDesc,
291                     this.configManager.getInstanceEnvironment().getName());
292         }
293     }
294
295     /**
296      * Delete jsr77 mBeans for ejbs within this module
297      */

298     void deleteLeafMBean(Descriptor descriptor) throws MBeanException JavaDoc {
299
300         EjbDescriptor ejbDescriptor = null;
301         try {
302             ejbDescriptor = (EjbDescriptor) descriptor;
303         } catch (Exception JavaDoc e) {
304             throw new MBeanException JavaDoc(e);
305         }
306         Switch.getSwitch().getManagementObjectManager().deleteEJBMBean(ejbDescriptor,
307                     this.configManager.getInstanceEnvironment().getName());
308     }
309
310
311     /**
312      * Delete jsr77 mBeans for the module and its' components
313      */

314     void deleteLeafAndRootMBeans() throws MBeanException JavaDoc {
315         deleteLeafMBeans();
316         deleteRootMBean();
317     }
318
319
320     /**
321      * Set state for the root mBean
322      */

323     void setState(int state) throws MBeanException JavaDoc {
324
325         java.util.Set JavaDoc ejbBundles = this.application.getEjbBundleDescriptors();
326
327         for(Iterator JavaDoc it=ejbBundles.iterator(); it.hasNext(); ) {
328
329             EjbBundleDescriptor bundleDesc = (EjbBundleDescriptor)it.next();
330
331             Switch.getSwitch().getManagementObjectManager().setEJBModuleState(state, bundleDesc,
332                 this.configManager.getInstanceEnvironment().getName());
333         }
334     }
335
336 }
337
Popular Tags