You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by jb...@apache.org on 2009/04/20 16:56:19 UTC
svn commit: r766720 - in
/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils:
./ ExecException.java ExecUtils.java
Author: jbonofre
Date: Mon Apr 20 14:56:19 2009
New Revision: 766720
URL: http://svn.apache.org/viewvc?rev=766720&view=rev
Log:
Add utils class to execute system command. This class supports Unix and Windows based system (and shell) and provide output and error buffer gobbler.
Added:
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java (with props)
servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java (with props)
Added: servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java
URL: http://svn.apache.org/viewvc/servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java?rev=766720&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java (added)
+++ servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java Mon Apr 20 14:56:19 2009
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+package org.apache.servicemix.exec.utils;
+
+/**
+ * Exception raised during a system command execution.
+ *
+ * @author jbonofre
+ */
+public class ExecException extends Exception {
+
+ private static final long serialVersionUID = -7151089812651503402L;
+
+ /**
+ * <p>
+ * Creates an <code>ExecException</code> with a description message.
+ * </p>
+ *
+ * @param message the exception description message.
+ */
+ public ExecException(String message) {
+ super(message);
+ }
+
+ /**
+ * <p>
+ * Creates an <code>ExecException</code> with an underlying cause.
+ * </p>
+ *
+ * @param cause the underlying cause.
+ */
+ public ExecException(Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * <p>
+ * Creates an <code>ExecException</code> with a description message and a underlying cause.
+ * </p>
+ *
+ * @param message the exception description message.
+ * @param cause the underlying cause.
+ */
+ public ExecException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+}
Propchange: servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecException.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: 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=766720&view=auto
==============================================================================
--- servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java (added)
+++ servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java Mon Apr 20 14:56:19 2009
@@ -0,0 +1,150 @@
+/*
+ * 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.
+ */
+package org.apache.servicemix.exec.utils;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Utility class to execute system command.
+ *
+ * @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("Command " + command + " execution failed: " + 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);
+ }
+ }
+
+}
+
+
+/**
+ * <p>
+ * Inner class to glob stream with a thread.
+ * </p>
+ *
+ * @author onofre
+ */
+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());
+ }
+ }
+
+}
Propchange: servicemix/components/engines/servicemix-exec/trunk/src/main/java/org/apache/servicemix/exec/utils/ExecUtils.java
------------------------------------------------------------------------------
svn:mime-type = text/plain