1   package eu.fbk.dkm.pikes.tintop;
2   
3   import org.apache.commons.cli.*;
4   
5   import java.io.FileInputStream;
6   import java.io.InputStreamReader;
7   import java.util.Properties;
8   
9   public class CommandLineWithLogger {
10  
11  	private Options options;
12  	private String logConfig;
13  	private Properties loggerProps;
14  	private String version;
15  
16  	private boolean debug;
17  	private boolean trace;
18  
19  	public CommandLineWithLogger() {
20  		logConfig = System.getProperty("log-config");
21  		if (logConfig == null) {
22  			logConfig = "log-config.txt";
23  		}
24  
25  		options = new Options();
26  		options.addOption("h", "help", false, "Print this message");
27  		options.addOption(OptionBuilder.withDescription("trace mode").withLongOpt("trace").create());
28  		options.addOption(OptionBuilder.withDescription("debug mode").withLongOpt("debug").create());
29  	}
30  
31  	public void addOption(Option option) {
32  		options.addOption(option);
33  	}
34  
35  	public String getVersion() {
36  		return version;
37  	}
38  
39  	public void setVersion(String version) {
40  		this.version = version;
41  	}
42  
43  	public boolean isDebug() {
44  		return debug;
45  	}
46  
47  	public boolean isTrace() {
48  		return trace;
49  	}
50  
51  	public Options getOptions() {
52  		return options;
53  	}
54  
55  	public void setLogConfig(String logConfig) {
56  		this.logConfig = logConfig;
57  	}
58  
59  	public void printHelp() {
60  		HelpFormatter formatter = new HelpFormatter();
61  		formatter.printHelp(150, "java <CLASS>", "\n", options, "\n", true);
62  	}
63  
64  	public Properties getLoggerProps() {
65  		return loggerProps;
66  	}
67  
68  	public CommandLine getCommandLine(String[] args) throws ParseException {
69  		CommandLineParser parser = new PosixParser();
70  		CommandLine commandLine = null;
71  
72  		if (version != null) {
73  			options.addOption(OptionBuilder.withDescription("Output version information and exit").withLongOpt("version").create());
74  		}
75  		try {
76  			commandLine = parser.parse(options, args);
77  
78  			debug = false;
79  			debug = false;
80  
81  			loggerProps = new Properties();
82  			try {
83  				loggerProps.load(new InputStreamReader(new FileInputStream(logConfig), "UTF-8"));
84  			} catch (Exception e) {
85  				loggerProps.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
86  				loggerProps.setProperty("log4j.appender.stdout.layout.ConversionPattern", "[%t] %-5p (%F:%L) - %m %n");
87  				loggerProps.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
88  				loggerProps.setProperty("log4j.appender.stdout.Encoding", "UTF-8");
89  			}
90  
91  			if (commandLine.hasOption("trace")) {
92  				loggerProps.setProperty("log4j.rootLogger", "trace,stdout");
93  				trace = true;
94  			}
95  			else if (commandLine.hasOption("debug")) {
96  				loggerProps.setProperty("log4j.rootLogger", "debug,stdout");
97  				debug = true;
98  			}
99  			else {
100 				if (loggerProps.getProperty("log4j.rootLogger") == null) {
101 					loggerProps.setProperty("log4j.rootLogger", "info,stdout");
102 				}
103 			}
104 
105 			if (commandLine.hasOption("help")) {
106 				throw new ParseException("");
107 			}
108 			if (commandLine.hasOption("version")) {
109 				throw new ParseException("Version: " + version);
110 			}
111 
112 		} catch (ParseException exp) {
113 			String message = exp.getMessage();
114 			if (message != null && message.length() > 0) {
115 				System.err.println("Parsing failed: " + message + "\n");
116 			}
117 			printHelp();
118 			throw exp;
119 		}
120 
121 		return commandLine;
122 	}
123 }