]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
Restructure HTTP service client
[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.argeo.slc.SlcException;
16 import org.argeo.slc.server.client.SlcServerHttpClient;
17 import org.argeo.slc.server.client.impl.SlcServerHttpClientImpl;
18
19 public class SlcMain {
20 public enum Type {
21 standalone, agent, server
22 }
23
24 private static Boolean debug = true;
25
26 private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
27 private final static String DEFAULT_AGENT_CONTEXT = "classpath:org/argeo/slc/cli/spring-agent-default.xml";
28
29 private final static Option typeOpt = OptionBuilder.withLongOpt("mode")
30 .withArgName("mode").hasArg().withDescription(
31 "Execution type, one of: " + listTypeValues()).create('t');
32
33 private final static Option propertyOpt = OptionBuilder.withLongOpt(
34 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
35 .withValueSeparator(',').withDescription(
36 "use value for given property").create('p');
37
38 private final static Option propertiesOpt = OptionBuilder.withLongOpt(
39 "properties").withArgName("properties file").hasArgs()
40 .withValueSeparator(',').withDescription(
41 "load properties from file (-p has priority)").create('P');
42
43 private final static Option moduleOpt = OptionBuilder.withLongOpt("module")
44 .withArgName("module").hasArg().withDescription("Execution module")
45 .create('m');
46
47 private final static Option flowsOpt = OptionBuilder.withLongOpt("flows")
48 .withArgName("flows").hasArg().withDescription("Flows to execute")
49 .create('f');
50
51 private final static Option runtimeOpt = OptionBuilder.withLongOpt(
52 "runtime").withArgName("runtime").hasArg().withDescription(
53 "Runtime URL").create('r');
54
55 private final static Options options;
56
57 private final static String commandName = "slc";
58
59 static {
60 options = new Options();
61 options.addOption(typeOpt);
62 options.addOption(moduleOpt);
63 options.addOption(flowsOpt);
64 options.addOption(propertyOpt);
65 options.addOption(propertiesOpt);
66 options.addOption(runtimeOpt);
67 }
68
69 public static void main(String[] args) {
70 Type type = null;
71 Properties properties = new Properties();
72 String module = null;
73 String flows = null;
74 String urlStr = null;
75
76 try {
77
78 CommandLineParser clParser = new GnuParser();
79 CommandLine cl = clParser.parse(options, args);
80
81 // Mode
82 String typeStr = cl.getOptionValue(typeOpt.getOpt());
83 if (typeStr == null) {
84 type = Type.standalone;
85 } else {
86 try {
87 type = Type.valueOf(typeStr);
88 } catch (IllegalArgumentException e) {
89 throw new SlcException("Unrecognized mode '" + typeStr
90 + "'", e);
91 }
92 }
93
94 // Script
95 if (type.equals(Type.standalone)) {
96 if (!cl.hasOption(moduleOpt.getOpt()))
97 throw new SlcException("Type " + Type.standalone
98 + " requires option '" + moduleOpt.getLongOpt()
99 + "'");
100 module = cl.getOptionValue(moduleOpt.getOpt());
101
102 // Targets
103 if (cl.hasOption(flowsOpt.getOpt()))
104 flows = cl.getOptionValue(flowsOpt.getOpt());
105 }
106
107 // Properties
108 if (cl.hasOption(propertiesOpt.getOpt())) {
109 for (String propertyFile : cl.getOptionValues(propertiesOpt
110 .getOpt())) {
111 loadPropertyFile(properties, propertyFile);
112 }
113 }
114 if (cl.hasOption(propertyOpt.getOpt())) {
115 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
116 addProperty(properties, property);
117 }
118 }
119
120 // Runtime
121 if (cl.hasOption(runtimeOpt.getOpt())) {
122 urlStr = cl.getOptionValue(runtimeOpt.getOpt());
123 }
124 } catch (ParseException e) {
125 System.err.println("Problem with command line arguments. "
126 + e.getMessage());
127 badExit();
128 } catch (SlcException e) {
129 System.err.println(e.getMessage());
130 badExit();
131 } catch (Exception e) {
132 System.err.println("Unexpected exception when bootstrapping.");
133 e.printStackTrace();
134 badExit();
135 }
136
137 if (debug) {
138 debug("Mode: " + type);
139 if (urlStr != null)
140 debug("Runtime: " + urlStr);
141 debug("User properties: " + properties);
142 if (module != null)
143 debug("Module: " + module);
144 if (flows != null)
145 debug("Flows: " + flows);
146 }
147
148 // Standalone
149 if (type.equals(Type.standalone)) {
150 }
151 // Agent
152 else if (type.equals(Type.agent)) {
153 }
154 // Server
155 else if (type.equals(Type.server)) {
156 SlcServerHttpClientImpl slcServerHttpClient = new SlcServerHttpClientImpl();
157 slcServerHttpClient.setBaseUrl(urlStr);
158 }
159 }
160
161 public static void printUsage() {
162 new HelpFormatter().printHelp(commandName, options, true);
163 }
164
165 private static String listTypeValues() {
166 StringBuffer buf = new StringBuffer("");
167 for (Type mode : Type.values()) {
168 buf.append(mode).append(", ");
169 }
170 String str = buf.toString();
171 // unsafe, but there will be at least one value in the enum
172 return str.substring(0, str.length() - 2);
173 }
174
175 protected static void addProperty(Properties properties, String property) {
176 int eqIndex = property.indexOf('=');
177 if (eqIndex == 0)
178 throw new SlcException("Badly formatted property " + property);
179
180 if (eqIndex > 0) {
181 String key = property.substring(0, eqIndex);
182 String value = property.substring(eqIndex + 1);
183 properties.setProperty(key, value);
184
185 } else {
186 properties.setProperty(property, "true");
187 }
188 }
189
190 protected static void loadPropertyFile(Properties properties,
191 String propertyFile) {
192 FileInputStream in = null;
193 try {
194 in = new FileInputStream(propertyFile);
195 properties.load(in);
196 } catch (Exception e) {
197 throw new SlcException("Could not load proeprty file "
198 + propertyFile);
199 } finally {
200 IOUtils.closeQuietly(in);
201 }
202 }
203
204 private static void badExit() {
205 printUsage();
206 System.exit(1);
207 }
208
209 protected static void info(Object msg) {
210 System.out.println(msg);
211 }
212
213 protected static void debug(Object msg) {
214 System.out.println(msg);
215 }
216 }