]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Merge from 0.11.mbaudier (OSGi libraries)
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
1 package org.argeo.slc.cli;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.util.Properties;
7
8 import org.apache.commons.cli.CommandLine;
9 import org.apache.commons.cli.CommandLineParser;
10 import org.apache.commons.cli.GnuParser;
11 import org.apache.commons.cli.HelpFormatter;
12 import org.apache.commons.cli.Option;
13 import org.apache.commons.cli.OptionBuilder;
14 import org.apache.commons.cli.Options;
15 import org.apache.commons.cli.ParseException;
16 import org.apache.commons.io.IOUtils;
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.argeo.slc.ant.AntConstants;
20 import org.argeo.slc.core.SlcException;
21 import org.argeo.slc.logging.Log4jUtils;
22
23 public class SlcMain {
24 public enum Mode {
25 single, agent
26 }
27
28 private static Log log = null;
29
30 private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
31
32 private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
33 .withArgName("mode").hasArg().withDescription(
34 "SLC execution mode, one of: " + listModeValues()).create(
35 'm');
36
37 private final static Option propertyOpt = OptionBuilder.withLongOpt(
38 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
39 .withValueSeparator(',').withDescription(
40 "use value for given property").create('p');
41
42 private final static Option propertiesOpt = OptionBuilder.withLongOpt(
43 "properties").withArgName("properties file").hasArgs()
44 .withValueSeparator(',').withDescription(
45 "load properties from file (-p has priority)").create('P');
46
47 private final static Option scriptOpt = OptionBuilder.withLongOpt("script")
48 .withArgName("script").hasArg().withDescription(
49 "SLC script to execute").create('s');
50
51 private final static Option targetsOpt = OptionBuilder.withLongOpt(
52 "targets").withArgName("targets").hasArg().withDescription(
53 "Targets to execute").create('t');
54
55 private final static Option runtimeOpt = OptionBuilder.withLongOpt(
56 "runtime").withArgName("runtime").hasArg().withDescription(
57 "Runtime to use, either a full path or relative to slc app conf dir: "
58 + "<conf dir>/runtime/<runtime>/.xml").create('r');
59
60 private final static Options options;
61
62 private final static String commandName = "slc";
63
64 static {
65 options = new Options();
66 options.addOption(modeOpt);
67 options.addOption(scriptOpt);
68 options.addOption(targetsOpt);
69 options.addOption(propertyOpt);
70 options.addOption(propertiesOpt);
71 options.addOption(runtimeOpt);
72 }
73
74 public static void main(String[] args) {
75 Mode mode = null;
76 Properties properties = new Properties();
77 String script = null;
78 String targets = null;
79 String runtimeStr = null;
80
81 try {
82
83 CommandLineParser clParser = new GnuParser();
84 CommandLine cl = clParser.parse(options, args);
85
86 // Mode
87 String modeStr = cl.getOptionValue(modeOpt.getOpt());
88 if (modeStr == null) {
89 mode = Mode.single;
90 } else {
91 try {
92 mode = Mode.valueOf(modeStr);
93 } catch (IllegalArgumentException e) {
94 throw new SlcException("Unrecognized mode '" + modeStr
95 + "'", e);
96 }
97 }
98
99 // Script
100 if (mode.equals(Mode.single)) {
101 if (!cl.hasOption(scriptOpt.getOpt()))
102 throw new SlcException("Mode " + Mode.single
103 + " requires option '" + scriptOpt.getLongOpt()
104 + "'");
105 script = cl.getOptionValue(scriptOpt.getOpt());
106
107 // Targets
108 if (cl.hasOption(targetsOpt.getOpt()))
109 targets = cl.getOptionValue(targetsOpt.getOpt());
110 }
111
112 // Properties
113 if (cl.hasOption(propertiesOpt.getOpt())) {
114 for (String propertyFile : cl.getOptionValues(propertiesOpt
115 .getOpt())) {
116 loadPropertyFile(properties, propertyFile);
117 }
118 }
119 if (cl.hasOption(propertyOpt.getOpt())) {
120 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
121 addProperty(properties, property);
122 }
123 }
124
125 // Runtime
126 if (cl.hasOption(runtimeOpt.getOpt())) {
127 runtimeStr = cl.getOptionValue(runtimeOpt.getOpt());
128 }
129 } catch (ParseException e) {
130 System.err.println("Problem with command line arguments. "
131 + e.getMessage());
132 badExit();
133 } catch (SlcException e) {
134 System.err.println(e.getMessage());
135 badExit();
136 } catch (Exception e) {
137 System.err.println("Unexpected exception when bootstrapping.");
138 e.printStackTrace();
139 badExit();
140 }
141
142 // Initializes logging and log arguments
143 initLogging(properties);
144 if (log.isDebugEnabled()) {
145 log.debug("Mode: " + mode);
146 if (runtimeStr != null)
147 log.debug("Runtime: " + runtimeStr);
148 log.debug("User properties: " + properties);
149 if (script != null)
150 log.debug("Script: " + script);
151 if (targets != null)
152 log.debug("Targets: " + targets);
153 }
154
155 // Execution
156 if (mode.equals(Mode.single)) {
157 try {
158 DefaultSlcRuntime runtime = new DefaultSlcRuntime();
159 runtime.executeScript(runtimeStr, script, targets, properties,
160 null, null);
161 //System.exit(0);
162 } catch (SlcException e) {
163 log.error("SLC client terminated with an error: ", e);
164 System.exit(1);
165 }
166 }
167 }
168
169 public static void printUsage() {
170 new HelpFormatter().printHelp(commandName, options, true);
171 }
172
173 private static String listModeValues() {
174 StringBuffer buf = new StringBuffer("");
175 for (Mode mode : Mode.values()) {
176 buf.append(mode).append(", ");
177 }
178 String str = buf.toString();
179 // unsafe, but there will be at least one value in the enum
180 return str.substring(0, str.length() - 2);
181 }
182
183 protected static void addProperty(Properties properties, String property) {
184 int eqIndex = property.indexOf('=');
185 if (eqIndex == 0)
186 throw new SlcException("Badly formatted property " + property);
187
188 if (eqIndex > 0) {
189 String key = property.substring(0, eqIndex);
190 String value = property.substring(eqIndex + 1);
191 properties.setProperty(key, value);
192
193 } else {
194 properties.setProperty(property, "true");
195 }
196 }
197
198 protected static void loadPropertyFile(Properties properties,
199 String propertyFile) {
200 FileInputStream in = null;
201 try {
202 in = new FileInputStream(propertyFile);
203 properties.load(in);
204 } catch (Exception e) {
205 throw new SlcException("Could not load proeprty file "
206 + propertyFile);
207 } finally {
208 IOUtils.closeQuietly(in);
209 }
210 }
211
212 private static void initLogging(Properties userProperties) {
213 System.setProperty("log4j.defaultInitOverride", "true");
214
215 // Add log4j user properties to System properties
216 for (Object obj : userProperties.keySet()) {
217 String key = obj.toString();
218 if (key.startsWith("log4j.")) {
219 System.setProperty(key, userProperties.getProperty(key));
220 }
221 }
222 Log4jUtils.initLog4j(System.getProperty("log4j.configuration",
223 "classpath:" + BOOTSTRAP_LOG4J_CONFIG));
224 log = LogFactory.getLog(SlcMain.class);
225
226 }
227
228 private static void badExit() {
229 printUsage();
230 System.exit(1);
231 }
232 }