From b870cda6389d3efcdb8018eb13aaa77186e9fdfe Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Fri, 4 Apr 2014 12:53:34 +0200 Subject: AbstractMain with config file checking got lost from previous commit. Refs #177 --- .../java/com/c2kernel/process/AbstractMain.java | 70 +++++++++------------- 1 file changed, 27 insertions(+), 43 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 03807f0..b594c1a 100644 --- a/src/main/java/com/c2kernel/process/AbstractMain.java +++ b/src/main/java/com/c2kernel/process/AbstractMain.java @@ -29,54 +29,31 @@ abstract public class AbstractMain { public static boolean runningAsWrapper = false; - /************************************************************************** - * - **************************************************************************/ - static protected void usage() - { - System.out.println(); - System.out.println("USAGE: com.c2kernel.process.AbstractMain \n" + - " -config \n" + - " [-connect ] (or LocalCentre in conf)\n" + - " [-h] [-help] \n" + - " [-logLevel 0-19] \n" + - " [-logFile ]"); - Logger.die("Initialisation error"); - } /************************************************************************** * reading and setting input paramaters **************************************************************************/ - public static Properties readC2KArgs( String[] args ) { + public static Properties readC2KArgs( String[] args ) throws Exception { Properties c2kProps; Properties argProps = new Properties(); int logLevel = 0; PrintStream logStream = System.out; - 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(); - } + if (argProps.containsKey(key)) + throw new Exception("Argument "+args[i]+" given twice"); String value = ""; if (!args[i+1].startsWith("-")) value = args[++i]; argProps.put(key, value); i++; } - else { - System.err.println("Bad argument: "+args[i]); - usage(); - } - } - - if( argProps.containsKey("h") || argProps.containsKey("help")) { - usage(); + else + throw new Exception("Bad argument: "+args[i]); + } if (argProps.containsKey("logFile")) @@ -84,7 +61,7 @@ abstract public class AbstractMain logStream = new PrintStream(new FileOutputStream(argProps.getProperty("logFile"), true)); } catch (FileNotFoundException e) { e.printStackTrace(); - System.err.println("Logfile "+argProps.getProperty("logFile")+" cannot be created"); + throw new Exception("Logfile "+argProps.getProperty("logFile")+" cannot be created"); } // Set up log stream @@ -100,9 +77,11 @@ abstract public class AbstractMain } String configPath = argProps.getProperty("config"); - if (configPath == null || !new File(configPath).exists()) { - System.err.println("Config file "+configPath+" not found"); - } + if (configPath == null) + throw new Exception("Config file not specified"); + + if (!new File(configPath).exists()) + throw new Exception("Config file "+configPath+" not found"); else Logger.msg(0, "Config file: "+configPath); @@ -110,17 +89,22 @@ abstract public class AbstractMain c2kProps = FileStringUtility.loadConfigFile(argProps.getProperty("config") ); c2kProps.putAll(argProps); // args overlap config - if (c2kProps.containsKey("connect")) { - String connectFile = c2kProps.getProperty("connect"); + String connectFile = c2kProps.getProperty("connect"); + if (connectFile == null) + throw new Exception("Connect file not specified"); + + if (!new File(connectFile).exists()) + throw new Exception("Connect file "+connectFile+" not found"); + else 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); - } - } + + 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."); -- cgit v1.2.3 From a399f7cb69c94f02a7942147a9c4766a0d5152e3 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Wed, 9 Apr 2014 11:48:45 +0200 Subject: BadArgumentsException - more specific exception thrown by readC2KArgs when the arguments are wrong. --- src/main/java/com/c2kernel/process/AbstractMain.java | 17 +++++++++-------- .../process/resource/BadArgumentsException.java | 17 +++++++++++++++++ src/test/java/LauncherTest.java | 9 +++++---- 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/c2kernel/process/resource/BadArgumentsException.java (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 b594c1a..e241ee2 100644 --- a/src/main/java/com/c2kernel/process/AbstractMain.java +++ b/src/main/java/com/c2kernel/process/AbstractMain.java @@ -17,6 +17,7 @@ import java.io.PrintStream; import java.util.Enumeration; import java.util.Properties; +import com.c2kernel.process.resource.BadArgumentsException; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Logger; @@ -33,7 +34,7 @@ abstract public class AbstractMain /************************************************************************** * reading and setting input paramaters **************************************************************************/ - public static Properties readC2KArgs( String[] args ) throws Exception { + public static Properties readC2KArgs( String[] args ) throws BadArgumentsException { Properties c2kProps; Properties argProps = new Properties(); int logLevel = 0; @@ -44,7 +45,7 @@ abstract public class AbstractMain if (args[i].startsWith("-") && args[i].length()>1) { String key = args[i].substring(1); if (argProps.containsKey(key)) - throw new Exception("Argument "+args[i]+" given twice"); + throw new BadArgumentsException("Argument "+args[i]+" given twice"); String value = ""; if (!args[i+1].startsWith("-")) value = args[++i]; @@ -52,7 +53,7 @@ abstract public class AbstractMain i++; } else - throw new Exception("Bad argument: "+args[i]); + throw new BadArgumentsException("Bad argument: "+args[i]); } @@ -61,7 +62,7 @@ abstract public class AbstractMain logStream = new PrintStream(new FileOutputStream(argProps.getProperty("logFile"), true)); } catch (FileNotFoundException e) { e.printStackTrace(); - throw new Exception("Logfile "+argProps.getProperty("logFile")+" cannot be created"); + throw new BadArgumentsException("Logfile "+argProps.getProperty("logFile")+" cannot be created"); } // Set up log stream @@ -78,10 +79,10 @@ abstract public class AbstractMain String configPath = argProps.getProperty("config"); if (configPath == null) - throw new Exception("Config file not specified"); + throw new BadArgumentsException("Config file not specified"); if (!new File(configPath).exists()) - throw new Exception("Config file "+configPath+" not found"); + throw new BadArgumentsException("Config file "+configPath+" not found"); else Logger.msg(0, "Config file: "+configPath); @@ -91,10 +92,10 @@ abstract public class AbstractMain String connectFile = c2kProps.getProperty("connect"); if (connectFile == null) - throw new Exception("Connect file not specified"); + throw new BadArgumentsException("Connect file not specified"); if (!new File(connectFile).exists()) - throw new Exception("Connect file "+connectFile+" not found"); + throw new BadArgumentsException("Connect file "+connectFile+" not found"); else Logger.msg(0, "Connect file: "+connectFile); diff --git a/src/main/java/com/c2kernel/process/resource/BadArgumentsException.java b/src/main/java/com/c2kernel/process/resource/BadArgumentsException.java new file mode 100644 index 0000000..c7fe7cf --- /dev/null +++ b/src/main/java/com/c2kernel/process/resource/BadArgumentsException.java @@ -0,0 +1,17 @@ +package com.c2kernel.process.resource; + +public class BadArgumentsException extends Exception { + + public BadArgumentsException() { + super(); + } + + public BadArgumentsException(String message) { + super(message); + } + + public BadArgumentsException(Throwable cause) { + super(cause); + } + +} diff --git a/src/test/java/LauncherTest.java b/src/test/java/LauncherTest.java index 39c2b6e..8abba93 100644 --- a/src/test/java/LauncherTest.java +++ b/src/test/java/LauncherTest.java @@ -1,6 +1,7 @@ import java.util.Properties; import com.c2kernel.process.AbstractMain; +import com.c2kernel.process.resource.BadArgumentsException; import com.c2kernel.utils.Logger; @@ -40,7 +41,7 @@ public class LauncherTest { try { props = AbstractMain.readC2KArgs(args); throw new Exception("Invalid connect file not detected"); - } catch (Exception ex) { } + } catch (BadArgumentsException ex) { } } public void testWrongConnectFileName() throws Exception { @@ -49,7 +50,7 @@ public class LauncherTest { try { props = AbstractMain.readC2KArgs(args); throw new Exception("Invalid connect file not detected"); - } catch (Exception ex) { } + } catch (BadArgumentsException ex) { } } public void testMissingConnectArg() throws Exception { @@ -59,7 +60,7 @@ public class LauncherTest { try { props = AbstractMain.readC2KArgs(args); throw new Exception("Missing connect file not detected"); - } catch (Exception ex) { } + } catch (BadArgumentsException ex) { } } public void testMissingConfigArg() throws Exception { @@ -69,6 +70,6 @@ public class LauncherTest { try { props = AbstractMain.readC2KArgs(args); throw new Exception("Missing config file not detected"); - } catch (Exception ex) { } + } catch (BadArgumentsException ex) { } } } -- cgit v1.2.3