]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Implementation of DefaultModulesManager.process
authorOlivier Capillon <olivier.capillon@gmail.com>
Wed, 11 Mar 2009 18:35:18 +0000 (18:35 +0000)
committerOlivier Capillon <olivier.capillon@gmail.com>
Wed, 11 Mar 2009 18:35:18 +0000 (18:35 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2255 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/execution/ExecutionModule.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/AbstractSpringExecutionModule.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultModulesManager.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/ExecutionContext.java
runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/DefaultModulesMangerTest.java [new file with mode: 0644]
runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/ExecutionFlowTest.java
runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/test.xml [new file with mode: 0644]

index 9be149475cc1776d73801838e0388bef3edbee32..fd197efc4ee2de9861f4896ce7b6729c389307db 100644 (file)
@@ -9,5 +9,8 @@ public interface ExecutionModule {
 
        public ExecutionModuleDescriptor getDescriptor();
 
+       //TODO: remove
        public void execute(SlcExecution slcExecution);
+       
+       public void execute(ExecutionFlowDescriptor descriptor);
 }
index 4e0842e37dd142d9c05ed6c9724060021059dad6..0d9770a453beff40feac0b2a668d635e42549c50 100644 (file)
@@ -93,6 +93,13 @@ public abstract class AbstractSpringExecutionModule implements ExecutionModule,
                applicationContext.publishEvent(new NewExecutionEvent(this,
                                slcExecution));
        }
