Author: [log in to unmask] Date: Sat Mar 7 14:20:57 2015 New Revision: 2348 Log: Improvements to conditions initialization in command line tool. Detector name should no longer be required but can still be supplied. A default run number of 0 will be used if none is given. Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java Modified: java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java ============================================================================= --- java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java (original) +++ java/trunk/conditions/src/main/java/org/hps/conditions/cli/CommandLineTool.java Sat Mar 7 14:20:57 2015 @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; import java.util.logging.Level; +import java.util.logging.Logger; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; @@ -13,6 +14,8 @@ import org.apache.commons.cli.PosixParser; import org.hps.conditions.database.DatabaseConditionsManager; import org.lcsim.conditions.ConditionsManager.ConditionsNotFoundException; +import org.lcsim.util.log.DefaultLogFormatter; +import org.lcsim.util.log.LogUtil; /** * <p> @@ -24,8 +27,9 @@ * * @author Jeremy McCormick <[log in to unmask]> */ -// TODO: Add detector name and run number as arguments on the basic tool rather than sub-commands. public class CommandLineTool { + + static Logger logger = LogUtil.create(CommandLineTool.class.getSimpleName(), new DefaultLogFormatter(), Level.WARNING); Options options = new Options(); Map<String, AbstractCommand> commands = new HashMap<String, AbstractCommand>(); @@ -43,12 +47,12 @@ printUsage(); exit(0); } - + CommandLine commandLine = null; try { commandLine = parser.parse(options, arguments, true); } catch (ParseException e) { - e.printStackTrace(); + logger.log(Level.SEVERE, "error parsing the options", e); printUsage(); exit(1); } @@ -58,22 +62,29 @@ exit(0); } + // Set verbosity. if (commandLine.hasOption("v")) { + logger.setLevel(Level.ALL); + logger.getHandlers()[0].setLevel(Level.ALL); verbose = true; + logger.config("verbose mode enabled"); } + // Setup conditions manager from command line options. setupConditionsManager(commandLine); + // Get the sub-command to use. String commandName = commandLine.getArgs()[0]; - AbstractCommand command = commands.get(commandName); if (command == null) { throw new IllegalArgumentException("Unknown command " + commandName); } + // Copy remaining arguments for sub-command. String[] commandArguments = new String[commandLine.getArgs().length - 1]; System.arraycopy(commandLine.getArgs(), 1, commandArguments, 0, commandArguments.length); + // Excecute the sub-command. command.setVerbose(verbose); command.execute(commandArguments); } catch (Exception e) { @@ -84,38 +95,64 @@ } } - private void setupConditionsManager(CommandLine commandLine) { + void setupConditionsManager(CommandLine commandLine) { + + logger.info("setting up conditions manager"); + + // Create new manager. conditionsManager = new DatabaseConditionsManager(); - if (verbose) { - conditionsManager.setLogLevel(Level.ALL); - } else { - conditionsManager.setLogLevel(Level.WARNING); - } + + // Set log level. + conditionsManager.setLogLevel(logger.getLevel()); + + // Connection properties. if (commandLine.hasOption("p")) { File connectionPropertiesFile = new File(commandLine.getOptionValue("p")); - if (verbose) - System.out.println("using connection properties file " + connectionPropertiesFile.getPath()); conditionsManager.setConnectionProperties(connectionPropertiesFile); + logger.config("connection properties -p " + connectionPropertiesFile); } + + // XML config. if (commandLine.hasOption("x")) { File xmlConfigFile = new File(commandLine.getOptionValue("x")); conditionsManager.setXmlConfig(xmlConfigFile); - if (verbose) - System.out.println("using XML config file " + xmlConfigFile.getPath()); - } + logger.config("XML config -x " + xmlConfigFile); + } - String detectorName = DatabaseConditionsManager.getDefaultEngRunDetectorName(); - if (commandLine.hasOption("d")) { - detectorName = commandLine.getOptionValue("d"); - } - int runNumber = 2000; - if (commandLine.hasOption("r")) { - runNumber = Integer.parseInt(commandLine.getOptionValue("r")); - } - try { - DatabaseConditionsManager.getInstance().setDetector(detectorName, runNumber); - } catch (ConditionsNotFoundException e) { - throw new RuntimeException(e); + // If there is a run number or detector number then attempt to initialize the conditions system. + if (commandLine.hasOption("r") || commandLine.hasOption("d")) { + + logger.config("detector name or run number supplied so manager will be initialized"); + + // Set detector name. + String detectorName = null; + if (commandLine.hasOption("d")) { + detectorName = commandLine.getOptionValue("d"); + logger.config("detector -d " + detectorName); + } else { + detectorName = "HPS-ECalCommissioning-v2"; + logger.config("default detector " + detectorName + " is being used"); + } + + // Get run number. + Integer run = null; + if (commandLine.hasOption("r")) { + run = Integer.parseInt(commandLine.getOptionValue("r")); + logger.config("run -r " + run); + } else { + run = 0; + logger.config("default run number " + run + " is being used"); + } + + // Setup the conditions manager with user detector name and run number. + try { + logger.config("initializing conditions manager with detector " + detectorName + " and run " + run); + DatabaseConditionsManager.getInstance().setDetector(detectorName, run); + logger.config("conditions manager initialized successfully"); + logger.getHandlers()[0].flush(); + } catch (ConditionsNotFoundException e) { + throw new RuntimeException(e); + } } } @@ -142,15 +179,14 @@ static CommandLineTool create() { CommandLineTool cli = new CommandLineTool(); cli.options.addOption(new Option("h", false, "Print help and exit")); - cli.options.addOption(new Option("v", false, "Enable verbose terminal output")); - cli.options.addOption(new Option("p", true, "Set the connection properties file")); - cli.options.addOption(new Option("x", true, "Set the conditions database XML configuration file")); cli.options.addOption(new Option("d", true, "Set the detector name")); cli.options.addOption(new Option("r", true, "Set the run number")); + cli.options.addOption(new Option("v", false, "Enable verbose print output")); + cli.options.addOption(new Option("p", true, "Set the database connection properties file")); + cli.options.addOption(new Option("x", true, "Set the conditions XML configuration file")); cli.registerCommand(new LoadCommand()); cli.registerCommand(new PrintCommand()); cli.registerCommand(new AddCommand()); return cli; } - -} +}