/************************************************************************** * AbstractMain * * $Revision: 1.67 $ * $Date: 2004/10/25 15:27:35 $ * * Copyright (C) 2001 CERN - European Organization for Nuclear Research * All rights reserved. **************************************************************************/ 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.process.resource.BadArgumentsException; import com.c2kernel.utils.FileStringUtility; import com.c2kernel.utils.Logger; /************************************************************************** * * @author $Author: abranson $ $Date: 2004/10/25 15:27:35 $ * @version $Revision: 1.67 $ **************************************************************************/ abstract public class AbstractMain { public static boolean isServer = false; private static ShutdownHandler shutdownHandler; public static String MAIN_ARG_NONEWLOGSTREAM = "noNewLogStream"; public static String MAIN_ARG_CONFIG = "config"; public static String MAIN_ARG_LOGLEVEL = "logLevel"; public static String MAIN_ARG_LOGFILE = "logFile"; public static String MAIN_ARG_CONNECT = "connect"; /************************************************************************** * reading and setting input paramaters ************************************************************************** * * Known arguments : * * * * @param args * @return * @throws BadArgumentsException */ public static Properties readC2KArgs( String[] args ) throws BadArgumentsException { Properties c2kProps; Properties argProps = new Properties(); int logLevel = 0; PrintStream logStream = System.out; 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)) throw new BadArgumentsException("Argument "+args[i]+" given twice"); String value = ""; if (!args[i+1].startsWith("-")) value = args[++i]; argProps.put(key, value); i++; } else throw new BadArgumentsException("Bad argument: "+args[i]); } if (argProps.containsKey("logFile")) try { logStream = new PrintStream(new FileOutputStream(argProps.getProperty("logFile"), true)); } catch (FileNotFoundException e) { e.printStackTrace(); throw new BadArgumentsException("Logfile "+argProps.getProperty("logFile")+" cannot be created"); } // if the optional arg "noNewLogStream" isn't present => add a // new LogStream boolean wMustAddNewLogStream = !argProps.contains(MAIN_ARG_NONEWLOGSTREAM); if (wMustAddNewLogStream) { // Set up log stream if (argProps.containsKey("logLevel")) logLevel = Integer.parseInt(argProps.getProperty("logLevel")); Logger.addLogStream(logStream, logLevel); } if (wMustAddNewLogStream) Logger.msg( String.format("New logStream added at logLevel %d: %s", logLevel, logStream.getClass().getName())); // 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) throw new BadArgumentsException("Config file not specified"); if (!new File(configPath).exists()) throw new BadArgumentsException("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 String connectFile = c2kProps.getProperty("connect"); if (connectFile == null) throw new BadArgumentsException("Connect file not specified"); if (!new File(connectFile).exists()) throw new BadArgumentsException("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); } c2kProps.putAll(argProps); // args override connect file too Logger.msg(7, "AbstractMain::standardSetUp() - readC2KArgs() DONE."); return c2kProps; } public static void setShutdownHandler(ShutdownHandler handler) { shutdownHandler = handler; } public static void shutdown(int errCode) { if (shutdownHandler!= null) shutdownHandler.shutdown(errCode, isServer); else try { Gateway.close(); } catch (Exception ex) { Logger.error(ex); } } }