lcsim/src/org/lcsim/job
diff -u -r1.35 -r1.36
--- JobControlManager.java 6 May 2010 20:53:33 -0000 1.35
+++ JobControlManager.java 10 May 2010 22:58:33 -0000 1.36
@@ -38,7 +38,11 @@
import org.lcsim.util.cache.FileCache;
import org.lcsim.util.loop.LCIOEventSource;
import org.lcsim.util.loop.LCSimLoop;
+import org.lcsim.util.xml.ClasspathEntityResolver;
import org.lcsim.util.xml.JDOMExpressionFactory;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
/**
* The <code>JobControlManager</code> provides an XML frontend for running LCSim jobs.
@@ -81,6 +85,28 @@
Map<String,Double> constants = new HashMap<String,Double>();
JDOMExpressionFactory factory;
private static ParameterConverters paramConverter;
+
+ /**
+ * Hard-coded resolution of schema to resource location in lcsim jar.
+ * @author jeremym
+ */
+ public class LCSimSchemaResolver
+ implements EntityResolver
+ {
+ public LCSimSchemaResolver()
+ {}
+
+ public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException
+ {
+ System.out.println("publicId: " + publicId);
+ System.out.println("systemId: " + systemId);
+ InputStream in = this.getClass().getClassLoader().getResourceAsStream("org/lcsim/schemas/lcsim/1.0/lcsim.xsd");
+ InputSource src = new InputSource(in);
+ src.setSystemId(systemId);
+ src.setPublicId(publicId);
+ return src;
+ }
+ }
/**
* Default no-argument constructor.
@@ -235,12 +261,22 @@
*/
public void setup(InputStream in)
{
+ // Make the Document builder.
SAXBuilder builder = new SAXBuilder();
- factory = new JDOMExpressionFactory();
+
+ // Setup XML schema validation.
+ builder.setEntityResolver(new ClasspathEntityResolver());
+ //builder.setEntityResolver(new LCSimSchemaResolver());
+ builder.setValidation(true);
+ builder.setFeature("http://apache.org/xml/features/validation/schema", true);
+
+ // Setup expression resolution.
+ factory = new JDOMExpressionFactory();
if (paramConverter == null)
- paramConverter = new ParameterConverters(factory);
+ paramConverter = new ParameterConverters(factory);
builder.setFactory(factory);
+ // Build the document.
Document doc = null;
try
{
@@ -250,6 +286,8 @@
{
throw new RuntimeException(x);
}
+
+ // Setup the JobControlManager from the XML file.
setup(doc);
}
@@ -489,131 +527,133 @@
private void setupJobControlParameters()
{
- Element control = root.getChild("control");
- if (control != null)
- {
- // Verbose mode.
- Element verboseElement = control.getChild("verbose");
- if (verboseElement != null)
- verbose = Boolean.valueOf(verboseElement.getText());
-
- // Log file setup needs to come first.
- Element logFileElement = control.getChild("logFile");
- String logFilePath = null;
- if (logFileElement != null)
- {
- logFilePath = logFileElement.getText();
- File logFile = new File(logFilePath);
- try {
- logFile.createNewFile();
- logStream = new PrintStream(new FileOutputStream(logFile));
-
- // Redirect standard out and err to log file.
- System.setOut(logStream);
- System.setErr(logStream);
- }
- catch (IOException x)
- {
- throw new RuntimeException(x);
- }
- }
-
- // Print hello world message to appear at top of log.
- if (verbose)
- {
- logStream.println(this.getClass().getCanonicalName() + " is initialized.");
- logStream.println();
- logStream.println("--- Job Control Parameters ---");
- }
+ Element control = root.getChild("control");
- // Print log file path now that PrintStream is set.
- if (verbose)
- logStream.println("logFile = " + logFilePath);
+ if (control == null)
+ return;
- // Number of events to run.
- Element controlElement = control.getChild("numberOfEvents");
- if (controlElement != null)
- {
- maxEvents = Integer.valueOf(controlElement.getText());
- if (verbose)
- logStream.println("numberOfEvents = " + maxEvents);
- }
-
- Element skipElement = control.getChild("skipEvents");
- if (skipElement != null)
- {
- skipEvents = Integer.valueOf(skipElement.getText());
- if (verbose)
- logStream.println("skipEvents = " + skipEvents);
- }
-
- Element dryRunElement = control.getChild("dryRun");
- if (dryRunElement != null)
- {
- dryRun = Boolean.valueOf(dryRunElement.getText());
- if (verbose)
- logStream.println("dryRun = " + dryRun);
- }
+ // Verbose mode.
+ Element verboseElement = control.getChild("verbose");
+ if (verboseElement != null)
+ verbose = Boolean.valueOf(verboseElement.getText());
+
+ // Log file setup needs to come first.
+ Element logFileElement = control.getChild("logFile");
+ String logFilePath = null;
+ if (logFileElement != null)
+ {
+ logFilePath = logFileElement.getText();
+ File logFile = new File(logFilePath);
+ try
+ {
+ logFile.createNewFile();
+ logStream = new PrintStream(new FileOutputStream(logFile));
+
+ // Redirect standard out and err to log file.
+ System.setOut(logStream);
+ System.setErr(logStream);
+ }
+ catch (IOException x)
+ {
+ throw new RuntimeException(x);
+ }
+ }
- // The cache directory. Defaults to the current directory.
- Element cacheDirElement = control.getChild("cacheDirectory");
- if (cacheDirElement != null)
- {
- cacheDirectory = new File(cacheDirElement.getText());
- if (!cacheDirectory.exists())
- throw new RuntimeException("cacheDirectory does not exist at location: " + cacheDirElement.getText());
- }
- else
- {
- // Default to the current working dir if cacheDirectory was not set explicitly.
- cacheDirectory = new File(System.getProperties().get("user.dir").toString());
- }
+ // Print hello world message to appear at top of log.
+ if (verbose)
+ {
+ logStream.println(this.getClass().getCanonicalName() + " is initialized.");
+ logStream.println();
+ logStream.println("--- Job Control Parameters ---");
+ }
- if (verbose)
- logStream.println("cacheDirectory = " + cacheDirectory);
+ // Print log file path now that PrintStream is set.
+ if (verbose)
+ logStream.println("logFile = " + logFilePath);
+
+ // Number of events to run.
+ Element controlElement = control.getChild("numberOfEvents");
+ if (controlElement != null)
+ {
+ maxEvents = Integer.valueOf(controlElement.getText());
+ if (verbose)
+ logStream.println("numberOfEvents = " + maxEvents);
+ }
- Element printStatisticsElement = control.getChild("printDriverStatistics");
- if (printStatisticsElement != null)
- {
- printDriverStatistics = Boolean.valueOf(printStatisticsElement.getText());
- }
+ Element skipElement = control.getChild("skipEvents");
+ if (skipElement != null)
+ {
+ skipEvents = Integer.valueOf(skipElement.getText());
+ if (verbose)
+ logStream.println("skipEvents = " + skipEvents);
+ }
- if (verbose)
- logStream.println("printDriverStatistics = " + printDriverStatistics);
+ Element dryRunElement = control.getChild("dryRun");
+ if (dryRunElement != null)
+ {
+ dryRun = Boolean.valueOf(dryRunElement.getText());
+ if (verbose)
+ logStream.println("dryRun = " + dryRun);
+ }
- Element printSystemPropertiesElement = control.getChild("printSystemProperties");
- if (printSystemPropertiesElement != null)
- {
- printSystemProperties = Boolean.valueOf(printSystemPropertiesElement.getText());
- }
+ // The cache directory. Defaults to the current directory.
+ Element cacheDirElement = control.getChild("cacheDirectory");
+ if (cacheDirElement != null)
+ {
+ cacheDirectory = new File(cacheDirElement.getText());
+ if (!cacheDirectory.exists())
+ throw new RuntimeException("cacheDirectory does not exist at location: " + cacheDirElement.getText());
+ }
+ else
+ {
+ // Default to the current working dir if cacheDirectory was not set explicitly.
+ cacheDirectory = new File(System.getProperties().get("user.dir").toString());
+ }
- if (verbose)
- logStream.println("printSystemProperties = " + printSystemProperties);
+ if (verbose)
+ logStream.println("cacheDirectory = " + cacheDirectory);
- Element printUserClassPathElement = control.getChild("printUserClassPath");
- if (printUserClassPathElement != null)
- printUserClassPath = Boolean.valueOf(printUserClassPathElement.getText());
+ Element printStatisticsElement = control.getChild("printDriverStatistics");
+ if (printStatisticsElement != null)
+ {
+ printDriverStatistics = Boolean.valueOf(printStatisticsElement.getText());
+ }
- if (verbose)
- logStream.println("printUserClassPath = " + printUserClassPath);
-
- Element printVersionElement = control.getChild("printVersion");
- if (printVersionElement != null)
- {
- printVersion = Boolean.valueOf(printVersionElement.getText());
- }
+ if (verbose)
+ logStream.println("printDriverStatistics = " + printDriverStatistics);
- Element printDriversDetailedElement = control.getChild("printDriversDetailed");
- if (printDriversDetailedElement != null)
- printDriversDetailed = Boolean.valueOf(printDriversDetailedElement.getText());
+ Element printSystemPropertiesElement = control.getChild("printSystemProperties");
+ if (printSystemPropertiesElement != null)
+ {
+ printSystemProperties = Boolean.valueOf(printSystemPropertiesElement.getText());
+ }
- if (verbose)
- {
- logStream.println("printDriversDetailed = " + printDriversDetailed);
- logStream.println("--- End Job Control Parameters ---");
- logStream.println();
- }
- }
+ if (verbose)
+ logStream.println("printSystemProperties = " + printSystemProperties);
+
+ Element printUserClassPathElement = control.getChild("printUserClassPath");
+ if (printUserClassPathElement != null)
+ printUserClassPath = Boolean.valueOf(printUserClassPathElement.getText());
+
+ if (verbose)
+ logStream.println("printUserClassPath = " + printUserClassPath);
+
+ Element printVersionElement = control.getChild("printVersion");
+ if (printVersionElement != null)
+ {
+ printVersion = Boolean.valueOf(printVersionElement.getText());
+ }
+
+ Element printDriversDetailedElement = control.getChild("printDriversDetailed");
+ if (printDriversDetailedElement != null)
+ printDriversDetailed = Boolean.valueOf(printDriversDetailedElement.getText());
+
+ if (verbose)
+ {
+ logStream.println("printDriversDetailed = " + printDriversDetailed);
+ logStream.println("--- End Job Control Parameters ---");
+ logStream.println();
+ }
}
private void setupClassLoader()
@@ -930,7 +970,7 @@
if (name.equals("boolean")) return boolean.class;
if (name.equals("String")) return String.class;
return null;
- }
+ }
}
/**