]> 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 OSGi console extension
[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 java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6
7 import org.argeo.slc.SlcException;
8 import org.argeo.slc.execution.ExecutionFlowDescriptor;
9 import org.argeo.slc.execution.ExecutionModule;
10 import org.eclipse.osgi.framework.console.CommandInterpreter;
11 import org.eclipse.osgi.framework.console.CommandProvider;
12 import org.osgi.framework.Bundle;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.BundleException;
15 import org.osgi.framework.ServiceReference;
16 import org.springframework.osgi.context.BundleContextAware;
17
18 public class ExecutionCommandProvider implements CommandProvider,
19 BundleContextAware {
20 private List<ExecutionModule> executionModules;
21 private BundleContext bundleContext;
22
23 public Object _slc_exec(CommandInterpreter ci) {
24 // TODO: check version
25 String firstArg = ci.nextArgument();
26 String executionName = ci.nextArgument();
27 String moduleName = null;
28
29 Long bundleId = null;
30 try {
31 bundleId = Long.parseLong(firstArg);
32 } catch (NumberFormatException e) {
33 // silent
34 }
35
36 Bundle bundle = null;
37 if (bundleId != null) {
38 bundle = bundleContext.getBundle(bundleId);
39 } else {
40 for (Bundle b : bundleContext.getBundles()) {
41 if (b.getSymbolicName().contains(firstArg)) {
42 bundle = b;
43 break;
44 }
45 }
46 }
47
48 if (bundle != null) {
49 moduleName = bundle.getSymbolicName();
50 // try {
51 // bundle.stop();
52 // bundle.update();
53 // bundle.start();
54 //
55 // // FIXME: potential infinite loop
56 // while (bundle.getState() == Bundle.STARTING) {
57 // try {
58 // Thread.sleep(500);
59 // } catch (InterruptedException e) {
60 // // silent
61 // }
62 // }
63 // } catch (BundleException e) {
64 // throw new SlcException(
65 // "Could not update the bundle for module " + moduleName,
66 // e);
67 // }
68 }
69
70 // Find module
71 ExecutionModule module = null;
72 if (moduleName != null) {
73 for (Iterator<ExecutionModule> it = executionModules.iterator(); it
74 .hasNext();) {
75 ExecutionModule moduleT = it.next();
76 if (moduleT.getName().equals(moduleName)) {
77 module = moduleT;
78 break;
79 }
80 }
81 }
82
83 if (module != null) {
84 ExecutionFlowDescriptor descriptor = new ExecutionFlowDescriptor();
85 descriptor.setName(executionName);
86 module.execute(descriptor);
87 return "Executed " + executionName + " from " + moduleName;
88 } else
89 return "Could not find any execution module matching these requirements.";
90 }
91
92 public String getHelp() {
93 StringBuffer buf = new StringBuffer();
94 buf.append("---SLC Execution Commands---\n");
95 buf
96 .append("\tslc_exec (<id>|<segment of bsn>) <execution bean> - execute an execution flow\n");
97 return buf.toString();
98
99 }
100
101 public void setExecutionModules(List<ExecutionModule> executionModules) {
102 this.executionModules = executionModules;
103 }
104
105 public void setBundleContext(BundleContext bundleContext) {
106 this.bundleContext = bundleContext;
107 }
108
109 }