KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > launcher > ProcessLauncherConfig


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  * ProcessLauncherConfig.java
26  *
27  * Created on November 25, 2003, 10:18 AM
28  */

29
30 package com.sun.enterprise.tools.launcher;
31 import java.io.FileWriter JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FilenameFilter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37 import java.util.logging.Level JavaDoc;
38
39 import javax.xml.parsers.DocumentBuilder JavaDoc;
40 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44 import org.w3c.dom.NodeList JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import javax.xml.parsers.ParserConfigurationException JavaDoc;
47 import com.sun.logging.LogDomains;
48 import com.sun.enterprise.config.ConfigException;
49 import com.sun.enterprise.util.RelativePathResolver;
50 import com.sun.enterprise.util.i18n.StringManager;
51
52
53 /**
54  * This class is used to read the processLauncer.xml file, parse it and provide a useful structure
55  * for use in the ProcessLauncher class.
56  *
57  * processLauncher.xml File layout
58  *
59  * <processes>
60  * <process name="s1as8-server">
61  * <sysproperty key="com.sun.aas.instanceName" value="${com.sun.aas.instanceName}" if="com.sun.aas.instanceName" />
62  * <main_class classname="com.sun.enterprise.server.PEMain" />
63  * <classpath dir="${com.sun.aas.installRoot}/lib"
64  * includes=".*jar$"
65  * excludes="appserv-rt.jar, ...." />
66  * prefix="/export/home/basler/test.jar"
67  * </process>
68  * ....
69  * </processes>
70  *
71  *
72  * processes - Docroot element that has the defined process element as its children
73  *
74  * process - Main element that can be mapped 1-to-1 to a process definition that is to be
75  * executed through the ProcessLauncher
76  * name - Name of the process definition type. Must be unique within the processLauncher.xml file.
77  *
78  * sysproperty - Defines system properties for the java command to be executed. A "-D" is pre-pended
79  * to the property key unless the key starts with "-X". An equal sign ("=") with the value is
80  * appended to the key if the value attribute exists. The property will not be added if the
81  * "if" system property designated by the associated attribute is not present. The
82  * com.sun.enterprise.util.RelativePathResolver class will be used to resolve system property
83  * tokens in the values of the sysproperty elements.
84  * key - Name of system property
85  * value - Value for the system property. This value is optional. An equals sign "=" will not be
86  * appended to the key if this value doesn't exist.
87  * if - System property that must exist for this system property to be added. This attribute is optional.
88  *
89  * main_class - Element that denotes the main class to execute in the java command.
90  * classname - Fully qualified class name of the class to run
91  *
92  * classpath - Element that builds the classpath portion of the java command.
93  * dir - This attribute holds the fully qualified path to the lib directory where the jar exists.
94  * This is optional, but if it is not entered the "includes" & "excludes" attributes are not used.
95  * This is to allow for a known fully qualified classpath to be enter via the "prefix" attritbute.
96  * The com.sun.enterprise.util.RelativePathResolver class will be used to resolve system property tokens
97  * in this attribute.
98  * includes - A comma delimited list of jar names to include in the classpath if they exist in the lib
99  * directory specified in the "dir" attribute. The names can contain a regular expression to
100  * assist name resolution. For users who are not familiar with regular expressions a short-cut has
101  * been added to allow a "*" as a wildcard prefix (e.g. "*.jar"). This functionality was added for
102  * backwards compatibility with the Apache Commons Launcher and for future uses the equivalent regular
103  * expression ".*jar$" should be used.
104  * excludes - A comma delimited list of jar names to exclude in the classpath if they exist in the lib directory
105  * specified in the "dir" attribute. The names can contain a regular expression to assist name resolution.
106  * For users who are not familiar with regular expressions a short-cut has been added to allow a "*" as a
107  * wildcard prefix (e.g. "*.jar"). This functionality was added for backwards compatibility with the
108  * Apache Commons Launcher and for future uses the equivalent regular expression ".*jar$" should be used.
109  * prefix - A list of fully qualified classpath jars. The com.sun.enterprise.util.RelativePathResolver class will
110  * be used to resolve system property tokens in this attribute. This attribute can also be used if a
111  * classpath is known before execution time. The java default system property "${path.separator}" should
112  * be used as the path delimiter to keep the profile platform agnostic.
113  */

