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
;
23 import java
.util
.UUID
;
25 import org
.apache
.commons
.cli
.CommandLine
;
26 import org
.apache
.commons
.cli
.CommandLineParser
;
27 import org
.apache
.commons
.cli
.GnuParser
;
28 import org
.apache
.commons
.cli
.HelpFormatter
;
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
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
35 import org
.osgi
.framework
.Bundle
;
36 import org
.osgi
.framework
.BundleContext
;
38 @SuppressWarnings("restriction")
39 public class SlcMain
implements Runnable
{
40 /** Unique launch module */
41 public final static String UNIQUE_LAUNCH_MODULE_PROPERTY
= "slc.launch.module";
43 /** Unique launch flow */
44 public final static String UNIQUE_LAUNCH_FLOW_PROPERTY
= "slc.launch.flow";
46 /** Unique launch flow */
47 public final static String UNIQUE_LAUNCH_ARGS_PROPERTY_BASE
= "slc.launch.args";
49 private final Options options
= new Options();
50 private final String
[] args
;
52 private final String commandName
= "slc";
54 // private static String bundlesToInstall = "/usr/share/osgi;in=*.jar";
55 private String bundlesToInstall
= System
.getProperty("user.home")
56 + "/dev/src/slc/dep/org.argeo.slc.dep.minimal/target/dependency;in=*.jar,"
57 + System
.getProperty("user.home")
58 + "/dev/src/slc/demo/modules;in=*;ex=pom.xml;ex=.svn";
60 private final List
<String
> bundlesToStart
= new ArrayList
<String
>();
62 public SlcMain(String
[] args
) {
64 // bundlesToStart.add("org.springframework.osgi.extender");
65 // bundlesToStart.add("org.argeo.slc.agent");
67 bundlesToStart
.add("org.springframework.osgi.extender");
68 bundlesToStart
.add("org.argeo.node.repo.jackrabbit");
69 bundlesToStart
.add("org.argeo.security.dao.os");
70 bundlesToStart
.add("org.argeo.slc.node.jackrabbit");
71 bundlesToStart
.add("org.argeo.slc.agent");
72 bundlesToStart
.add("org.argeo.slc.agent.jcr");
75 @SuppressWarnings("unchecked")
78 String moduleUrl
= null;
83 CommandLineParser clParser
= new GnuParser();
84 CommandLine cl
= clParser
.parse(options
, args
);
86 List
<String
> arguments
= cl
.getArgList();
87 if (arguments
.size() == 0) {
88 // TODO default behaviour
90 module
= arguments
.get(0);
91 File moduleFile
= new File(module
);
92 if (moduleFile
.exists()) {
93 if (moduleFile
.isDirectory()) {
94 moduleUrl
= "reference:file:"
95 + moduleFile
.getCanonicalPath();
97 moduleUrl
= "file:" + moduleFile
.getCanonicalPath();
101 if (arguments
.size() == 1) {
104 flow
= arguments
.get(1);
108 String executionDir
= System
.getProperty("user.dir");
109 File slcDir
= new File(executionDir
, "target/.slc");
110 File tempDir
= new File(System
.getProperty("java.io.tmpdir"));
112 File dataDir
= new File(tempDir
, "slc-data-"
113 + UUID
.randomUUID().toString());
114 if (!dataDir
.exists())
117 File confDir
= new File(slcDir
, "conf");
118 if (!confDir
.exists())
121 BundleContext bundleContext
= null;
123 String
[] osgiRuntimeArgs
= { "-configuration",
124 confDir
.getCanonicalPath(), "-data",
125 dataDir
.getCanonicalPath(), "-console", "-clean" };
126 bundleContext
= EclipseStarter
.startup(osgiRuntimeArgs
, null);
127 } catch (Exception e
) {
128 throw new RuntimeException("Cannot start Equinox.", e
);
132 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
133 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(bundlesToInstall
));
135 if (moduleUrl
!= null) {
136 Bundle bundle
= osgiBoot
.installUrl(moduleUrl
);
137 module
= bundle
.getSymbolicName();
138 // TODO deal with version
141 System
.setProperty(UNIQUE_LAUNCH_MODULE_PROPERTY
, module
);
142 System
.setProperty(UNIQUE_LAUNCH_FLOW_PROPERTY
, flow
);
143 System
.setProperty("log4j.configuration", "file:./log4j.properties");
144 System
.setProperty("argeo.node.repo.configuration",
145 "osgibundle:repository-memory.xml");
147 osgiBoot
.startBundles(bundlesToStart
);
149 } catch (ParseException e
) {
150 System
.err
.println("Problem with command line arguments. "
153 } catch (SlcException e
) {
154 System
.err
.println(e
.getMessage());
156 } catch (Exception e
) {
157 System
.err
.println("Unexpected exception when bootstrapping.");
163 public static void main(String
[] args
) {
164 new SlcMain(args
).run();
167 public void printUsage() {
168 new HelpFormatter().printHelp(commandName
, options
, true);
171 protected static void addProperty(Properties properties
, String property
) {
172 int eqIndex
= property
.indexOf('=');
174 throw new SlcException("Badly formatted property " + property
);
177 String key
= property
.substring(0, eqIndex
);
178 String value
= property
.substring(eqIndex
+ 1);
179 properties
.setProperty(key
, value
);
182 properties
.setProperty(property
, "true");
186 protected static void loadPropertyFile(Properties properties
,
187 String propertyFile
) {
188 FileInputStream in
= null;
190 in
= new FileInputStream(propertyFile
);
192 } catch (Exception e
) {
193 throw new SlcException("Could not load proeprty file "
196 IOUtils
.closeQuietly(in
);
200 private void badExit() {
205 protected static void info(Object msg
) {
206 System
.out
.println(msg
);
209 protected static void debug(Object msg
) {
210 System
.out
.println(msg
);