KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#) ApplicationLoader.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.io.File JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Set JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import com.sun.enterprise.Switch;
47 import com.sun.enterprise.loader.EJBClassPathUtils;
48 import com.sun.enterprise.instance.AppsManager;
49 import com.sun.enterprise.config.ConfigException;
50 import java.util.jar.Attributes JavaDoc;
51 import java.util.jar.JarFile JavaDoc;
52 import java.util.jar.Manifest JavaDoc;
53 import java.util.logging.Logger JavaDoc;
54 import java.util.logging.Level JavaDoc;
55 import com.sun.logging.LogDomains;
56 import com.sun.enterprise.loader.EJBClassLoader;
57 import javax.management.MBeanException JavaDoc;
58 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
59 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
60 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
61 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
62 import com.sun.enterprise.deployment.*;
63 import com.sun.enterprise.deployment.util.ModuleDescriptor;
64
65 import com.sun.enterprise.server.event.ApplicationEvent;
66
67 /**
68  * Application loader loads and unloads an applicaton.
69  *
70  * @author Mahesh Kannan
71  * @author Nazrul Islam
72  * @since JDK1.4
73  */

74 public class ApplicationLoader extends AbstractLoader {
75  
76     /** logger to log loader messages */
77     static Logger JavaDoc _logger = LogDomains.getLogger(LogDomains.LOADER_LOGGER);
78
79     /** Indicates if the deployment being performed is a force Deploy */
80     private boolean isForceDeploy = false;
81     
82     /**
83      * ApplicationLoader loads one application.
84      *
85      * @param appID the name of the application
86      * @param parentClassLoader parent class loader for this application
87      * @param appsManager the AppsManager for this VS
88      */

89     public ApplicationLoader(String JavaDoc appID, ClassLoader JavaDoc parentClassLoader,
90             AppsManager appsManager) {
91
92         super(appID, parentClassLoader, appsManager);
93         boolean createClassLoader = true;
94         try {
95             application = appsManager.getRegisteredDescriptor(appID);
96             // if application was registered, it has to be the complete
97
// descriptor.
98
boolean finishLoading = application==null;
99             
100             //application object would be null if this is during server
101
//startup or deployment to remote instance
102
if (application==null) {
103                 application = appsManager.getAppDescriptor(appID, parentClassLoader);
104                 createClassLoader = false;
105             }
106             //not null during deployment on DAS or in some case of
107
//remote serer start up where the clasloader is initialized
108
//by the ResourceUtil
109
else {
110                 try {
111                     //the following check is to make sure we do not create
112
//a new classloader if one is already initialized and
113
//not void. note that the classloader used in deployment
114
//is voided (done called) at this point.
115
ClassLoader JavaDoc clazzloader = application.getClassLoader();
116                     if (clazzloader != null
117                         && (clazzloader instanceof EJBClassLoader)
118                         && !((EJBClassLoader)clazzloader).isDone()) {
119                         createClassLoader = false;
120                     }
121                 } catch (Exception JavaDoc ex) {}
122             }
123             
124             if (createClassLoader) {
125                 String JavaDoc[] classPath = (String JavaDoc[])
126                 EJBClassPathUtils.getAppClasspath(application, appsManager).toArray(new String JavaDoc[0]);
127             
128                 initializeLoader(classPath, appsManager.getLocation(application.getRegistrationName()),
129                                       ModuleType.EAR);
130                 application.setClassLoader(this.ejbClassLoader);
131             } else {
132                 initializeLoader(application.getClassLoader());
133             }
134
135             // finish loading application descriptors...
136
if (finishLoading) {
137                 application = appsManager.getAppDescriptor(application);
138             }
139             initializeRegistry();
140         } catch (Exception JavaDoc confEx) {
141             //@@ i18n
142
_logger.log(Level.SEVERE, "ERROR while loading application " + appID);
143             _logger.log(Level.SEVERE,"loader.error_while_loading_app_desc",
144                         confEx);
145         }
146     }
147     
148     /**
149      * Called from ApplicationManager. Called to load an application.
150      * This routine creates the EJB and MDB container.
151      *
152      * @param jsr77 create jsr77 mBeans if true
153      * @return true if all modules were loaded successfully
154      */

155     boolean load(boolean jsr77) {
156         boolean allModulesDeployed = false;
157
158         // Possible values for loading:
159
// 1. LOAD_ALL is for loading regular application
160
// 2. LOAD_RAR is for loading the rar part of the embedded rar
161
// 3. LOAD_REST is for loading the rest part of the embedded rar
162
// Embedded rar is loaded in two steps so we can create connector
163
// resources in between.
164

165         //set default value
166
if (loadUnloadAction == Constants.LOAD_UNSET) {
167             loadUnloadAction = Constants.LOAD_ALL;
168         }
169
170         if (loadUnloadAction == Constants.LOAD_ALL ||
171             loadUnloadAction == Constants.LOAD_RAR) {
172         notifyAppEvent(ApplicationEvent.BEFORE_APPLICATION_LOAD);
173             allModulesDeployed = loadRars(jsr77);
174             if (loadUnloadAction == Constants.LOAD_RAR) {
175                 return allModulesDeployed;
176             }
177         }
178
179         // Now load the EJB 3.0 persistence entities.
180
allModulesDeployed = loadPersistenceUnits();
181         if(!allModulesDeployed) return false;
182
183         if (allModulesDeployed) {
184             allModulesDeployed = loadEjbs(jsr77);
185         notifyAppEvent(ApplicationEvent.AFTER_APPLICATION_LOAD);
186     }
187         if (!allModulesDeployed) {
188             // remove all loaded pars
189
unloadPersistenceUnits();
190             return allModulesDeployed;
191         }
192         loadWebserviceEndpoints(jsr77);
193
194         // The web modules of this application are loaded from
195
// J2EERunner.confPostInit() as part of the WebContainer start()
196
// method
197

198         return allModulesDeployed;
199     }
200
201
202     /**
203      * Unloads this application.
204      *
205      * @param jsr77 delete jsr77 mBeans if true
206      * @return true if all modules were removed successfully
207      */

208     boolean unload(boolean jsr77) {
209         
210         // Possible values for unloading:
211
// 1. UNLOAD_ALL is for unloading regular application
212
// 2. UNLOAD_RAR is for unloading the rar part of the embedded rar
213
// 3. UNLOAD_REST is for unloading the rest part of the embedded rar
214
// Embedded rar is unloaded in two steps so we can delete connector
215
// resources in between.
216

217         //set default value
218
if (loadUnloadAction == Constants.LOAD_UNSET) {
219             loadUnloadAction = Constants.UNLOAD_ALL;
220         }
221
222         boolean wsUnloaded = false;
223         boolean ejbUnloaded = false;
224         boolean pusUnloaded = false;
225
226         if (loadUnloadAction == Constants.UNLOAD_ALL ||
227             loadUnloadAction == Constants.UNLOAD_REST) {
228         notifyAppEvent(ApplicationEvent.BEFORE_APPLICATION_UNLOAD);
229             wsUnloaded = unloadWebserviceEndpoints(jsr77);
230
231             // undeploy the ejb modules
232
ejbUnloaded = unloadEjbs(jsr77);
233
234             // Web modules are undeployed as part of the NSAPI reconfig
235
// callback interface implemented in J2EERunner
236

237             //undeploy persistence units
238
pusUnloaded = unloadPersistenceUnits();
239         
240             if (loadUnloadAction == Constants.UNLOAD_REST) {
241                 // return true status if components were unloaded OK
242
if (wsUnloaded && ejbUnloaded && pusUnloaded) {
243                     return true;
244                 } else {
245                     return false;
246                 }
247             }
248         }
249
250         // undeploy rar module
251
//START OF IASRI 4666595
252
boolean rarUnloaded = unloadRars(jsr77);
253         //END OF IASRI 4666595
254

255         configManager.unregisterDescriptor(id);
256
257         notifyAppEvent(ApplicationEvent.AFTER_APPLICATION_UNLOAD);
258
259         // helps garbage collection
260
done();
261
262         if (loadUnloadAction == Constants.UNLOAD_RAR) {
263             return rarUnloaded;
264         } else {
265             return (wsUnloaded && ejbUnloaded && pusUnloaded && rarUnloaded);
266         }
267     }
268
269     /**
270      * Create jsr77 root mBean
271      */

272     void createRootMBean() throws MBeanException JavaDoc {
273
274     try {
275
276             Switch.getSwitch().getManagementObjectManager().createAppMBean(
277                 this.application,
278                 this.configManager.getInstanceEnvironment().getName(),
279             this.configManager.getLocation(this.id));
280
281             Switch.getSwitch().getManagementObjectManager().createAppMBeanModules(
282         this.application,
283                 this.configManager.getInstanceEnvironment().getName(),
284         this.configManager.getLocation(this.id));
285
286     } catch (Exception JavaDoc e) {
287         throw new MBeanException JavaDoc(e);
288     }
289     }
290
291     /**
292      * Delete jsr77 root mBean
293      */

294     void deleteRootMBean() throws MBeanException JavaDoc {
295         Switch.getSwitch().getManagementObjectManager().deleteAppMBean(this.application,
296             this.configManager.getInstanceEnvironment().getName());
297     }
298
299
300     /**
301      * Create jsr77 mBeans for all components within this application
302      */

303     void createLeafMBeans() throws MBeanException JavaDoc {
304     try {
305             Switch.getSwitch().getManagementObjectManager().createAppMBeans(
306         this.application,
307                 this.configManager.getInstanceEnvironment().getName(),
308         this.configManager.getLocation(this.id));
309     } catch (Exception JavaDoc e) {
310         throw new MBeanException JavaDoc(e);
311     }
312     }
313
314     /**
315      * Create jsr77 mBean for the leaf object
316      * In case of application loader it will be a NO-OP
317      */

318     void createLeafMBean(Descriptor descriptor) throws MBeanException JavaDoc {
319         if (descriptor instanceof EjbDescriptor) {
320             EjbDescriptor ejbDescriptor = null;
321             try {
322                 ejbDescriptor = (EjbDescriptor) descriptor;
323             } catch (Exception JavaDoc e) {
324                 throw new MBeanException JavaDoc(e);
325             }
326             Switch.getSwitch().getManagementObjectManager().createEJBMBean(ejbDescriptor,
327                 this.configManager.getInstanceEnvironment().getName());
328         } else if (descriptor instanceof ConnectorDescriptor) {
329             ConnectorDescriptor cd = null;
330             try {
331                 cd = (ConnectorDescriptor) descriptor;
332             } catch (Exception JavaDoc e) {
333                 throw new MBeanException JavaDoc(e);
334             }
335             Switch.getSwitch().getManagementObjectManager().createRARMBean(cd,
336                 this.configManager.getInstanceEnvironment().getName());
337         }
338     }
339
340     /**
341      * Delete jsr77 mBean for the leaf object
342      * In case of application loader it will be a NO-OP
343      */

344     void deleteLeafMBean(Descriptor descriptor) throws MBeanException JavaDoc {
345         if (descriptor instanceof EjbDescriptor) {
346             EjbDescriptor ejbDescriptor = null;
347             try {
348                 ejbDescriptor = (EjbDescriptor) descriptor;
349             } catch (Exception JavaDoc e) {
350                 throw new MBeanException JavaDoc(e);
351             }
352             Switch.getSwitch().getManagementObjectManager().deleteEJBMBean(ejbDescriptor,
353                 this.configManager.getInstanceEnvironment().getName());
354         } else if (descriptor instanceof ConnectorDescriptor) {
355             ConnectorDescriptor cd = null;
356             try {
357                 cd = (ConnectorDescriptor) descriptor;
358             } catch (Exception JavaDoc e) {
359                 throw new MBeanException JavaDoc(e);
360             }
361             Switch.getSwitch().getManagementObjectManager().deleteRARMBean(cd,
362                 this.configManager.getInstanceEnvironment().getName());
363         }
364     }
365
366     /**
367      * Delete jsr77 mBeans for all components within this application
368      */

369     void deleteLeafMBeans() throws MBeanException JavaDoc {
370         Switch.getSwitch().getManagementObjectManager().deleteAppMBeans(this.application,
371             this.configManager.getInstanceEnvironment().getName());
372     }
373
374     /**
375      * Delete jsr77 mBeans for the application and its' components
376      */

377     void deleteLeafAndRootMBeans() throws MBeanException JavaDoc {
378         deleteLeafMBeans();
379         deleteRootMBean();
380     }
381
382
383     /**
384      * Set the state for the rootMBean
385      */

386     void setState(int state) throws MBeanException JavaDoc {
387         if (application == null) {
388             //the application object can be null if this is a redeployment
389
//of an app that previously failed to load. at the point of
390
//failure we cleaned out the loaders, including the application
391
//object. therefore, if the application object is null, we
392
//would log it and skip this step
393
_logger.log(Level.FINE,
394             "Application descriptor is NULL. setState skipped");
395         } else {
396             Switch.getSwitch().getManagementObjectManager().setApplicationState(
397                 state, application,
398                 configManager.getInstanceEnvironment().getName());
399         }
400     }
401
402     public boolean isForceDeploy(){
403         return this.isForceDeploy;
404     }
405     
406     public void setForceDeploy(boolean isForceDeploy) {
407         this.isForceDeploy = isForceDeploy;
408     }
409 }
410
Popular Tags