]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
15cfcb2185173b0954d2c861da9be57dd6c4b8b4
[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
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;
36
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";
41
42 /** Unique launch flow */
43 public final static String UNIQUE_LAUNCH_FLOW_PROPERTY = "slc.launch.flow";
44
45 /** Unique launch flow */
46 public final static String UNIQUE_LAUNCH_ARGS_PROPERTY_BASE = "slc.launch.args";
47
48 private final Options options = new Options();
49 private final String[] args;
50
51 private final String commandName = "slc";
52
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";
58
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>();
66
67 public SlcMain(String[] args) {
68 this.args = args;
69 bundlesToStart.add("org.springframework.osgi.extender");
70 bundlesToStart.add("org.argeo.slc.agent");
71 }
72
73 @SuppressWarnings("unchecked")
74 public void run() {
75 String module = null;
76 String moduleUrl = null;
77 String flow = null;
78
79 try {
80
81 CommandLineParser clParser = new GnuParser();
82 CommandLine cl = clParser.parse(options, args);
83
84 List<String> arguments = cl.getArgList();
85 if (arguments.size() == 0) {
86 // TODO default behaviour
87 } else {
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();
94 } else {
95 moduleUrl = "file:" + moduleFile.getCanonicalPath();
96 }
97 }
98
99 if (arguments.size() == 1) {
100 // TODO module info
101 } else {
102 flow = arguments.get(1);
103 }
104 }
105
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())
110 dataDir.mkdirs();
111 File confDir = new File(slcDir, "conf");
112 if (!confDir.exists())
113 confDir.mkdirs();
114
115 BundleContext bundleContext = null;
116 try {
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);
123 }
124
125 // OSGi bootstrap
126 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
127 osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall));
128
129 if (moduleUrl != null) {
130 Bundle bundle = osgiBoot.installUrl(moduleUrl);
131 module = bundle.getSymbolicName();
132 // TODO deal with version
133 }
134
135 System.setProperty(UNIQUE_LAUNCH_MODULE_PROPERTY, module);
136 System.setProperty(UNIQUE_LAUNCH_FLOW_PROPERTY, flow);
137 System.setProperty("log4j.configuration", "file:./log4j.properties");
138
139 // start runtime
140 osgiBoot.startBundles(bundlesToStart);
141
142 } catch (ParseException e) {
143 System.err.println("Problem with command line arguments. "
144 + e.getMessage());
145 badExit();
146 } catch (SlcException e) {
147 System.err.println(e.getMessage());
148 badExit();
149 } catch (Exception e) {
150 System.err.println("Unexpected exception when bootstrapping.");
151 e.printStackTrace();
152 badExit();
153 }
154 }
155
156 public static void main(String[] args) {
157 new SlcMain(args).run();
158 }
159
160 public void printUsage() {
161 new HelpFormatter().printHelp(commandName, options, true);
162 }
163
164 protected static void addProperty(Properties properties, String property) {
165 int eqIndex = property.indexOf('=');
166 if (eqIndex == 0)
167 throw new SlcException("Badly formatted property " + property);
168
169 if (eqIndex > 0) {
170 String key = property.substring(0, eqIndex);
171 String value = property.substring(eqIndex + 1);
172 properties.setProperty(key, value);
173
174 } else {
175 properties.setProperty(property, "true");
176 }
177 }
178
179 protected static void loadPropertyFile(Properties properties,
180 String propertyFile) {
181 FileInputStream in = null;
182 try {
183 in = new FileInputStream(propertyFile);
184 properties.load(in);
185 } catch (Exception e) {
186 throw new SlcException("Could not load proeprty file "
187 + propertyFile);
188 } finally {
189 IOUtils.closeQuietly(in);
190 }
191 }
192
193 private void badExit() {
194 printUsage();
195 System.exit(1);
196 }
197
198 protected static void info(Object msg) {
199 System.out.println(msg);
200 }
201
202 protected static void debug(Object msg) {
203 System.out.println(msg);
204 }
205 }