2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.cli
;
18 import java
.io
.FileInputStream
;
19 import java
.util
.Properties
;
21 import org
.apache
.commons
.cli
.CommandLine
;
22 import org
.apache
.commons
.cli
.CommandLineParser
;
23 import org
.apache
.commons
.cli
.GnuParser
;
24 import org
.apache
.commons
.cli
.HelpFormatter
;
25 import org
.apache
.commons
.cli
.Option
;
26 import org
.apache
.commons
.cli
.OptionBuilder
;
27 import org
.apache
.commons
.cli
.Options
;
28 import org
.apache
.commons
.cli
.ParseException
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.argeo
.slc
.SlcException
;
32 @SuppressWarnings("static-access")
33 public class SlcMain
{
35 standalone
, agent
, server
38 private static Boolean debug
= true;
40 // private final static String BOOTSTRAP_LOG4J_CONFIG =
41 // "org/argeo/slc/cli/bootstrapLog4j.properties";
42 // private final static String DEFAULT_AGENT_CONTEXT =
43 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
45 private final static Option typeOpt
= OptionBuilder
.withLongOpt("mode")
46 .withArgName("mode").hasArg()
47 .withDescription("Execution type, one of: " + listTypeValues())
50 private final static Option propertyOpt
= OptionBuilder
51 .withLongOpt("property").withArgName("prop1=val1,prop2=val2")
52 .hasArgs().withValueSeparator(',')
53 .withDescription("use value for given property").create('p');
55 private final static Option propertiesOpt
= OptionBuilder
56 .withLongOpt("properties").withArgName("properties file").hasArgs()
57 .withValueSeparator(',')
58 .withDescription("load properties from file (-p has priority)")
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
70 .withLongOpt("runtime").withArgName("runtime").hasArg()
71 .withDescription("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
)) {
174 public static void printUsage() {
175 new HelpFormatter().printHelp(commandName
, options
, true);
178 private static String
listTypeValues() {
179 StringBuffer buf
= new StringBuffer("");
180 for (Type mode
: Type
.values()) {
181 buf
.append(mode
).append(", ");
183 String str
= buf
.toString();
184 // unsafe, but there will be at least one value in the enum
185 return str
.substring(0, str
.length() - 2);
188 protected static void addProperty(Properties properties
, String property
) {
189 int eqIndex
= property
.indexOf('=');
191 throw new SlcException("Badly formatted property " + property
);
194 String key
= property
.substring(0, eqIndex
);
195 String value
= property
.substring(eqIndex
+ 1);
196 properties
.setProperty(key
, value
);
199 properties
.setProperty(property
, "true");
203 protected static void loadPropertyFile(Properties properties
,
204 String propertyFile
) {
205 FileInputStream in
= null;
207 in
= new FileInputStream(propertyFile
);
209 } catch (Exception e
) {
210 throw new SlcException("Could not load proeprty file "
213 IOUtils
.closeQuietly(in
);
217 private static void badExit() {
222 protected static void info(Object msg
) {
223 System
.out
.println(msg
);
226 protected static void debug(Object msg
) {
227 System
.out
.println(msg
);