KickJava   Java API By Example, From Geeks To Geeks.

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


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.enterprise.server;
25
26 import java.io.File JavaDoc;
27 import java.io.FilenameFilter JavaDoc;
28 import java.lang.reflect.Method JavaDoc;
29
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.Arrays JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.jar.*;
37 import java.util.StringTokenizer JavaDoc;
38 import java.util.logging.Level JavaDoc;
39 import java.util.logging.Logger JavaDoc;
40
41 import com.sun.appserv.server.util.ASURLClassLoader;
42 import com.sun.appserv.server.util.ClassLoaderChain;
43 import com.sun.enterprise.util.ASenvPropertyReader;
44 import com.sun.enterprise.util.SystemPropertyConstants;
45
46
47 /**
48  * New Start up class for PE/RI. For AS9 this is set as the default
49  * <main-class> in processLauncher.xml.
50  *
51  * To disable the new classloader hierarchy the following needs to be done:
52  * - Modify the system property '-Dcom.sun.aas.processName="as9-server"' to
53  * '-Dcom.sun.aas.processName="s1as8-server' in the startserv/stopserv scripts
54  * - Modify system-classpath attribute in the relevant java-config to
55  * if PE: "com.sun.aas.classloader.serverClassPath" element in processLauncher.xml
56  * if EE: "com.sun.aas.classloader.serverClassPath.ee" element in processLauncher.xml
57  *
58  * @author Harsha RA, Sivakumar Thyagarajan
59  */

