]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - 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
index ca49e8fdc6b8db7370f79ed26d0343b2d215c5c3..3a90807807225921eee47c728f3d25446e61712a 100644 (file)
@@ -1,41 +1,99 @@
 package org.argeo.slc.equinox;
 
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
 
+import org.argeo.slc.SlcException;
+import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModule;
-import org.argeo.slc.process.SlcExecution;
 import org.eclipse.osgi.framework.console.CommandInterpreter;
 import org.eclipse.osgi.framework.console.CommandProvider;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+import org.springframework.osgi.context.BundleContextAware;
 
-public class ExecutionCommandProvider implements CommandProvider {
+public class ExecutionCommandProvider implements CommandProvider,
+               BundleContextAware {
        private List<ExecutionModule> executionModules;
+       private BundleContext bundleContext;
 
-       public Object _slc_execute(CommandInterpreter ci) {
-               String moduleName = ci.nextArgument();
+       public Object _slc_exec(CommandInterpreter ci) {
+               // TODO: check version
+               String firstArg = ci.nextArgument();
                String executionName = ci.nextArgument();
-               
-               SlcExecution slcExecution = new SlcExecution();
-               slcExecution.getAttributes().put("slc.flows", executionName);
+               String moduleName = null;
 
+               Long bundleId = null;
+               try {
+                       bundleId = Long.parseLong(firstArg);
+               } catch (NumberFormatException e) {
+                       // silent
+               }
+
+               Bundle bundle = null;
+               if (bundleId != null) {
+                       bundle = bundleContext.getBundle(bundleId);
+               } else {
+                       for (Bundle b : bundleContext.getBundles()) {
+                               if (b.getSymbolicName().contains(firstArg)) {
+                                       bundle = b;
+                                       break;
+                               }
+                       }
+               }
+
+               if (bundle != null) {
+                       moduleName = bundle.getSymbolicName();
+//                     try {
+//                             bundle.stop();
+//                             bundle.update();
+//                             bundle.start();
+//
+//                             // FIXME: potential infinite loop
+//                             while (bundle.getState() == Bundle.STARTING) {
+//                                     try {
+//                                             Thread.sleep(500);
+//                                     } catch (InterruptedException e) {
+//                                             // silent
+//                                     }
+//                             }
+//                     } catch (BundleException e) {
+//                             throw new SlcException(
+//                                             "Could not update the bundle for module " + moduleName,
+//                                             e);
+//                     }
+               }
+
+               // Find module
                ExecutionModule module = null;
-               for (ExecutionModule moduleT : executionModules) {
-                       if(moduleT.getName().equals(moduleName)){
-                               // TODO: check version
-                               module = moduleT;
-                               break;
+               if (moduleName != null) {
+                       for (Iterator<ExecutionModule> it = executionModules.iterator(); it
+                                       .hasNext();) {
+                               ExecutionModule moduleT = it.next();
+                               if (moduleT.getName().equals(moduleName)) {
+                                       module = moduleT;
+                                       break;
+                               }
                        }
                }
 
-               if(module!=null)
-                       module.execute(slcExecution);
-               
-               return null;
+               if (module != null) {
+                       ExecutionFlowDescriptor descriptor = new ExecutionFlowDescriptor();
+                       descriptor.setName(executionName);
+                       module.execute(descriptor);
+                       return "Executed " + executionName + " from " + moduleName;
+               } else
+                       return "Could not find any execution module matching these requirements.";
        }
 
        public String getHelp() {
                StringBuffer buf = new StringBuffer();
                buf.append("---SLC Execution Commands---\n");
-               buf.append("\tslc_execute - Execute an execution flow\n");
+               buf
+                               .append("\tslc_exec (<id>|<segment of bsn>) <execution bean>  - execute an execution flow\n");
                return buf.toString();
 
        }
@@ -44,4 +102,8 @@ public class ExecutionCommandProvider implements CommandProvider {
                this.executionModules = executionModules;
        }
 
+       public void setBundleContext(BundleContext bundleContext) {
+               this.bundleContext = bundleContext;
+       }
+
 }