114 public class ProcessLauncherConfig {
115
116     private String JavaDoc _process="";
117     private String JavaDoc _configFile="";
118     private String JavaDoc _classpathExcludes="";
119     private String JavaDoc _classpathIncludes="";
120     private String JavaDoc _classpathLibDir="";
121     private String JavaDoc _classpathPrefix="";
122     private String JavaDoc _classpathJ2se14Prefix="";
123     private String JavaDoc _classpathJ2se15OrLaterPrefix="";
124     private String JavaDoc _mainClass="";
125     private StringManager _strMgr=null;
126     private Properties JavaDoc _sysProperties=null;
127     private static boolean bDebug=false;
128
129     private static final String JavaDoc PROCESS="process";
130     private static final String JavaDoc SYSTEM_PROPERTY="sysproperty";
131     private static final String JavaDoc MAIN_CLASS="main_class";
132     private static final String JavaDoc MAIN_CLASS_CLASSNAME="classname";
133     private static final String JavaDoc CLASSPATH="classpath";
134     private static final String JavaDoc CLASSPATH_INCLUDES="includes";
135     private static final String JavaDoc CLASSPATH_EXCLUDES="excludes";
136     private static final String JavaDoc CLASSPATH_PREFIX="prefix";
137     private static final String JavaDoc CLASSPATH_J2SE1_4_PREFIX="j2se1_4_prefix";
138     private static final String JavaDoc CLASSPATH_J2SE1_5_OR_LATER_PREFIX="j2se1_5_or_later_prefix";
139     private static final String JavaDoc CLASSPATH_DIR="dir";
140
141
142     protected ProcessLauncherConfig() {
143     }
144
145     /**
146      * Overloaded construnctor to intialize the process artifacts into a struncture that can be read
147      * by the ProcessLauncher in one shot
148      */

149     protected ProcessLauncherConfig(String JavaDoc configFile, String JavaDoc process) throws ConfigException {
150         initializeConfig(configFile, process);
151     }
152
153     /**
154      * This method read the processLauncher.xml file and calls a method that loads the named process configuration into
155      * the a struncture that can be used by the processlauncher
156      */

157     protected void initializeConfig(String JavaDoc configFile, String JavaDoc process) throws ConfigException {
158         // set internal variables
159
_configFile=configFile;
160         _process=process;
161         _sysProperties=new Properties JavaDoc();
162         _strMgr=StringManager.getManager(ProcessLauncherConfig.class);
163
164         try {
165             // read in config
166
boolean bFoundProcess=false;
167             String JavaDoc key=null;
168             Document JavaDoc doc=readDOM(configFile);
169             Element JavaDoc element=null;
170             NodeList JavaDoc nl=doc.getElementsByTagName(PROCESS);
171
172             for(int ii=0;ii < nl.getLength(); ii++) {
173                 // find correct process
174
element=(Element JavaDoc)nl.item(ii);
175                 key=element.getAttribute("name");
176                 if(key.equals(process)) {
177                     // found correct process, extract information
178
bFoundProcess=true;
179                     loadProcess(element);
180                 }
181             }
182
183             if(!bFoundProcess) {
184                 // error flaguser
185
throw new ConfigException(_strMgr.getString("launcher.process_launcher_config_not_found",
186                     new String JavaDoc[]{process, configFile}));
187             }
188         } catch (ConfigException ce) {
189             throw ce;
190         } catch (Exception JavaDoc e) {
191             throw new ConfigException(_strMgr.getString("launcher.process_launcher_config_exception", _configFile), e);
192         }
193     }
194
195     /**
196      * This methods digests the name process into a struncture that can be used by the ProcessLauncer
197      */

198     protected void loadProcess(Element JavaDoc process) {
199         // read in process
200
String JavaDoc key=null, value=null, ifx=null;
201         Element JavaDoc element=null;
202
203         // load system properties specificaly for this process
204
NodeList JavaDoc nl=process.getChildNodes();
205         for(int ii=0;ii < nl.getLength(); ii++) {
206
207             if(nl.item(ii) instanceof Element JavaDoc) {
208                 element=(Element JavaDoc)nl.item(ii);
209
210                 if(element.getTagName().equals(SYSTEM_PROPERTY)) {
211                     // system_properties and add to config
212
key=element.getAttribute("key");
213                     value=element.getAttribute("value");
214                     ifx=element.getAttribute("if");
215
216                     // get attribte always returns "" if it doesn't exist
217
if(!key.equals("")) {
218                         // check to see if there is a condition on the property
219
if(ifx.equals("") || System.getProperty(ifx) != null) {
220                             // add system properties
221
_sysProperties.setProperty(key, RelativePathResolver.resolvePath(value));
222                         }
223                     }
224
225                 } else if(element.getTagName().equals(MAIN_CLASS)) {
226                     // main class add to config
227
_mainClass=element.getAttribute(MAIN_CLASS_CLASSNAME);
228
229                 } else if(element.getTagName().equals(CLASSPATH)) {
230                     // classpath add to config
231
_classpathLibDir=element.getAttribute(CLASSPATH_DIR);
232                     _classpathIncludes=element.getAttribute(CLASSPATH_INCLUDES);
233                     _classpathExcludes=element.getAttribute(CLASSPATH_EXCLUDES);
234                     _classpathPrefix=element.getAttribute(CLASSPATH_PREFIX);
235                     _classpathJ2se14Prefix=element.getAttribute(CLASSPATH_J2SE1_4_PREFIX);
236                     _classpathJ2se15OrLaterPrefix=element.getAttribute(CLASSPATH_J2SE1_5_OR_LATER_PREFIX);
237                 }
238             }
239         }
240     }
241
242
243     protected String JavaDoc getConfigFile() {
244         return _configFile;
245     }
246     protected String JavaDoc getClasspathLibDir() {
247         return _classpathLibDir;
248     }
249     protected String JavaDoc getClasspathIncludes() {
250         return _classpathIncludes;
251     }
252     protected String JavaDoc getClasspathExcludes() {
253         return _classpathExcludes;
254     }
255     protected String JavaDoc getClasspathPrefix() {
256         return _classpathPrefix;
257     }
258     protected String JavaDoc getClasspathJ2se14Prefix() {
259         return _classpathJ2se14Prefix;
260     }
261     protected String JavaDoc getClasspathJ2se15OrLaterPrefix() {
262         return _classpathJ2se15OrLaterPrefix;
263     }
264
265     
266     
267     protected String JavaDoc getMainClass() {
268         return _mainClass;
269     }
270     protected Properties JavaDoc getSystemProperties() {
271         return _sysProperties;
272     }
273
274     /**
275     * readDOM - This method reads in XML into a DOM
276     *
277     * @param file - A qualified file where to read the XML
278     * @return Document - The read in DOM
279     * @exception - Any thrown exception that may occur during the read process
280     */

281     protected Document JavaDoc readDOM(String JavaDoc file) throws SAXException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc {
282         DocumentBuilderFactory JavaDoc dbf=DocumentBuilderFactory.newInstance();
283         DocumentBuilder JavaDoc db=dbf.newDocumentBuilder();
284         return db.parse(new File JavaDoc(file));
285     }
286 }
287
Popular Tags