]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.equinox/src/main/java/org/argeo/slc/equinox/ExecutionCommandProvider.java
Unique launch
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.equinox / src / main / java / org / argeo / slc / equinox / ExecutionCommandProvider.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.equinox;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.argeo.slc.SlcException;
22 import org.argeo.slc.osgi.OsgiExecutionModulesManager;
23 import org.argeo.slc.process.RealizedFlow;
24 import org.eclipse.osgi.framework.console.CommandInterpreter;
25 import org.eclipse.osgi.framework.console.CommandProvider;
26
27 @SuppressWarnings("restriction")
28 public class ExecutionCommandProvider implements CommandProvider {
29 private final static Log log = LogFactory
30 .getLog(ExecutionCommandProvider.class);
31
32 private final static String SLC_WITH_REFRESH = "slc";
33 private final static String SLC_NO_REFRESH = "slcnr";
34
35 private OsgiExecutionModulesManager modulesManager;
36
37 private RealizedFlow lastLaunch = null;
38
39 public Object _slc(CommandInterpreter ci) {
40 return exec(SLC_WITH_REFRESH, ci);
41 }
42
43 public Object _slcnr(CommandInterpreter ci) {
44 return exec(SLC_NO_REFRESH, ci);
45 }
46
47 protected Object exec(String slcCommand, CommandInterpreter ci) {
48 // TODO: check version
49 String firstArg = ci.nextArgument();
50 if (firstArg == null) {
51 if (lastLaunch != null) {
52 String cmd = slcCommand + " " + lastLaunch.getModuleName()
53 + " " + lastLaunch.getFlowDescriptor().getName();
54 if (log.isDebugEnabled())
55 log.debug("Execute again last command: " + cmd);
56 return ci.execute(cmd);
57 } else {
58 ci.execute("help");
59 throw new SlcException("Command not properly formatted");
60 }
61 }
62 String executionName = ci.nextArgument();
63
64 launch(slcCommand, firstArg, executionName);
65 return "COMMAND COMPLETED";
66 }
67
68 protected void launch(String slcCommand, String firstArg,
69 String executionName) {
70 lastLaunch = modulesManager.findRealizedFlow(firstArg, executionName);
71 if (lastLaunch == null)
72 throw new SlcException("Cannot find launch for " + firstArg + " "
73 + executionName);
74
75 // Execute
76 if (SLC_WITH_REFRESH.equals(slcCommand)) {
77 modulesManager.upgrade(lastLaunch.getModuleNameVersion());
78 modulesManager.execute(lastLaunch);
79 } else if (SLC_NO_REFRESH.equals(slcCommand))
80 modulesManager.execute(lastLaunch);
81 else
82 throw new SlcException("Unrecognized SLC command " + slcCommand);
83 }
84
85 public String getHelp() {
86 StringBuffer buf = new StringBuffer();
87 buf.append("---SLC Execution Commands---\n");
88 buf
89 .append("\tslc (<id>|<segment of bsn>) <execution bean>"
90 + " - refresh the bundle, execute an execution flow (without arg, execute last)\n");
91 buf
92 .append("\tslcnr (<id>|<segment of bsn>) <execution bean>"
93 + " - execute an execution flow (without arg, execute last)\n");
94 return buf.toString();
95
96 }
97
98 public void setModulesManager(OsgiExecutionModulesManager osgiModulesManager) {
99 this.modulesManager = osgiModulesManager;
100 }
101
102 }