2 * Copyright (C) 2007-2012 Argeo GmbH
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
.Options
;
28 import org
.apache
.commons
.cli
.ParseException
;
29 import org
.apache
.commons
.io
.IOUtils
;
30 import org
.argeo
.osgi
.boot
.OsgiBoot
;
31 import org
.argeo
.slc
.SlcException
;
32 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
33 import org
.osgi
.framework
.Bundle
;
34 import org
.osgi
.framework
.BundleContext
;
36 @SuppressWarnings("restriction")
37 public class SlcMain
{
38 /** Unique launch module */
39 public static String UNIQUE_LAUNCH_MODULE_PROPERTY
= "slc.launch.module";
41 /** Unique launch flow */
42 public static String UNIQUE_LAUNCH_FLOW_PROPERTY
= "slc.launch.flow";
45 // standalone, agent, server
48 // private static Boolean debug = true;
50 // private final static String BOOTSTRAP_LOG4J_CONFIG =
51 // "org/argeo/slc/cli/bootstrapLog4j.properties";
52 // private final static String DEFAULT_AGENT_CONTEXT =
53 // "classpath:org/argeo/slc/cli/spring-agent-default.xml";
55 // private final static Option typeOpt = OptionBuilder.withLongOpt("mode")
56 // .withArgName("mode").hasArg()
57 // .withDescription("Execution type, one of: " + listTypeValues())
60 // private final static Option propertyOpt = OptionBuilder
61 // .withLongOpt("property").withArgName("prop1=val1,prop2=val2")
62 // .hasArgs().withValueSeparator(',')
63 // .withDescription("use value for given property").create('p');
65 // private final static Option propertiesOpt = OptionBuilder
66 // .withLongOpt("properties").withArgName("properties file").hasArgs()
67 // .withValueSeparator(',')
68 // .withDescription("load properties from file (-p has priority)")
71 // private final static Option moduleOpt =
72 // OptionBuilder.withLongOpt("module")
73 // .withArgName("module").hasArg().withDescription("Execution module")
76 // private final static Option flowsOpt = OptionBuilder.withLongOpt("flows")
77 // .withArgName("flows").hasArg().withDescription("Flows to execute")
80 // private final static Option runtimeOpt = OptionBuilder
81 // .withLongOpt("runtime").withArgName("runtime").hasArg()
82 // .withDescription("Runtime URL").create('r');
84 private final static Options options
;
86 private final static String commandName
= "slc";
88 // private static String bundlesToInstall = "/usr/share/osgi;in=*.jar";
89 private static String bundlesToInstall
= System
.getProperty("user.home")
90 + "/dev/src/slc/runtime/org.argeo.slc.launcher/target/dependency;in=*.jar";
92 // private static String bundlesToStart =
93 // "org.springframework.osgi.extender,"
94 // + "org.argeo.node.repofactory.jackrabbit,"
95 // + "org.argeo.node.repo.jackrabbit," + "org.argeo.security.dao.os,"
96 // + "org.argeo.slc.node.jackrabbit," + "org.argeo.slc.agent,"
97 // + "org.argeo.slc.agent.jcr";
98 private static String bundlesToStart
= "org.springframework.osgi.extender,"
99 + "org.argeo.slc.agent";
102 options
= new Options();
103 // options.addOption(typeOpt);
104 // options.addOption(moduleOpt);
105 // options.addOption(flowsOpt);
106 // options.addOption(propertyOpt);
107 // options.addOption(propertiesOpt);
108 // options.addOption(runtimeOpt);
111 @SuppressWarnings({ "unchecked" })
112 public static void main(String
[] args
) {
114 // Properties properties = new Properties();
115 // String flows = null;
116 // String urlStr = null;
118 String module
= null;
119 String moduleUrl
= null;
124 CommandLineParser clParser
= new GnuParser();
125 CommandLine cl
= clParser
.parse(options
, args
);
127 List
<String
> arguments
= cl
.getArgList();
128 if (arguments
.size() == 0) {
129 // TODO default behaviour
131 module
= arguments
.get(0);
132 File moduleFile
= new File(module
);
133 if (moduleFile
.exists()) {
134 if (moduleFile
.isDirectory()) {
135 moduleUrl
= "reference:file:"
136 + moduleFile
.getCanonicalPath();
138 moduleUrl
= "file:" + moduleFile
.getCanonicalPath();
142 if (arguments
.size() == 1) {
145 flow
= arguments
.get(1);
149 String executionDir
= System
.getProperty("user.dir");
150 File slcDir
= new File(executionDir
, "target/.slc");
151 File dataDir
= new File(slcDir
, "data");
152 if (!dataDir
.exists())
154 File confDir
= new File(slcDir
, "conf");
155 if (!confDir
.exists())
158 BundleContext bundleContext
= null;
160 String
[] osgiRuntimeArgs
= { "-configuration",
161 confDir
.getCanonicalPath(), "-data",
162 dataDir
.getCanonicalPath(), "-console", "-clean" };
163 bundleContext
= EclipseStarter
.startup(osgiRuntimeArgs
, null);
164 } catch (Exception e
) {
165 throw new RuntimeException("Cannot start Equinox.", e
);
169 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
170 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(bundlesToInstall
));
172 if (moduleUrl
!= null) {
173 Bundle bundle
= osgiBoot
.installUrl(moduleUrl
);
174 module
= bundle
.getSymbolicName();
175 // TODO deal with version
178 System
.setProperty(UNIQUE_LAUNCH_MODULE_PROPERTY
, module
);
179 System
.setProperty(UNIQUE_LAUNCH_FLOW_PROPERTY
, flow
);
180 System
.setProperty("log4j.configuration", "file:./log4j.properties");
183 osgiBoot
.startBundles(bundlesToStart
);
185 // Bundle bundle = (Bundle) osgiBoot.getBundlesBySymbolicName().get(
186 // "org.argeo.slc.specs");
187 // bundle.loadClass(Execu)
189 // // retrieve modulesManager
190 // BundlesManager bundlesManager = new
191 // BundlesManager(bundleContext);
192 // ExecutionModulesManager modulesManager = bundlesManager
193 // .getSingleService(ExecutionModulesManager.class, null, true);
195 // RealizedFlow realizedFlow = RealizedFlow.create(module, null,
198 // modulesManager.start(new BasicNameVersion(module, "0.0.0"));
199 // modulesManager.execute(realizedFlow);
201 // osgiBoot.bootstrap();
202 // osgiBoot.bootstrap();
205 // String typeStr = cl.getOptionValue(typeOpt.getOpt());
206 // if (typeStr == null) {
207 // type = Type.standalone;
210 // type = Type.valueOf(typeStr);
211 // } catch (IllegalArgumentException e) {
212 // throw new SlcException("Unrecognized mode '" + typeStr
218 // if (type.equals(Type.standalone)) {
219 // if (!cl.hasOption(moduleOpt.getOpt()))
220 // throw new SlcException("Type " + Type.standalone
221 // + " requires option '" + moduleOpt.getLongOpt()
223 // module = cl.getOptionValue(moduleOpt.getOpt());
226 // if (cl.hasOption(flowsOpt.getOpt()))
227 // flows = cl.getOptionValue(flowsOpt.getOpt());
231 // if (cl.hasOption(propertiesOpt.getOpt())) {
232 // for (String propertyFile : cl.getOptionValues(propertiesOpt
234 // loadPropertyFile(properties, propertyFile);
237 // if (cl.hasOption(propertyOpt.getOpt())) {
238 // for (String property : cl.getOptionValues(propertyOpt.getOpt()))
240 // addProperty(properties, property);
245 // if (cl.hasOption(runtimeOpt.getOpt())) {
246 // urlStr = cl.getOptionValue(runtimeOpt.getOpt());
248 } catch (ParseException e
) {
249 System
.err
.println("Problem with command line arguments. "
252 } catch (SlcException e
) {
253 System
.err
.println(e
.getMessage());
255 } catch (Exception e
) {
256 System
.err
.println("Unexpected exception when bootstrapping.");
262 // debug("Mode: " + type);
263 // if (urlStr != null)
264 // debug("Runtime: " + urlStr);
265 // debug("User properties: " + properties);
266 // if (module != null)
267 // debug("Module: " + module);
268 // if (flows != null)
269 // debug("Flows: " + flows);
273 // if (type.equals(Type.standalone)) {
276 // else if (type.equals(Type.agent)) {
280 public static void printUsage() {
281 new HelpFormatter().printHelp(commandName
, options
, true);
284 // private static String listTypeValues() {
285 // StringBuffer buf = new StringBuffer("");
286 // for (Type mode : Type.values()) {
287 // buf.append(mode).append(", ");
289 // String str = buf.toString();
290 // // unsafe, but there will be at least one value in the enum
291 // return str.substring(0, str.length() - 2);
294 protected static void addProperty(Properties properties
, String property
) {
295 int eqIndex
= property
.indexOf('=');
297 throw new SlcException("Badly formatted property " + property
);
300 String key
= property
.substring(0, eqIndex
);
301 String value
= property
.substring(eqIndex
+ 1);
302 properties
.setProperty(key
, value
);
305 properties
.setProperty(property
, "true");
309 protected static void loadPropertyFile(Properties properties
,
310 String propertyFile
) {
311 FileInputStream in
= null;
313 in
= new FileInputStream(propertyFile
);
315 } catch (Exception e
) {
316 throw new SlcException("Could not load proeprty file "
319 IOUtils
.closeQuietly(in
);
323 private static void badExit() {
328 protected static void info(Object msg
) {
329 System
.out
.println(msg
);
332 protected static void debug(Object msg
) {
333 System
.out
.println(msg
);