60
61 public class PELaunch {
62     
63     //The new ClassLoader Hierarchy would be enabled only when this system
64
//property is set.
65
public static final String JavaDoc USE_NEW_CLASSLOADER_PROPERTY
66                                     = "com.sun.aas.useNewClassLoader";
67     
68     public static final String JavaDoc PROPERTIES_FILES
69                                     = "processLauncher.properties";
70
71     //These properties are set by the ProcessLauncher in the new classloader
72
//scheme. The classpath prefix and server classpath are prefixed to the
73
//shared classloader chain. The classpath suffix is suffixed to the shared classloader
74
//chain
75
private static final String JavaDoc CLASSPATH_PREFIX_PROPERTY
76                                     = "com.sun.aas.ClassPathPrefix";
77     private static final String JavaDoc CLASSPATH_SUFFIX_PROPERTY
78                                     = "com.sun.aas.ClassPathSuffix";
79     private static final String JavaDoc SERVER_CLASSPATH_PROPERTY
80                                     = "com.sun.aas.ServerClassPath";
81
82     private static final String JavaDoc fileSeparator = File.separator;
83     private static final String JavaDoc pathSeparator = File.pathSeparator;
84     
85     private static String JavaDoc installRoot = null;
86     private static String JavaDoc javaHome = null;
87
88     private static List JavaDoc<String JavaDoc> _appserverClasspath = null;
89     private static List JavaDoc<String JavaDoc> _sharedClasspath = null;
90     private static List JavaDoc<String JavaDoc> _optionalClasspath = null;
91
92     private static ASURLClassLoader _sharedClassLoader = null;
93     private static ClassLoaderChain _optionalChain = null;
94     private static ClassLoaderChain _asChain = null;
95     
96     private static final boolean bDebug = new Boolean JavaDoc(
97                     System.getProperty("com.sun.aas.useNewClassLoader.debug", "false")).booleanValue();
98
99
100     //By default we set the new classloader hierarchy as the default
101
static {
102         if (System.getProperty(USE_NEW_CLASSLOADER_PROPERTY) == null) {
103             System.setProperty(USE_NEW_CLASSLOADER_PROPERTY, "true");
104         }
105     }
106     
107     public static ClassLoader JavaDoc getSharedChain(){
108         return _sharedClassLoader;
109     }
110
111     public static ClassLoader JavaDoc getOptionalChain(){
112         return _optionalChain;
113     }
114     
115     public static ClassLoader JavaDoc getAppServerChain(){
116         return _asChain;
117     }
118
119     public static List JavaDoc<String JavaDoc> getSharedClasspath() {
120         if(_sharedClasspath!=null)
121             return _sharedClasspath;
122         //PE/EE Shared jars
123
String JavaDoc asLib = installRoot + fileSeparator + "lib" + fileSeparator;
124         String JavaDoc sharedJarsList = System.getProperty("com.sun.aas.classloader.sharedChainJars");
125         if (isEE()) {
126             String JavaDoc eeSharedJarsList = System.getProperty("com.sun.aas.classloader.sharedChainJars.ee");
127             sharedJarsList += ("," + eeSharedJarsList);
128         }
129         logFine("shared jar list " + sharedJarsList);
130         
131         List JavaDoc<String JavaDoc> shr = getLibraryList(asLib, sharedJarsList);
132         
133         //Computing classpath prefix,suffix and server classpath
134
String JavaDoc prefixString = System.getProperty(CLASSPATH_PREFIX_PROPERTY);
135         logFine(" prefixString " + prefixString );
136         String JavaDoc[] classpathPrefix = null;
137         if (prefixString != null) classpathPrefix = prefixString.split("" +File.pathSeparatorChar);
138         
139         String JavaDoc suffixString = System.getProperty(CLASSPATH_SUFFIX_PROPERTY);
140         logFine(" suffixString " + suffixString);
141         String JavaDoc[] classpathSuffix = null;
142         if (suffixString != null) classpathSuffix = suffixString.split("" +File.pathSeparatorChar);
143         
144         String JavaDoc serverClassPathString = System.getProperty(SERVER_CLASSPATH_PROPERTY);
145         logFine(" serverClassPathString " + serverClassPathString);
146         String JavaDoc[] serverClassPath = null;
147         if (serverClassPathString != null) serverClassPath = serverClassPathString.split("" +File.pathSeparatorChar);
148         
149         //Creating final shared chain list.
150
List JavaDoc<String JavaDoc> sharedChainList = new ArrayList JavaDoc<String JavaDoc>();
151         if (classpathPrefix != null) sharedChainList.addAll(Arrays.asList(classpathPrefix));
152         sharedChainList.addAll(shr);
153         if (serverClassPath != null) sharedChainList.addAll(Arrays.asList(serverClassPath));
154         if (classpathSuffix != null) sharedChainList.addAll(Arrays.asList(classpathSuffix));
155         
156         List JavaDoc manifestaddonJars = getManifestAddonJars();
157         if(manifestaddonJars.size() != 0)
158             sharedChainList.addAll(manifestaddonJars);
159         _sharedClasspath=sharedChainList;
160         return _sharedClasspath;
161     }
162
163     
164     public static List JavaDoc getAppServerClasspath() {
165         return _appserverClasspath;
166     }
167     
168     public static List JavaDoc<String JavaDoc> getOptionalClasspath() {
169         return _optionalClasspath;
170     }
171       
172     /**
173      * ServerClassPath was earlier provided as "server-classpath" attribute
174      * in the "java-config" element of domain.xml and this is used by
175      * BaseManager Only.
176      */

177     public static List JavaDoc getServerClasspath() {
178         String JavaDoc asLib = installRoot + fileSeparator + "lib" + fileSeparator;
179         String JavaDoc serverJarsList = System.getProperty("com.sun.aas.classloader.serverClassPath");
180
181         if (isEE()) {
182             String JavaDoc eeServerJarsList = System.getProperty("com.sun.aas.classloader.serverClassPath.ee");
183             serverJarsList += ("," + eeServerJarsList);
184         }
185         logFine("serverClassPathJarsList " + serverJarsList);
186         
187         List JavaDoc<String JavaDoc> serverClasspathList = getLibraryList(asLib, serverJarsList);
188         return serverClasspathList;
189     }
190     
191     /**
192      ** Get all Non-manifest jars under lib/addons directory and add it to sharedClassPath.
193      */

194     private static List JavaDoc getManifestAddonJars() {
195         List JavaDoc<String JavaDoc> manifestJars = new ArrayList JavaDoc<String JavaDoc>();
196         JarFile file = null;
197         try {
198         String JavaDoc addonDir = installRoot + fileSeparator + "lib" + fileSeparator + "addons";
199         File JavaDoc libAddonDirectory = new File JavaDoc(addonDir);
200         if(!libAddonDirectory.isDirectory())
201             return manifestJars;
202         
203         File JavaDoc[] fileArray = libAddonDirectory.listFiles();
204         for(int i = 0;i<fileArray.length;i++) {
205                 String JavaDoc addonJar = fileArray[i].getName();
206                 String JavaDoc jarExtension = "";
207                 int dotLastIndex = addonJar.lastIndexOf(".");
208                 if(dotLastIndex != -1)
209                     jarExtension = addonJar.substring(dotLastIndex + 1);
210                 if(jarExtension.equalsIgnoreCase("jar")) {
211                     manifestJars.add(fileArray[i].getAbsolutePath());
212                     file = new JarFile(fileArray[i].getAbsolutePath());
213                     Manifest mf = file.getManifest();
214                     Attributes attributes = null;
215                     if(mf != null) {
216                         attributes = mf.getMainAttributes();
217                         if(attributes != null) {
218                             String JavaDoc classPath = attributes.getValue(Attributes.Name.CLASS_PATH);
219                             if(classPath != null && !classPath.trim().equals("")) {
220                                 StringTokenizer JavaDoc stoken = new StringTokenizer JavaDoc(classPath);
221                                 while(stoken.hasMoreTokens()) {
222                                     String JavaDoc classPathJar = addonDir + fileSeparator + stoken.nextElement();
223                                     manifestJars.add(classPathJar);
224                                 }
225                             }
226                             //Logger.getAnonymousLogger().log(Level.FINE, "Main Class "+mainClass);
227
}
228                         file.close();
229                     }
230                 }
231         }
232         }catch(Exception JavaDoc e) {
233             e.printStackTrace(System.err);
234         }finally {
235             try {
236             if(file != null)
237                 file.close();
238             }catch(Exception JavaDoc ex) {
239                 ex.printStackTrace(System.err);
240             }
241         }
242         //Logger.getAnonymousLogger().log(Level.INFO, "nonManifestJars list: "+nonManifestJars);
243
return manifestJars;
244     }
245     /**
246      * Entry point into the application server
247      */

248     public static void main(String JavaDoc[] args) {
249         try{
250             Class JavaDoc peMainClass = null;
251             
252             if(Boolean.getBoolean(USE_NEW_CLASSLOADER_PROPERTY)){
253                 ASenvPropertyReader reader = new ASenvPropertyReader(
254                     System.getProperty(SystemPropertyConstants.CONFIG_ROOT_PROPERTY),
255                     false);
256                 reader.setSystemProperties();
257                 installRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
258                 javaHome = System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY);
259                 
260                 setupClassloaders();
261                 //Use the new classloader hierarchy
262
peMainClass = _asChain.loadClass(
263                                 "com.sun.enterprise.server.PEMain", true);
264                 Thread.currentThread().setContextClassLoader(_asChain);
265             } else {
266                 peMainClass = Class.forName("com.sun.enterprise.server.PEMain");
267             }
268             
269             Class JavaDoc[] argTypes = new Class JavaDoc[]{String JavaDoc[].class};
270             Method JavaDoc m = peMainClass.getMethod("main", argTypes);
271             Object JavaDoc[] argListForInvokedMain = new Object JavaDoc[]{args};
272             m.invoke(null, argListForInvokedMain);
273         } catch(Exception JavaDoc e) {
274             e.printStackTrace(System.err);
275             System.exit(1);
276         }
277     }
278
279     /*
280      * Sets up all internal classloader chains
281      */

