2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.cli
;
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
;
27 import java
.util
.ServiceLoader
;
28 import java
.util
.UUID
;
30 import javax
.security
.auth
.Subject
;
31 import javax
.security
.auth
.login
.LoginContext
;
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
;
39 /** Configures an SLC runtime and runs a process. */
40 public class SlcMain
implements Runnable
{
41 private final String
[] args
;
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";
49 private final List
<String
> bundlesToStart
= new ArrayList
<String
>();
51 public SlcMain(String
[] 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");
62 final LoginContext lc
;
65 lc
= new LoginContext("NIX");
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"));
73 File dataDir
= new File(tempDir
, "slc-data-"
74 + UUID
.randomUUID().toString());
75 if (!dataDir
.exists())
78 File confDir
= new File(slcDir
, "conf");
79 if (!confDir
.exists())
82 System
.setProperty("log4j.configuration", "file:./log4j.properties");
83 System
.setProperty("argeo.node.repo.configuration",
84 "osgibundle:repository-memory.xml");
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");
96 // Spring configs currently require System properties
97 System
.getProperties().putAll(configuration
);
99 Framework framework
= frameworkFactory
.newFramework(configuration
);
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);
109 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
110 osgiBoot
.installUrls(osgiBoot
.getBundlesUrls(bundlesToInstall
));
113 osgiBoot
.startBundles(bundlesToStart
);
116 ServiceTracker agentTracker
= new ServiceTracker(bundleContext
,
117 "org.argeo.slc.execution.SlcAgentCli", null);
119 final Object agentCli
= agentTracker
.waitForService(30 * 1000);
121 // Run as a privileged action
122 Subject
.doAs(Subject
.getSubject(AccessController
.getContext()),
123 new PrivilegedAction
<String
>() {
125 public String
run() {
127 Class
<?
>[] parameterTypes
= { String
[].class };
128 Method method
= agentCli
.getClass().getMethod(
129 "process", parameterTypes
);
130 Object
[] methodArgs
= { args
};
132 .invoke(agentCli
, methodArgs
);
133 return ret
.toString();
134 } catch (Exception e
) {
135 throw new RuntimeException("Cannot run "
136 + Arrays
.toString(args
) + " on "
143 // Shutdown OSGi runtime
145 framework
.waitForStop(60 * 1000);
148 } catch (Exception e
) {
154 public static void main(String
[] args
) {
155 new SlcMain(args
).run();
158 protected static void info(Object msg
) {
159 System
.out
.println(msg
);
162 protected static void debug(Object msg
) {
163 System
.out
.println(msg
);