1 package org
.argeo
.slc
.cli
;
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
;
15 import java
.util
.ServiceLoader
;
16 import java
.util
.UUID
;
18 import javax
.security
.auth
.Subject
;
19 import javax
.security
.auth
.login
.LoginContext
;
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
;
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";
33 public final static String os
;
34 public final static String slcDirName
= ".slc";
35 final static File homeDir
= new File(System
.getProperty("user.home"));
38 String osName
= System
.getProperty("os.name");
39 if (osName
.startsWith("Win"))
41 else if (osName
.startsWith("Solaris"))
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
;
53 private final List
<String
> bundlesToStart
= new ArrayList
<String
>();
55 public SlcMain(String
[] args
, File confDir
, File dataDir
, File modulesDir
) {
57 this.confDir
= confDir
;
58 this.dataDir
= dataDir
;
59 this.modulesDir
= modulesDir
;
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");
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");
79 long begin
= System
.currentTimeMillis();
81 Framework framework
= null;
83 info("## Date : " + new Date());
84 info("## Data : " + dataDir
.getCanonicalPath());
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());
93 configuration
.put("osgi.clean", "true");
94 if (args
.length
== 0) {
95 configuration
.put("osgi.console", "");
98 // Spring configs currently require System properties
99 System
.getProperties().putAll(configuration
);
101 framework
= frameworkFactory
.newFramework(configuration
);
103 BundleContext bundleContext
= framework
.getBundleContext();
106 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
108 // working copy modules
109 if (modulesDir
.exists())
110 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(modulesDir
.getCanonicalPath() + ";in=*;ex=.gitignore"));
113 if (System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BUNDLES
) != null)
114 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BUNDLES
)));
116 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(System
.getProperty("user.home") + "/.slc/modules/;in=**"));
119 osgiBoot
.startBundles(bundlesToStart
);
122 ServiceReference 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");
129 Object agentCli
= bundleContext
.getService(sr
);
131 // Initialization completed
132 long duration
= System
.currentTimeMillis() - begin
;
133 info("[[ Initialized in " + (duration
/ 1000) + "s " + (duration
% 1000) + "ms ]]");
135 if (args
.length
== 0)
136 return null;// console mode
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
);
145 // Shutdown OSGi runtime
147 framework
.waitForStop(60 * 1000);
149 return ret
.toString();
150 } catch (Exception e
) {
151 // Shutdown OSGi runtime
152 if (framework
!= null)
155 framework
.waitForStop(15 * 1000);
156 } catch (Exception silent
) {
158 throw new RuntimeException("Cannot run SLC command line", e
);
164 public static void main(String
[] args
) {
166 // Prepare directories
167 File executionDir
= new File(System
.getProperty("user.dir"));
169 Boolean isTransient
= false;
171 File tempDir
= new File(System
.getProperty("java.io.tmpdir") + "/" + System
.getProperty("user.name"));
172 slcDir
= new File(tempDir
, "slc-" + UUID
.randomUUID().toString());
174 System
.setProperty("argeo.node.repo.configuration", "osgibundle:repository-memory.xml");
176 slcDir
= findSlcDir(executionDir
);
177 if (slcDir
== null) {
178 slcDir
= new File(executionDir
, slcDirName
);
180 info("## Creating an SLC node at " + slcDir
+ " ...");
184 File dataDir
= new File(slcDir
, "data");
185 if (!dataDir
.exists())
188 File confDir
= new File(slcDir
, "conf");
189 if (!confDir
.exists())
192 File modulesDir
= new File(slcDir
, "modules");
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());
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);
211 // Subject.getSubject(AccessController.getContext());
212 // Subject.doAs(subject, new SlcMain(args, confDir, dataDir,
214 SlcMain slcMain
= new SlcMain(args
, confDir
, dataDir
, modulesDir
);
216 if (args
.length
!= 0)
218 } catch (Exception e
) {
225 * Recursively look in parent directories for a directory named
226 * {@link #slcDirName}
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())
233 File parentDir
= currentDir
.getParentFile();
234 if (parentDir
== null)
237 // ~/.slc reserved for agent
238 if (parentDir
.getCanonicalPath().equals(homeDir
.getCanonicalPath()))
240 } catch (IOException e
) {
241 throw new RuntimeException("Cannot check home directory", e
);
243 return findSlcDir(parentDir
);
246 protected static void copyResource(String resource
, File targetFile
) {
247 InputStream input
= null;
248 FileOutputStream output
= null;
250 input
= SlcMain
.class.getResourceAsStream(resource
);
251 output
= new FileOutputStream(targetFile
);
252 byte[] buf
= new byte[8192];
254 int length
= input
.read(buf
);
257 output
.write(buf
, 0, length
);
259 } catch (Exception e
) {
260 throw new RuntimeException("Cannot write " + resource
+ " file to " + targetFile
, e
);
264 } catch (Exception ignore
) {
268 } catch (Exception ignore
) {
274 protected static void info(Object msg
) {
275 System
.out
.println(msg
);
278 protected static void err(Object msg
) {
279 System
.err
.println(msg
);
282 protected static void debug(Object msg
) {
283 System
.out
.println(msg
);
288 // private String bundlesToInstall = System.getProperty("user.home")
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";
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");
301 // protected class AgentCliCall implements PrivilegedAction<String> {
302 // private final Object agentCli;
304 // public AgentCliCall(Object agentCli) {
306 // this.agentCli = agentCli;
309 // public String run() {
311 // Class<?>[] parameterTypes = { String[].class };
312 // Method method = agentCli.getClass().getMethod("process",
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);