+ // Find SLC Agent
+ ServiceReference sr = null;
+ while (sr == null) {
+ sr = bundleContext
+ .getServiceReference("org.argeo.slc.execution.SlcAgentCli");
+ if (System.currentTimeMillis() - begin > timeout)
+ throw new RuntimeException("Cannot find SLC agent CLI");
+ Thread.sleep(100);
+ }
+ final Object agentCli = bundleContext.getService(sr);
+
+ // ServiceTracker agentTracker = new ServiceTracker(bundleContext,
+ // "org.argeo.slc.execution.SlcAgentCli", null);
+ // agentTracker.open();
+ // final Object agentCli = agentTracker.waitForService(30 * 1000);
+ // if (agentCli == null)
+ // throw new RuntimeException("Cannot find SLC agent CLI");
+
+ long duration = System.currentTimeMillis() - begin;
+ System.out.println("Initialized in " + (duration / 1000) + "s "
+ + (duration % 1000) + "ms");
+ // 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);