From 4168117e84eaa719006b2fc448a8060e2d6a3045 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Sat, 22 Jun 2013 13:42:05 +0200 Subject: Rewrote cmdline parser to allow cristal processes to use their own. Also cmdline params may override config or connect params. --- .../java/com/c2kernel/process/AbstractMain.java | 177 +++++++++------------ 1 file changed, 72 insertions(+), 105 deletions(-) (limited to 'src/main/java/com/c2kernel/process/AbstractMain.java') diff --git a/src/main/java/com/c2kernel/process/AbstractMain.java b/src/main/java/com/c2kernel/process/AbstractMain.java index a5c4fe8..5267f39 100644 --- a/src/main/java/com/c2kernel/process/AbstractMain.java +++ b/src/main/java/com/c2kernel/process/AbstractMain.java @@ -11,8 +11,11 @@ package com.c2kernel.process; import java.io.File; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; +import java.util.Enumeration; +import java.util.Properties; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Logger; @@ -42,118 +45,82 @@ abstract public class AbstractMain } /************************************************************************** - * reading and setting the standard c2k input paramaters + * reading and setting input paramaters **************************************************************************/ - static public java.util.Properties readC2KArgs( String[] args ) - { - int i = 0; - String configPath = null; - String connectPath = null; - java.util.Properties c2kProps = null; + public static Properties readC2KArgs( String[] args ) { + Properties c2kProps; + Properties argProps = new Properties(); int logLevel = 0; PrintStream logStream = System.out; - String centreId = null; - - try - { - if( args != null ) - { - while( i < args.length ) - { - if( args[i].equals("-h") || args[i].equals("-help") ) - { - usage(); - } - else if(args[i].equals("-config")) - { - if( (i+1) >= args.length ) - { - System.out.println("AbstractMain::readC2KArgs() - argument expected " + - "for -config"); - usage(); - } - System.out.println("Config file: "+args[i+1]); - configPath = args[++i]; - - } - else if(args[i].equals("-connect")) - { - if( (i+1) < args.length ) // batch file will have no arg if no cmd line arg - { - connectPath = args[++i]; - } - } - else if(args[i].equals("-logLevel")) - { - if( (i+1) >= args.length ) - { - System.out.println("AbstractMain::readC2KArgs() - argument expected " + - "for -logLevel"); - usage(); - } - logLevel = Integer.parseInt(args[++i]); - } - else if(args[i].equals("-logFile")) - { - if( (i+1) >= args.length ) - { - System.out.println("AbstractMain::readC2KArgs() - argument expected " + - "for -logFile"); - usage(); - } - logStream = new PrintStream(new FileOutputStream(args[++i], true)); - } - i++; - } - - // Set up log stream - Logger.addLogStream(logStream, logLevel); - - if (configPath == null) { - System.out.println("No config file specified"); - usage(); - } - - // Load config & connect files into c2kprops - c2kProps = FileStringUtility.loadConfigFile( configPath ); - if (connectPath == null) { - // see if LC is listed in the config - Logger.msg(6, "No connect file specified in arguments. Looking in config."); - connectPath = c2kProps.getProperty("CLCPath"); - if (connectPath == null) { - centreId = c2kProps.getProperty("LocalCentre"); - if (centreId!= null) connectPath = "connect/"+centreId+".clc"; - } - } - - if (connectPath != null) { - Logger.msg(6, "Connect file: "+connectPath); - if (centreId == null) { - String connectFileName = new File(connectPath).getName(); - centreId = connectFileName.substring(0, connectFileName.lastIndexOf(".clc")); - c2kProps.setProperty("LocalCentre", centreId); - } - FileStringUtility.appendConfigFile( c2kProps, connectPath); - } - else { - System.out.println("No connect file specified in args nor config file. Cannot continue."); - usage(); - } - } - else - { - System.out.println("AbstractMain::readC2KArgs() - no arguments!"); - usage(); - } + if (args == null) return argProps; + + int i = 0; + while( i < args.length ) { + if (args[i].startsWith("-") && args[i].length()>1) { + String key = args[i].substring(1); + if (argProps.containsKey(key)) { + System.err.println("Argument "+args[i]+" given twice"); + usage(); + } + String value = ""; + if (!args[i+1].startsWith("-")) + value = args[++i]; + argProps.put(key, value); + i++; + } + else + System.err.println("Bad argument: "+args[i]); } - catch( Exception ex ) - { - System.out.println("Main::readC2KArgs() - bad arguments! "); - ex.printStackTrace(); - usage(); + + if( argProps.containsKey("h") || argProps.containsKey("help")) { + usage(); } + + if (argProps.containsKey("logFile")) + try { + logStream = new PrintStream(new FileOutputStream(argProps.getProperty("logFile"), true)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + System.err.println("Logfile "+argProps.getProperty("logFile")+" cannot be created"); + } + + // Set up log stream + if (argProps.containsKey("logLevel")) + logLevel = Integer.parseInt(argProps.getProperty("logLevel")); + Logger.addLogStream(logStream, logLevel); + + // Dump params if log high enough + + if (Logger.doLog(3)) for (Enumeration e = argProps.propertyNames(); e.hasMoreElements();) { + String next = (String)e.nextElement(); + System.out.println("AbstractMain: Param "+next+": "+argProps.getProperty(next)); + } + + String configPath = argProps.getProperty("config"); + if (configPath == null || !new File(configPath).exists()) { + System.err.println("Config file "+configPath+" not found"); + } + else + Logger.msg(0, "Config file: "+configPath); + + // Load config & connect files into c2kprops + c2kProps = FileStringUtility.loadConfigFile(argProps.getProperty("config") ); + c2kProps.putAll(argProps); // args overlap config + + if (c2kProps.containsKey("connect")) { + String connectFile = c2kProps.getProperty("connect"); + Logger.msg(0, "Connect file: "+connectFile); + FileStringUtility.appendConfigFile( c2kProps, connectFile); + + if (!c2kProps.containsKey("LocalCentre")) { + String connectFileName = new File(connectFile).getName(); + String centreId = connectFileName.substring(0, connectFileName.lastIndexOf(".clc")); + c2kProps.setProperty("LocalCentre", centreId); + } + } + c2kProps.putAll(argProps); // args override connect file too Logger.msg(7, "AbstractMain::standardSetUp() - readC2KArgs() DONE."); return c2kProps; -- cgit v1.2.3