From 61dd39a53c7635693b520e7e26583a5bde50430e Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Mon, 19 Jan 2009 11:47:20 +0000 Subject: [PATCH] Introduce remote launching of agents git-svn-id: https://svn.argeo.org/slc/trunk@2105 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- examples/org.argeo.slc.example01/pom.xml | 45 +++++++++ runtime/org.argeo.slc.launcher/pom.xml | 20 ---- .../main/java/org/argeo/slc/cli/SlcMain.java | 2 +- .../argeo/slc/cli/spring-agent-default.xml | 41 +++++++++ .../org/argeo/slc/runtime/SlcApplication.java | 12 +++ .../main/java/org/argeo/slc/jms/JmsAgent.java | 6 +- .../org/argeo/slc/activemq/spring-agent.xml | 22 +---- .../org/argeo/slc/ant/AntSlcApplication.java | 92 ++++++++++++++++++- .../java/org/argeo/slc/ant/AntSlcRuntime.java | 84 ++--------------- .../slc/ant/SlcExecutionBuildListener.java | 20 ++-- .../argeo/slc/core/runtime/AbstractAgent.java | 34 +++++++ 11 files changed, 247 insertions(+), 131 deletions(-) create mode 100644 runtime/org.argeo.slc.launcher/src/main/resources/org/argeo/slc/cli/spring-agent-default.xml create mode 100644 runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/runtime/SlcApplication.java create mode 100644 runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/runtime/AbstractAgent.java diff --git a/examples/org.argeo.slc.example01/pom.xml b/examples/org.argeo.slc.example01/pom.xml index ab4876737..2bbd3e32f 100644 --- a/examples/org.argeo.slc.example01/pom.xml +++ b/examples/org.argeo.slc.example01/pom.xml @@ -84,6 +84,28 @@ + + org.codehaus.mojo + exec-maven-plugin + 1.1-beta-1 + + org.argeo.slc.cli.SlcMain + + + slc.runtime + ws + + + slc.rootFile + file:${basedir}/src/slc/root/slcRoot.properties + + + + --mode + agent + + + @@ -114,5 +136,28 @@ 1.1.1 testing + + + + org.argeo.slc.runtime + org.argeo.slc.launcher + testing + + + org.argeo.slc.runtime + org.argeo.slc.support.activemq + testing + + + org.argeo.slc.runtime + org.argeo.slc.support.ant + testing + + + org.argeo.slc.runtime + org.argeo.slc.support.castor + testing + + diff --git a/runtime/org.argeo.slc.launcher/pom.xml b/runtime/org.argeo.slc.launcher/pom.xml index bf69adb28..364223c10 100644 --- a/runtime/org.argeo.slc.launcher/pom.xml +++ b/runtime/org.argeo.slc.launcher/pom.xml @@ -60,14 +60,6 @@ - - org.codehaus.mojo - exec-maven-plugin - 1.1-beta-1 - - org.argeo.slc.cli.SlcMain - - @@ -81,17 +73,5 @@ org.argeo.dep.osgi.commons.cli - - - org.argeo.slc.runtime - org.argeo.slc.support.activemq - testing - - - org.argeo.slc.runtime - org.argeo.slc.support.castor - testing - - \ No newline at end of file diff --git a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java index 8db1330a6..04caaab9e 100644 --- a/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java +++ b/runtime/org.argeo.slc.launcher/src/main/java/org/argeo/slc/cli/SlcMain.java @@ -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 index 000000000..a61538cea --- /dev/null +++ b/runtime/org.argeo.slc.launcher/src/main/resources/org/argeo/slc/cli/spring-agent-default.xml @@ -0,0 +1,41 @@ + + + + + + + + + tcp://localhost:61616 + + + + + + + + + + + + + + + + + + + + + + + + \ 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 index 000000000..7324562c2 --- /dev/null +++ b/runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/runtime/SlcApplication.java @@ -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 { + public void execute(SlcExecution slcExecution, Properties properties, + Map references, + SlcExecutionOutput executionOutput); +} diff --git a/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAgent.java b/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAgent.java index 66c0718fc..099498caa 100644 --- a/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAgent.java +++ b/runtime/org.argeo.slc.support.activemq/src/main/java/org/argeo/slc/jms/JmsAgent.java @@ -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) { diff --git a/runtime/org.argeo.slc.support.activemq/src/main/resources/org/argeo/slc/activemq/spring-agent.xml b/runtime/org.argeo.slc.support.activemq/src/main/resources/org/argeo/slc/activemq/spring-agent.xml index 32aa12fe0..3802ecc04 100644 --- a/runtime/org.argeo.slc.support.activemq/src/main/resources/org/argeo/slc/activemq/spring-agent.xml +++ b/runtime/org.argeo.slc.support.activemq/src/main/resources/org/argeo/slc/activemq/spring-agent.xml @@ -5,37 +5,17 @@ http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd" default-lazy-init="false"> - - - - - tcp://localhost:61616 - - - - - - - - + - - - - - diff --git a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcApplication.java b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcApplication.java index f340db27f..0caa144a0 100644 --- a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcApplication.java +++ b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcApplication.java @@ -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 { 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 references, SlcExecutionOutput 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 Properties. */ + 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; + } + } diff --git a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java index b51759673..d70b80064 100644 --- a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java +++ b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/AntSlcRuntime.java @@ -72,10 +72,16 @@ public class AntSlcRuntime implements SlcRuntime { "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 { 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 { + currDir, e); } } - - /** Loads the content of a file as Properties. */ - 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; - } } diff --git a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java index 2077ec5eb..33a389d2a 100644 --- a/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java +++ b/runtime/org.argeo.slc.support.ant/src/main/java/org/argeo/slc/ant/SlcExecutionBuildListener.java @@ -163,16 +163,18 @@ public class SlcExecutionBuildListener extends AppenderSkeleton implements @Override protected void append(LoggingEvent event) { Project project = (Project) MDC.get(AntConstants.MDC_ANT_PROJECT); - if (project == null) - throw new SlcException("No Ant project registered in Log4j MDC."); - - SlcExecution slcExecution = getSlcExecution(project); - if (currentStepNotified) { - slcExecution.getSteps().add( - new SlcExecutionStep(event.getMessage().toString())); - currentStepNotified = false; + if (project == null) { + System.err.println("No Ant project registered in Log4j MDC."); } else { - slcExecution.currentStep().addLog(event.getMessage().toString()); + SlcExecution slcExecution = getSlcExecution(project); + if (currentStepNotified) { + slcExecution.getSteps().add( + new SlcExecutionStep(event.getMessage().toString())); + currentStepNotified = false; + } else { + slcExecution.currentStep() + .addLog(event.getMessage().toString()); + } } } 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 index 000000000..e6b8a3845 --- /dev/null +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/runtime/AbstractAgent.java @@ -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 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 application) { + this.slcApplication = application; + } + +} -- 2.39.2