+       
+       public void execute(ExecutionFlowDescriptor descriptor) {
+               ExecutionFlow flow = (ExecutionFlow) applicationContext.getBean(descriptor.getName(), 
+                               ExecutionFlow.class);
+               flow.execute();
+       }
+       
 
        public void setApplicationContext(ApplicationContext applicationContext)
                        throws BeansException {
index 5daa5822b2d09f5889ec4627f298b2c990399693..75d94db02fd46f83ae6805d6abc192685bcb4173 100644 (file)
@@ -5,9 +5,12 @@ import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.execution.ExecutionFlow;
+import org.argeo.slc.execution.ExecutionFlowDescriptor;
 import org.argeo.slc.execution.ExecutionModule;
 import org.argeo.slc.execution.ExecutionModuleDescriptor;
 import org.argeo.slc.execution.ExecutionModulesManager;
+import org.argeo.slc.process.RealizedFlow;
 import org.argeo.slc.process.SlcExecution;
 import org.springframework.util.Assert;
 
@@ -16,18 +19,21 @@ public class DefaultModulesManager implements ExecutionModulesManager {
                        .getLog(DefaultModulesManager.class);
 
        private List<ExecutionModule> executionModules = new ArrayList<ExecutionModule>();
-
-       public ExecutionModuleDescriptor getExecutionModuleDescriptor(
-                       String moduleName, String version) {
-               ExecutionModule module = null;
+       
+       protected ExecutionModule getExecutionModule(String moduleName, String version) {
                for (ExecutionModule moduleT : executionModules) {
                        if (moduleT.getName().equals(moduleName)) {
                                // TODO: check version
-                               module = moduleT;
-                               break;
+                               return moduleT;
                        }
                }
-
+               return null;
+       }
+       
+       public ExecutionModuleDescriptor getExecutionModuleDescriptor(
+                       String moduleName, String version) {
+               ExecutionModule module = getExecutionModule(moduleName, version);
+               
                Assert.notNull(module);
 
                return module.getDescriptor();
@@ -44,6 +50,45 @@ public class DefaultModulesManager implements ExecutionModulesManager {
        public void process(SlcExecution slcExecution) {
                log.info("SlcExecution " + slcExecution);
 
+               for(RealizedFlow flow : slcExecution.getRealizedFlows()) {
+                       ExecutionModule module = getExecutionModule(flow.getModuleName(),
+                                       flow.getModuleVersion());
+                       if(module != null) {
+                               ExecutionContext executionContext = new ExecutionContext();
+                               executionContext.addVariables(slcExecution.getAttributes());
+                               ExecutionThread thread = new ExecutionThread(executionContext, flow.getFlowDescriptor(),
+                                               module);
+                               thread.start();
+                       }
+                       else {
+                               // throw exception ?
+                       }
+               }
        }
 
+       private class ExecutionThread extends Thread {
+               private final ExecutionFlowDescriptor executionFlowDescriptor;
+               private final ExecutionContext executionContext;
+               private final ExecutionModule executionModule;
+
+               public ExecutionThread(ExecutionContext executionContext,
+                               ExecutionFlowDescriptor executionFlowDescriptor,
+                               ExecutionModule executionModule) {
+                       super("SLC Execution #" + executionContext.getUuid());
+                       this.executionFlowDescriptor = executionFlowDescriptor;
+                       this.executionContext = executionContext;
+                       this.executionModule = executionModule;
+               }
+
+               public void run() {
+                       ExecutionContext.registerExecutionContext(executionContext);                            
+                       try {
+                               executionModule.execute(executionFlowDescriptor);
+                       } catch (Exception e) {
+                               log.error("Execution " + executionContext.getUuid()
+                                               + " failed.", e);
+                       }
+               }
+       }       
+       
 }
index 9332826084b29bdd972162b39960745961f2816f..d6b69af8485494dbd18cf66e693af489af1ce515 100644 (file)
@@ -30,6 +30,10 @@ public class ExecutionContext {
                        return null;
                return executionContext.get().variables;
        }
+       
+       public void addVariables(Map<? extends String, ? extends Object> variablesToAdd) {
+               variables.putAll(variablesToAdd);
+       }
 
        public static ExecutionContext getCurrent() {
                return executionContext.get();
diff --git a/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/DefaultModulesMangerTest.java b/runtime/org.argeo.slc.support.simple/src/test/java/org/argeo/slc/core/execution/DefaultModulesMangerTest.java
new file mode 100644 (file)
index 0000000..b3a48fd
--- /dev/null
@@ -0,0 +1,64 @@
+package org.argeo.slc.core.execution;\r
+\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.argeo.slc.SlcException;\r
+import org.argeo.slc.execution.ExecutionFlowDescriptor;\r
+import org.argeo.slc.execution.ExecutionModule;\r
+import org.argeo.slc.process.RealizedFlow;\r
+import org.argeo.slc.process.SlcExecution;\r
+import org.argeo.slc.unit.AbstractSpringTestCase;\r
+import org.springframework.context.ApplicationContext;\r
+import org.springframework.context.ConfigurableApplicationContext;\r
+import org.springframework.context.support.ClassPathXmlApplicationContext;\r
+\r
+import junit.framework.TestCase;\r
+\r
+public class DefaultModulesMangerTest extends AbstractSpringTestCase {\r
+\r
+       public void testSimpleExecution() throws Exception {\r
+               //do nothing\r
+               \r
+               // create an execution\r
+               SlcExecution execution = new SlcExecution();\r
+               execution.setUuid("TestUUID");\r
+               List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();\r
+               RealizedFlow flow = new RealizedFlow();\r
+               flow.setModuleName("dummyname");\r
+               flow.setModuleVersion("dummyversion");\r
+               ExecutionFlowDescriptor executionFlowDescriptor = new ExecutionFlowDescriptor();\r
+               executionFlowDescriptor.setName("main");\r
+               flow.setFlowDescriptor(executionFlowDescriptor);\r
+               realizedFlows.add(flow);\r
+               execution.setRealizedFlows(realizedFlows);\r
+               \r
+               // create a module\r
+               ApplicationContext applicationContext = prepareExecution("applicationContext.xml");\r
+               ExecutionModule module = createExecutionModule(applicationContext);\r
+               \r
+               // create an Execution Module Manager\r
+               DefaultModulesManager manager = new DefaultModulesManager();\r
+               List<ExecutionModule> modules = new ArrayList<ExecutionModule>();\r
+               modules.add(module);\r
+               manager.setExecutionModules(modules);\r
+               \r
+               manager.process(execution);\r
+       }       \r
+       \r
+       protected ExecutionModule createExecutionModule(ApplicationContext applicationContext) {\r
+               AbstractSpringExecutionModule module = new AbstractSpringExecutionModule() {\r
+                       public String getName() {return "dummyname";}\r
+                       public String getVersion() {return "dummyversion";}                     \r
+               };\r
+               module.setApplicationContext(applicationContext);\r
+               return module;\r
+       }\r
+       \r
+       protected ConfigurableApplicationContext prepareExecution(String applicationContextSuffix) {\r
+               ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(inPackage(applicationContextSuffix));\r
+               applicationContext.start();\r
+               return applicationContext;\r
+       }       \r
+       \r
+}\r
index baacc6dd9e783fc11ac96c2118f7b8a5402a951a..4698e8148200ab1384c98cfadd5d8f59c7fa9546 100644 (file)
@@ -1,6 +1,8 @@
 package org.argeo.slc.core.execution;\r
 \r
+import org.argeo.slc.core.test.SimpleTestResult;\r
 import org.argeo.slc.execution.ExecutionFlow;\r
+import org.argeo.slc.test.TestStatus;\r
 import org.argeo.slc.unit.AbstractSpringTestCase;\r
 import org.springframework.beans.factory.BeanCreationException;\r
 import org.springframework.context.ConfigurableApplicationContext;\r
@@ -9,88 +11,67 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
 public class ExecutionFlowTest extends AbstractSpringTestCase {\r
        \r
        public void testSimpleExecution() throws Exception {\r
-//             configureAndExecuteSlcFlow("main");\r
                configureAndExecuteSlcFlow("applicationContext.xml", "main");\r
        }\r
        \r
        public void testCanonic() throws Exception {\r
-               configureAndExecuteSlcFlow("minimal.xml", "minimal");\r
                // Parameter without default value in specification\r
                configureAndExecuteSlcFlow("canonic-001.xml", "canonic.001");\r
-//             configureAndExecuteSlcFlow("canonic-002.xml", "canonic.002");\r
+               configureAndExecuteSlcFlow("canonic-002.xml", "canonic.002");\r
 \r
 /*             try {\r
                        configureAndExecuteSlcFlow("canonic-003.error.xml", "canonic.003");\r
                        fail("Parameter not set - should be rejected.");\r
                } catch (BeanCreationException e) {\r
                        // exception expected\r
-                       //e.printStackTrace();\r
-               }*/\r
-               \r
+                       logException(e);\r
+               }\r
+*/             \r
 /*             try {\r
                        configureAndExecuteSlcFlow("canonic-004.error.xml", "canonic.004");\r
-                       fail("Unkown parameter set - should be rejected.");\r
+                       fail("Unknown parameter set - should be rejected.");\r
                } catch (BeanCreationException e) {\r
                        // exception expected\r
-                       //e.printStackTrace();\r
-               }               */\r
+                       logException(e);\r
+               }       */\r
        }       \r
        \r
-       protected void configureSlcFlow(String beanName) {\r
+/*     public void testRecursive() throws Exception {\r
+               ConfigurableApplicationContext applicationContext = prepareExecution("test.xml");\r
+               ExecutionFlow executionFlow = (ExecutionFlow) applicationContext.getBean("first");\r
+               executionFlow.execute();                \r
+               SimpleTestResult res = (SimpleTestResult) applicationContext.getBean("basicTestResult");\r
+               if(res.getParts().get(0).getStatus() != TestStatus.PASSED) {\r
+                       fail("Unexpected string returned");\r
+               }\r
+               applicationContext.close();             \r
+       }*/\r
+       \r
+       protected void logException(Throwable ex) {\r
+               log.info("Got Exception of class " + ex.getClass().toString()\r
+                               + " with message '" + ex.getMessage() + "'.");\r
+       }\r
+       \r
+       protected void initExecutionContext() {\r
                // if an execution context was registered, unregister it\r
                if(ExecutionContext.getCurrent() != null) {\r
                        ExecutionContext.unregisterExecutionContext();\r
                }\r
                // register a new ExecutionContext\r
-               ExecutionContext.registerExecutionContext(new ExecutionContext());\r
-               \r
-//             ExecutionContext.getVariables().put("slc.flows", beanName);\r
-       }\r
-       \r
-/*     \r
-       @Override\r
-       protected Boolean getIsStartContext() {\r
-               return true;\r
-       }\r
-\r
-       @Override\r
-       protected ConfigurableApplicationContext getContext() {\r
-               return getStaticContext();\r
-       }\r
-\r
-       private static ConfigurableApplicationContext staticContext;    \r
-               \r
-       protected ConfigurableApplicationContext getStaticContext() {\r
-               if (staticContext == null) {\r
-                       staticContext = new ClassPathXmlApplicationContext(\r
-                                       getApplicationContextLocation());\r
-                       if(getIsStartContext())\r
-                               staticContext.start();\r
-               }\r
-               return staticContext;           \r
+               ExecutionContext.registerExecutionContext(new ExecutionContext());              \r
        }\r
        \r
-       protected void configureAndExecuteSlcFlow(String beanName) {\r
-               // Triggers a start of the ApplicationContext\r
-               // Required before starting some tests\r
-               // TODO: understand why !\r
-               getContext(); \r
-               configureSlcFlow(beanName);\r
-               ExecutionFlow executionFlow = (ExecutionFlow) getContext().getBean(beanName);\r
-               executionFlow.execute();                \r
+       protected ConfigurableApplicationContext prepareExecution(String applicationContextSuffix) {\r
+               ConfigurableApplicationContext applicationContext = new ClassPathXmlApplicationContext(inPackage(applicationContextSuffix));\r
+               applicationContext.start();\r
+               initExecutionContext();\r
+               return applicationContext;\r
        }\r
-       */\r
        \r
        protected void configureAndExecuteSlcFlow(String applicationContextSuffix, String beanName) {\r
-               // create a new context\r
-               ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(\r
-                               inPackage(applicationContextSuffix));\r
-               applicationContext.start();\r
-               \r
-               configureSlcFlow(beanName);\r
+               ConfigurableApplicationContext applicationContext = prepareExecution(applicationContextSuffix);\r
                ExecutionFlow executionFlow = (ExecutionFlow) applicationContext.getBean(beanName);\r
                executionFlow.execute();                \r
-//             applicationContext.stop();\r
                applicationContext.close();\r
        }       \r
 }\r
diff --git a/runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/test.xml b/runtime/org.argeo.slc.support.simple/src/test/resources/org/argeo/slc/core/execution/test.xml
new file mode 100644 (file)
index 0000000..8d08ca1
--- /dev/null
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+       xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+       xmlns:aop="http://www.springframework.org/schema/aop"\r
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd\r
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">\r
+\r
+       <import resource="imports.xml" /> \r
+\r
+       <bean id="second" parent="slcTemplate.simpleFlow">\r
+               <property name="executionSpec">\r
+                       <bean parent="slcTemplate.simpleSpec">\r
+                               <property name="attributes">\r
+                                       <map>\r
+                                               <entry key="testKey">\r
+                                                       <bean parent="specAttr.primitive" p:value="second" />\r
+                                               </entry>\r
+                                       </map>\r
+                               </property>\r
+                       </bean>\r
+               </property>\r
+               <property name="executables">\r
+                       <list>\r
+                               <ref local="echo1" />\r
+                               <bean class="org.argeo.slc.core.test.SimpleTestRun">\r
+                                       <property name="testDefinition" ref="basic.testDef" />\r
+                                       <property name="testData" ref="basic.testData" />\r
+                                       <property name="testResult" ref="basicTestResult"/>\r
+                               </bean>                         \r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+\r
+       <bean id="first" parent="slcTemplate.simpleFlow">\r
+               <property name="executionSpec">\r
+                       <bean parent="slcTemplate.simpleSpec">\r
+                               <property name="attributes">\r
+                                       <map>\r
+                                               <entry key="testKey">\r
+                                                       <bean parent="specAttr.primitive" p:value="first" />\r
+                                               </entry>\r
+                                       </map>\r
+                               </property>\r
+                       </bean>\r
+               </property>\r
+               <property name="executables">\r
+                       <list>\r
+                               <ref local="second" />\r
+                       </list>\r
+               </property>\r
+       </bean>\r
+\r
+       <bean id="basic.testData" class="org.argeo.slc.core.test.BasicTestData" scope="execution">\r
+               <aop:scoped-proxy />\r
+               <property name="expected" value="first" />\r
+               <property name="reached" value="@{testKey}" />\r
+       </bean>\r
+       \r
+       <bean id="basic.testDef" class="org.argeo.slc.core.test.BasicTestDefinition">\r
+               <aop:scoped-proxy />\r
+       </bean> \r
+\r
+\r
+       <bean id="echo1" parent="task.echo" scope="execution">\r
+               <property name="message"\r
+                       value="testKey=@{testKey}" />\r
+               <aop:scoped-proxy />\r
+       </bean>\r
+       \r
+       <bean id="basicTestResult" class="org.argeo.slc.core.test.SimpleTestResult" />\r
+\r
+</beans>
\ No newline at end of file