]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
Remove runtime packages
[gpl/argeo-slc.git] / runtime / org.argeo.slc.launcher / src / main / java / org / argeo / slc / cli / SlcMain.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.cli;
17
18 import java.io.File;
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;
24
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;
37
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";
42
43 /** Unique launch flow */
44 public final static String UNIQUE_LAUNCH_FLOW_PROPERTY = "slc.launch.flow";
45
46 /** Unique launch flow */
47 public final static String UNIQUE_LAUNCH_ARGS_PROPERTY_BASE = "slc.launch.args";
48
49 private final Options options = new Options();
50 private final String[] args;
51
52 private final String commandName = "slc";
53
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";
59
60 private final List<String> bundlesToStart = new ArrayList<String>();
61
62 public SlcMain(String[] args) {
63 this.args = args;
64 // bundlesToStart.add("org.springframework.osgi.extender");
65 // bundlesToStart.add("org.argeo.slc.agent");
66
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");
73 }
74
75 @SuppressWarnings("unchecked")
76 public void run() {
77 String module = null;
78 String moduleUrl = null;
79 String flow = null;
80
81 try {
82
83 CommandLineParser clParser = new GnuParser();
84 CommandLine cl = clParser.parse(options, args);
85
86 List<String> arguments = cl.getArgList();
87 if (arguments.size() == 0) {
88 // TODO default behaviour
89 } else {
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();
96 } else {
97 moduleUrl = "file:" + moduleFile.getCanonicalPath();
98 }
99 }
100
101 if (arguments.size() == 1) {
102 // TODO module info
103 } else {
104 flow = arguments.get(1);
105 }
106 }
107
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"));
111
112 File dataDir = new File(tempDir, "slc-data-"
113 + UUID.randomUUID().toString());
114 if (!dataDir.exists())
115 dataDir.mkdirs();
116
117 File confDir = new File(slcDir, "conf");
118 if (!confDir.exists())
119 confDir.mkdirs();
120
121 BundleContext bundleContext = null;
122 try {
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);
129 }
130
131 // OSGi bootstrap
132 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
133 osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall));
134
135 if (moduleUrl != null) {
136 Bundle bundle = osgiBoot.installUrl(moduleUrl);
137 module = bundle.getSymbolicName();
138 // TODO deal with version
139 }
140
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");
146 // start runtime
147 osgiBoot.startBundles(bundlesToStart);
148
149 } catch (ParseException e) {
150 System.err.println("Problem with command line arguments. "
151 + e.getMessage());
152 badExit();
153 } catch (SlcException e) {
154 System.err.println(e.getMessage());
155 badExit();
156 } catch (Exception e) {
157 System.err.println("Unexpected exception when bootstrapping.");
158 e.printStackTrace();
159 badExit();
160 }
161 }
162
163 public static void main(String[] args) {
164 new SlcMain(args).run();
165 }
166
167 public void printUsage() {
168 new HelpFormatter().printHelp(commandName, options, true);
169 }
170
171 protected static void addProperty(Properties properties, String property) {
172 int eqIndex = property.indexOf('=');
173 if (eqIndex == 0)
174 throw new SlcException("Badly formatted property " + property);
175
176 if (eqIndex > 0) {
177 String key = property.substring(0, eqIndex);
178 String value = property.substring(eqIndex + 1);
179 properties.setProperty(key, value);
180
181 } else {
182 properties.setProperty(property, "true");
183 }
184 }
185
186 protected static void loadPropertyFile(Properties properties,
187 String propertyFile) {
188 FileInputStream in = null;
189 try {
190 in = new FileInputStream(propertyFile);
191 properties.load(in);
192 } catch (Exception e) {
193 throw new SlcException("Could not load proeprty file "
194 + propertyFile);
195 } finally {
196 IOUtils.closeQuietly(in);
197 }
198 }
199
200 private void badExit() {
201 printUsage();
202 System.exit(1);
203 }
204
205 protected static void info(Object msg) {
206 System.out.println(msg);
207 }
208
209 protected static void debug(Object msg) {
210 System.out.println(msg);
211 }
212 }