Author: lhein
Date: Thu May 7 19:47:09 2009
New Revision: 772751
URL: http://svn.apache.org/viewvc?rev=772751&view=rev
Log:
did some tweaks for SMXCOMP-491
Modified:
servicemix/components/engines/servicemix-exec/trunk/ (props changed)
servicemix/components/engines/servicemix-exec/trunk/pom.xml
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java
Propchange: servicemix/components/engines/servicemix-exec/trunk/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Thu May 7 19:47:09 2009
@@ -0,0 +1,5 @@
+target
+
+.project
+
+.classpath
Modified: servicemix/components/engines/servicemix-exec/trunk/pom.xml
URL:
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/pom.xml?rev=772751&r1=772750&r2=772751&view=diff
==============================================================================
--- servicemix/components/engines/servicemix-exec/trunk/pom.xml (original)
+++ servicemix/components/engines/servicemix-exec/trunk/pom.xml Thu May 7
19:47:09 2009
@@ -1,146 +1,149 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
-
-<!--
-
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-
- <modelVersion>4.0.0</modelVersion>
-
- <parent>
- <groupId>org.apache.servicemix</groupId>
- <artifactId>components-pom</artifactId>
- <version>4-SNAPSHOT</version>
- </parent>
-
- <groupId>org.apache.servicemix</groupId>
- <artifactId>servicemix-exec</artifactId>
- <version>2009.02-SNAPSHOT</version>
- <packaging>jbi-component</packaging>
-
- <name>ServiceMix :: Exec</name>
- <description>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or
more
+ contributor license agreements. See the NOTICE file distributed
with
+ this work for additional information regarding copyright
ownership.
+ The ASF licenses this file to You under the Apache License,
Version
+ 2.0 (the "License"); you may not use this file except in
compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+ applicable law or agreed to in writing, software distributed
under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
OR
+ CONDITIONS OF ANY KIND, either express or implied. See the
License for
+ the specific language governing permissions and limitations
under the
+ License.
+ -->
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.servicemix</groupId>
+ <artifactId>components-pom</artifactId>
+ <version>4-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.apache.servicemix</groupId>
+ <artifactId>servicemix-exec</artifactId>
+ <version>2009.02-SNAPSHOT</version>
+ <packaging>jbi-component</packaging>
+
+ <name>ServiceMix :: Exec</name>
+ <description>
A exec ServiceEngine component that launch a command using data into
the incoming normalized message and return the command output in a new
normalized message.
</description>
-
- <properties>
- <servicemix-version>3.3</servicemix-version>
- <servicemix-shared-version>2009.02-SNAPSHOT</servicemix-shared-version>
-
- <servicemix.osgi.import>
- org.apache.servicemix;resolution:=optional,
- org.apache.servicemix.client;resolution:=optional,
- org.apache.servicemix.common,
- org.apache.servicemix.common.osgi,
- org.apache.servicemix.components*;resolution:=optional,
- org.apache.servicemix.executors.impl,
- org.apache.servicemix.expression*;resolution:=optional,
- org.apache.servicemix.jbi*;resolution:=optional,
- org.apache.xbean.spring.context.v2,
- org.springframework.beans.factory.xml,
- sun.misc;resolution:=optional,
- *
+
+ <properties>
+ <servicemix-version>3.3</servicemix-version>
+
<servicemix-shared-version>2009.02-SNAPSHOT</servicemix-shared-version>
+
+ <servicemix.osgi.import>
+ org.apache.servicemix;resolution:=optional,
+ org.apache.servicemix.client;resolution:=optional,
+ org.apache.servicemix.common,
+ org.apache.servicemix.common.osgi,
+ org.apache.servicemix.components*;resolution:=optional,
+ org.apache.servicemix.executors.impl,
+ org.apache.servicemix.expression*;resolution:=optional,
+ org.apache.servicemix.jbi*;resolution:=optional,
+ org.apache.xbean.spring.context.v2,
+ org.springframework.beans.factory.xml,
+ sun.misc;resolution:=optional,
+ *
</servicemix.osgi.import>
- <servicemix.osgi.export>
- org.apache.servicemix.exec*;version=${project.version},
-
META-INF.services.org.apache.xbean.spring.exec.servicemix.apache.org.exec
+ <servicemix.osgi.export>
+ org.apache.servicemix.exec*;version=${project.version},
+
META-INF.services.org.apache.xbean.spring.http.servicemix.apache.org.exec
</servicemix.osgi.export>
- <servicemix.osgi.bundles>
- org.apache.servicemix.bundles.jaxrp-ri,
- org.springframework.core,
- org.springframework.beans,
- org.springframework.context,
+ <servicemix.osgi.bundles>
+ org.springframework.core,
+ org.springframework.beans,
+ org.springframework.context,
</servicemix.osgi.bundles>
- </properties>
-
- <scm>
-
<connection>scm:svn:http://svn.apache.org/repos/asf/servicemix/components/engines/servicemix-exec/trunk</connection>
-
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/servicemix/components/engines/servicemix-exec/trunk</developerConnection>
-
<url>http://svn.apache.org/viewcvs.cgi/servicemix/components/engines/servicemix-exec/trunk</url>
- </scm>
-
- <dependencies>
- <dependency>
- <groupId>org.apache.servicemix</groupId>
- <artifactId>servicemix-shared</artifactId>
- <version>${servicemix-shared-version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.servicemix.specs</groupId>
- <artifactId>org.apache.servicemix.specs.jbi-api-1.0</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.geronimo.specs</groupId>
- <artifactId>geronimo-jta_1.1_spec</artifactId>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- </dependency>
- <!-- Testing stuff -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- <dependency>
- <groupId>org.apache.servicemix</groupId>
- <artifactId>servicemix-core</artifactId>
- <version>${servicemix-version}</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.servicemix.tooling</groupId>
- <artifactId>jbi-maven-plugin</artifactId>
- <extensions>true</extensions>
- <configuration>
- <type>service-engine</type>
-
<component>org.apache.servicemix.exec.ExecComponent</component>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.xbean</groupId>
- <artifactId>maven-xbean-plugin</artifactId>
- <executions>
- <execution>
- <goals>
- <goal>mapping</goal>
- </goals>
- <configuration>
-
<namespace>http://servicemix.apache.org/exec/1.0</namespace>
- </configuration>
- </execution>
- </executions>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <forkMode>always</forkMode>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
+ </properties>
+
+ <scm>
+
<connection>scm:svn:http://svn.apache.org/repos/asf/servicemix/components/engines/servicemix-exec/trunk</connection>
+
<developerConnection>scm:svn:https://svn.apache.org/repos/asf/servicemix/components/engines/servicemix-exec/trunk</developerConnection>
+
<url>http://svn.apache.org/viewcvs.cgi/servicemix/components/engines/servicemix-exec/trunk</url>
+ </scm>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.servicemix</groupId>
+ <artifactId>servicemix-shared</artifactId>
+ <version>${servicemix-shared-version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicemix.specs</groupId>
+
<artifactId>org.apache.servicemix.specs.jbi-api-1.0</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.geronimo.specs</groupId>
+ <artifactId>geronimo-jta_1.1_spec</artifactId>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </dependency>
+ <!-- Testing stuff -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.servicemix</groupId>
+ <artifactId>servicemix-core</artifactId>
+ <version>${servicemix-version}</version>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-jmx</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.servicemix.tooling</groupId>
+ <artifactId>jbi-maven-plugin</artifactId>
+ <extensions>true</extensions>
+ <configuration>
+ <type>service-engine</type>
+
<component>org.apache.servicemix.exec.ExecComponent</component>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.xbean</groupId>
+ <artifactId>maven-xbean-plugin</artifactId>
+ <executions>
+ <execution>
+ <goals>
+ <goal>mapping</goal>
+ </goals>
+ <configuration>
+
<namespace>http://servicemix.apache.org/exec/1.0</namespace>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <forkMode>always</forkMode>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
</project>
\ No newline at end of file
Modified:
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
URL:
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java?rev=772751&r1=772750&r2=772751&view=diff
==============================================================================
---
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
(original)
+++
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/ExecEndpoint.java
Thu May 7 19:47:09 2009
@@ -36,123 +36,138 @@
*/
public class ExecEndpoint extends ProviderEndpoint {
- private String command; // the command can be static (define in the
- // descriptor) or provided in the incoming message
- private ExecMarshalerSupport marshaler = new DefaultExecMarshaler(); //
the marshaler that parse the in message
-
- public String getCommand() {
- return command;
- }
-
- /**
- * <p>
- * This attribute specifies the default command to use if no is provided
- * in the incoming message.
- * </p>
- * <i> The default value is <code>null</code>.
- *
- * @param command
- */
- public void setCommand(String command) {
- this.command = command;
- }
-
- public ExecMarshalerSupport getMarshaler() {
- return marshaler;
- }
-
- /**
- * <p>
- * With this method you can specify a marshaler class which provides the
- * logic for converting a message into a execution command. This class
- * has to implement the interface class <code>ExecMarshalerSupport</code>.
- * If you don't specify a marshaler, the <code>DefaultExecMarshaler</code>
- * will be used.
- * </p>
- *
- * @param marshaler a <code>ExecMarshalerSupport</code> class representing
the
- * marshaler.
- */
- public void setMarshaler(ExecMarshalerSupport marshaler) {
- this.marshaler = marshaler;
- }
-
- /*
- * (non-Javadoc)
- * @see
org.apache.servicemix.common.endpoints.ProviderEndpoint#process(javax.jbi.messaging.MessageExchange)
- */
- @Override
- public void process(MessageExchange exchange) throws Exception {
- // The component acts as a provider, this means that another component
- // has requested our service
- // As this exchange is active, this is either an in or a fault (out are
- // sent by this component)
- if (exchange.getStatus() == ExchangeStatus.DONE) {
- // exchange is finished
- return;
- } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
- // exchange has been aborted with an exception
- return;
- } else {
- // exchange is active
- this.handleProviderExchange(exchange);
- }
- }
-
- /**
- * <p>
- * Handles on the message exchange (provider role).
- * </p>
- *
- * @param exchange
- * the <code>MessageExchange</code>.
- */
- protected void handleProviderExchange(MessageExchange exchange) throws
Exception {
- // fault message
- if (exchange.getFault() != null) {
- done(exchange);
- } else if (exchange.getMessage("in") != null) {
- // in message presents
- if (logger.isDebugEnabled()) {
- logger.debug("Received exchange: " + exchange);
- }
- // gets the in message
- NormalizedMessage in = exchange.getMessage("in");
- // parses the in message and get the execution command
- String exec = marshaler.constructExecCommand(in);
- if (exec == null || exec.trim().length() < 1) {
- exec = command;
- }
- if (exec == null || exec.trim().length() < 1) {
- throw new MessagingException("No command to execute.");
- }
- // executes the command
- String output = ExecUtils.execute(exec);
- if (exchange instanceof InOut) {
- // pushes the execution output in out message
- NormalizedMessage out = exchange.createMessage();
- out.setContent(new StringSource("<output>" + output +
"</output>"));
- exchange.setMessage(out, "out");
- send(exchange);
- } else {
- done(exchange);
- }
- } else {
- if (command != null) {
- // executes the user-defined command
- String output = ExecUtils.execute(command);
- if (exchange instanceof InOut) {
- NormalizedMessage out = exchange.createMessage();
- out.setContent(new
StringSource(marshaler.formatExecutionOutput(output)));
- exchange.setMessage(out, "out");
- send(exchange);
- } else {
- done(exchange);
- }
- } else {
- throw new IllegalStateException("Provider exchange is ACTIVE,
but no fault or command is provided.");
- }
- }
- }
-
+ private String command; // the command can be static (define in the
+ // descriptor) or provided in the incoming message
+ private ExecMarshalerSupport marshaler = new DefaultExecMarshaler(); //
the
+
//
marshaler
+
// that
+
// parse
+
// the
+
// in
+
//
message
+
+ public String getCommand() {
+ return command;
+ }
+
+ /**
+ * <p>
+ * This attribute specifies the default command to use if no is
provided in
+ * the incoming message.
+ * </p>
+ * <i> The default value is <code>null</code>.
+ *
+ * @param command
+ */
+ public void setCommand(String command) {
+ this.command = command;
+ }
+
+ public ExecMarshalerSupport getMarshaler() {
+ return marshaler;
+ }
+
+ /**
+ * <p>
+ * With this method you can specify a marshaler class which provides the
+ * logic for converting a message into a execution command. This class
has
+ * to implement the interface class <code>ExecMarshalerSupport</code>.
If
+ * you don't specify a marshaler, the <code>DefaultExecMarshaler</code>
will
+ * be used.
+ * </p>
+ *
+ * @param marshaler
+ * a <code>ExecMarshalerSupport</code> class representing the
+ * marshaler.
+ */
+ public void setMarshaler(ExecMarshalerSupport marshaler) {
+ this.marshaler = marshaler;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.apache.servicemix.common.endpoints.ProviderEndpoint#process(javax
+ * .jbi.messaging.MessageExchange)
+ */
+ @Override
+ public void process(MessageExchange exchange) throws Exception {
+ // The component acts as a provider, this means that another
component
+ // has requested our service
+ // As this exchange is active, this is either an in or a fault
(out are
+ // sent by this component)
+ if (exchange.getStatus() == ExchangeStatus.DONE) {
+ // exchange is finished
+ return;
+ } else if (exchange.getStatus() == ExchangeStatus.ERROR) {
+ // exchange has been aborted with an exception
+ return;
+ } else {
+ // exchange is active
+ this.handleProviderExchange(exchange);
+ }
+ }
+
+ /**
+ * <p>
+ * Handles on the message exchange (provider role).
+ * </p>
+ *
+ * @param exchange
+ * the <code>MessageExchange</code>.
+ */
+ protected void handleProviderExchange(MessageExchange exchange)
+ throws Exception {
+ // fault message
+ if (exchange.getFault() != null) {
+ done(exchange);
+ return;
+ } else {
+ // prepare the buffers
+ StringBuffer output = new StringBuffer();
+ StringBuffer error = new StringBuffer();
+
+ String exec = null;
+
+ // try to extract the command from the in message
content
+ if (exchange.getMessage("in") != null) {
+ // in message presents
+ if (logger.isDebugEnabled()) {
+ logger.debug("Received exchange: " +
exchange);
+ }
+ // gets the in message
+ NormalizedMessage in =
exchange.getMessage("in");
+ // parses the in message and get the execution
command
+ exec = marshaler.constructExecCommand(in);
+ }
+
+ // fall back to static command if extracted is null or
empty
+ if (exec == null || exec.trim().length() < 1) {
+ exec = command;
+ }
+
+ // if even the fall back is empty then we can't do
anything
+ if (exec == null || exec.trim().length() < 1) {
+ throw new MessagingException("No command to
execute.");
+ }
+
+ // execute the command
+ int exitValue = ExecUtils.execute(exec, output, error);
+
+ // prepare the output
+ String result =
marshaler.formatExecutionResult(exitValue, output
+ .toString(), error.toString());
+
+ if (exchange instanceof InOut) {
+ // pushes the execution output in out message
+ NormalizedMessage out =
exchange.createMessage();
+ out.setContent(new StringSource(result));
+ exchange.setMessage(out, "out");
+ send(exchange);
+ } else {
+ done(exchange);
+ }
+ }
+ }
}
Modified:
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
URL:
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java?rev=772751&r1=772750&r2=772751&view=diff
==============================================================================
---
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
(original)
+++
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/DefaultExecMarshaler.java
Thu May 7 19:47:09 2009
@@ -30,8 +30,14 @@
*/
public class DefaultExecMarshaler implements ExecMarshalerSupport {
- private static final String TAG_COMMAND = "command";
- private static final String TAG_ARGUMENT = "argument";
+ public static final String TAG_COMMAND = "command";
+ public static final String TAG_ARGUMENT = "argument";
+ public static final String TAG_RESULT = "result";
+ public static final String TAG_EXITCODE = "exitcode";
+ public static final String TAG_OUTPUT = "output";
+ public static final String TAG_ERROR = "error";
+
+ public static final String RESULT_FORMAT =
"<%s><%s>%d</%s><%s><![CDATA[%s]]></%s><%s><![CDATA[%s]]></%s></%s>";
/*
* (non-Javadoc)
@@ -67,12 +73,26 @@
return execString;
}
- /*
- * (non-Javadoc)
- * @see
org.apache.servicemix.exec.marshaler.ExecMarshalerSupport#formatExecutionOutput(java.lang.String)
+ /* (non-Javadoc)
+ * @see
org.apache.servicemix.exec.marshaler.ExecMarshalerSupport#formatExecutionResult(int,
java.lang.String, java.lang.String)
*/
- public String formatExecutionOutput(String output) {
- return "<output><![CDATA[" + output + "]></output>";
+ public String formatExecutionResult(int exitValue, String output,
+ String error) {
+
+ String result = String.format(RESULT_FORMAT,
+ TAG_RESULT,
+ TAG_EXITCODE,
+ exitValue,
+ TAG_EXITCODE,
+ TAG_OUTPUT,
+ output != null ? output : "",
+ TAG_OUTPUT,
+ TAG_ERROR,
+ error != null ? error : "",
+ TAG_ERROR,
+ TAG_RESULT
+ );
+
+ return result;
}
-
}
Modified:
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
URL:
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java?rev=772751&r1=772750&r2=772751&view=diff
==============================================================================
---
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
(original)
+++
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/marshaler/ExecMarshalerSupport.java
Thu May 7 19:47:09 2009
@@ -43,9 +43,10 @@
* Formats the execution command output to be embedded in the exchange out
message.
* </p>
*
+ * @param exitValue the process exit value
* @param output the command execution output.
- * @return the command execution output formatted to be embedded in the
exchagen out message.
+ * @param error the command execution error output.
+ * @return the command execution output formatted to be embedded in the
exchange out message.
*/
- public String formatExecutionOutput(String output);
-
+ public String formatExecutionResult(int exitValue, String output, String
error);
}
Modified:
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java
URL:
http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java?rev=772751&r1=772750&r2=772751&view=diff
==============================================================================
---
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java
(original)
+++
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java
Thu May 7 19:47:09 2009
@@ -30,86 +30,119 @@
* @author jbonofre
*/
public class ExecUtils {
-
- private static final transient Log LOG =
LogFactory.getLog(ExecUtils.class);
-
- /**
- * <p>
- * Executes a system command and return the output buffer.
- * </p>
- *
- * @param command the system command to execute.
- * @return the command execution output buffer.
- * @throws AutoDeployException
- */
- public static String execute(String command) throws ExecException {
- LOG.info("Execute command " + command);
- String[] shellCommand = null;
- LOG.debug("Define the shell.");
- LOG.debug("Get the OS name property.");
- String osName = System.getProperty("os.name");
- if(osName.startsWith("Windows")) {
- LOG.debug("Microsoft Windows platform detected.");
- String comSpec = System.getProperty("ComSpec");
- if(comSpec != null) {
- LOG.debug("The ComSpec MS Windows environment variable is
defined, using it: " + comSpec + " /C " + command);
- shellCommand = new String[]{ comSpec, "/C", command};
- }
- else {
- LOG.debug("The ComSpec MS Windows environment variable is not
defined, found the shell command depending of the MS Windows version.");
- if(osName.startsWith("Windows 3") || osName.startsWith("Windows
95") || osName.startsWith("Windows 98") || osName.startsWith("Windows ME")) {
- LOG.debug("MS Windows 3.1/95/98/Me detected, using:
command.com /C " + command);
- shellCommand = new String[]{ "command.com", "/C", command};
- }
- else {
- LOG.debug("MS Windows NT/XP/Vista detected, using: cmd.exe /C
" + command);
- shellCommand = new String[]{ "cmd.exe", "/C", command};
- }
- }
- }
- else {
- LOG.debug("Unix platform detected.");
- String shell = System.getProperty("SHELL");
- if(shell != null) {
- LOG.debug("The SHELL Unix environment variable is defined, using
it: " + shell + " -c " + command);
- shellCommand = new String[]{ shell, "-c", command};
- }
- else {
- LOG.debug("The SHELL Unix environment variable is not defined,
using the default Unix shell: /bin/sh -c " + command);
- shellCommand = new String[]{ "/bin/sh", "-c", command};
- }
- }
- try {
- Runtime runtime = Runtime.getRuntime();
- // launch the system command
- Process process = runtime.exec(shellCommand);
- // get the error stream gobbler
- StringBuffer errorBuffer = new StringBuffer();
- StreamGobbler errorGobbler = new
StreamGobbler(process.getErrorStream(), errorBuffer);
- // get the output stream gobbler
- StringBuffer outputBuffer = new StringBuffer();
- StreamGobbler outputGobbler = new
StreamGobbler(process.getInputStream(), outputBuffer);
- // start both gobblers
- errorGobbler.start();
- outputGobbler.start();
- // wait the end of the process
- int exitValue = process.waitFor();
- if(exitValue != 0) {
- // an error occurs
- LOG.error("Command " + command + " execution failed: " +
errorBuffer.toString());
- throw new ExecException(errorBuffer.toString());
- }
- // command is OK
- LOG.info("Command " + command + " execution completed: " +
outputBuffer.toString());
- return outputBuffer.toString();
- } catch(Exception exception) {
- LOG.error("Command " + command + " execution failed.", exception);
- throw new ExecException("Command " + command + " execution
failed.", exception);
- }
- }
-}
+ private static final transient Log LOG =
LogFactory.getLog(ExecUtils.class);
+ /**
+ * <p>
+ * Executes a command and returns the output and error buffer and also
the
+ * return value.
+ * </p>
+ *
+ * @param command
+ * the system command to execute.
+ * @param outputBuffer
+ * the buffer for storing the command output
+ * @param errorBuffer
+ * the buffer for storing the command error output
+ * @return the command return value
+ * @throws ExecException
+ */
+ public static int execute(String command, StringBuffer outputBuffer,
+ StringBuffer errorBuffer) throws ExecException {
+ int exitValue = -1;
+
+ LOG.info("Execute command " + command);
+ String[] shellCommand = null;
+ LOG.debug("Define the shell.");
+ LOG.debug("Get the OS name property.");
+ String osName = System.getProperty("os.name");
+ if (osName.startsWith("Windows")) {
+ LOG.debug("Microsoft Windows platform detected.");
+ String comSpec = System.getProperty("ComSpec");
+ if (comSpec != null) {
+ LOG
+ .debug("The ComSpec MS Windows
environment variable is defined, using it: "
+ + comSpec + "
/C " + command);
+ shellCommand = new String[] { comSpec, "/C",
command };
+ } else {
+ LOG
+ .debug("The ComSpec MS Windows
environment variable is not defined, found the shell command depending of the
MS Windows version.");
+ if (osName.startsWith("Windows 3")
+ || osName.startsWith("Windows
95")
+ || osName.startsWith("Windows
98")
+ || osName.startsWith("Windows
ME")) {
+ LOG
+ .debug("MS Windows
3.1/95/98/Me detected, using: command.com /C "
+ +
command);
+ shellCommand = new String[] {
"command.com", "/C", command };
+ } else {
+ LOG
+ .debug("MS Windows
NT/XP/Vista detected, using: cmd.exe /C "
+ +
command);
+ shellCommand = new String[] {
"cmd.exe", "/C", command };
+ }
+ }
+ } else {
+ LOG.debug("Unix platform detected.");
+ String shell = System.getProperty("SHELL");
+ if (shell != null) {
+ LOG
+ .debug("The SHELL Unix
environment variable is defined, using it: "
+ + shell + " -c
" + command);
+ shellCommand = new String[] { shell, "-c",
command };
+ } else {
+ LOG
+ .debug("The SHELL Unix
environment variable is not defined, using the default Unix shell: /bin/sh -c "
+ + command);
+ shellCommand = new String[] { "/bin/sh", "-c",
command };
+ }
+ }
+ try {
+ // check and create buffers if needed
+ if (errorBuffer == null) {
+ errorBuffer = new StringBuffer();
+ }
+ if (outputBuffer == null) {
+ outputBuffer = new StringBuffer();
+ }
+
+ // launch the system command
+ Process process =
Runtime.getRuntime().exec(shellCommand);
+
+ // get and start the error stream gobbler
+ StreamGobbler errorGobbler = new StreamGobbler(process
+ .getErrorStream(), errorBuffer);
+ errorGobbler.start();
+
+ // get and start the output stream gobbler
+ StreamGobbler outputGobbler = new StreamGobbler(process
+ .getInputStream(), outputBuffer);
+ outputGobbler.start();
+
+ // wait the end of the process
+ exitValue = process.waitFor();
+
+ if (exitValue != 0) {
+ // an error occured
+ LOG.error("Command " + command
+ + " execution failed with
return code " + exitValue
+ + " : " +
errorBuffer.toString());
+ } else {
+ // command was successful
+ LOG.debug("Command " + command + " execution
completed: "
+ + outputBuffer.toString());
+ }
+ } catch (Exception exception) {
+ LOG.error("Command " + command + " execution failed.",
exception);
+ throw new ExecException(
+ "Command " + command + " execution
failed.", exception);
+ }
+
+ // return the exit value of the process (defaults to -1)
+ return exitValue;
+ }
+}
/**
* <p>
@@ -120,31 +153,36 @@
*/
class StreamGobbler extends Thread {
- // log facility
- private final static transient Log LOG =
LogFactory.getLog(StreamGobbler.class);
-
- InputStream in;
- StringBuffer response;
-
- StreamGobbler(InputStream in, StringBuffer response) {
- this.in = in;
- this.response = response;
- }
-
- /*
- * (non-Javadoc)
- * @see java.lang.Thread#run()
- */
- public void run() {
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- String row = null;
- while((row = reader.readLine()) != null) {
- response.append(row + "\n");
- }
- } catch(IOException ioException) {
- LOG.warn("System command stream gobbler error : " +
ioException.getMessage());
- }
- }
+ // log facility
+ private final static transient Log LOG = LogFactory
+ .getLog(StreamGobbler.class);
+
+ InputStream in;
+ StringBuffer response;
+
+ StreamGobbler(InputStream in, StringBuffer response) {
+ this.in = in;
+ this.response = response;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Thread#run()
+ */
+ public void run() {
+ try {
+ BufferedReader reader = new BufferedReader(
+ new InputStreamReader(in));
+ String row = null;
+ while ((row = reader.readLine()) != null) {
+ response.append(row);
+ response.append('\n');
+ }
+ } catch (IOException ioException) {
+ LOG.warn("System command stream gobbler error : "
+ + ioException.getMessage());
+ }
+ }
}