2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org
.argeo
.slc
.cli
;
19 import java
.io
.FileInputStream
;
20 import java
.util
.Properties
;
22 import org
.apache
.commons
.cli
.CommandLine
;
23 import org
.apache
.commons
.cli
.CommandLineParser
;
24 import org
.apache
.commons
.cli
.GnuParser
;
25 import org
.apache
.commons
.cli
.HelpFormatter
;
26 import org
.apache
.commons
.cli
.Option
;
27 import org
.apache
.commons
.cli
.OptionBuilder
;
28 import org
.apache
.commons
.cli
.Options
;
29 import org
.apache
.commons
.cli
.ParseException
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.argeo
.slc
.SlcException
;
32 import org
.argeo
.slc
.server
.client
.impl
.SlcServerHttpClientImpl
;
34 @SuppressWarnings("static-access")
35 public class SlcMain
{
37 standalone
, agent
, server
40 private static Boolean debug
= true;
42 // private final static String BOOTSTRAP_LOG4J_CONFIG =
43 // "org/argeo/slc/cli/bootstrapLog4j.properties";
44 // private final static String DEFAULT_AGENT_CONTEXT =
45 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
47 private final static Option typeOpt
= OptionBuilder
.withLongOpt("mode")
48 .withArgName("mode").hasArg().withDescription(
49 "Execution type, one of: " + listTypeValues()).create('t');
51 private final static Option propertyOpt
= OptionBuilder
.withLongOpt(
52 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
53 .withValueSeparator(',').withDescription(
54 "use value for given property").create('p');
56 private final static Option propertiesOpt
= OptionBuilder
.withLongOpt(
57 "properties").withArgName("properties file").hasArgs()
58 .withValueSeparator(',').withDescription(
59 "load properties from file (-p has priority)").create('P');
61 private final static Option moduleOpt
= OptionBuilder
.withLongOpt("module")
62 .withArgName("module").hasArg().withDescription("Execution module")
65 private final static Option flowsOpt
= OptionBuilder
.withLongOpt("flows")
66 .withArgName("flows").hasArg().withDescription("Flows to execute")
69 private final static Option runtimeOpt
= OptionBuilder
.withLongOpt(
70 "runtime").withArgName("runtime").hasArg().withDescription(
71 "Runtime URL").create('r');
73 private final static Options options
;
75 private final static String commandName
= "slc";
78 options
= new Options();
79 options
.addOption(typeOpt
);
80 options
.addOption(moduleOpt
);
81 options
.addOption(flowsOpt
);
82 options
.addOption(propertyOpt
);
83 options
.addOption(propertiesOpt
);
84 options
.addOption(runtimeOpt
);
87 public static void main(String
[] args
) {
89 Properties properties
= new Properties();
96 CommandLineParser clParser
= new GnuParser();
97 CommandLine cl
= clParser
.parse(options
, args
);
100 String typeStr
= cl
.getOptionValue(typeOpt
.getOpt());
101 if (typeStr
== null) {
102 type
= Type
.standalone
;
105 type
= Type
.valueOf(typeStr
);
106 } catch (IllegalArgumentException e
) {
107 throw new SlcException("Unrecognized mode '" + typeStr
113 if (type
.equals(Type
.standalone
)) {
114 if (!cl
.hasOption(moduleOpt
.getOpt()))
115 throw new SlcException("Type " + Type
.standalone
116 + " requires option '" + moduleOpt
.getLongOpt()
118 module
= cl
.getOptionValue(moduleOpt
.getOpt());
121 if (cl
.hasOption(flowsOpt
.getOpt()))
122 flows
= cl
.getOptionValue(flowsOpt
.getOpt());
126 if (cl
.hasOption(propertiesOpt
.getOpt())) {
127 for (String propertyFile
: cl
.getOptionValues(propertiesOpt
129 loadPropertyFile(properties
, propertyFile
);
132 if (cl
.hasOption(propertyOpt
.getOpt())) {
133 for (String property
: cl
.getOptionValues(propertyOpt
.getOpt())) {
134 addProperty(properties
, property
);
139 if (cl
.hasOption(runtimeOpt
.getOpt())) {
140 urlStr
= cl
.getOptionValue(runtimeOpt
.getOpt());
142 } catch (ParseException e
) {
143 System
.err
.println("Problem with command line arguments. "
146 } catch (SlcException e
) {
147 System
.err
.println(e
.getMessage());
149 } catch (Exception e
) {
150 System
.err
.println("Unexpected exception when bootstrapping.");
156 debug("Mode: " + type
);
158 debug("Runtime: " + urlStr
);
159 debug("User properties: " + properties
);
161 debug("Module: " + module
);
163 debug("Flows: " + flows
);
167 if (type
.equals(Type
.standalone
)) {
170 else if (type
.equals(Type
.agent
)) {
173 else if (type
.equals(Type
.server
)) {
174 SlcServerHttpClientImpl slcServerHttpClient
= new SlcServerHttpClientImpl();
175 slcServerHttpClient
.setBaseUrl(urlStr
);
179 public static void printUsage() {
180 new HelpFormatter().printHelp(commandName
, options
, true);
183 private static String
listTypeValues() {
184 StringBuffer buf
= new StringBuffer("");
185 for (Type mode
: Type
.values()) {
186 buf
.append(mode
).append(", ");
188 String str
= buf
.toString();
189 // unsafe, but there will be at least one value in the enum
190 return str
.substring(0, str
.length() - 2);
193 protected static void addProperty(Properties properties
, String property
) {
194 int eqIndex
= property
.indexOf('=');
196 throw new SlcException("Badly formatted property " + property
);
199 String key
= property
.substring(0, eqIndex
);
200 String value
= property
.substring(eqIndex
+ 1);
201 properties
.setProperty(key
, value
);
204 properties
.setProperty(property
, "true");
208 protected static void loadPropertyFile(Properties properties
,
209 String propertyFile
) {
210 FileInputStream in
= null;
212 in
= new FileInputStream(propertyFile
);
214 } catch (Exception e
) {
215 throw new SlcException("Could not load proeprty file "
218 IOUtils
.closeQuietly(in
);
222 private static void badExit() {
227 protected static void info(Object msg
) {
228 System
.out
.println(msg
);
231 protected static void debug(Object msg
) {
232 System
.out
.println(msg
);