]> git.argeo.org Git - gpl/argeo-slc.git/commitdiff
Introduce remote launching of agents
authorMathieu Baudier <mbaudier@argeo.org>
Mon, 19 Jan 2009 11:47:20 +0000 (11:47 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Mon, 19 Jan 2009 11:47:20 +0000 (11:47 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2105 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

examples/org.argeo.slc.example01/pom.xml
runtime/org.argeo.slc.launcher/pom.xml
runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java
runtime/org.argeo.slc.launcher/src/main/resources/org/argeo/slc/cli/spring-agent-default.xml [new file with mode: 0644]
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/runtime/SlcApplication.java [new file with mode: 0644]
runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAgent.java
runtime/org.argeo.slc.support.activemq/src/main/resources/org/argeo/slc/activemq/spring-agent.xml
runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcApplication.java
runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java
runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/runtime/AbstractAgent.java [new file with mode: 0644]

index ab487673726f76d48f031cb38a58a3c0a19b60fc..2bbd3e32f9d677732b7c10174e827424fd7cb37f 100644 (file)
                                        </instructions>
                                </configuration>
                        </plugin>
+                       <plugin>
+                               <groupId>org.codehaus.mojo</groupId>
+                               <artifactId>exec-maven-plugin</artifactId>
+                               <version>1.1-beta-1</version>
+                               <configuration>
+                                       <mainClass>org.argeo.slc.cli.SlcMain</mainClass>
+                                       <systemProperties>
+                                               <systemProperty>
+                                                       <key>slc.runtime</key>
+                                                       <value>ws</value>
+                                               </systemProperty>
+                                               <systemProperty>
+                                                       <key>slc.rootFile</key>
+                                                       <value>file:${basedir}/src/slc/root/slcRoot.properties</value>
+                                               </systemProperty>
+                                       </systemProperties>
+                                       <arguments>
+                                               <argument>--mode</argument>
+                                               <argument>agent</argument>
+                                       </arguments>
+                               </configuration>
+                       </plugin>
                </plugins>
        </build>
        <dependencies>
                        <version>1.1.1</version>
                        <scope>testing</scope>
                </dependency>
+
+               <!-- TESTING -->
+               <dependency>
+                       <groupId>org.argeo.slc.runtime</groupId>
+                       <artifactId>org.argeo.slc.launcher</artifactId>
+                       <scope>testing</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.argeo.slc.runtime</groupId>
+                       <artifactId>org.argeo.slc.support.activemq</artifactId>
+                       <scope>testing</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.argeo.slc.runtime</groupId>
+                       <artifactId>org.argeo.slc.support.ant</artifactId>
+                       <scope>testing</scope>
+               </dependency>
+               <dependency>
+                       <groupId>org.argeo.slc.runtime</groupId>
+                       <artifactId>org.argeo.slc.support.castor</artifactId>
+                       <scope>testing</scope>
+               </dependency>
+
        </dependencies>
 </project>
index bf69adb284515d00000402a333cfa7a2c776156f..364223c10201c57cb7b99759c049d9b04b0643d3 100644 (file)
                                        </instructions>
                                </configuration>
                        </plugin>
-                       <plugin>
-                               <groupId>org.codehaus.mojo</groupId>
-                               <artifactId>exec-maven-plugin</artifactId>
-                               <version>1.1-beta-1</version>
-                               <configuration>
-                                       <mainClass>org.argeo.slc.cli.SlcMain</mainClass>
-                               </configuration>
-                       </plugin>
                </plugins>
        </build>
        <dependencies>
                        <artifactId>org.argeo.dep.osgi.commons.cli</artifactId>
                </dependency>
 
-               <!-- TESTING -->
-               <dependency>
-                       <groupId>org.argeo.slc.runtime</groupId>
-                       <artifactId>org.argeo.slc.support.activemq</artifactId>
-                       <scope>testing</scope>
-               </dependency>
-               <dependency>
-                       <groupId>org.argeo.slc.runtime</groupId>
-                       <artifactId>org.argeo.slc.support.castor</artifactId>
-                       <scope>testing</scope>
-               </dependency>
-
        </dependencies>
 </project>
\ No newline at end of file
index 8db1330a6e1d0106bce79bd381cde8b4fd30f3e7..04caaab9eef388d907b755c4ba3779a8aafa6eba 100644 (file)
@@ -31,7 +31,7 @@ public class SlcMain {
        private static Log log = null;
 
        private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
-       private final static String DEFAULT_AGENT_CONTEXT = "classpath:org/argeo/slc/activemq/spring-agent.xml";
+       private final static String DEFAULT_AGENT_CONTEXT = "classpath:org/argeo/slc/cli/spring-agent-default.xml";
 
        private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
                        .withArgName("mode").hasArg().withDescription(
diff --git a/runtime/org.argeo.slc.launcher/src/main/resources/org/argeo/slc/cli/spring-agent-default.xml b/runtime/org.argeo.slc.launcher/src/main/resources/org/argeo/slc/cli/spring-agent-default.xml
new file mode 100644 (file)
index 0000000..a61538c
--- /dev/null
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+       http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"
+       default-lazy-init="false">
+
+
+       <bean
+               class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
+               lazy-init="false">
+               <property name="properties">
+                       <props>
+                               <prop key="org.apache.activemq.brokerURL">
+                                       tcp://localhost:61616
+                </prop>
+                       </props>
+               </property>
+               <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
+               <property name="ignoreUnresolvablePlaceholders" value="true" />
+       </bean>
+
+       <import resource="classpath:org/argeo/slc/activemq/spring-agent.xml" />
+
+       <bean id="slcDefault.cli.agent" parent="slcDefault.jms.agent">
+               <property name="slcApplication" ref="slcDefault.cli.slcApplication" />
+       </bean>
+
+       <bean id="slcDefault.cli.jms.agentListenerContainer.newExecution"
+               parent="slcTemplate.jms.agentListenerContainer">
+               <property name="action" value="newExecution" />
+               <property name="jmsAgent" ref="slcDefault.cli.agent" />
+       </bean>
+
+
+       <bean id="slcDefault.cli.slcApplication" class="org.argeo.slc.ant.AntSlcApplication"
+               init-method="initFromSlcRootFile">
+               <property name="slcRootFile" value="${slc.rootFile}" />
+       </bean>
+
+</beans>
\ No newline at end of file
diff --git a/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/runtime/SlcApplication.java b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/runtime/SlcApplication.java
new file mode 100644 (file)
index 0000000..7324562
--- /dev/null
@@ -0,0 +1,12 @@
+package org.argeo.slc.runtime;
+
+import java.util.Map;
+import java.util.Properties;
+
+import org.argeo.slc.process.SlcExecution;
+
+public interface SlcApplication<T extends SlcExecutionContext> {
+       public void execute(SlcExecution slcExecution, Properties properties,
+                       Map<String, Object> references,
+                       SlcExecutionOutput<T> executionOutput);
+}
index 66c0718fc946c29f6a8199990123496357f3633d..099498caa28409cf2225578057c8dfe5d6d65d17 100644 (file)
@@ -12,6 +12,7 @@ import javax.jms.MessageListener;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.SlcException;
+import org.argeo.slc.core.runtime.AbstractAgent;
 import org.argeo.slc.process.SlcExecution;
 import org.argeo.slc.runtime.SlcAgent;
 import org.argeo.slc.runtime.SlcAgentDescriptor;
@@ -20,7 +21,7 @@ import org.springframework.beans.factory.InitializingBean;
 import org.springframework.jms.core.JmsTemplate;
 
 /** JMS based implementation of SLC Agent. */
-public class JmsAgent implements SlcAgent, InitializingBean, DisposableBean {
+public class JmsAgent extends AbstractAgent implements SlcAgent, InitializingBean, DisposableBean {
        private final static Log log = LogFactory.getLog(JmsAgent.class);
 
        private final SlcAgentDescriptor agentDescriptor;
@@ -60,7 +61,8 @@ public class JmsAgent implements SlcAgent, InitializingBean, DisposableBean {
        }
 
        public void newExecution(SlcExecution slcExecution) {
-               log.info("Would execute SlcExecution :" + slcExecution);
+               log.info("Execute SlcExecution :" + slcExecution);
+               runSlcExecution(slcExecution);
        }
 
        public void setJmsTemplate(JmsTemplate jmsTemplate) {
index 32aa12fe03a9428133d5f82390cb4768df17c4bd..3802ecc04dd559e81b586615a6590733e1d0b4cf 100644 (file)
@@ -5,37 +5,17 @@
        http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"
        default-lazy-init="false">
 
-       <bean
-               class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
-               lazy-init="false">
-               <property name="properties">
-                       <props>
-                               <prop key="org.apache.activemq.brokerURL">
-                                       tcp://localhost:61616
-                </prop>
-                       </props>
-               </property>
-               <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
-               <property name="ignoreUnresolvablePlaceholders" value="true" />
-       </bean>
-
        <import resource="classpath:org/argeo/slc/activemq/spring.xml" />
 
-       <bean id="slcDefault.jms.agent" class="org.argeo.slc.jms.JmsAgent">
+       <bean id="slcDefault.jms.agent" class="org.argeo.slc.jms.JmsAgent" abstract="true">
                <property name="jmsTemplate" ref="slcDefault.jms.jmsTemplate" />
                <property name="agentRegister" ref="slcJms.destination.agent.register" />
                <property name="agentUnregister" ref="slcJms.destination.agent.unregister" />
        </bean>
 
-       <bean id="slcTemplate.jms.agentListenerContainer.newExecution"
-               parent="slcTemplate.jms.agentListenerContainer">
-               <property name="action" value="newExecution" />
-       </bean>
-
        <!-- Templates -->
        <bean id="slcTemplate.jms.agentListenerContainer" class="org.argeo.slc.jms.AgentListenerContainer"
                abstract="true">
-               <property name="jmsAgent" ref="slcDefault.jms.agent" />
                <property name="connectionFactory" ref="slcDefault.jms.connectionFactory" />
                <property name="messageConverter" ref="slcDefault.jms.castorMessageConverter" />
                <property name="pubSubDomain" value="true" />
index f340db27fad181b5e96a253807c5551d92c985f9..0caa144a0865b9adbf54d03e261fb447498f3676 100644 (file)
@@ -25,7 +25,9 @@ import org.argeo.slc.core.structure.tree.TreeSPath;
 import org.argeo.slc.core.structure.tree.TreeSRegistry;
 import org.argeo.slc.logging.Log4jUtils;
 import org.argeo.slc.process.SlcExecution;
+import org.argeo.slc.runtime.SlcApplication;
 import org.argeo.slc.runtime.SlcExecutionOutput;
+import org.argeo.slc.spring.SpringUtils;
 import org.argeo.slc.structure.StructureRegistry;
 import org.springframework.beans.factory.BeanFactoryUtils;
 import org.springframework.beans.factory.ListableBeanFactory;
@@ -38,7 +40,7 @@ import org.springframework.core.io.Resource;
 import org.springframework.core.io.ResourceLoader;
 import org.springframework.util.SystemPropertyUtils;
 
-public class AntSlcApplication {
+public class AntSlcApplication implements SlcApplication<AntExecutionContext> {
        private final static String DEFAULT_APP_LOG4J_PROPERTIES = "org/argeo/slc/ant/defaultAppLog4j.properties";
 
        private final static Log log = LogFactory.getLog(AntSlcApplication.class);
@@ -50,6 +52,8 @@ public class AntSlcApplication {
        private Resource confDir;
        private File workDir;
 
+       private Resource slcRootFile;
+
        public void execute(SlcExecution slcExecution, Properties properties,
                        Map<String, Object> references,
                        SlcExecutionOutput<AntExecutionContext> executionOutput) {
@@ -421,4 +425,90 @@ public class AntSlcApplication {
                this.parentContext = runtimeContext;
        }
 
+       public void setSlcRootFile(Resource slcRootFile) {
+               this.slcRootFile = slcRootFile;
+       }
+
+       /**
+        * Init all directories based on the SLC root file. TODO: don't override
+        * already specified dirs.
+        */
+       public void initFromSlcRootFile() {
+               // AntSlcApplication application = new AntSlcApplication();
+               InputStream inRootFile = null;
+               try {
+                       // Remove basedir property in order to avoid conflict with Maven
+                       // if (all.containsKey("basedir"))
+                       // all.remove("basedir");
+
+                       inRootFile = slcRootFile.getInputStream();
+                       Properties rootProps = loadFile(inRootFile);
+
+                       Resource confDir = null;
+                       File workDir = null;
+                       // Root dir
+                       final Resource rootDir = SpringUtils.getParent(slcRootFile);
+
+                       // Conf dir
+                       String confDirStr = rootProps
+                                       .getProperty(AntConstants.CONF_DIR_PROPERTY);
+                       if (confDirStr != null)
+                               confDir = new DefaultResourceLoader(getClass().getClassLoader())
+                                               .getResource(confDirStr);
+
+                       if (confDir == null || !confDir.exists()) {
+                               // confDir = rootDir.createRelative("../conf");
+                               confDir = SpringUtils.getParent(rootDir)
+                                               .createRelative("conf/");
+                       }
+
+                       // Work dir
+                       String workDirStr = rootProps
+                                       .getProperty(AntConstants.WORK_DIR_PROPERTY);
+                       if (workDirStr != null) {
+                               workDir = new File(workDirStr);
+                       }
+
+                       if (workDir == null || !workDir.exists()) {
+                               try {
+                                       File rootDirAsFile = rootDir.getFile();
+                                       workDir = new File(rootDirAsFile.getParent()
+                                                       + File.separator + "work").getCanonicalFile();
+                               } catch (IOException e) {
+                                       workDir = new File(System.getProperty("java.io.tmpdir")
+                                                       + File.separator + "slcExecutions" + File.separator
+                                                       + slcRootFile.getURL().getPath());
+                                       log.debug("Root dir is not a file: " + e.getMessage()
+                                                       + ", creating work dir in temp: " + workDir);
+                               }
+                               workDir.mkdirs();
+                       }
+
+                       setConfDir(confDir);
+                       setRootDir(rootDir);
+                       setWorkDir(workDir);
+
+                       if (log.isDebugEnabled())
+                               log.debug("Ant SLC application initialized based on root file "
+                                               + slcRootFile);
+               } catch (IOException e) {
+                       throw new SlcException(
+                                       "Could not prepare SLC application for root file "
+                                                       + slcRootFile, e);
+               } finally {
+                       IOUtils.closeQuietly(inRootFile);
+               }
+       }
+
+       /** Loads the content of a file as <code>Properties</code>. */
+       private Properties loadFile(InputStream in) {
+               Properties p = new Properties();
+               try {
+                       p.load(in);
+               } catch (IOException e) {
+                       throw new SlcException("Cannot read SLC root file", e);
+               }
+               return p;
+       }
+
 }
index b5175967351d9217e8399b2b9223c5e9df25324b..d70b800640ca6280de58c1bc881810f15664e447 100644 (file)
@@ -72,10 +72,16 @@ public class AntSlcRuntime implements SlcRuntime<AntExecutionContext> {
                                        "Could not find any SLC root file, "
                                                        + "please configure one at the root of your scripts hierarchy.");
 
+               // Create SlcExecution from arguments
                SlcExecution slcExecution = createSlcExecution(runtime, slcRootFile,
                                scriptRes, targets);
 
-               AntSlcApplication application = getApplication(slcRootFile);
+               // Init application
+               AntSlcApplication application = new AntSlcApplication();
+               application.setSlcRootFile(slcRootFile);
+               application.initFromSlcRootFile();
+
+               // Execute test
                application.execute(slcExecution, properties, references,
                                executionOutput);
        }
@@ -121,71 +127,6 @@ public class AntSlcRuntime implements SlcRuntime<AntExecutionContext> {
                return slcExecution;
        }
 
-       protected AntSlcApplication getApplication(Resource slcRootFile) {
-               AntSlcApplication application = new AntSlcApplication();
-               InputStream inRootFile = null;
-               try {
-                       // Remove basedir property in order to avoid conflict with Maven
-                       // if (all.containsKey("basedir"))
-                       // all.remove("basedir");
-
-                       inRootFile = slcRootFile.getInputStream();
-                       Properties rootProps = loadFile(inRootFile);
-
-                       Resource confDir = null;
-                       File workDir = null;
-                       // Root dir
-                       final Resource rootDir = SpringUtils.getParent(slcRootFile);
-
-                       // Conf dir
-                       String confDirStr = rootProps
-                                       .getProperty(AntConstants.CONF_DIR_PROPERTY);
-                       if (confDirStr != null)
-                               confDir = new DefaultResourceLoader(application.getClass()
-                                               .getClassLoader()).getResource(confDirStr);
-
-                       if (confDir == null || !confDir.exists()) {
-                               // confDir = rootDir.createRelative("../conf");
-                               confDir = SpringUtils.getParent(rootDir)
-                                               .createRelative("conf/");
-                       }
-
-                       // Work dir
-                       String workDirStr = rootProps
-                                       .getProperty(AntConstants.WORK_DIR_PROPERTY);
-                       if (workDirStr != null) {
-                               workDir = new File(workDirStr);
-                       }
-
-                       if (workDir == null || !workDir.exists()) {
-                               try {
-                                       File rootDirAsFile = rootDir.getFile();
-                                       workDir = new File(rootDirAsFile.getParent()
-                                                       + File.separator + "work").getCanonicalFile();
-                               } catch (IOException e) {
-                                       workDir = new File(System.getProperty("java.io.tmpdir")
-                                                       + File.separator + "slcExecutions" + File.separator
-                                                       + slcRootFile.getURL().getPath());
-                                       log.debug("Root dir is not a file: " + e.getMessage()
-                                                       + ", creating work dir in temp: " + workDir);
-                               }
-                               workDir.mkdirs();
-                       }
-
-                       application.setConfDir(confDir);
-                       application.setRootDir(rootDir);
-                       application.setWorkDir(workDir);
-
-                       return application;
-               } catch (IOException e) {
-                       throw new SlcException(
-                                       "Could not prepare SLC application for root file "
-                                                       + slcRootFile, e);
-               } finally {
-                       IOUtils.closeQuietly(inRootFile);
-               }
-       }
-
        /**
         * Recursively scans directories downwards until it find a file name as
         * defined by {@link #SLC_ROOT_FILE_NAME}.
@@ -213,15 +154,4 @@ public class AntSlcRuntime implements SlcRuntime<AntExecutionContext> {
                                        + currDir, e);
                }
        }
-
-       /** Loads the content of a file as <code>Properties</code>. */
-       private Properties loadFile(InputStream in) {
-               Properties p = new Properties();
-               try {
-                       p.load(in);
-               } catch (IOException e) {
-                       throw new SlcException("Cannot read SLC root file", e);
-               }
-               return p;
-       }
 }
index 2077ec5eb97b97b1a7482286539397d68b740e90..33a389d2a9079e15c865e167cdc1924a9373e6ec 100644 (file)
@@ -163,16 +163,18 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements
        @Override\r
        protected void append(LoggingEvent event) {\r
                Project project = (Project) MDC.get(AntConstants.MDC_ANT_PROJECT);\r
-               if (project == null)\r
-                       throw new SlcException("No Ant project registered in Log4j MDC.");\r
-\r
-               SlcExecution slcExecution = getSlcExecution(project);\r
-               if (currentStepNotified) {\r
-                       slcExecution.getSteps().add(\r
-                                       new SlcExecutionStep(event.getMessage().toString()));\r
-                       currentStepNotified = false;\r
+               if (project == null) {\r
+                       System.err.println("No Ant project registered in Log4j MDC.");\r
                } else {\r
-                       slcExecution.currentStep().addLog(event.getMessage().toString());\r
+                       SlcExecution slcExecution = getSlcExecution(project);\r
+                       if (currentStepNotified) {\r
+                               slcExecution.getSteps().add(\r
+                                               new SlcExecutionStep(event.getMessage().toString()));\r
+                               currentStepNotified = false;\r
+                       } else {\r
+                               slcExecution.currentStep()\r
+                                               .addLog(event.getMessage().toString());\r
+                       }\r
                }\r
        }\r
 \r
diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/runtime/AbstractAgent.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/runtime/AbstractAgent.java
new file mode 100644 (file)
index 0000000..e6b8a38
--- /dev/null
@@ -0,0 +1,34 @@
+package org.argeo.slc.core.runtime;
+
+import java.util.Properties;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.argeo.slc.process.SlcExecution;
+import org.argeo.slc.runtime.SlcApplication;
+import org.argeo.slc.runtime.SlcExecutionContext;
+
+public abstract class AbstractAgent {
+       private final static Log log = LogFactory.getLog(AbstractAgent.class);
+
+       private SlcApplication<SlcExecutionContext> slcApplication;
+
+       protected void runSlcExecution(final SlcExecution slcExecution) {
+               // TODO: in a separate process
+               Thread thread = new Thread("SlcExecution " + slcExecution.getUuid()) {
+                       public void run() {
+                               slcApplication.execute(slcExecution, new Properties(), null,
+                                               null);
+                               log.debug("Thread for SLC execution #" + slcExecution.getUuid()
+                                               + " finished.");
+                       }
+               };
+               thread.start();
+       }
+
+       public void setSlcApplication(
+                       SlcApplication<SlcExecutionContext> application) {
+               this.slcApplication = application;
+       }
+
+}