1 29 package jegg.impl; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileNotFoundException ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.util.Map ; 37 38 import javax.xml.parsers.ParserConfigurationException ; 39 import javax.xml.parsers.SAXParserFactory ; 40 41 42 43 import org.apache.commons.logging.Log; 44 import org.apache.commons.logging.LogFactory; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.XMLReader ; 48 49 54 public class AppRunner 55 { 56 private static final Log LOG = LogFactory.getLog(AppRunner.class); 57 58 public static final String JEGG_FILE_PROP = "jegg.xml.path"; 59 public static final String DEFAULT_JEGG_FILE = "jegg.xml"; 60 61 public static final void main(final String [] args) throws ParserConfigurationException , IOException , SAXException 62 { 63 68 InputSource input = null; 69 70 if (0 < args.length) 72 { 73 input = findInput(args[0]); 74 } 75 76 if (null == input) 78 { 79 String prop = System.getProperty(JEGG_FILE_PROP); 80 if (null != prop) 81 { 82 input = findInput(prop); 83 } 84 } 85 86 if (null == input) 88 { 89 input = findInput(DEFAULT_JEGG_FILE); 90 } 91 92 if (null == input) 94 { 95 exit("No application descriptor found."); 96 } 97 98 else 100 { 101 load(input,null, true); 102 } 103 } 104 105 private static void exit(final String message) 106 { 107 System.err.println(message); 108 System.exit(-1); 109 } 110 111 static InputSource findInput(String name) 112 { 113 if (null == name) {return null;} 114 115 File xml = new File (name); 117 if (xml.exists()) 118 { 119 if (xml.canRead()) 120 { 121 try 122 { 123 return new InputSource (new FileInputStream (xml)); 124 } 125 catch (FileNotFoundException e) 126 { 127 exit(e.getMessage()); return null; 129 } 130 } 131 exit("Unable to read: "+name); return null; 133 } 134 135 InputStream is = AppRunner.class.getClassLoader().getResourceAsStream(name); 137 return (null == is) ? null : new InputSource (is); 138 } 139 140 static void load(final InputSource source, final Map env, final boolean resolve) throws ParserConfigurationException , IOException , SAXException 141 { 142 SAXParserFactory factory = SAXParserFactory.newInstance(); 143 factory.setNamespaceAware(false); 144 145 XMLReader 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 |