]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
Remove bundles conflicting with Commons v1.2
[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.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.lang.reflect.Method;
23 import java.security.AccessController;
24 import java.security.PrivilegedAction;
25 import java.util.ArrayList;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.ServiceLoader;
31 import java.util.UUID;
32
33 import javax.security.auth.Subject;
34 import javax.security.auth.login.LoginContext;
35
36 import org.argeo.osgi.boot.OsgiBoot;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.BundleException;
39 import org.osgi.framework.ServiceReference;
40 import org.osgi.framework.launch.Framework;
41 import org.osgi.framework.launch.FrameworkFactory;
42
43 /** Configures an SLC runtime and runs a process. */
44 public class SlcMain implements PrivilegedAction<String> {
45 public final static String NIX = "NIX";
46 public final static String WINDOWS = "WINDOWS";
47 public final static String SOLARIS = "SOLARIS";
48
49 public final static String os;
50 public final static String slcDirName = ".slc";
51 final static File homeDir = new File(System.getProperty("user.home"));
52
53 static {
54 String osName = System.getProperty("os.name");
55 if (osName.startsWith("Win"))
56 os = WINDOWS;
57 else if (osName.startsWith("Solaris"))
58 os = SOLARIS;
59 else
60 os = NIX;
61 }
62
63 private Long timeout = 30 * 1000l;
64 private final String[] args;
65 private final File confDir;
66 private final File dataDir;
67 private final File modulesDir;
68
69 private final List<String> bundlesToStart = new ArrayList<String>();
70
71 public SlcMain(String[] args, File confDir, File dataDir, File modulesDir) {
72 this.args = args;
73 this.confDir = confDir;
74 this.dataDir = dataDir;
75 this.modulesDir = modulesDir;
76 bundlesToStart.add("org.springframework.osgi.extender");
77 bundlesToStart.add("org.argeo.node.repo.jackrabbit");
78 bundlesToStart.add("org.argeo.security.dao.os");
79 bundlesToStart.add("org.argeo.slc.node.jackrabbit");
80 bundlesToStart.add("org.argeo.slc.agent");
81 bundlesToStart.add("org.argeo.slc.agent.jcr");
82 if (args.length == 0)
83 bundlesToStart.add("org.argeo.slc.support.equinox");
84 // bundlesToStart.add("org.argeo.slc.agent.cli");
85 }
86
87 public String run() {
88 long begin = System.currentTimeMillis();
89
90 Framework framework = null;
91 try {
92 info("## Date : " + new Date());
93 info("## Data : " + dataDir.getCanonicalPath());
94
95 // Start Equinox
96 ServiceLoader<FrameworkFactory> ff = ServiceLoader
97 .load(FrameworkFactory.class);
98 FrameworkFactory frameworkFactory = ff.iterator().next();
99 Map<String, String> configuration = new HashMap<String, String>();
100 configuration.put("osgi.configuration.area",
101 confDir.getCanonicalPath());
102 configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
103 if (args.length == 0) {
104 // configuration.put("osgi.clean", "true");
105 configuration.put("osgi.console", "");
106 }
107
108 // Spring configs currently require System properties
109 System.getProperties().putAll(configuration);
110
111 framework = frameworkFactory.newFramework(configuration);
112 framework.start();
113 BundleContext bundleContext = framework.getBundleContext();
114
115 // OSGi bootstrap
116 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
117
118 // working copy modules
119 if (modulesDir.exists())
120 osgiBoot.installUrls(osgiBoot.getBundlesUrls(modulesDir
121 .getCanonicalPath() + ";in=*;ex=.gitignore"));
122
123 // system modules
124 if (System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BUNDLES) != null)
125 osgiBoot.installUrls(osgiBoot.getBundlesUrls(System
126 .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BUNDLES)));
127 else
128 osgiBoot.installUrls(osgiBoot.getBundlesUrls(System
129 .getProperty("user.home") + "/.slc/modules/;in=**"));
130
131 // Start runtime
132 osgiBoot.startBundles(bundlesToStart);
133
134 // Find SLC Agent
135 ServiceReference sr = null;
136 while (sr == null) {
137 sr = bundleContext
138 .getServiceReference("org.argeo.slc.execution.SlcAgentCli");
139 if (System.currentTimeMillis() - begin > timeout)
140 throw new RuntimeException("Cannot find SLC agent CLI");
141 Thread.sleep(100);
142 }
143 Object agentCli = bundleContext.getService(sr);
144
145 // Initialization completed
146 long duration = System.currentTimeMillis() - begin;
147 info("[[ Initialized in " + (duration / 1000) + "s "
148 + (duration % 1000) + "ms ]]");
149
150 if (args.length == 0)
151 return null;// console mode
152
153 // Subject.doAs(Subject.getSubject(AccessController.getContext()),
154 // new AgentCliCall(agentCli));
155 Class<?>[] parameterTypes = { String[].class };
156 Method method = agentCli.getClass().getMethod("process",
157 parameterTypes);
158 Object[] methodArgs = { args };
159 Object ret = method.invoke(agentCli, methodArgs);
160
161 // Shutdown OSGi runtime
162 framework.stop();
163 framework.waitForStop(60 * 1000);
164
165 return ret.toString();
166 } catch (Exception e) {
167 // Shutdown OSGi runtime
168 if (framework != null)
169 try {
170 framework.stop();
171 framework.waitForStop(15 * 1000);
172 } catch (Exception silent) {
173 }
174 throw new RuntimeException("Cannot run SLC command line", e);
175 } finally {
176
177 }
178 }
179
180 public static void main(String[] args) {
181 try {
182 // Prepare directories
183 File executionDir = new File(System.getProperty("user.dir"));
184 File slcDir;
185 Boolean isTransient = false;
186 if (isTransient) {
187 File tempDir = new File(System.getProperty("java.io.tmpdir")
188 + "/" + System.getProperty("user.name"));
189 slcDir = new File(tempDir, "slc-"
190 + UUID.randomUUID().toString());
191 slcDir.mkdirs();
192 System.setProperty("argeo.node.repo.configuration",
193 "osgibundle:repository-memory.xml");
194 } else {
195 slcDir = findSlcDir(executionDir);
196 if (slcDir == null) {
197 slcDir = new File(executionDir, slcDirName);
198 slcDir.mkdirs();
199 info("## Creating an SLC node at " + slcDir + " ...");
200 }
201 }
202
203 File dataDir = new File(slcDir, "data");
204 if (!dataDir.exists())
205 dataDir.mkdirs();
206
207 File confDir = new File(slcDir, "conf");
208 if (!confDir.exists())
209 confDir.mkdirs();
210
211 File modulesDir = new File(slcDir, "modules");
212
213 // JAAS
214 File jaasFile = new File(confDir, "jaas.config");
215 if (!jaasFile.exists())
216 copyResource("/org/argeo/slc/cli/jaas.config", jaasFile);
217 System.setProperty("java.security.auth.login.config",
218 jaasFile.getCanonicalPath());
219
220 // log4j
221 File log4jFile = new File(confDir, "log4j.properties");
222 if (!log4jFile.exists())
223 copyResource("/org/argeo/slc/cli/log4j.properties", log4jFile);
224 System.setProperty("log4j.configuration",
225 "file://" + log4jFile.getCanonicalPath());
226 // Run as a privileged action
227 LoginContext lc = new LoginContext(os);
228 lc.login();
229
230 Subject subject = Subject.getSubject(AccessController.getContext());
231 Subject.doAs(subject, new SlcMain(args, confDir, dataDir,
232 modulesDir));
233
234 if (args.length != 0)
235 System.exit(0);
236 } catch (Exception e) {
237 e.printStackTrace();
238 System.exit(1);
239 }
240 }
241
242 /**
243 * Recursively look in parent directories for a directory named
244 * {@link #slcDirName}
245 */
246 protected static File findSlcDir(File currentDir) {
247 File slcDir = new File(currentDir, slcDirName);
248 if (slcDir.exists() && slcDir.isDirectory())
249 return slcDir;
250 File parentDir = currentDir.getParentFile();
251 if (parentDir == null)
252 return null;
253 try {
254 // ~/.slc reserved for agent
255 if (parentDir.getCanonicalPath().equals(homeDir.getCanonicalPath()))
256 return null;
257 } catch (IOException e) {
258 throw new RuntimeException("Cannot check home directory", e);
259 }
260 return findSlcDir(parentDir);
261 }
262
263 protected static void copyResource(String resource, File targetFile) {
264 InputStream input = null;
265 FileOutputStream output = null;
266 try {
267 input = SlcMain.class.getResourceAsStream(resource);
268 output = new FileOutputStream(targetFile);
269 byte[] buf = new byte[8192];
270 while (true) {
271 int length = input.read(buf);
272 if (length < 0)
273 break;
274 output.write(buf, 0, length);
275 }
276 } catch (Exception e) {
277 throw new RuntimeException("Cannot write " + resource + " file to "
278 + targetFile, e);
279 } finally {
280 try {
281 input.close();
282 } catch (Exception ignore) {
283 }
284 try {
285 output.close();
286 } catch (Exception ignore) {
287 }
288 }
289
290 }
291
292 protected static void info(Object msg) {
293 System.out.println(msg);
294 }
295
296 protected static void err(Object msg) {
297 System.err.println(msg);
298 }
299
300 protected static void debug(Object msg) {
301 System.out.println(msg);
302 }
303
304 }
305
306 // private String bundlesToInstall = System.getProperty("user.home")
307 // +
308 // "/dev/src/slc/dep/org.argeo.slc.dep.minimal/target/dependency;in=*.jar,"
309 // + System.getProperty("user.home")
310 // + "/dev/src/slc/demo/modules;in=*;ex=pom.xml;ex=.svn";
311
312 // ServiceTracker agentTracker = new ServiceTracker(bundleContext,
313 // "org.argeo.slc.execution.SlcAgentCli", null);
314 // agentTracker.open();
315 // final Object agentCli = agentTracker.waitForService(30 * 1000);
316 // if (agentCli == null)
317 // throw new RuntimeException("Cannot find SLC agent CLI");
318
319 // protected class AgentCliCall implements PrivilegedAction<String> {
320 // private final Object agentCli;
321 //
322 // public AgentCliCall(Object agentCli) {
323 // super();
324 // this.agentCli = agentCli;
325 // }
326 //
327 // public String run() {
328 // try {
329 // Class<?>[] parameterTypes = { String[].class };
330 // Method method = agentCli.getClass().getMethod("process",
331 // parameterTypes);
332 // Object[] methodArgs = { args };
333 // Object ret = method.invoke(agentCli, methodArgs);
334 // return ret.toString();
335 // } catch (Exception e) {
336 // throw new RuntimeException("Cannot run "
337 // + Arrays.toString(args) + " on " + agentCli, e);
338 // }
339 // }
340 //
341 // }
342