From: Mathieu Baudier Date: Thu, 9 Jul 2009 15:28:50 +0000 (+0000) Subject: Introduce JvmProcess task X-Git-Tag: argeo-slc-2.1.7~1669 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=09ab1aca27488e1feef6c8f46b34b7d27284be9a;p=gpl%2Fargeo-slc.git Introduce JvmProcess task git-svn-id: https://svn.argeo.org/slc/trunk@2716 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/demo/pom.xml b/demo/pom.xml index b6ac6b456..6a6b21dbc 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -71,10 +71,13 @@ + Unit Tests --> + + org.argeo.slc.runtime + org.argeo.slc.unit + test + + diff --git a/demo/site/org.argeo.slc.demo.detached/META-INF/spring/detached.xml b/demo/site/org.argeo.slc.demo.detached/META-INF/spring/detached.xml index add860c2b..6e13d1833 100644 --- a/demo/site/org.argeo.slc.demo.detached/META-INF/spring/detached.xml +++ b/demo/site/org.argeo.slc.demo.detached/META-INF/spring/detached.xml @@ -8,7 +8,7 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> - + 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 index 000000000..c0ec16a57 --- /dev/null +++ b/demo/site/org.argeo.slc.demo.detached/META-INF/spring/launch.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + ${osgi.framework} + file:@{detachedLauncherJar} + + + + + \ 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 index 000000000..ba1d67eb5 --- /dev/null +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/JvmProcess.java @@ -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 classpath = new ArrayList(); + private List pBootClasspath = new ArrayList(); + private Resource jvm = null; + private String mainClass; + private List jvmArgs = new ArrayList(); + private List args = new ArrayList(); + + @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 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 getClasspath() { + return classpath; + } + + public void setClasspath(List classpath) { + this.classpath = classpath; + } + + public List getPBootClasspath() { + return pBootClasspath; + } + + public void setPBootClasspath(List 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 getJvmArgs() { + return jvmArgs; + } + + public void setJvmArgs(List jvmArgs) { + this.jvmArgs = jvmArgs; + } + + public List getArgs() { + return args; + } + + public void setArgs(List args) { + this.args = args; + } + +} diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java index a14e550bf..f3314ac7f 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/tasks/SystemCall.java @@ -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 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 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 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. diff --git a/runtime/org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java b/runtime/org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java index 3fc42326f..fca2a0387 100644 --- a/runtime/org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java +++ b/runtime/org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java @@ -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();