-
- CommandLineParser clParser = new GnuParser();
- CommandLine cl = clParser.parse(options, args);
-
- // Mode
- String typeStr = cl.getOptionValue(typeOpt.getOpt());
- if (typeStr == null) {
- type = Type.standalone;
- } else {
- try {
- type = Type.valueOf(typeStr);
- } catch (IllegalArgumentException e) {
- throw new SlcException("Unrecognized mode '" + typeStr
- + "'", e);
- }
- }
-
- // Script
- if (type.equals(Type.standalone)) {
- if (!cl.hasOption(moduleOpt.getOpt()))
- throw new SlcException("Type " + Type.standalone
- + " requires option '" + moduleOpt.getLongOpt()
- + "'");
- module = cl.getOptionValue(moduleOpt.getOpt());
-
- // Targets
- if (cl.hasOption(flowsOpt.getOpt()))
- flows = cl.getOptionValue(flowsOpt.getOpt());
- }
-
- // Properties
- if (cl.hasOption(propertiesOpt.getOpt())) {
- for (String propertyFile : cl.getOptionValues(propertiesOpt
- .getOpt())) {
- loadPropertyFile(properties, propertyFile);
- }
- }
- if (cl.hasOption(propertyOpt.getOpt())) {
- for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
- addProperty(properties, property);
- }
- }
-
- // Runtime
- if (cl.hasOption(runtimeOpt.getOpt())) {
- urlStr = cl.getOptionValue(runtimeOpt.getOpt());
- }
- } catch (ParseException e) {
- System.err.println("Problem with command line arguments. "
- + e.getMessage());
- badExit();
- } catch (SlcException e) {
- System.err.println(e.getMessage());
- badExit();
+ // Authenticate
+ lc = new LoginContext("NIX");
+ lc.login();
+
+ // Prepare directories
+ String executionDir = System.getProperty("user.dir");
+ File slcDir = new File(executionDir, "target/.slc");
+ File tempDir = new File(System.getProperty("java.io.tmpdir"));
+
+ File dataDir = new File(tempDir, "slc-data-"
+ + UUID.randomUUID().toString());
+ if (!dataDir.exists())
+ dataDir.mkdirs();
+
+ File confDir = new File(slcDir, "conf");
+ if (!confDir.exists())
+ confDir.mkdirs();
+
+ System.setProperty("log4j.configuration", "file:./log4j.properties");
+ System.setProperty("argeo.node.repo.configuration",
+ "osgibundle:repository-memory.xml");
+
+ // Start Equinox
+ ServiceLoader<FrameworkFactory> ff = ServiceLoader
+ .load(FrameworkFactory.class);
+ FrameworkFactory frameworkFactory = ff.iterator().next();
+ Map<String, String> configuration = new HashMap<String, String>();
+ configuration.put("osgi.configuration.area",
+ confDir.getCanonicalPath());
+ configuration.put("osgi.instance.area", dataDir.getCanonicalPath());
+ configuration.put("osgi.clean", "true");
+
+ // Spring configs currently require System properties
+ System.getProperties().putAll(configuration);
+
+ Framework framework = frameworkFactory.newFramework(configuration);
+ framework.start();
+ BundleContext bundleContext = framework.getBundleContext();
+ // String[] osgiRuntimeArgs = { "-configuration",
+ // confDir.getCanonicalPath(), "-data",
+ // dataDir.getCanonicalPath(), "-clean" };
+ // BundleContext bundleContext = EclipseStarter.startup(
+ // osgiRuntimeArgs, null);
+
+ // OSGi bootstrap
+ OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
+ osgiBoot.installUrls(osgiBoot.getBundlesUrls(bundlesToInstall));
+
+ // Start runtime
+ osgiBoot.startBundles(bundlesToStart);
+
+ // Find SLC Agent
+ ServiceTracker agentTracker = new ServiceTracker(bundleContext,
+ "org.argeo.slc.execution.SlcAgentCli", null);
+ agentTracker.open();
+ final Object agentCli = agentTracker.waitForService(30 * 1000);
+
+ // Run as a privileged action
+ Subject.doAs(Subject.getSubject(AccessController.getContext()),
+ new PrivilegedAction<String>() {
+
+ public String run() {
+ try {
+ Class<?>[] parameterTypes = { String[].class };
+ Method method = agentCli.getClass().getMethod(
+ "process", parameterTypes);
+ Object[] methodArgs = { args };
+ Object ret = method
+ .invoke(agentCli, methodArgs);
+ return ret.toString();
+ } catch (Exception e) {
+ throw new RuntimeException("Cannot run "
+ + Arrays.toString(args) + " on "
+ + agentCli, e);
+ }
+ }
+
+ });
+
+ // Shutdown OSGi runtime
+ framework.stop();
+ framework.waitForStop(60 * 1000);
+
+ System.exit(0);