lcsim/src/org/lcsim/job
diff -u -r1.64 -r1.65
--- JobControlManager.java 10 Jul 2012 00:10:43 -0000 1.64
+++ JobControlManager.java 14 Feb 2013 22:30:07 -0000 1.65
@@ -72,11 +72,12 @@
* matching settings should be left out of the XML job file. This is not the case, however, for input files
* specified by the "-i" option, which are appended to the ones listed in the steering file.
*
- * @version $Id: JobControlManager.java,v 1.64 2012/07/10 00:10:43 jeremy Exp $
+ * @version $Id: JobControlManager.java,v 1.65 2013/02/14 22:30:07 jeremy Exp $
* @author Jeremy McCormick
*/
// TODO: Do proper logging using Java Logger class rather than just a PrintStream.
-public class JobControlManager {
+public class JobControlManager
+{
// The LCIO record loop.
private LCSimLoop loop;
@@ -98,6 +99,7 @@
// Boolean job options.
private boolean performDryRun;
private boolean rewrite;
+ private boolean useSteeringResource;
// Settings effecting logging verbosity.
private boolean printInputFiles;
@@ -139,7 +141,8 @@
/**
* The default constructor.
*/
- public JobControlManager() {
+ public JobControlManager()
+ {
try {
fileCache = new FileCache();
} catch (IOException x) {
@@ -153,7 +156,8 @@
*
* @param args The command line arguments.
*/
- public static void main(String args[]) {
+ public static void main(String args[])
+ {
JobControlManager mgr = new JobControlManager();
if (args.length == 0) {
mgr.logStream.println("java -jar lcsim-bin.jar [options] steeringFile.xml");
@@ -170,7 +174,8 @@
*
* @return The command line options for the manager.
*/
- private static Options createCommandLineOptions() {
+ private static Options createCommandLineOptions()
+ {
Options options = new Options();
options.addOption(new Option("p", true, "Load a properties file containing variable definitions"));
options.addOption(new Option("D", true, "Define a variable with form [name]=[value]"));
@@ -181,6 +186,7 @@
options.addOption(new Option("x", false, "Perform a dry run which does not process events"));
options.addOption(new Option("q", false, "Turn on quiet mode."));
options.addOption(new Option("i", true, "Add an LCIO input file to process."));
+ options.addOption(new Option("r", false, "Use a steering resource rather than a file."));
return options;
}
@@ -191,7 +197,8 @@
*
* @param args The command line arguments.
*/
- private void parseCommandLineOptions(String args[]) {
+ private void parseCommandLineOptions(String args[])
+ {
// Setup parser.
CommandLineParser parser = new PosixParser();
@@ -276,6 +283,10 @@
if (cl.hasOption("x")) {
this.performDryRun = true;
}
+
+ if (cl.hasOption("r")) {
+ this.useSteeringResource = true;
+ }
// Check that there is exactly one extra argument for the XML steering file.
if (cl.getArgList().size() == 0) {
@@ -283,13 +294,7 @@
} else if (cl.getArgList().size() > 1) {
throw new RuntimeException("Too many extra arguments.");
}
-
- // Setup the XML steering file and check for its existence.
- File xmlRunControlFile = new File((String) cl.getArgList().get(0));
- if (!xmlRunControlFile.exists()) {
- throw new RuntimeException("The steering file " + args[0] + " does not exist!");
- }
-
+
// Local LCIO files to process.
if (cl.hasOption("i")) {
String[] files = cl.getOptionValues("i");
@@ -301,9 +306,21 @@
inputFiles.add(new File(fileName));
}
}
-
- // Setup job parameters from XML now that CL options are all processed.
- setup(xmlRunControlFile);
+
+ // Steering argument points to either a file or embedded resource.
+ String steering = (String) cl.getArgList().get(0);
+
+ // Using an embedded resource in the jar for steering.
+ if (this.useSteeringResource) {
+ setup(steering);
+ // Steering from a local file.
+ } else {
+ File xmlRunControlFile = new File(steering);
+ if (!xmlRunControlFile.exists()) {
+ throw new RuntimeException("The steering file " + args[0] + " does not exist!");
+ }
+ setup(xmlRunControlFile);
+ }
}
/**
@@ -313,7 +330,8 @@
* @param key The variable name.
* @param value The variable's value.
*/
- public void addVariableDefinition(String key, String value) {
+ public void addVariableDefinition(String key, String value)
+ {
if (verbose) {
logStream.println(key + " = " + value);
}
@@ -327,7 +345,8 @@
/**
* Execute a job using the current parameters.
*/
- public boolean run() {
+ public boolean run()
+ {
// If setup was not called first, then abort the job.
if (!wasSetup) {
logStream.println("Aborting job! Setup was never called.");
@@ -373,7 +392,7 @@
// Execute the loop.
long processedEvents = loop.loop(maxEvents, statsStream);
if (processedEvents != maxEvents) {
- logStream.println("End of file reached");
+ logStream.println("End of file reached");
} else if (verbose) {
logStream.println();
logStream.println("End time: " + (new Date()));
@@ -397,7 +416,8 @@
*
* @return The LCSimLoop.
*/
- public LCSimLoop getLCSimLoop() {
+ public LCSimLoop getLCSimLoop()
+ {
if (loop == null) {
loop = new LCSimLoop();
// Add the drivers to the loop.
@@ -414,7 +434,8 @@
*
* @return A <code>List</code> of <code>Drivers</code>.
*/
- public List<Driver> getDriverExecList() {
+ public List<Driver> getDriverExecList()
+ {
return this.driverExec;
}
@@ -423,7 +444,8 @@
*
* @param file
*/
- public void setup(File file) {
+ public void setup(File file)
+ {
try {
setup((new FileInputStream(file)));
} catch (FileNotFoundException x) {
@@ -434,7 +456,8 @@
/**
* Setup job parameters from an embedded resource. This method calls {@link #setup(InputStream)}.
*/
- public void setup(String resourceURL) {
+ public void setup(String resourceURL)
+ {
setup(this.getClass().getResourceAsStream(resourceURL));
}
@@ -443,7 +466,8 @@
*
* @param in The XML input stream.
*/
- public void setup(InputStream in) {
+ public void setup(InputStream in)
+ {
// Make the Document builder.
SAXBuilder builder = new SAXBuilder();
@@ -476,7 +500,8 @@
*
* @param doc The lcsim recon XML document describing the job.
*/
- private void setup(Document doc) {
+ private void setup(Document doc)
+ {
if (wasSetup) {
logStream.println("Ignoring call to setup as it was already called.");
return;
@@ -538,7 +563,8 @@
*
* @param doc The XML document.
*/
- private void substituteVariables(Document doc) {
+ private void substituteVariables(Document doc)
+ {
substituteVariables(doc.getRootElement());
}
@@ -549,7 +575,8 @@
* @param element The XML element.
* @throw RuntimeException If a variable does not exist in the <code>variableMap</code>.
*/
- private void substituteVariables(Element element) {
+ private void substituteVariables(Element element)
+ {
String text = element.getTextNormalize();
if (text.length() != 0) {
// Create a new matcher.
@@ -602,7 +629,8 @@
*
* @param doc The XML steering doc with variables substituted.
*/
- private void rewriteXMLSteering(Document doc) {
+ private void rewriteXMLSteering(Document doc)
+ {
if (verbose) {
logStream.println("Rewriting XML to " + this.rewriteFile + " ...");
}
@@ -620,7 +648,8 @@
/**
* Setup the drivers from the XML job file, adding them to the JobManager's <code>driverMap</code> .
*/
- private void setupDrivers() {
+ private void setupDrivers()
+ {
if (printDriversDetailed) {
logStream.println("--- Drivers ---");
}
@@ -660,11 +689,9 @@
try {
newDriver = (Driver) driverClass.newInstance();
} catch (InstantiationException x) {
- throw new RuntimeException("Failed to create a Driver of class " + type + ". "
- + "This class might be missing a public constructor with no arguments.", x);
+ throw new RuntimeException("Failed to create a Driver of class " + type + ". " + "This class might be missing a public constructor with no arguments.", x);
} catch (IllegalAccessException x) {
- throw new RuntimeException("Cannot access Driver type " + type + ". " + "You may need to make this class or its constructor public.",
- x);
+ throw new RuntimeException("Cannot access Driver type " + type + ". " + "You may need to make this class or its constructor public.", x);
}
// Get a list of Driver parameters.
@@ -705,8 +732,7 @@
// Found several, overloaded methods. Try to disambiguate them.
else if (methodCandidates.size() > 1) {
if (parameterElement.getAttribute("type") == null)
- throw new RuntimeException("Parameter " + pname + " in Driver " + driverClass.getCanonicalName()
- + " is overloaded, but a type field is missing from the parameter's XML element.");
+ throw new RuntimeException("Parameter " + pname + " in Driver " + driverClass.getCanonicalName() + " is overloaded, but a type field is missing from the parameter's XML element.");
try {
// Try a primitive type first.
propertyType = getPrimitiveType(parameterElement.getAttribute("type").getValue());
@@ -716,8 +742,7 @@
if (propertyType == null)
propertyType = Class.forName(parameterElement.getAttribute("type").getValue());
} catch (ClassNotFoundException x) {
- throw new RuntimeException("Bad user type: " + parameterElement.getAttribute("type").getValue() + " for parameter " + pname
- + ".");
+ throw new RuntimeException("Bad user type: " + parameterElement.getAttribute("type").getValue() + " for parameter " + pname + ".");
}
// Find a method that matches the user type.
for (Method candidateMethod : methodCandidates) {
@@ -740,8 +765,7 @@
// Convert the parameter to the appropriate type.
IParameterConverter converter = paramConverter.getConverterForType(propertyType);
if (converter == null) {
- throw new RuntimeException("No converter found for parameter " + parameterElement.getName() + " with type "
- + propertyType.getName() + ".");
+ throw new RuntimeException("No converter found for parameter " + parameterElement.getName() + " with type " + propertyType.getName() + ".");
}
Object nextParameter = converter.convert(factory, parameterElement);
@@ -787,14 +811,16 @@
* @param name The unique name of the Driver.
* @param driver The instance of the Driver.
*/
- private void addDriver(String name, Driver driver) {
+ private void addDriver(String name, Driver driver)
+ {
if (driverMap.containsKey(name)) {
throw new RuntimeException("Duplicate driver name: " + name);
}
driverMap.put(name, driver);
}
- private void printSystemProperties(PrintStream ps) {
+ private void printSystemProperties(PrintStream ps)
+ {
logStream.println("--- System Properties ---");
for (Entry<Object, Object> entry : System.getProperties().entrySet()) {
logStream.println(entry.getKey() + " = " + entry.getValue());
@@ -810,7 +836,8 @@
* @param fileList The list files to which new file will be appended.
* @return The file that was created from the text.
*/
- private File processFileText(String fileText, List<File> fileList) {
+ private File processFileText(String fileText, List<File> fileList)
+ {
Element fileElement = new Element("file");
fileElement.setText(fileText.trim());
return processFileElement(fileElement, fileList);
@@ -823,7 +850,8 @@
* @param fileList List to append new <code>File</code>.
* @return The <code>File</code> object.
*/
- private File processFileElement(Element fileElement, List<File> fileList) {
+ private File processFileElement(Element fileElement, List<File> fileList)
+ {
String fileLoc = processPath(fileElement.getText().trim());
File file = null;
@@ -861,7 +889,8 @@
* Setup the list of input files to be processed from the XML job file.
*/
@SuppressWarnings("unchecked")
- private void setupInputFiles() {
+ private void setupInputFiles()
+ {
if (root.getChild("inputFiles") == null) {
logStream.println("No input files in XML file.");
// this.performDryRun = true;
@@ -973,7 +1002,8 @@
*
* @param verbose True to turn on verbose mode; false to turn on quiet mode.
*/
- private void setVerbose(boolean verbose) {
+ private void setVerbose(boolean verbose)
+ {
this.verbose = verbose;
this.printInputFiles = verbose;
this.printDriversDetailed = verbose;
@@ -986,7 +1016,8 @@
/**
* Setup the job control parameters using the XML steering document.
*/
- private void setupJobControlParameters() {
+ private void setupJobControlParameters()
+ {
Element control = root.getChild("control");
if (control == null)
@@ -1115,7 +1146,8 @@
/**
* Setup the manager's class loader.
*/
- private void setupClassLoader() {
+ private void setupClassLoader()
+ {
Element classpath = root.getChild("classpath");
List<URL> urlList = new ArrayList<URL>();
if (classpath != null) {
@@ -1174,7 +1206,8 @@
/**
* Setup the file cache.
*/
- private void setupFileCache() {
+ private void setupFileCache()
+ {
if (cacheDirectory == null)
return;
try {
@@ -1189,7 +1222,8 @@
/**
* Create the constants from the XML file.
*/
- private void processConstants() {
+ private void processConstants()
+ {
Element define = root.getChild("define");
if (define != null) {
for (Object o : define.getChildren()) {
@@ -1205,7 +1239,8 @@
/**
* Setup the system of units.
*/
- private void setupUnits() {
+ private void setupUnits()
+ {
Constants constants = Constants.getInstance();
for (Entry<String, Double> unit : constants.entrySet()) {
factory.addConstant(unit.getKey(), unit.getValue());
@@ -1218,7 +1253,8 @@
* @param name The name of the type.
* @return The primitive type class.
*/
- private static Class getPrimitiveType(String name) {
+ private static Class getPrimitiveType(String name)
+ {
if (name.equals("byte"))
return byte.class;
if (name.equals("short"))
@@ -1246,7 +1282,8 @@
* @param klass The class.
* @return A list of setter methods.
*/
- private List<Method> getSetterMethods(Class klass) {
+ private List<Method> getSetterMethods(Class klass)
+ {
List<Method> methods = new ArrayList<Method>();
Class currentClass = klass;
while (currentClass != null) {
@@ -1267,7 +1304,8 @@
* @param path The original path.
* @return The path with home dir substitution.
*/
- private String processPath(String path) {
+ private String processPath(String path)
+ {
if (path.startsWith("~")) {
return path.replaceFirst("~", System.getProperty("user.home"));
} else {
@@ -1276,7 +1314,8 @@
}
// Wrap Drivers with a DriverAdapter. Only one will be created per job.
- public DriverAdapter getDriverAdapter() {
+ public DriverAdapter getDriverAdapter()
+ {
if (driverAdapter == null) {
Driver topDriver = new Driver();
for (Driver driver : getDriverExecList()) {
@@ -1288,14 +1327,16 @@
}
// Process a single LCSim event using the DriverAdapter.
- public void processEvent(EventHeader event) {
+ public void processEvent(EventHeader event)
+ {
getDriverAdapter().recordSupplied(new RecordEvent(loop, event));
}
/**
* Reset the class's state so another job can be run.
*/
- public synchronized void reset() {
+ public synchronized void reset()
+ {
driverAdapter = null;
loop = null;
@@ -1348,27 +1389,33 @@
wasSetup = false;
}
- public void configure() {
+ public void configure()
+ {
getDriverAdapter().start(null);
}
- public void reconfigure() {
+ public void reconfigure()
+ {
getDriverAdapter().start(null);
}
- public void suspend() {
+ public void suspend()
+ {
getDriverAdapter().suspend(null);
}
- public void finish() {
+ public void finish()
+ {
getDriverAdapter().finish(null);
}
- public void setPerformDryRun(boolean d) {
+ public void setPerformDryRun(boolean d)
+ {
this.performDryRun = d;
}
- private void checkConditions() {
+ private void checkConditions()
+ {
// Get conditions element if it is there; otherwise return.
Element conditionsElement = root.getChild("conditions");
@@ -1406,8 +1453,7 @@
} catch (ConditionsSetNotFoundException e) {
logStream.println(conditionElement.getTextTrim() + " - NOT FOUND");
if (required)
- throw new RuntimeException("Required conditions " + conditionElement.getTextTrim() + " are missing from detector "
- + detector + ".");
+ throw new RuntimeException("Required conditions " + conditionElement.getTextTrim() + " are missing from detector " + detector + ".");
}
}
}
@@ -1429,7 +1475,8 @@
}
}
- private ConditionsCheckDriver createConditionsCheckDriver() {
+ private ConditionsCheckDriver createConditionsCheckDriver()
+ {
ConditionsCheckDriver driver = new ConditionsCheckDriver();
Element conditionsElement = root.getChild("conditions");
if (conditionsElement == null)