]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.equinox/src/main/java/org/argeo/slc/equinox/ExecutionCommandProvider.java
Improve executions and system calls
[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.upgrade(lastLaunch.getModuleNameVersion());
63 modulesManager.execute(lastLaunch);
64 } else if (SLC_NO_REFRESH.equals(slcCommand))
65 modulesManager.execute(lastLaunch);
66 else
67 throw new SlcException("Unrecognized SLC command " + slcCommand);
68 }
69
70 public String getHelp() {
71 StringBuffer buf = new StringBuffer();
72 buf.append("---SLC Execution Commands---\n");
73 buf
74 .append("\tslc (<id>|<segment of bsn>) <execution bean>"
75 + " - refresh the bundle, execute an execution flow (without arg, execute last)\n");
76 buf
77 .append("\tslcnr (<id>|<segment of bsn>) <execution bean>"
78 + " - execute an execution flow (without arg, execute last)\n");
79 return buf.toString();
80
81 }
82
83 public void setModulesManager(OsgiExecutionModulesManager osgiModulesManager) {
84 this.modulesManager = osgiModulesManager;
85 }
86
87 public void init() throws Exception {
88 final String module = System.getProperty("slc.launch.module");
89 final String executionName = System.getProperty("slc.launch.execution");
90 if (module != null) {
91 new Thread() {
92
93 @Override
94 public void run() {
95 try {
96 launch(SLC_NO_REFRESH, module, executionName);
97 // in case of failure OSGi runtime stays up and last
98 // launch can be used to debug by calling 'slc'
99 } catch (Exception e) {
100 throw new SlcException("Error when executing "
101 + executionName + " on " + module, e);
102 }
103 try {
104 EclipseStarter.shutdown();
105 } catch (Exception e) {
106 throw new SlcException("Cannot shutdown equinox.", e);
107 }
108 }
109
110 }.start();
111 }
112
113 }
114 }