KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > impl > AppRunner


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of GOSSIP nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.impl;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.InputStream JavaDoc;
36 import java.util.Map JavaDoc;
37
38 import javax.xml.parsers.ParserConfigurationException JavaDoc;
39 import javax.xml.parsers.SAXParserFactory JavaDoc;
40
41
42
43 import org.apache.commons.logging.Log;
44 import org.apache.commons.logging.LogFactory;
45 import org.xml.sax.InputSource JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47 import org.xml.sax.XMLReader JavaDoc;
48
49 /**
50  *
51  *
52  * @author Bruce Lowery
53  */

54 public class AppRunner
55 {
56     private static final Log LOG = LogFactory.getLog(AppRunner.class);
57     
58     public static final String JavaDoc JEGG_FILE_PROP = "jegg.xml.path";
59     public static final String JavaDoc DEFAULT_JEGG_FILE = "jegg.xml";
60     
61     public static final void main(final String JavaDoc[] args) throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc
62     {
63         // Try to load the application descriptor from, in order:
64
// - command line
65
// - property setting
66
// - classpath
67

68         InputSource JavaDoc input = null;
69         
70         // Is name of app descriptor on command line?
71
if (0 < args.length)
72         {
73             input = findInput(args[0]);
74         }
75         
76         // Is name of app descriptor set in properties?
77
if (null == input)
78         {
79             String JavaDoc prop = System.getProperty(JEGG_FILE_PROP);
80             if (null != prop)
81             {
82                 input = findInput(prop);
83             }
84         }
85         
86         // Is there a default app descriptor?
87
if (null == input)
88         {
89             input = findInput(DEFAULT_JEGG_FILE);
90         }
91         
92         // Nothing - exit
93
if (null == input)
94         {
95             exit("No application descriptor found.");
96         }
97         
98         // Found it - load and go
99
else
100         {
101             load(input,null, true);
102         }
103     }
104     
105     private static void exit(final String JavaDoc message)
106     {
107         System.err.println(message);
108         System.exit(-1);
109     }
110     
111     static InputSource JavaDoc findInput(String JavaDoc name)
112     {
113         if (null == name) {return null;}
114         
115         // Check filesystem
116
File JavaDoc xml = new File JavaDoc(name);
117         if (xml.exists())
118         {
119             if (xml.canRead())
120             {
121                 try
122                 {
123                     return new InputSource JavaDoc(new FileInputStream JavaDoc(xml));
124                 }
125                 catch (FileNotFoundException JavaDoc e)
126                 {
127                     exit(e.getMessage()); // doesn't return
128
return null;
129                 }
130             }
131             exit("Unable to read: "+name); // doesn't return
132
return null;
133         }
134         
135         // Check classpath
136
InputStream JavaDoc is = AppRunner.class.getClassLoader().getResourceAsStream(name);
137         return (null == is) ? null : new InputSource JavaDoc(is);
138     }
139     
140     static void load(final InputSource JavaDoc source, final Map JavaDoc env, final boolean resolve) throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc
141     {
142         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
143         factory.setNamespaceAware(false);
144
145         XMLReader JavaDoc xmlReader = factory.newSAXParser().getXMLReader();
146
147         xmlReader.setContentHandler(new AppXMLContentHandler(env, resolve));
148         if (LOG.isDebugEnabled())
149             LOG.debug("Parsing source "+source);
150         xmlReader.parse(source);
151     }
152 }
153
Popular Tags