KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PLBootstrap


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  * This class is used for executing processes that are in an envronment that has a VERY limited
26  * command line execute size. The links off the start menu in windows has a maximum size of 259 characters.
27  * The PLBootstart requires that appserver installroot path is in the command twice and
28  * the java installroot is the in the command once, so allowance have to be given for user directory location
29  * preference. The reason, PLBoostrap is not placed inside a package is to keep the command size as small as possible.
30  * The associated properties file holds the classpath and property information requirer to setup the
31  * com.sun.enterprise.tools.ProcessLauncer environment.
32  */

33
34 import java.util.Properties JavaDoc;
35 import java.io.File JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.net.URLClassLoader JavaDoc;
38 import java.net.MalformedURLException JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40 import java.io.FileNotFoundException JavaDoc;
41 import java.io.FileInputStream JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.lang.reflect.Method JavaDoc;
44
45 public class PLBootstrap {
46     
47     public static String JavaDoc INSTALL_ROOT_PROPERTY_NAME="com.sun.aas.installRoot";
48     public static String JavaDoc PROCESS_LAUCHER_PROPERTIES_FILE_NAME="processLauncher.properties";
49     public static String JavaDoc PROCESS_LAUCHER_LIBRARIES="processLauncher.libraries";
50     public static String JavaDoc PROCESS_LAUCHER_MAIN_CLASS="processLauncher.main.class";
51     
52     
53     public static boolean bDebug=false;
54     
55     public static void main(String JavaDoc[] args) {
56         
57         try {
58             
59             Properties JavaDoc props=System.getProperties();
60             if (bDebug) props.list(System.out);
61             
62             // get processLauncher properties from installroot property
63
// directory as this class
64
String JavaDoc installRoot=System.getProperty(INSTALL_ROOT_PROPERTY_NAME);
65             if (installRoot == null) {
66                 System.out.println("ERROR: The System property \"com.sun.aas.installRoot\" has to be set!");
67                 System.exit(1);
68             }
69             
70             String JavaDoc appservLibPath=installRoot + File.separator + "lib" + File.separator;
71             // compile location to the properties file
72
if (bDebug) System.out.println("Reading properties from - " + appservLibPath + PROCESS_LAUCHER_PROPERTIES_FILE_NAME);
73             File JavaDoc propertiesFile=new File JavaDoc(appservLibPath + PROCESS_LAUCHER_PROPERTIES_FILE_NAME);
74             // see if properties exit, if not exception
75
if (!propertiesFile.canRead()) {
76                 throw new FileNotFoundException JavaDoc(propertiesFile.getPath());
77             }
78
79             // load properties from file
80
Properties JavaDoc properties=new Properties JavaDoc();
81             FileInputStream JavaDoc fInput=new FileInputStream JavaDoc(propertiesFile);
82             properties.load(fInput);
83             fInput.close();
84
85             // get the classpath property and create an appropriate classloader
86
URL JavaDoc[] classJars=stringToURLArray(appservLibPath, properties.getProperty(PROCESS_LAUCHER_LIBRARIES));
87             URLClassLoader JavaDoc classLoader=new URLClassLoader JavaDoc(classJars, Thread.currentThread().getContextClassLoader());
88
89             // Load ProcessLauncher class
90
Class JavaDoc plClass=classLoader.loadClass(properties.getProperty(PROCESS_LAUCHER_MAIN_CLASS));
91             if (bDebug) System.out.println("classloader = " + classLoader + " - " + plClass.getClassLoader());
92
93             // get the method
94
Method JavaDoc mainMethod=plClass.getDeclaredMethod("bootstrap", new Class JavaDoc[]{ String JavaDoc[].class });
95             mainMethod.invoke(null, new Object JavaDoc[]{ args });
96             
97             // explicit exit
98
System.exit(0);
99             
100        } catch (Throwable JavaDoc t) {
101            t.printStackTrace();
102            System.exit(1);
103
104         }
105     }
106     
107     
108     private static URL JavaDoc[] stringToURLArray(String JavaDoc appservLibPath, String JavaDoc jarList) throws MalformedURLException JavaDoc {
109
110         if (bDebug) System.out.println("jar list - " + jarList);
111         
112         // make sure list exists
113
if (jarList == null || jarList.equals("")) {
114             return new URL JavaDoc[0];
115         }
116
117         // loop though a build arraylist of jars
118
ArrayList JavaDoc jars=new ArrayList JavaDoc();
119         StringTokenizer JavaDoc stJars=new StringTokenizer JavaDoc(jarList, ",");
120         URL JavaDoc resultantURL=null;
121         String JavaDoc jarName=null, jarPath=null;
122         while (stJars.hasMoreTokens()) {
123             jarName=stJars.nextToken().trim();
124             if (bDebug) System.out.println("creating url for - " + jarName);
125             
126             // if name starts with a "/" use is as a full path
127
if (jarName.startsWith("/")) {
128                 jarPath=jarName;
129             } else {
130                 jarPath=appservLibPath + jarName;
131             }
132             
133             // should be in local path
134
resultantURL=new URL JavaDoc("file:" + jarPath);
135             if (bDebug) System.out.println("resultant url - " + resultantURL);
136             jars.add(resultantURL);
137         }
138         return (URL JavaDoc[])jars.toArray(new URL JavaDoc[jars.size()]);
139     }
140     
141     
142 }
Popular Tags