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