]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
Working command line SLC
[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.lang.reflect.Method;
20 import java.security.AccessController;
21 import java.security.PrivilegedAction;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.ServiceLoader;
28 import java.util.UUID;
29
30 import javax.security.auth.Subject;
31 import javax.security.auth.login.LoginContext;
32
33 import org.argeo.osgi.boot.OsgiBoot;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.launch.Framework;
36 import org.osgi.framework.launch.FrameworkFactory;
37 import org.osgi.util.tracker.ServiceTracker;
38
39 /** Configures an SLC runtime and runs a process. */
40 public class SlcMain implements Runnable {
41 private final String[] args;
42
43 // private static String bundlesToInstall = "/usr/share/osgi;in=*.jar";
44 private String bundlesToInstall = System.getProperty("user.home")
45 + "/dev/src/slc/dep/org.argeo.slc.dep.minimal/target/dependency;in=*.jar,"
46 + System.getProperty("user.home")
47 + "/dev/src/slc/demo/modules;in=*;ex=pom.xml;ex=.svn";
48
49 private final List<String> bundlesToStart = new ArrayList<String>();
50
51 public SlcMain(String[] args) {
52 this.args = args;
53 bundlesToStart.add("org.springframework.osgi.extender");
54 bundlesToStart.add("org.argeo.node.repo.jackrabbit");
55 bundlesToStart.add("org.argeo.security.dao.os");
56 bundlesToStart.add("org.argeo.slc.node.jackrabbit");
57 bundlesToStart.add("org.argeo.slc.agent");
58 bundlesToStart.add("org.argeo.slc.agent.jcr");
59 }
60
61 public void run() {
62 final LoginContext lc;
63 try {
64 // Authenticate
65 lc = new LoginContext("NIX");
66 lc.login();
67
68 // Prepare directories
69 String executionDir = System.getProperty("user.dir");
70 File slcDir = new File(executionDir, "target/.slc");
71 File tempDir = new File(System.getProperty("java.io.tmpdir"));
72
73 File dataDir = new File(tempDir, "slc-data-"
74 + UUID.randomUUID().toString());
75 if (!dataDir.exists())
76 dataDir.mkdirs();
77
78 File confDir = new File(slcDir, "conf");
79 if (!confDir.exists())
80 confDir.mkdirs();
81
82 System.setProperty("log4j.configuration", "file:./log4j.properties");
83 System.setProperty("argeo.node.repo.configuration",
84 "osgibundle:repository-memory.xml");
85
86 // Start Equinox
87 ServiceLoader<FrameworkFactory> ff = ServiceLoader
88 .load(FrameworkFactory.class);
89 FrameworkFactory frameworkFactory = ff.iterator().next();
90 Map<String, String> configuration = new HashMap<String, String>();
91 configuration.put("osgi.configuration.area",
92 confDir.getCanonicalPath());
93 configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
94 configuration.put("osgi.clean", "true");
95
96 // Spring configs currently require System properties
97 System.getProperties().putAll(configuration);
98
99 Framework framework = frameworkFactory.newFramework(configuration);
100 framework.start();
101 BundleContext bundleContext = framework.getBundleContext();
102 // String[] osgiRuntimeArgs = { "-configuration",
103 // confDir.getCanonicalPath(), "-data",
104 // dataDir.getCanonicalPath(), "-clean" };
105 // BundleContext bundleContext = EclipseStarter.startup(
106 // osgiRuntimeArgs, null);
107
108 // OSGi bootstrap
109 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
110 osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall));
111
112 // Start runtime
113 osgiBoot.startBundles(bundlesToStart);
114
115 // Find SLC Agent
116 ServiceTracker agentTracker = new ServiceTracker(bundleContext,
117 "org.argeo.slc.execution.SlcAgentCli", null);
118 agentTracker.open();
119 final Object agentCli = agentTracker.waitForService(30 * 1000);
120
121 // Run as a privileged action
122 Subject.doAs(Subject.getSubject(AccessController.getContext()),
123 new PrivilegedAction<String>() {
124
125 public String run() {
126 try {
127 Class<?>[] parameterTypes = { String[].class };
128 Method method = agentCli.getClass().getMethod(
129 "process", parameterTypes);
130 Object[] methodArgs = { args };
131 Object ret = method
132 .invoke(agentCli, methodArgs);
133 return ret.toString();
134 } catch (Exception e) {
135 throw new RuntimeException("Cannot run "
136 + Arrays.toString(args) + " on "
137 + agentCli, e);
138 }
139 }
140
141 });
142
143 // Shutdown OSGi runtime
144 framework.stop();
145 framework.waitForStop(60 * 1000);
146
147 System.exit(0);
148 } catch (Exception e) {
149 e.printStackTrace();
150 System.exit(1);
151 }
152 }
153
154 public static void main(String[] args) {
155 new SlcMain(args).run();
156 }
157
158 protected static void info(Object msg) {
159 System.out.println(msg);
160 }
161
162 protected static void debug(Object msg) {
163 System.out.println(msg);
164 }
165
166 }