KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > DeploymentUtils


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  * @(#) DeploymentUtils.java
26  *
27  */

28 package com.sun.enterprise.deployment.backend;
29
30 import com.sun.enterprise.config.ConfigException;
31 import com.sun.enterprise.deployment.Application;
32 import com.sun.enterprise.deployment.BundleDescriptor;
33 import com.sun.enterprise.deployment.archivist.ArchivistFactory;
34 import com.sun.enterprise.deployment.archivist.AppClientArchivist;
35 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
36 import com.sun.enterprise.deployment.archivist.Archivist;
37 import com.sun.enterprise.deployment.archivist.ConnectorArchivist;
38 import com.sun.enterprise.deployment.archivist.EjbArchivist;
39 import com.sun.enterprise.deployment.archivist.WebArchivist;
40 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
41 import com.sun.enterprise.deployment.RootDeploymentDescriptor;
42 import com.sun.enterprise.instance.AppclientModulesManager;
43 import com.sun.enterprise.instance.AppsManager;
44 import com.sun.enterprise.instance.BaseManager;
45 import com.sun.enterprise.instance.ConnectorModulesManager;
46 import com.sun.enterprise.instance.EjbModulesManager;
47 import com.sun.enterprise.instance.InstanceEnvironment;
48 import com.sun.enterprise.instance.WebModulesManager;
49 import com.sun.enterprise.loader.ClassLoaderUtils;
50 import com.sun.enterprise.loader.EJBClassLoader;
51 import com.sun.enterprise.server.Constants;
52 import com.sun.enterprise.util.io.FileUtils;
53
54 import java.io.File JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.util.logging.Level JavaDoc;
57 import java.util.logging.Logger JavaDoc;
58 import java.util.*;
59
60 /**
61  * Utility methods for deployment backend.
62  *
63  * @author Nazrul Islam
64  * @since JDK 1.4
65  */

66 public class DeploymentUtils {
67
68     private static Logger JavaDoc _logger = DeploymentLogger.get();
69
70     /**
71      * Sets the parent class loader for the given deployment request.
72      *
73      * @param bootStrap bootStrap (or parent) class loader
74      * @param baseMgr provides access to server configuration
75      * @param req deployment request
76      *
77      * @throws ConfigException if an error while reading the server.xml
78      * @throws IOException if an i/o error
79      */

80     static void setParentClassLoader(ClassLoader JavaDoc bootStrap, BaseManager baseMgr,
81             DeploymentRequest req) throws ConfigException, IOException JavaDoc {
82
83         List allClassPaths = new ArrayList();
84         
85         // system class loader
86
List systemClasspath = baseMgr.getSystemCPathPrefixNSuffix();
87         if (systemClasspath.size() > 0) {
88             allClassPaths.addAll(systemClasspath);
89         }
90
91         // common class loader
92
List commonClassPath = getCommonClasspath(baseMgr);
93         if (commonClassPath.size() > 0) {
94             allClassPaths.addAll(commonClassPath);
95         }
96
97         // shared class loader
98
// Some explanation on the special handling below:
99
// Per the platform specification, connector classes are to be availble
100
// to all applications, i.e. a connector deployed to target foo should
101
// be available to apps deployed on foo (and not target bar). During
102
// deployment process on DAS, we don't really need to load any
103
// connector classes EXCEPT when the verifier is turned on. In that
104
// case, we will need to figure out all connector module deployed to
105
// the target on which the application is deployed. Resolving the
106
// classpath accordlingly. If verifier is not on, we bypass this logic
107
// to save some time.
108
boolean resolveConnectorClassPath = false;
109         String JavaDoc target = null;
110         if (req.isVerifying()) { //--verify=true
111
resolveConnectorClassPath = true;
112             String JavaDoc targetString = req.getResourceTargetList();
113             //Since verifier is not supported by JSR88 code path, we don't
114
//have to deal with multiple target deployment.
115
target = (new java.util.StringTokenizer JavaDoc(targetString)).nextToken();
116         }
117
118         List sharedClassPath =
119             baseMgr.getSharedClasspath(resolveConnectorClassPath, target);
120         if (sharedClassPath.size() > 0) {
121             allClassPaths.addAll(sharedClassPath);
122         }
123
124         ClassLoader JavaDoc parentClassLoader =
125             getClassLoader(allClassPaths, bootStrap, null);
126
127         // sets the parent class loader
128
req.setParentClassLoader(parentClassLoader);
129
130         // sets the class path for the class loader
131
req.setParentClasspath(allClassPaths);
132     }
133
134     /**
135      * Returns the class loader to be used for deployment. It contains all the
136      * urls for the given application.
137      *
138      * @param classPaths name of the application
139      * @param parent parent class loader
140      * @param other additional directory to be added to the class path
141      *
142      * @throws exception IOException if an error while creating the
143      * class loader
144      */

145     static EJBClassLoader getClassLoader(List paths, ClassLoader JavaDoc parent,
146             File JavaDoc other) throws IOException JavaDoc {
147
148         EJBClassLoader ejbCl = null;
149
150         if (parent != null) {
151             ejbCl = new EJBClassLoader(parent);
152         } else {
153             ejbCl = new EJBClassLoader();
154         }
155
156         final int LIST_SZ = paths.size();
157         for (int i=0; i<LIST_SZ; i++) {
158             String JavaDoc path = (String JavaDoc) paths.get(i);
159             ejbCl.appendURL(new File JavaDoc(path));
160         }
161
162         if (other != null) {
163             ejbCl.appendURL(other);
164         }
165
166         return ejbCl;
167     }
168
169     /**
170      * Returns the common class loader paths, if any, or an empty list.
171      *
172      * @param mgr manager obj that provides access to application config
173      * @return the common class path urls
174      */

175     static List getCommonClasspath(BaseManager mgr) throws IOException JavaDoc {
176
177         InstanceEnvironment env = mgr.getInstanceEnvironment();
178         String JavaDoc dir = env.getLibClassesPath();
179         String JavaDoc jarDir = env.getLibPath();
180
181         return ClassLoaderUtils.getUrlList(new File JavaDoc[] {new File JavaDoc(dir)},
182                                            new File JavaDoc[] {new File JavaDoc(jarDir)});
183     }
184
185     protected static String JavaDoc getSystemPropertyIgnoreCase(final String JavaDoc key)
186     {
187         Properties p = System.getProperties();
188         Set set = p.entrySet();
189
190         for(Iterator it = set.iterator(); it.hasNext(); )
191         {
192             Map.Entry me = (Map.Entry)it.next();
193             String JavaDoc propKey = (String JavaDoc)me.getKey();
194             
195             if(key.compareToIgnoreCase(propKey) == 0)
196                 return (String JavaDoc)me.getValue();
197         }
198         
199         return null;
200     }
201
202     /**
203      * Returns the deployment descriptor object for the application.
204      * Turn off the annotation processing during undeployment. Note that
205      * this method is called *only* during the undeployment phase right now.
206      *
207      * @param appDir exploded application dir location
208      *
209      * @return the deployment descriptor object for the application living in
210      * the given appdir
211      *
212      * @throws IASDeploymentException if any of these other Exceptions are caught:
213      * ConfigException if unable to load the deployment descriptor
214      * AppConfigException if an error if the while reading
215      * the deployment descriptor
216      * IOException if an i/o error
217      */

218     static Application getAppDescriptor(String JavaDoc appDir) throws IASDeploymentException
219     {
220         return getAppDescriptor(appDir, false);
221     }
222
223     /**
224      * Returns the deployment descriptor object for the application.
225      *
226      * @param appDir exploded application dir location
227      * @param annotationProcessing whether to process annotation
228      *
229      * @return the deployment descriptor object for the application living in
230      * the given appdir
231      *
232      * @throws IASDeploymentException if any of these other Exceptions are caught:
233      * ConfigException if unable to load the deployment descriptor
234      * AppConfigException if an error if the while reading
235      * the deployment descriptor
236      * IOException if an i/o error
237      */

238     static Application getAppDescriptor(String JavaDoc appDir, boolean annotationProcessing)
239             throws IASDeploymentException
240     {
241         try {
242             FileArchive archive = new FileArchive();
243             archive.open(appDir);
244
245             ApplicationArchivist archivist = new ApplicationArchivist();
246             archivist.setAnnotationProcessingRequested(annotationProcessing);
247             archivist.setXMLValidation(false);
248
249             return (Application) archivist.open(archive);
250         } catch(Throwable JavaDoc t) {
251             throw new IASDeploymentException(t);
252         }
253     }
254
255     /**
256      * Turn off the annotation processing during undeployment. Note that
257      * this method is called *only* during the undeployment phase right now.
258      */

259     static Application getModuleDescriptor(String JavaDoc appDir) throws IASDeploymentException
260     {
261         return getModuleDescriptor(appDir, false);
262     }
263
264     static Application getModuleDescriptor(String JavaDoc appDir, boolean annotationProcessing)
265             throws IASDeploymentException
266     {
267         try {
268             FileArchive archive = new FileArchive();
269             archive.open(appDir);
270
271             Archivist archivist =
272                 ArchivistFactory.getArchivistForArchive(archive);
273
274             archivist.setAnnotationProcessingRequested(annotationProcessing);
275             archivist.setXMLValidation(false);
276
277             return (Application) ApplicationArchivist.openArchive(archivist, archive, true);
278         } catch(Throwable JavaDoc t) {
279             throw new IASDeploymentException(t);
280         }
281     }
282
283     /**
284      * This method returns a fully populated top level descriptor object.
285      * It does so by first checking the cache in the instance manager.
286      * If the descriptor object does not exist in cache, it then proceeds
287      * to read off of the disk. Note that reading from the disk is an
288      * expensive operation. So please use this method with caution. In
289      * addition, this method does not cache the read Descriptor object
290      * in the instance manager so that query for Descriptors from cli and
291      * gui would not (and should not) meddle with the cache management for
292      * descriptors.
293      *
294      *@param appId The module ID of the application
295      *@param manager The instance manager for the module
296      *@return The top level Descriptor object
297      */

298     public static RootDeploymentDescriptor getDescriptor(
299             String JavaDoc appId, BaseManager manager) throws IASDeploymentException {
300         Application application = manager.getRegisteredDescriptor(appId);
301         if (application != null) {
302             if (application.isVirtual()) {
303                 return application.getStandaloneBundleDescriptor();
304             } else {
305                 return application;
306             }
307         }
308
309         //Now, load from disk
310
FileArchive in = new FileArchive();
311         try {
312             String JavaDoc appDir = manager.getLocation(appId);
313             // if is system predeployed app, load from original app dir
314
// else load from generated/xml dir
315
// print a warning if generated/xml dir is not there
316
// and load from original dir (upgrade scenario)
317
if (manager.isSystemAdmin(appId)) {
318                 in.open(appDir);
319             } else {
320                 String JavaDoc xmlDir = manager.getGeneratedXMLLocation(appId);
321                 if (FileUtils.safeIsDirectory(xmlDir)) {
322                     in.open(xmlDir);
323                 } else {
324                     // log a warning message in the server log
325
_logger.log(Level.WARNING,
326                         "enterprise.deployment.backend.no_generated_xmldir",
327                         new Object JavaDoc[]{appId, xmlDir, appDir});
328                     in.open(appDir);
329                 }
330             }
331
332             Archivist archivist = null;
333             if (manager instanceof AppsManager) {
334                 archivist = new ApplicationArchivist();
335             } else if (manager instanceof EjbModulesManager) {
336                 archivist = new EjbArchivist();
337             } else if (manager instanceof WebModulesManager) {
338                 archivist = new WebArchivist();
339             } else if (manager instanceof AppclientModulesManager) {
340                 archivist = new AppClientArchivist();
341             } else if (manager instanceof ConnectorModulesManager) {
342                 archivist = new ConnectorArchivist();
343             }
344
345             archivist.setAnnotationProcessingRequested(false);
346             archivist.setXMLValidation(false);
347             Application desc = ApplicationArchivist.openArchive(
348                                         appId, archivist, in, true);
349
350             //note: we are not reading back the persistence information here
351
//we could, if ever the tools need it.
352
if (!desc.isVirtual()) {
353                 archivist.setHandleRuntimeInfo(false);
354                 ((ApplicationArchivist)
355                     archivist).readModulesDescriptors(desc, in);
356                 // now process runtime DDs
357
archivist.setHandleRuntimeInfo(true);
358                 archivist.readRuntimeDeploymentDescriptor(in, desc);
359             } else {
360                 return (BundleDescriptor)
361                         desc.getBundleDescriptors().iterator().next();
362             }
363             return desc;
364         } catch (Exception JavaDoc ex) {
365             _logger.log(Level.SEVERE,
366                         "enterprise.deployment.backend.get_descriptor_failed",
367                         new Object JavaDoc[]{appId});
368             IASDeploymentException de = new IASDeploymentException(ex.getMessage());
369             de.initCause(ex);
370             throw de;
371         } finally {
372             try {
373                 in.close();
374             } catch (Exception JavaDoc ex) {}
375         }
376     }
377 }
378
Popular Tags