KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > server > util > ASClassLoaderUtil


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.appserv.server.util;
25
26 import java.io.File JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.logging.*;
31 import com.sun.enterprise.config.ConfigContext;
32 import com.sun.enterprise.config.ConfigException;
33 import com.sun.enterprise.config.serverbeans.Applications;
34 import com.sun.enterprise.config.serverbeans.Domain;
35 import com.sun.enterprise.config.serverbeans.EjbModule;
36 import com.sun.enterprise.config.serverbeans.J2eeApplication;
37 import com.sun.enterprise.config.serverbeans.WebModule;
38 import com.sun.enterprise.server.ApplicationServer;
39 import com.sun.enterprise.server.PELaunch;
40 import com.sun.enterprise.server.ServerContext;
41 import com.sun.enterprise.util.SystemPropertyConstants;
42
43 public class ASClassLoaderUtil {
44     private static Logger _logger = Logger.getAnonymousLogger();
45
46     /** shared class loader of current server context */
47     private static ClassLoader JavaDoc sharedCL;
48     
49     /**
50      * Gets the classpath associated with a web module, suffixing libraries defined
51      * [if any] for the application
52      * @param moduleId Module id of the web module
53      * @return A <code>File.pathSeparator</code> separated list of classpaths
54      * for the passed in web module, including the module specified "libraries"
55      * defined for the web module.
56      */

57     public static String JavaDoc getWebModuleClassPath(String JavaDoc moduleId) {
58             if (_logger.isLoggable(Level.FINE)) {
59                 _logger.log(Level.FINE, "module Id : " + moduleId);
60             }
61
62             StringBuilder JavaDoc classpath = new StringBuilder JavaDoc();
63             if (Boolean.getBoolean(PELaunch.USE_NEW_CLASSLOADER_PROPERTY)) {
64                 //XXX: Instead of computing this everytime, add a method in PELaunch to return
65
//this pre-formatted as a string
66
List JavaDoc<String JavaDoc> cp = PELaunch.getSharedClasspath();
67                 for(String JavaDoc s:cp){
68                     classpath.append(s);
69                     classpath.append(File.pathSeparatorChar);
70                 }
71             } else {
72                 classpath.append(System.getProperty("java.class.path"));
73             }
74
75             if (moduleId != null) {
76                 String JavaDoc specifiedLibraries = getLibrariesForWebModule(moduleId);
77                 URL JavaDoc[] libs = getLibraries(specifiedLibraries);
78                 if (libs == null) {
79                     if (_logger.isLoggable(Level.FINE)) {
80                         _logger.log(Level.FINE, "classpath: " + classpath.toString());
81                     }
82                     return classpath.toString();
83                 }
84   
85                 for (URL JavaDoc u : libs) {
86                     classpath.append(u + File.pathSeparator);
87                 }
88             }
89
90             if (_logger.isLoggable(Level.FINE)) {
91                 _logger.log(Level.FINE, "Final classpath: " + classpath.toString());
92             }
93             return classpath.toString();
94     }
95     
96     /**
97      * Gets the deploy-time "libraries" attribute specified for a J2EE application (.ear file)
98      * @param moduleId The module id of the J2EE application
99      * @return A comma separated list representing the libraries
100      * specified by the deployer.
101      */

102     public static String JavaDoc getLibrariesForJ2EEApplication(String JavaDoc moduleId) {
103         J2eeApplication app = null;
104         try {
105             app = getApplications().getJ2eeApplicationByName(moduleId);
106             if(app == null) return null;
107         } catch(ConfigException malEx) {
108             _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url",
109                                                                                            moduleId);
110             _logger.log(Level.WARNING,"loader.exception", malEx);
111         }
112         return app.getLibraries();
113     }
114
115     /**
116      * Gets the deploy-time "libraries" attribute specified for a web module (.war file)
117      * @param moduleId The module id of the web module
118      * @return A comma separated list representing the libraries
119      * specified by the deployer.
120      */

