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