summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/process/AbstractMain.java
blob: 5267f397592bb8580293570370f8965ffcaf33b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**************************************************************************
 * 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.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 runningAsWrapper = false;

   /**************************************************************************
    *
    **************************************************************************/
    static protected void usage()
    {
        System.out.println();
        System.out.println("USAGE: com.c2kernel.process.AbstractMain \n" +
                           "    -config <server/client config file> \n" +
                           "    [-connect <LC connect file> ] (or LocalCentre in conf)\n" +
                           "    [-h] [-help] \n" +
                           "    [-logLevel 0-19] \n" +
                           "    [-logFile <path to log file>]");
        Logger.die("Initialisation error");
    }

   /**************************************************************************
    * reading and setting input paramaters
    **************************************************************************/
    public static Properties readC2KArgs( String[] args ) {
        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();
        		}
        		String value = "";
        		if (!args[i+1].startsWith("-"))
        			value = args[++i];
        		argProps.put(key, value);
        		i++;
        	}
        	else 
        		System.err.println("Bad argument: "+args[i]);
        }
                
        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;
    }

}