121     public static String JavaDoc getLibrariesForWebModule(String JavaDoc moduleId) {
122         WebModule app = null;
123         try {
124             app = getApplications().getWebModuleByName(moduleId);
125             if(app == null) return null;
126         } catch(ConfigException malEx) {
127             _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url",
128                                                                                            moduleId);
129             _logger.log(Level.WARNING,"loader.exception", malEx);
130         }
131
132         String JavaDoc librariesStr = app.getLibraries();
133         if (_logger.isLoggable(Level.FINE)) {
134             _logger.log(Level.FINE, "app = " + app + " library = " + librariesStr);
135         }
136         return librariesStr;
137     }
138
139     /**
140      * Gets the deploy-time "libraries" attribute specified for an EJB module [EJB Jars]
141      * @param moduleId The module id of the EJB module
142      * @return A comma separated list representing the libraries
143      * specified by the deployer.
144      */

145     public static String JavaDoc getLibrariesForEJBJars(String JavaDoc moduleId) {
146         EjbModule app = null;
147         try {
148             app = getApplications().getEjbModuleByName(moduleId);
149             if(app == null) return null;
150         } catch(ConfigException malEx) {
151             _logger.log(Level.WARNING, "loader.cannot_convert_classpath_into_url",
152                                                                                            moduleId);
153             _logger.log(Level.WARNING,"loader.exception", malEx);
154         }
155         return app.getLibraries();
156     }
157     
158     //Gets the Applications config bean from the application server's configcontext
159
private static Applications getApplications() throws ConfigException {
160         ConfigContext serverConfigCtx = ApplicationServer.getServerContext().getConfigContext();
161         Domain domain = ((Domain)serverConfigCtx.getRootConfigBean());
162         return domain.getApplications();
163     }
164     
165     /**
166      * Utility method to obtain a resolved list of URLs representing the
167      * libraries specified for an application using the libraries
168      * application deploy-time attribute
169      * @param librariesStr The deploy-time libraries attribute as specified by
170      * the deployer for an application
171      * @return A list of URLs representing the libraries specified for
172      * the application
173      */

174     public static URL JavaDoc[] getLibraries(String JavaDoc librariesStr) {
175         if(librariesStr == null)
176             return null;
177         
178         String JavaDoc [] librariesStrArray = librariesStr.split(",");
179         if(librariesStrArray == null)
180             return null;
181         
182         URL JavaDoc [] urls = new URL JavaDoc[librariesStrArray.length];
183         //Using the string from lib and applibs requires admin which is
184
//built after appserv-core.
185
String JavaDoc appLibsDir = System.getProperty(
186                         SystemPropertyConstants.INSTANCE_ROOT_PROPERTY)
187                         + File.separator + "lib"
188                         + File.separator + "applibs";
189         
190         int i=0;
191         for(String JavaDoc libraryStr:librariesStrArray){
192             try {
193                 File JavaDoc f = new File JavaDoc(libraryStr);
194                 if(!f.isAbsolute())
195                     f = new File JavaDoc(appLibsDir, libraryStr);
196                 URL JavaDoc url = f.toURL();
197                 urls[i++] = url;
198             } catch (MalformedURLException JavaDoc malEx) {
199                 _logger.log(Level.WARNING,
200                         "loader.cannot_convert_classpath_into_url",
201                         libraryStr);
202                 _logger.log(Level.WARNING,"loader.exception", malEx);
203             }
204         }
205         return urls;
206     }
207     
208     /**
209      * Returns the shared class loader
210      * @return ClassLoader
211      */

212     public static ClassLoader JavaDoc getSharedClassLoader() {
213         if(sharedCL == null) {
214             ServerContext sc = ApplicationServer.getServerContext();
215             sharedCL = sc.getSharedClassLoader();
216         }
217         return sharedCL;
218     }
219 }
220
Popular Tags