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
;
19 import java
.io
.FileInputStream
;
20 import java
.util
.List
;
21 import java
.util
.Properties
;
23 import org
.apache
.commons
.cli
.CommandLine
;
24 import org
.apache
.commons
.cli
.CommandLineParser
;
25 import org
.apache
.commons
.cli
.GnuParser
;
26 import org
.apache
.commons
.cli
.HelpFormatter
;
27 import org
.apache
.commons
.cli
.Option
;
28 import org
.apache
.commons
.cli
.OptionBuilder
;
29 import org
.apache
.commons
.cli
.Options
;
30 import org
.apache
.commons
.cli
.ParseException
;
31 import org
.apache
.commons
.io
.IOUtils
;
32 import org
.argeo
.osgi
.boot
.OsgiBoot
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
35 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
36 import org
.osgi
.framework
.Bundle
;
37 import org
.osgi
.framework
.BundleContext
;
38 import org
.osgi
.framework
.ServiceReference
;
40 @SuppressWarnings("static-access")
41 public class SlcMain
{
43 standalone
, agent
, server
46 private static Boolean debug
= true;
48 // private final static String BOOTSTRAP_LOG4J_CONFIG =
49 // "org/argeo/slc/cli/bootstrapLog4j.properties";
50 // private final static String DEFAULT_AGENT_CONTEXT =
51 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
53 private final static Option typeOpt
= OptionBuilder
.withLongOpt("mode")
54 .withArgName("mode").hasArg()
55 .withDescription("Execution type, one of: " + listTypeValues())
58 private final static Option propertyOpt
= OptionBuilder
59 .withLongOpt("property").withArgName("prop1=val1,prop2=val2")
60 .hasArgs().withValueSeparator(',')
61 .withDescription("use value for given property").create('p');
63 private final static Option propertiesOpt
= OptionBuilder
64 .withLongOpt("properties").withArgName("properties file").hasArgs()
65 .withValueSeparator(',')
66 .withDescription("load properties from file (-p has priority)")
69 private final static Option moduleOpt
= OptionBuilder
.withLongOpt("module")
70 .withArgName("module").hasArg().withDescription("Execution module")
73 private final static Option flowsOpt
= OptionBuilder
.withLongOpt("flows")
74 .withArgName("flows").hasArg().withDescription("Flows to execute")
77 private final static Option runtimeOpt
= OptionBuilder
78 .withLongOpt("runtime").withArgName("runtime").hasArg()
79 .withDescription("Runtime URL").create('r');
81 private final static Options options
;
83 private final static String commandName
= "slc";
85 private static String bundlesToInstall
= "/usr/share/osgi;in=*.jar";
87 private static String bundlesToStart
= "org.springframework.osgi.extender,"
88 + "org.argeo.node.repofactory.jackrabbit,"
89 + "org.argeo.node.repo.jackrabbit," + "org.argeo.security.dao.os,"
90 + "org.argeo.slc.node.jackrabbit," + "org.argeo.slc.agent,"
91 + "org.argeo.slc.agent.jcr";
94 options
= new Options();
95 // options.addOption(typeOpt);
96 // options.addOption(moduleOpt);
97 // options.addOption(flowsOpt);
98 // options.addOption(propertyOpt);
99 // options.addOption(propertiesOpt);
100 // options.addOption(runtimeOpt);
103 public static void main(String
[] args
) {
105 // Properties properties = new Properties();
106 // String flows = null;
107 // String urlStr = null;
109 String module
= null;
110 String moduleUrl
= null;
115 CommandLineParser clParser
= new GnuParser();
116 CommandLine cl
= clParser
.parse(options
, args
);
118 List
<String
> arguments
= cl
.getArgList();
119 if (arguments
.size() == 0) {
120 // TODO default behaviour
122 module
= arguments
.get(0);
123 File moduleFile
= new File(module
);
124 if (moduleFile
.exists()) {
125 if (moduleFile
.isDirectory()) {
126 moduleUrl
= "reference:file:"
127 + moduleFile
.getCanonicalPath();
129 moduleUrl
= "file:" + moduleFile
.getCanonicalPath();
133 if (arguments
.size() == 1) {
136 flow
= arguments
.get(1);
140 // System.setProperty(
141 // ExecutionModulesManager.UNIQUE_LAUNCH_MODULE_PROPERTY,
143 // System.setProperty(
144 // ExecutionModulesManager.UNIQUE_LAUNCH_FLOW_PROPERTY, flow);
146 String executionDir
= System
.getProperty("user.dir");
147 File slcDir
= new File(executionDir
, ".slc");
148 File dataDir
= new File(slcDir
, "data");
149 if (!dataDir
.exists())
151 File confDir
= new File(slcDir
, "conf");
152 if (!confDir
.exists())
155 BundleContext bundleContext
= null;
157 String
[] osgiRuntimeArgs
= { "-configuration",
158 confDir
.getCanonicalPath(), "-data",
159 dataDir
.getCanonicalPath(), "-console", "-clean" };
160 bundleContext
= EclipseStarter
.startup(osgiRuntimeArgs
, null);
161 } catch (Exception e
) {
162 throw new RuntimeException("Cannot start Equinox.", e
);
166 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
167 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(bundlesToInstall
));
168 osgiBoot
.startBundles(bundlesToStart
);
170 if (moduleUrl
!= null) {
171 Bundle bundle
= osgiBoot
.installUrl(moduleUrl
);
172 module
= bundle
.getSymbolicName();
173 // TODO deal with version
176 // retrieve modulesManager
177 ServiceReference sr
= bundleContext
178 .getServiceReference(ExecutionModulesManager
.class
180 ExecutionModulesManager modulesManager
= (ExecutionModulesManager
) bundleContext
184 modulesManager
.execute(null);
186 // osgiBoot.bootstrap();
187 // osgiBoot.bootstrap();
190 // String typeStr = cl.getOptionValue(typeOpt.getOpt());
191 // if (typeStr == null) {
192 // type = Type.standalone;
195 // type = Type.valueOf(typeStr);
196 // } catch (IllegalArgumentException e) {
197 // throw new SlcException("Unrecognized mode '" + typeStr
203 // if (type.equals(Type.standalone)) {
204 // if (!cl.hasOption(moduleOpt.getOpt()))
205 // throw new SlcException("Type " + Type.standalone
206 // + " requires option '" + moduleOpt.getLongOpt()
208 // module = cl.getOptionValue(moduleOpt.getOpt());
211 // if (cl.hasOption(flowsOpt.getOpt()))
212 // flows = cl.getOptionValue(flowsOpt.getOpt());
216 // if (cl.hasOption(propertiesOpt.getOpt())) {
217 // for (String propertyFile : cl.getOptionValues(propertiesOpt
219 // loadPropertyFile(properties, propertyFile);
222 // if (cl.hasOption(propertyOpt.getOpt())) {
223 // for (String property : cl.getOptionValues(propertyOpt.getOpt()))
225 // addProperty(properties, property);
230 // if (cl.hasOption(runtimeOpt.getOpt())) {
231 // urlStr = cl.getOptionValue(runtimeOpt.getOpt());
233 } catch (ParseException e
) {
234 System
.err
.println("Problem with command line arguments. "
237 } catch (SlcException e
) {
238 System
.err
.println(e
.getMessage());
240 } catch (Exception e
) {
241 System
.err
.println("Unexpected exception when bootstrapping.");
247 // debug("Mode: " + type);
248 // if (urlStr != null)
249 // debug("Runtime: " + urlStr);
250 // debug("User properties: " + properties);
251 // if (module != null)
252 // debug("Module: " + module);
253 // if (flows != null)
254 // debug("Flows: " + flows);
258 // if (type.equals(Type.standalone)) {
261 // else if (type.equals(Type.agent)) {
265 public static void printUsage() {
266 new HelpFormatter().printHelp(commandName
, options
, true);
269 private static String
listTypeValues() {
270 StringBuffer buf
= new StringBuffer("");
271 for (Type mode
: Type
.values()) {
272 buf
.append(mode
).append(", ");
274 String str
= buf
.toString();
275 // unsafe, but there will be at least one value in the enum
276 return str
.substring(0, str
.length() - 2);
279 protected static void addProperty(Properties properties
, String property
) {
280 int eqIndex
= property
.indexOf('=');
282 throw new SlcException("Badly formatted property " + property
);
285 String key
= property
.substring(0, eqIndex
);
286 String value
= property
.substring(eqIndex
+ 1);
287 properties
.setProperty(key
, value
);
290 properties
.setProperty(property
, "true");
294 protected static void loadPropertyFile(Properties properties
,
295 String propertyFile
) {
296 FileInputStream in
= null;
298 in
= new FileInputStream(propertyFile
);
300 } catch (Exception e
) {
301 throw new SlcException("Could not load proeprty file "
304 IOUtils
.closeQuietly(in
);
308 private static void badExit() {
313 protected static void info(Object msg
) {
314 System
.out
.println(msg
);
317 protected static void debug(Object msg
) {
318 System
.out
.println(msg
);