282     private static void setupClassloaders(){
283         prepareAppServerJars();
284         prepareOptionalOverrideableJars();
285         appendOtherJarsToSharedChain();
286         setupSharedChain();
287         setupOptionalOverrideableChain();
288         setupAppServerChain();
289         
290         List JavaDoc<String JavaDoc> cp = getSharedClasspath();
291         //Seed the classpath string with the system classpath
292
//to support users setting "env-classpath-ignored" to false.
293
StringBuilder JavaDoc classpath = new StringBuilder JavaDoc(System.getProperty("java.class.path"));
294         for(String JavaDoc s:cp){
295             classpath.append(s);
296             classpath.append(pathSeparator);
297         }
298         System.setProperty("java.class.path", classpath.toString());
299     }
300
301     /**
302      * SPI for Verifier to use when it runs in appserver mode. Returns the server classpath
303      * for an application. This as now includes all libraries in installRoot and
304      * server-classpath.
305      */

306     public static List JavaDoc<String JavaDoc> getServerClassPath(String JavaDoc configDir,
307                                                   String JavaDoc domainDir){
308         ASenvPropertyReader reader = new ASenvPropertyReader(configDir,false);
309         reader.setSystemProperties();
310         installRoot = System.getProperty(SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
311         javaHome = System.getProperty(SystemPropertyConstants.JAVA_ROOT_PROPERTY);
312
313         //Add all libraries in as-install-dir/lib
314
String JavaDoc asLib = installRoot + fileSeparator ;
315         List JavaDoc<String JavaDoc >serverClassPathList = new ArrayList JavaDoc<String JavaDoc>();
316         File JavaDoc[] fls= getAllLibrariesInLib(asLib);
317         
318         for (File JavaDoc element : fls) {
319             serverClassPathList.add(element.getAbsolutePath());
320         }
321         
322         //add server-classpath
323
String JavaDoc mqlib = System.getProperty(SystemPropertyConstants.IMQ_LIB_PROPERTY);
324         String JavaDoc antlib = System.getProperty(SystemPropertyConstants.ANT_LIB_PROPERTY);
325         String JavaDoc jdmklib = System.getProperty(SystemPropertyConstants.JDMK_HOME_PROPERTY);
326         String JavaDoc hadbRoot = System.getProperty(SystemPropertyConstants.HADB_ROOT_PROPERTY);
327         
328         String JavaDoc[] peServerClassPath = {installRoot + "/lib/install/applications/jmsra/imqjmsra.jar",
329                         mqlib + "/jaxm-api.jar" , mqlib + "/fscontext.jar",
330                         mqlib + "/imqbroker.jar", mqlib + "/imqjmx.jar",
331                         mqlib + "/imqxm.jar",
332                         antlib + "/ant.jar", jdmklib + "/lib/jdmkrt.jar"} ;
333         String JavaDoc[] eeServerClassPath = {hadbRoot + "/lib/hadbjdbc4.jar",
334                         jdmklib + "/lib/jdmkrt.jar",hadbRoot + "/lib/dbstate.jar",
335                         hadbRoot + "/lib/hadbm.jar", hadbRoot + "/lib/hadbmgt.jar"} ;
336         for (String JavaDoc element : peServerClassPath) {
337             File JavaDoc f = new File JavaDoc(element);
338             if(f.exists()) {
339                 serverClassPathList.add(f.getAbsolutePath());
340             }
341         }
342         for (String JavaDoc element : eeServerClassPath) {
343             File JavaDoc f = new File JavaDoc(element);
344             if(f.exists()) {
345                 serverClassPathList.add(f.getAbsolutePath());
346             }
347         }
348         
349         logFine("Server Classpath for verifier " + serverClassPathList + "\n");
350         Logger.getAnonymousLogger().log(Level.FINE, "Server Classpath for verifier " + serverClassPathList);
351         return serverClassPathList;
352     }
353     
354     /**
355      * The Shared chain is the child of SystemClassLoader in the AS classloader
356      * hierarchy and houses all immutable, platform classes. [eg j2ee.jar]
357      *
358      */

359     private static void setupSharedChain(){
360         List JavaDoc<String JavaDoc> sharedChainJarList = getSharedClasspath();
361         logFine("shared classpath jars : " + sharedChainJarList + "\n");
362         URL JavaDoc[] urls = getURLList(sharedChainJarList);
363         logFine(" SharedChain URL List " + urls);
364          _sharedClassLoader = new ASURLClassLoader(urls,
365                          ClassLoader.getSystemClassLoader());
366         _sharedClassLoader.setName("Shared ClassLoader Chain");
367     }
368
369     
370     /**
371      * The optional overrideable chain is "suffixed" to the chain of
372      * dependencies of an application. It consists of libraries that are by
373      * default provided to an application when an application has not specified
374      * an explicit dependency. This chain consists of all AS provided libraries
375      * that could be overridden by an application.
376      */

377     private static void setupOptionalOverrideableChain(){
378          
379         _optionalChain = new ClassLoaderChain(_sharedClassLoader);
380         _optionalChain.setName("optionalChain");
381         
382         URL JavaDoc[] urls = getURLList(_optionalClasspath);
383         //Parent set to Shared Chain
384
ASURLClassLoader optionalJarsLoader = new ASURLClassLoader(urls,
385                                                                 _sharedClassLoader);
386         _optionalChain.addToList(optionalJarsLoader);
387
388     }
389     
390     private static void prepareOptionalOverrideableJars(){
391         String JavaDoc asLib = installRoot + fileSeparator + "lib" + fileSeparator;
392         String JavaDoc optionalJarsString = System.getProperty("com.sun.aas.classloader.optionalOverrideableChain");
393         if (isEE()) {
394             String JavaDoc eeOptionalJarsList = System.getProperty("com.sun.aas.classloader.optionalOverrideableChain.ee");
395             optionalJarsString += ("," + eeOptionalJarsList);
396         }
397         
398         logFine(" optionalOverrideableChain" + optionalJarsString );
399         
400         _optionalClasspath = getLibraryList(asLib, optionalJarsString);
401         logFine("Optional overrideable chain classpath : " + _optionalClasspath + "\n");
402     }
403     
404     /**
405      * The application server chain is composed of jars/libraries that are used
406      * and visible to application server classes only. The optional overrideable
407      * chain is also a part of this chain. The Shared ClassLoader chain is set
408      * as the parent of the application server chain.
409      */

410     private static void setupAppServerChain(){
411         URL JavaDoc[] urls = getURLList(_appserverClasspath);
412         
413         //parent set to Shared Chain
414
_asChain = new ClassLoaderChain(_sharedClassLoader);
415         _asChain.setName("ASChain");
416         
417         ASURLClassLoader asimplloader = new ASURLClassLoader(urls, _asChain);
418         asimplloader.setName("asimpl");
419         _asChain.addToList(asimplloader);
420         _asChain.addToList(_optionalChain);
421     }
422
423     private static void prepareAppServerJars(){
424         //AS only jars
425
String JavaDoc asLib = installRoot + fileSeparator + "lib";
426         String JavaDoc appserverJarsStr = System.getProperty("com.sun.aas.classloader.appserverChainJars");
427         if (isEE()) {
428             String JavaDoc eeAppserverJarsList = System.getProperty("com.sun.aas.classloader.appserverChainJars.ee");
429             appserverJarsStr += ("," + eeAppserverJarsList);
430         }
431         logFine("appserverJarsString " + appserverJarsStr );
432         
433         _appserverClasspath = getLibraryList(asLib, appserverJarsStr);
434         logFine("Application server classpath : " + _appserverClasspath + "\n");
435     }
436     
437     /**
438      * Determines if the AS process is running in EE.
439      * XXX: to refactor this to use the common implementation.
440      */

441     private static boolean isEE() {
442         boolean isEE = false;
443         final String JavaDoc eepffc = "com.sun.enterprise.ee.server.pluggable.EEPluggableFeatureImpl";
444         final String JavaDoc pn = "com.sun.appserv.pluggable.features";
445         final String JavaDoc pv = System.getProperty(pn);
446         if (eepffc.equals(pv)) {
447             isEE = true;
448         }
449         return ( isEE );
450     }
451     
452     private static List JavaDoc<String JavaDoc> getLibraryList(String JavaDoc libraryRoot, String JavaDoc librariesString) {
453         String JavaDoc[] libraries = librariesString.split(",");
454         List JavaDoc<String JavaDoc> shr = new ArrayList JavaDoc<String JavaDoc>();
455         for(String JavaDoc library: libraries){
456             library = library.trim();
457             File JavaDoc file = new File JavaDoc(library);
458             if (!file.isAbsolute()) {
459                 shr.add(libraryRoot+library);
460             } else {
461                 shr.add(library);
462             }
463         }
464         return shr;
465     }
466     
467     private static URL JavaDoc[] getURLList(List JavaDoc<String JavaDoc> librariesList) {
468         int i=0;
469         String JavaDoc [] sharedJars = librariesList.toArray(new String JavaDoc[] {});
470         URL JavaDoc [] urls = new URL JavaDoc[sharedJars.length];
471         for(String JavaDoc s:sharedJars){
472             try{
473                 URL JavaDoc url = (new File JavaDoc(s)).toURI().toURL();
474                 urls[i++] = url;
475             }catch(MalformedURLException JavaDoc e){
476                 Logger.getAnonymousLogger().warning(e.getMessage());
477                 Logger.getAnonymousLogger().log(Level.WARNING, "Exception while" +
478                                                                                                  "setting up shared chain", e);
479             }
480         }
481         return urls;
482     }
483     
484     private static void logFine(String JavaDoc s) {
485         if(bDebug) {
486             System.err.println(s);
487         }
488     }
489     
490     private static void appendOtherJarsToSharedChain(){
491         List JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
492         list.addAll(getSharedClasspath());
493         list.addAll(getAppServerClasspath());
494         list.addAll(getOptionalClasspath());
495         File JavaDoc[] files = getAllLibrariesInLib(installRoot);
496         for(File JavaDoc file:files){
497             try{
498                 if(!list.contains(file.getCanonicalPath())){
499                     _sharedClasspath.add(file.getCanonicalPath());
500                 }
501             }catch(java.io.IOException JavaDoc ioe){
502                 System.err.println("Error getting "+file.getAbsolutePath()+" "+ioe.getMessage());
503             }
504         }
505 }
506
507     private static File JavaDoc[] getAllLibrariesInLib(String JavaDoc asLib) {
508         File JavaDoc installLib = new File JavaDoc(asLib,"lib");
509         File JavaDoc [] files = installLib.listFiles(new FilenameFilter JavaDoc() {
510             public boolean accept(File JavaDoc dir, String JavaDoc name) {
511                 if(name.endsWith(".jar") || name.endsWith(".zip")) {
512                     // Exclude j2ee.jar, it is not needed in server runtime
513
// It is present for backward compatibility with AS 8.x
514
if (name.equals("j2ee.jar")) {
515                         return false;
516                     }
517                     return true;
518                 }
519                 return false;
520             }
521         });
522         return files;
523     }
524 }
525
Popular Tags