Introduce JvmProcess task
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 9 Jul 2009 15:28:50 +0000 (15:28 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 9 Jul 2009 15:28:50 +0000 (15:28 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2716 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

demo/pom.xml
demo/site/org.argeo.slc.demo.detached/META-INF/spring/detached.xml
demo/site/org.argeo.slc.demo.detached/META-INF/spring/launch.xml [new file with mode: 0644]
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java [new file with mode: 0644]
runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java
runtime/org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java

index b6ac6b4564f91b4492f0d2a9e05abd11b9a528a2..6a6b21dbc68327bfdc845a83a47cd4c3bf100fef 100644 (file)
                </dependency>
 
                <!--
-                       Unit Tests <dependency> <groupId>org.argeo.slc.runtime</groupId>
-                       <artifactId>org.argeo.slc.unit</artifactId> <scope>test</scope>
-                       </dependency>
-               -->
+                       Unit Tests -->
+               <dependency>
+                       <groupId>org.argeo.slc.runtime</groupId>
+                       <artifactId>org.argeo.slc.unit</artifactId>
+                       <scope>test</scope>
+               </dependency>
+
 
        </dependencies>
 
index add860c2b78aad11b86aa4565b72f339fc143623..6e13d18330f18123e65f1ed890040f2e8b1237c2 100644 (file)
@@ -8,7 +8,7 @@
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
 
        <bean id="detachedTest" parent="slcTemplate.simpleFlow">
-               <property name="path" value="/autoui/testcases" />
+               <property name="path" value="/detached" />
                <property name="executables">
                        <list>
                                <bean parent="task.echo" scope="execution">
diff --git a/demo/site/org.argeo.slc.demo.detached/META-INF/spring/launch.xml b/demo/site/org.argeo.slc.demo.detached/META-INF/spring/launch.xml
new file mode 100644 (file)
index 0000000..c0ec16a
--- /dev/null
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+       xmlns:aop="http://www.springframework.org/schema/aop"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
+       http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
+       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
+
+       <bean id="launch" parent="slcTemplate.simpleFlow">
+               <constructor-arg>
+                       <bean parent="slcTemplate.simpleSpec">
+                               <property name="attributes">
+                                       <map>
+                                               <entry key="spartaDist">
+                                                       <bean parent="specAttr.primitive"
+                                                               p:value="${user.home}/Desktop/sparta-dist-0.9.3-SNAPSHOT" />
+                                               </entry>
+                                               <entry key="detachedLauncherJar">
+                                                       <bean parent="specAttr.primitive"
+                                                               p:value="${user.home}/.m2/repository/org/argeo/slc/runtime/org.argeo.slc.detached.launcher/0.11.4-SNAPSHOT/org.argeo.slc.detached.launcher-0.11.4-SNAPSHOT.jar" />
+                                               </entry>
+                                       </map>
+                               </property>
+                       </bean>
+               </constructor-arg>
+               <property name="executables">
+                       <list>
+
+                               <bean parent="task.echo" p:message="osgi.install.area=${osgi.install.area}" />
+                               <bean parent="task.echo" p:message="osgi.framework=${osgi.framework}" />
+
+                               <ref local="jvmProcess" />
+                       </list>
+               </property>
+       </bean>
+
+       <bean id="jvmProcess" class="org.argeo.slc.core.execution.tasks.JvmProcess">
+               <property name="mainClass" value="org.argeo.slc.detached.launcher.Main" />
+               <property name="classpath">
+                       <list>
+                               <value>${osgi.framework}</value>
+                               <value>file:@{detachedLauncherJar}</value>
+                       </list>
+               </property>
+       </bean>
+
+</beans>
\ No newline at end of file
diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java
new file mode 100644 (file)
index 0000000..ba1d67e
--- /dev/null
@@ -0,0 +1,134 @@
+package org.argeo.slc.core.execution.tasks;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.exec.CommandLine;
+import org.argeo.slc.SlcException;
+import org.springframework.core.io.Resource;
+
+public class JvmProcess extends SystemCall {
+       private Properties systemProperties = new Properties();
+       private List<Resource> classpath = new ArrayList<Resource>();
+       private List<Resource> pBootClasspath = new ArrayList<Resource>();
+       private Resource jvm = null;
+       private String mainClass;
+       private List<String> jvmArgs = new ArrayList<String>();
+       private List<String> args = new ArrayList<String>();
+
+       @Override
+       protected CommandLine createCommandLine() {
+               final CommandLine cl;
+               if (jvm != null)
+                       cl = new CommandLine(asFile(jvm));
+               else
+                       cl = new CommandLine("java");
+
+               if (pBootClasspath.size() > 0) {
+                       StringBuffer buf = new StringBuffer("-Xbootclasspath/p:");
+                       for (Resource res : pBootClasspath) {
+                               if (buf.length() != 0)
+                                       buf.append(File.pathSeparatorChar);
+                               buf.append(asFile(res));
+                       }
+                       cl.addArgument(buf.toString());
+               }
+
+               for (String jvmArg : jvmArgs) {
+                       cl.addArgument(jvmArg);
+               }
+
+               cl.addArgument("-cp");
+               StringBuffer buf = new StringBuffer("");
+               for (Resource res : classpath) {
+                       if (buf.length() != 0)
+                               buf.append(File.pathSeparatorChar);
+                       buf.append(asFile(res));
+               }
+               cl.addArgument(buf.toString());
+
+               for (Map.Entry<Object, Object> entry : systemProperties.entrySet()) {
+                       cl.addArgument("-D" + entry.getKey() + "=" + entry.getValue());
+               }
+
+               // Program
+               cl.addArgument(mainClass);
+
+               for (String arg : args) {
+                       cl.addArgument(arg);
+               }
+
+               return cl;
+       }
+
+       protected File asFile(Resource res) {
+               // TODO: implements local copy
+               try {
+                       return res.getFile().getCanonicalFile();
+               } catch (IOException e) {
+                       throw new SlcException("Cannot convert resource to file", e);
+               }
+
+       }
+
+       public Properties getSystemProperties() {
+               return systemProperties;
+       }
+
+       public void setSystemProperties(Properties systemProperties) {
+               this.systemProperties = systemProperties;
+       }
+
+       public List<Resource> getClasspath() {
+               return classpath;
+       }
+
+       public void setClasspath(List<Resource> classpath) {
+               this.classpath = classpath;
+       }
+
+       public List<Resource> getPBootClasspath() {
+               return pBootClasspath;
+       }
+
+       public void setPBootClasspath(List<Resource> bootClasspath) {
+               pBootClasspath = bootClasspath;
+       }
+
+       public Resource getJvm() {
+               return jvm;
+       }
+
+       public void setJvm(Resource jvm) {
+               this.jvm = jvm;
+       }
+
+       public String getMainClass() {
+               return mainClass;
+       }
+
+       public void setMainClass(String mainClass) {
+               this.mainClass = mainClass;
+       }
+
+       public List<String> getJvmArgs() {
+               return jvmArgs;
+       }
+
+       public void setJvmArgs(List<String> jvmArgs) {
+               this.jvmArgs = jvmArgs;
+       }
+
+       public List<String> getArgs() {
+               return args;
+       }
+
+       public void setArgs(List<String> args) {
+               this.args = args;
+       }
+
+}
index a14e550bf85eb39455fec181ff517e562c395641..f3314ac7f46616b9b84334e1a60e4dec6410d335 100644 (file)
@@ -96,28 +96,17 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable,
                                dir = new File(execDir).getCanonicalFile();
                        }
 
-                       // Check if an OS specific command overrides
-                       String osName = System.getProperty("os.name");
-                       List<Object> commandToUse = null;
-                       if (osCommands.containsKey(osName))
-                               commandToUse = osCommands.get(osName);
-                       else
-                               commandToUse = command;
-                       String cmdToUse = null;
-                       if (osCmds.containsKey(osName))
-                               cmdToUse = osCmds.get(osName);
-                       else
-                               cmdToUse = cmd;
-
                        // Prepare executor
                        if (dir == null)
                                dir = new File(getUsedDir(dir));
                        if (!dir.exists())
                                dir.mkdirs();
 
+                       // Watchdog to check for lost processes
                        Executor executor = new DefaultExecutor();
                        executor.setWatchdog(new ExecuteWatchdog(watchdogTimeout));
 
+                       // Redirect standard streams
                        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(
                                        new LogOutputStream() {
                                                protected void processLine(String line, int level) {
@@ -135,34 +124,9 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable,
                        executor.setStreamHandler(pumpStreamHandler);
                        executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
                        executor.setWorkingDirectory(dir);
-                       final CommandLine commandLine;
-
-                       // Which command definition to use
-                       if (commandToUse == null && cmdToUse == null)
-                               throw new SlcException("Please specify a command.");
-                       else if (commandToUse != null && cmdToUse != null)
-                               throw new SlcException(
-                                               "Specify the command either as a line or as a list.");
-                       else if (cmdToUse != null) {
-                               if (log.isTraceEnabled())
-                                       log.trace("Execute '" + cmdToUse + "' in "
-                                                       + getUsedDir(dir));
-
-                               commandLine = CommandLine.parse(cmdToUse);
-                       } else if (commandToUse != null) {
-                               if (log.isTraceEnabled())
-                                       log.trace("Execute '" + commandToUse + "' in "
-                                                       + getUsedDir(dir));
-                               if (commandToUse.size() == 0)
-                                       throw new SlcException("Command line is empty.");
-
-                               commandLine = new CommandLine(commandToUse.get(0).toString());
-                               for (int i = 1; i < commandToUse.size(); i++)
-                                       commandLine.addArgument(commandToUse.get(i).toString());
-                       } else {
-                               // all cases covered previously
-                               throw new UnsupportedOperationException();
-                       }
+
+                       // Command line to use
+                       final CommandLine commandLine = createCommandLine();
 
                        // Env variables
                        Map<String, String> environmentVariablesToUse = environmentVariables
@@ -216,6 +180,45 @@ public class SystemCall extends TreeSRelatedHelper implements Runnable,
 
        }
 
+       /** Can be overridden by specific command wrapper*/
+       protected CommandLine createCommandLine() {
+               // Check if an OS specific command overrides
+               String osName = System.getProperty("os.name");
+               List<Object> commandToUse = null;
+               if (osCommands.containsKey(osName))
+                       commandToUse = osCommands.get(osName);
+               else
+                       commandToUse = command;
+               String cmdToUse = null;
+               if (osCmds.containsKey(osName))
+                       cmdToUse = osCmds.get(osName);
+               else
+                       cmdToUse = cmd;
+
+               final CommandLine commandLine;
+
+               // Which command definition to use
+               if (commandToUse == null && cmdToUse == null)
+                       throw new SlcException("Please specify a command.");
+               else if (commandToUse != null && cmdToUse != null)
+                       throw new SlcException(
+                                       "Specify the command either as a line or as a list.");
+               else if (cmdToUse != null) {
+                       commandLine = CommandLine.parse(cmdToUse);
+               } else if (commandToUse != null) {
+                       if (commandToUse.size() == 0)
+                               throw new SlcException("Command line is empty.");
+
+                       commandLine = new CommandLine(commandToUse.get(0).toString());
+                       for (int i = 1; i < commandToUse.size(); i++)
+                               commandLine.addArgument(commandToUse.get(i).toString());
+               } else {
+                       // all cases covered previously
+                       throw new UnsupportedOperationException();
+               }
+               return commandLine;
+       }
+
        /**
         * Shortcut method returning the current exec dir if the specified one is
         * null.
index 3fc42326fb766040f49a6932c11724a3b4f4bca2..fca2a03872970a301a6471a8b14899a6add965ff 100644 (file)
@@ -40,11 +40,7 @@ public class Main {
                        startApp(config);
 
                        // Start OSGi framework
-                       try {
-                               startEquinox(config);
-                       } catch (Exception e) {
-                               e.printStackTrace();
-                       }
+                       startEquinox(config);
                        info("Argeo SLC Detached launcher started.");
                } catch (Exception e) {
                        e.printStackTrace();