You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sg...@apache.org on 2008/02/08 18:02:49 UTC
svn commit: r619940 - in /commons/sandbox/exec/trunk: ./ src/changes/
src/main/java/org/apache/commons/exec/
src/main/java/org/apache/commons/exec/environment/
src/main/java/org/apache/commons/exec/util/ src/site/
src/test/java/org/apache/commons/exec/...
Author: sgoeschl
Date: Fri Feb 8 09:02:37 2008
New Revision: 619940
URL: http://svn.apache.org/viewvc?rev=619940&view=rev
Log:
Added DebugUtils to improve testing - the class can be configured using system properties to rethrow any previosly ignored exception
Added:
commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/util/DebugUtils.java
Modified:
commons/sandbox/exec/trunk/build.xml
commons/sandbox/exec/trunk/src/changes/changes.xml
commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java
commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java
commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
commons/sandbox/exec/trunk/src/site/site.xml
commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java
Modified: commons/sandbox/exec/trunk/build.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/build.xml?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/build.xml (original)
+++ commons/sandbox/exec/trunk/build.xml Fri Feb 8 09:02:37 2008
@@ -64,6 +64,8 @@
<mkdir dir="${maven.test.reports}"/>
<junit printSummary="yes" haltonerror="true" haltonfailure="true" fork="true" dir=".">
<sysproperty key="basedir" value="."/>
+ <sysproperty key="COMMONS_EXEC_DEBUG" value="true"/>
+ <sysproperty key="COMMONS_EXEC_LENIENT" value="false"/>
<formatter type="xml"/>
<formatter type="plain" usefile="false"/>
<classpath>
Modified: commons/sandbox/exec/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/changes/changes.xml?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/changes/changes.xml (original)
+++ commons/sandbox/exec/trunk/src/changes/changes.xml Fri Feb 8 09:02:37 2008
@@ -24,6 +24,9 @@
</properties>
<body>
<release version="1.0-SNAPSHOT" date="as in SVN" description="Sandbox release">
+ <action dev="sgoeschl" type="add">
+ Added DebugUtils to improve cross-platform testing.
+ </action>
<action dev="sgoeschl" type="remove">
Removed commons-logging integration
</action>
Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/DefaultExecutor.java Fri Feb 8 09:02:37 2008
@@ -263,8 +263,12 @@
}
/**
- * Close the streams belonging to the given Process.
- *
+ * Close the streams belonging to the given Process. In the
+ * original implementation all exceptions were dropped which
+ * is probably not a good thing. On the other hand the signature
+ * allows throwing an IOException so the curent implementation
+ * might be quite okay.
+ *
* @param process the <CODE>Process</CODE>.
* @throws IOException closing one of the three streams failed
*/
Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/PumpStreamHandler.java Fri Feb 8 09:02:37 2008
@@ -18,6 +18,8 @@
package org.apache.commons.exec;
+import org.apache.commons.exec.util.DebugUtils;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -124,7 +126,8 @@
try {
os.close();
} catch (IOException e) {
- // ignore
+ String msg = "Got exception while closing output stream";
+ DebugUtils.handleException(msg ,e);
}
}
}
@@ -166,12 +169,14 @@
try {
err.flush();
} catch (IOException e) {
- // ignore
+ String msg = "Got exception while flushing the error stream";
+ DebugUtils.handleException(msg ,e);
}
try {
out.flush();
} catch (IOException e) {
- // ignore
+ String msg = "Got exception while flushing the output stream";
+ DebugUtils.handleException(msg ,e);
}
}
Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/StreamPumper.java Fri Feb 8 09:02:37 2008
@@ -18,6 +18,8 @@
package org.apache.commons.exec;
+import org.apache.commons.exec.util.DebugUtils;
+
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -27,44 +29,60 @@
*/
public class StreamPumper implements Runnable {
- // TODO: make SIZE an instance variable.
- // TODO: add a status flag to note if an error occurred in run.
-
- private static final int SIZE = 1024;
+ /** the default size of the internal buffer for copying the streams */
+ private static final int DEFAULT_SIZE = 1024;
+ /** the input stream to pump from */
private InputStream is;
+ /** the output stream to pmp into */
private OutputStream os;
+ /** the size of the internal buffer for copying the streams */
+ private int size;
+
+ /** was the end of the stream reached */
private boolean finished;
+ /** close the output stream when exhausted */
private boolean closeWhenExhausted;
-
+
/**
* Create a new stream pumper.
*
- * @param is
- * input stream to read data from
- * @param os
- * output stream to write data to.
- * @param closeWhenExhausted
- * if true, the output stream will be closed when the input is
- * exhausted.
+ * @param is input stream to read data from
+ * @param os output stream to write data to.
+ * @param closeWhenExhausted if true, the output stream will be closed when the input is exhausted.
*/
public StreamPumper(final InputStream is, final OutputStream os,
final boolean closeWhenExhausted) {
this.is = is;
this.os = os;
+ this.size = DEFAULT_SIZE;
+ this.closeWhenExhausted = closeWhenExhausted;
+ }
+
+ /**
+ * Create a new stream pumper.
+ *
+ * @param is input stream to read data from
+ * @param os output stream to write data to.
+ * @param closeWhenExhausted if true, the output stream will be closed when the input is exhausted.
+ * @param size the size of the internal buffer for copying the streams
+ */
+ public StreamPumper(final InputStream is, final OutputStream os,
+ final boolean closeWhenExhausted, final int size) {
+ this.is = is;
+ this.os = os;
+ this.size = (size > 0 ? size : DEFAULT_SIZE);
this.closeWhenExhausted = closeWhenExhausted;
}
/**
* Create a new stream pumper.
*
- * @param is
- * input stream to read data from
- * @param os
- * output stream to write data to.
+ * @param is input stream to read data from
+ * @param os output stream to write data to.
*/
public StreamPumper(final InputStream is, final OutputStream os) {
this(is, os, false);
@@ -80,7 +98,7 @@
finished = false;
}
- final byte[] buf = new byte[SIZE];
+ final byte[] buf = new byte[this.size];
int length;
try {
@@ -88,13 +106,15 @@
os.write(buf, 0, length);
}
} catch (Exception e) {
- // ignore errors
+ String msg = "Got exception while reading/writing the stream";
+ DebugUtils.handleException(msg ,e);
} finally {
if (closeWhenExhausted) {
try {
os.close();
} catch (IOException e) {
- // ignore
+ String msg = "Got exception while closing exhausted output stream";
+ DebugUtils.handleException(msg ,e);
}
}
synchronized (this) {
Modified: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java (original)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/environment/DefaultProcessingEnvironment.java Fri Feb 8 09:02:37 2008
@@ -130,11 +130,8 @@
ByteArrayOutputStream out = new ByteArrayOutputStream();
Executor exe = new DefaultExecutor();
exe.setStreamHandler(new PumpStreamHandler(out));
-
- int retval = exe.execute(getProcEnvCommand(), new HashMap());
- if (retval != 0) {
- // Just try to use what we got
- }
+ // ignore the exit value - Just try to use what we got
+ exe.execute(getProcEnvCommand(), new HashMap());
return new BufferedReader(new StringReader(toString(out)));
}
Added: commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/util/DebugUtils.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/util/DebugUtils.java?rev=619940&view=auto
==============================================================================
--- commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/util/DebugUtils.java (added)
+++ commons/sandbox/exec/trunk/src/main/java/org/apache/commons/exec/util/DebugUtils.java Fri Feb 8 09:02:37 2008
@@ -0,0 +1,85 @@
+/*
+ * 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.commons.exec.util;
+
+/**
+ * Helper classes to provide debugging support.
+ *
+ * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
+ */
+public class DebugUtils
+{
+ /**
+ * System property to determine how to handle exceptions. When
+ * set to "false" we rethrow the otherwise silently catched
+ * exceptions found in the original code. The default value
+ * is "true"
+ */
+ public static final String COMMONS_EXEC_LENIENT = "COMMONS_EXEC_LENIENT";
+
+ /**
+ * System property to determine how to dump an exception. When
+ * set to "true" we print any exception to stderr. The default
+ * value is "false"
+ */
+ public static final String COMMONS_EXEC_DEBUG = "COMMONS_EXEC_DEBUG";
+
+ /**
+ * Handle an exception based on the system properties.
+ *
+ * @param e an exception being handled
+ */
+ public static void handleException(String msg, Exception e) {
+
+ if(isDebugEnabled()) {
+ System.err.println(msg);
+ e.printStackTrace();
+ }
+
+ if(!isLenientEnabled()) {
+ if(e instanceof RuntimeException) {
+ throw (RuntimeException) e;
+ }
+ else {
+ // can't pass root cause since the constructor is not available on JDK 1.3
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+ }
+
+ /**
+ * Determine if debugging is enabled based on the
+ * system property "COMMONS_EXEC_DEBUG".
+ *
+ * @return true if debug mode is enabled
+ */
+ public static boolean isDebugEnabled() {
+ return "true".equalsIgnoreCase(System.getProperty("COMMONS_EXEC_DEBUG", "false"));
+ }
+
+ /**
+ * Determine if lenient mode is enabled.
+ *
+ * @return true if lenient mode is enabled
+ */
+ public static boolean isLenientEnabled() {
+ return "true".equalsIgnoreCase(System.getProperty("COMMONS_EXEC_LENIENT", "true"));
+ }
+
+}
Modified: commons/sandbox/exec/trunk/src/site/site.xml
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/site/site.xml?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/site/site.xml (original)
+++ commons/sandbox/exec/trunk/src/site/site.xml Fri Feb 8 09:02:37 2008
@@ -21,6 +21,7 @@
<body>
<menu name="Commons Exec">
<item name="Overview" href="/index.html" />
+ <item name="Tutorial" href="/tutorial.html" />
</menu>
</body>
</site>
Modified: commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java?rev=619940&r1=619939&r2=619940&view=diff
==============================================================================
--- commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java (original)
+++ commons/sandbox/exec/trunk/src/test/java/org/apache/commons/exec/util/MapUtilTest.java Fri Feb 8 09:02:37 2008
@@ -18,7 +18,6 @@
package org.apache.commons.exec.util;
-import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.environment.EnvironmentUtil;
import java.util.HashMap;