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
.ArrayList
;
21 import java
.util
.List
;
22 import java
.util
.Properties
;
24 import org
.apache
.commons
.cli
.CommandLine
;
25 import org
.apache
.commons
.cli
.CommandLineParser
;
26 import org
.apache
.commons
.cli
.GnuParser
;
27 import org
.apache
.commons
.cli
.HelpFormatter
;
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
.osgi
.boot
.OsgiBoot
;
32 import org
.argeo
.slc
.SlcException
;
33 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
34 import org
.osgi
.framework
.Bundle
;
35 import org
.osgi
.framework
.BundleContext
;
37 @SuppressWarnings("restriction")
38 public class SlcMain
implements Runnable
{
39 /** Unique launch module */
40 public final static String UNIQUE_LAUNCH_MODULE_PROPERTY
= "slc.launch.module";
42 /** Unique launch flow */
43 public final static String UNIQUE_LAUNCH_FLOW_PROPERTY
= "slc.launch.flow";
45 /** Unique launch flow */
46 public final static String UNIQUE_LAUNCH_ARGS_PROPERTY_BASE
= "slc.launch.args";
48 private final Options options
= new Options();
49 private final String
[] args
;
51 private final String commandName
= "slc";
53 // private static String bundlesToInstall = "/usr/share/osgi;in=*.jar";
54 private String bundlesToInstall
= System
.getProperty("user.home")
55 + "/dev/src/slc/runtime/org.argeo.slc.launcher/target/dependency;in=*.jar,"
56 + System
.getProperty("user.home")
57 + "/dev/src/slc/demo/modules;in=*;ex=pom.xml;ex=.svn";
59 // private static String bundlesToStart =
60 // "org.springframework.osgi.extender,"
61 // + "org.argeo.node.repofactory.jackrabbit,"
62 // + "org.argeo.node.repo.jackrabbit," + "org.argeo.security.dao.os,"
63 // + "org.argeo.slc.node.jackrabbit," + "org.argeo.slc.agent,"
64 // + "org.argeo.slc.agent.jcr";
65 private final List
<String
> bundlesToStart
= new ArrayList
<String
>();
67 public SlcMain(String
[] args
) {
69 bundlesToStart
.add("org.springframework.osgi.extender");
70 bundlesToStart
.add("org.argeo.slc.agent");
73 @SuppressWarnings("unchecked")
76 String moduleUrl
= null;
81 CommandLineParser clParser
= new GnuParser();
82 CommandLine cl
= clParser
.parse(options
, args
);
84 List
<String
> arguments
= cl
.getArgList();
85 if (arguments
.size() == 0) {
86 // TODO default behaviour
88 module
= arguments
.get(0);
89 File moduleFile
= new File(module
);
90 if (moduleFile
.exists()) {
91 if (moduleFile
.isDirectory()) {
92 moduleUrl
= "reference:file:"
93 + moduleFile
.getCanonicalPath();
95 moduleUrl
= "file:" + moduleFile
.getCanonicalPath();
99 if (arguments
.size() == 1) {
102 flow
= arguments
.get(1);
106 String executionDir
= System
.getProperty("user.dir");
107 File slcDir
= new File(executionDir
, "target/.slc");
108 File dataDir
= new File(slcDir
, "data");
109 if (!dataDir
.exists())
111 File confDir
= new File(slcDir
, "conf");
112 if (!confDir
.exists())
115 BundleContext bundleContext
= null;
117 String
[] osgiRuntimeArgs
= { "-configuration",
118 confDir
.getCanonicalPath(), "-data",
119 dataDir
.getCanonicalPath(), "-console", "-clean" };
120 bundleContext
= EclipseStarter
.startup(osgiRuntimeArgs
, null);
121 } catch (Exception e
) {
122 throw new RuntimeException("Cannot start Equinox.", e
);
126 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
127 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(bundlesToInstall
));
129 if (moduleUrl
!= null) {
130 Bundle bundle
= osgiBoot
.installUrl(moduleUrl
);
131 module
= bundle
.getSymbolicName();
132 // TODO deal with version
135 System
.setProperty(UNIQUE_LAUNCH_MODULE_PROPERTY
, module
);
136 System
.setProperty(UNIQUE_LAUNCH_FLOW_PROPERTY
, flow
);
137 System
.setProperty("log4j.configuration", "file:./log4j.properties");
140 osgiBoot
.startBundles(bundlesToStart
);
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 public static void main(String
[] args
) {
157 new SlcMain(args
).run();
160 public void printUsage() {
161 new HelpFormatter().printHelp(commandName
, options
, true);
164 protected static void addProperty(Properties properties
, String property
) {
165 int eqIndex
= property
.indexOf('=');
167 throw new SlcException("Badly formatted property " + property
);
170 String key
= property
.substring(0, eqIndex
);
171 String value
= property
.substring(eqIndex
+ 1);
172 properties
.setProperty(key
, value
);
175 properties
.setProperty(property
, "true");
179 protected static void loadPropertyFile(Properties properties
,
180 String propertyFile
) {
181 FileInputStream in
= null;
183 in
= new FileInputStream(propertyFile
);
185 } catch (Exception e
) {
186 throw new SlcException("Could not load proeprty file "
189 IOUtils
.closeQuietly(in
);
193 private void badExit() {
198 protected static void info(Object msg
) {
199 System
.out
.println(msg
);
202 protected static void debug(Object msg
) {
203 System
.out
.println(msg
);