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