]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.equinox/src/main/java/org/argeo/slc/equinox/ExecutionCommandProvider.java
Private keys support
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.equinox / src / main / java / org / argeo / slc / equinox / ExecutionCommandProvider.java
1 package org.argeo.slc.equinox;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.SlcException;
6 import org.argeo.slc.osgi.OsgiExecutionModulesManager;
7 import org.argeo.slc.process.RealizedFlow;
8 import org.eclipse.core.runtime.adaptor.EclipseStarter;
9 import org.eclipse.osgi.framework.console.CommandInterpreter;
10 import org.eclipse.osgi.framework.console.CommandProvider;
11
12 @SuppressWarnings("restriction")
13 public class ExecutionCommandProvider implements CommandProvider {
14 private final static Log log = LogFactory
15 .getLog(ExecutionCommandProvider.class);
16
17 private final static String SLC_WITH_REFRESH = "slc";
18 private final static String SLC_NO_REFRESH = "slcnr";
19
20 private OsgiExecutionModulesManager modulesManager;
21
22 private RealizedFlow lastLaunch = null;
23
24 public Object _slc(CommandInterpreter ci) {
25 return exec(SLC_WITH_REFRESH, ci);
26 }
27
28 public Object _slcnr(CommandInterpreter ci) {
29 return exec(SLC_NO_REFRESH, ci);
30 }
31
32 protected Object exec(String slcCommand, CommandInterpreter ci) {
33 // TODO: check version
34 String firstArg = ci.nextArgument();
35 if (firstArg == null) {
36 if (lastLaunch != null) {
37 String cmd = slcCommand + " " + lastLaunch.getModuleName()
38 + " " + lastLaunch.getFlowDescriptor().getName();
39 if (log.isDebugEnabled())
40 log.debug("Execute again last command: " + cmd);
41 return ci.execute(cmd);
42 } else {
43 ci.execute("help");
44 throw new SlcException("Command not properly formatted");
45 }
46 }
47 String executionName = ci.nextArgument();
48
49 launch(slcCommand, firstArg, executionName);
50 return "COMMAND COMPLETED";
51 }
52
53 protected void launch(String slcCommand, String firstArg,
54 String executionName) {
55 lastLaunch = modulesManager.findRealizedFlow(firstArg, executionName);
56 if (lastLaunch == null)
57 throw new SlcException("Cannot find launch for " + firstArg + " "
58 + executionName);
59
60 // Execute
61 if (SLC_WITH_REFRESH.equals(slcCommand))
62 modulesManager.updateAndExecute(lastLaunch);
63 else if (SLC_NO_REFRESH.equals(slcCommand))
64 modulesManager.execute(lastLaunch);
65 else
66 throw new SlcException("Unrecognized SLC command " + slcCommand);
67 }
68
69 public String getHelp() {
70 StringBuffer buf = new StringBuffer();
71 buf.append("---SLC Execution Commands---\n");
72 buf
73 .append("\tslc (<id>|<segment of bsn>) <execution bean>"
74 + " - refresh the bundle, execute an execution flow (without arg, execute last)\n");
75 buf
76 .append("\tslcnr (<id>|<segment of bsn>) <execution bean>"
77 + " - execute an execution flow (without arg, execute last)\n");
78 return buf.toString();
79
80 }
81
82 public void setModulesManager(OsgiExecutionModulesManager osgiModulesManager) {
83 this.modulesManager = osgiModulesManager;
84 }
85
86 public void init() throws Exception {
87 final String module = System.getProperty("slc.launch.module");
88 final String executionName = System.getProperty("slc.launch.execution");
89 if (module != null) {
90 new Thread() {
91
92 @Override
93 public void run() {
94 try {
95 launch(SLC_NO_REFRESH, module, executionName);
96 // in case of failure OSGi runtime stays up and last
97 // launch can be used to debug by calling 'slc'
98 } catch (Exception e) {
99 throw new SlcException("Error when executing "
100 + executionName + " on " + module, e);
101 }
102 try {
103 EclipseStarter.shutdown();
104 } catch (Exception e) {
105 throw new SlcException("Cannot shutdown equinox.", e);
106 }
107 }
108
109 }.start();
110 }
111
112 }
113 }