You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by jd...@apache.org on 2006/06/26 20:58:27 UTC
svn commit: r417278 - in
/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main:
java/org/apache/geronimo/gshell/commands/standard/
java/org/apache/geronimo/gshell/commands/standard/util/
resources/META-INF/org.apache.geronimo...
Author: jdillon
Date: Mon Jun 26 11:58:26 2006
New Revision: 417278
URL: http://svn.apache.org/viewvc?rev=417278&view=rev
Log:
Adding a simple command to execute system processes; GSHELL-18
Added:
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java (with props)
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java (with props)
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java (with props)
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties (with props)
geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties (with props)
Added: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java?rev=417278&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java Mon Jun 26 11:58:26 2006
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.geronimo.gshell.commands.standard;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import org.apache.geronimo.gshell.command.CommandSupport;
+import org.apache.geronimo.gshell.command.CommandException;
+import org.apache.geronimo.gshell.command.MessageSource;
+import org.apache.geronimo.gshell.console.IO;
+import org.apache.geronimo.gshell.commands.standard.util.PumpStreamHandler;
+
+/**
+ * Execute system processes.
+ *
+ * @version $Id$
+ */
+public class ExecuteCommand
+ extends CommandSupport
+{
+ private ProcessBuilder builder;
+
+ public ExecuteCommand() {
+ super("exec");
+ }
+
+ protected Options getOptions() {
+ MessageSource messages = getMessageSource();
+
+ Options options = super.getOptions();
+
+// options.addOption(OptionBuilder
+// .withDescription(messages.getMessage("cli.option.n"))
+// .create('n'));
+
+ return options;
+ }
+
+ protected String getUsage() {
+ return super.getUsage() + " <command> (<arg>)*";
+ }
+
+ protected boolean processCommandLine(final CommandLine line) throws CommandException {
+ assert line != null;
+
+ String[] args = line.getArgs();
+
+ // Need at least one argument
+ if (args.length < 1) {
+ return true;
+ }
+ else {
+ builder = new ProcessBuilder(args);
+ }
+
+ //
+ // TODO: Allow ENV to be changed (default is given environ)
+ //
+
+ //
+ // TODO: Allow working dir to be set (default is user.dir)
+ //
+
+ //
+ // TODO: Allow error redirection to be enabled (default is false)
+ //
+
+ //
+ // TODO: Add timeout; default is no timeout
+ //
+
+ //
+ // TODO: Add spawn flag (process not killed when vm exits; default is not to spawn)
+ //
+
+ return false;
+ }
+
+ protected Object doExecute(final Object[] args) throws Exception {
+ assert args != null;
+ assert builder != null;
+
+ boolean info = log.isInfoEnabled();
+
+ if (info) {
+ log.info("Executing: " + builder.command());
+ }
+
+ IO io = getIO();
+
+ //
+ // TODO: May need to expose the Process's destroy() if Command abort() is issued?
+ //
+
+ Process p = builder.start();
+
+ PumpStreamHandler handler = new PumpStreamHandler(io);
+ handler.attach(p);
+ handler.start();
+
+ log.debug("Waiting for process to exit...");
+
+ int status = p.waitFor();
+
+ if (info) {
+ log.info("Process exited w/status: " + status);
+ }
+
+ handler.stop();
+
+ return status;
+ }
+}
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/ExecuteCommand.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java?rev=417278&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java Mon Jun 26 11:58:26 2006
@@ -0,0 +1,241 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.geronimo.gshell.commands.standard.util;
+
+import org.apache.geronimo.gshell.console.IO;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+//
+// Based on Apache Ant 1.6.5
+//
+
+/**
+ * Copies standard output and error of children streams to standard output and error of the parent.
+ *
+ * @version $Id$
+ */
+public class PumpStreamHandler
+{
+ private static final Log log = LogFactory.getLog(PumpStreamHandler.class);
+
+ private InputStream in;
+
+ private OutputStream out;
+
+ private OutputStream err;
+
+ private Thread outputThread;
+
+ private Thread errorThread;
+
+ private StreamPumper inputPump;
+
+ //
+ // NOTE: May want to use a ThreadPool here, 3 threads per/pair seems kinda expensive :-(
+ //
+
+ public PumpStreamHandler(final InputStream in, final OutputStream out, final OutputStream err) {
+ assert in != null;
+ assert out != null;
+ assert err != null;
+
+ this.in = in;
+ this.out = out;
+ this.err = err;
+ }
+
+ public PumpStreamHandler(final IO io) {
+ this(io.inputStream, io.outputStream, io.errorStream);
+ }
+
+ public PumpStreamHandler(final OutputStream out, final OutputStream err) {
+ this(null, out, err);
+ }
+
+ public PumpStreamHandler(final OutputStream outAndErr) {
+ this(outAndErr, outAndErr);
+ }
+
+ /**
+ * Set the input stream from which to read the standard output of the child.
+ */
+ public void setChildOutputStream(final InputStream in) {
+ assert in != null;
+
+ createChildOutputPump(in, out);
+ }
+
+ /**
+ * Set the input stream from which to read the standard error of the child.
+ */
+ public void setChildErrorStream(final InputStream in) {
+ assert in != null;
+
+ if (err != null) {
+ createChildErrorPump(in, err);
+ }
+ }
+
+ /**
+ * Set the output stream by means of which input can be sent to the child.
+ */
+ public void setChildInputStream(final OutputStream out) {
+ assert out != null;
+
+ if (in != null) {
+ inputPump = createInputPump(in, out, true);
+ }
+ else {
+ try {
+ out.close();
+ }
+ catch (IOException e) {
+ // ignore
+ }
+ }
+ }
+
+ /**
+ * Attach to a child streams from the given process.
+ *
+ * @param p The process to attach to.
+ */
+ public void attach(final Process p) {
+ assert p != null;
+
+ setChildInputStream(p.getOutputStream());
+ setChildOutputStream(p.getInputStream());
+ setChildErrorStream(p.getErrorStream());
+ }
+
+ /**
+ * Start pumping the streams.
+ */
+ public void start() {
+ outputThread.start();
+ errorThread.start();
+
+ if (inputPump != null) {
+ Thread inputThread = new Thread(inputPump);
+ inputThread.setDaemon(true);
+ inputThread.start();
+ }
+ }
+
+ /**
+ * Stop pumping the streams.
+ */
+ public void stop() {
+ try {
+ outputThread.join();
+ }
+ catch (InterruptedException e) {
+ // ignore
+ }
+
+ try {
+ errorThread.join();
+ }
+ catch (InterruptedException e) {
+ // ignore
+ }
+
+ if (inputPump != null) {
+ inputPump.stop();
+ }
+
+ try {
+ err.flush();
+ }
+ catch (IOException e) {
+ // ignore
+ }
+
+ try {
+ out.flush();
+ }
+ catch (IOException e) {
+ // ignore
+ }
+ }
+
+ /**
+ * Create the pump to handle child output.
+ */
+ protected void createChildOutputPump(final InputStream in, final OutputStream out) {
+ assert in != null;
+ assert out != null;
+
+ outputThread = createPump(in, out);
+ }
+
+ /**
+ * Create the pump to handle error output.
+ */
+ protected void createChildErrorPump(final InputStream in, final OutputStream out) {
+ assert in != null;
+ assert out != null;
+
+ errorThread = createPump(in, out);
+ }
+
+ /**
+ * Creates a stream pumper to copy the given input stream to the given output stream.
+ */
+ protected Thread createPump(final InputStream in, final OutputStream out) {
+ assert in != null;
+ assert out != null;
+
+ return createPump(in, out, false);
+ }
+
+ /**
+ * Creates a stream pumper to copy the given input stream to the
+ * given output stream.
+ *
+ * @param in The input stream to copy from.
+ * @param out The output stream to copy to.
+ * @param closeWhenExhausted If true close the inputstream.
+ * @return A thread object that does the pumping.
+ */
+ protected Thread createPump(final InputStream in, final OutputStream out, final boolean closeWhenExhausted) {
+ assert in != null;
+ assert out != null;
+
+ final Thread result = new Thread(new StreamPumper(in, out, closeWhenExhausted));
+ result.setDaemon(true);
+ return result;
+ }
+
+ /**
+ * Creates a stream pumper to copy the given input stream to the
+ * given output stream. Used for standard input.
+ */
+ protected StreamPumper createInputPump(final InputStream in, final OutputStream out, final boolean closeWhenExhausted) {
+ assert in != null;
+ assert out != null;
+
+ StreamPumper pumper = new StreamPumper(in, out, closeWhenExhausted);
+ pumper.setAutoflush(true);
+ return pumper;
+ }
+}
\ No newline at end of file
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/PumpStreamHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java?rev=417278&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java Mon Jun 26 11:58:26 2006
@@ -0,0 +1,204 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed 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.geronimo.gshell.commands.standard.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+//
+// Based on Apache Ant 1.6.5
+//
+
+/**
+ * Copies all data from an input stream to an output stream.
+ *
+ * @version $Id$
+ */
+public class StreamPumper
+ implements Runnable
+{
+ private static final Log log = LogFactory.getLog(StreamPumper.class);
+
+ private InputStream in;
+
+ private OutputStream out;
+
+ private volatile boolean finish;
+
+ private volatile boolean finished;
+
+ private boolean closeWhenExhausted;
+
+ private boolean autoflush;
+
+ private Exception exception;
+
+ private int bufferSize = 128;
+
+ private boolean started;
+
+ /**
+ * Create a new stream pumper.
+ *
+ * @param in Input stream to read data from
+ * @param out 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 in, final OutputStream out, final boolean closeWhenExhausted) {
+ assert in != null;
+ assert out != null;
+
+ this.in = in;
+ this.out = out;
+ this.closeWhenExhausted = closeWhenExhausted;
+ }
+
+ /**
+ * Create a new stream pumper.
+ *
+ * @param in Input stream to read data from
+ * @param out Output stream to write data to.
+ */
+ public StreamPumper(final InputStream in, final OutputStream out) {
+ this(in, out, false);
+ }
+
+ /**
+ * Set whether data should be flushed through to the output stream.
+ *
+ * @param autoflush If true, push through data; if false, let it be buffered
+ */
+ public void setAutoflush(boolean autoflush) {
+ this.autoflush = autoflush;
+ }
+
+ /**
+ * Copies data from the input stream to the output stream.
+ *
+ * Terminates as soon as the input stream is closed or an error occurs.
+ */
+ public void run() {
+ synchronized (this) {
+ started = true;
+ }
+ finished = false;
+ finish = false;
+
+ final byte[] buf = new byte[bufferSize];
+
+ int length;
+ try {
+ while ((length = in.read(buf)) > 0 && !finish) {
+ out.write(buf, 0, length);
+ if (autoflush) {
+ out.flush();
+ }
+ }
+ out.flush();
+ }
+ catch (Exception e) {
+ synchronized (this) {
+ exception = e;
+ }
+ }
+ finally {
+ if (closeWhenExhausted) {
+ try {
+ out.close();
+ }
+ catch (IOException e) {
+ // ignore
+ }
+ }
+ finished = true;
+
+ synchronized (this) {
+ notifyAll();
+ }
+ }
+ }
+
+ /**
+ * Tells whether the end of the stream has been reached.
+ *
+ * @return true If the stream has been exhausted.
+ */
+ public boolean isFinished() {
+ return finished;
+ }
+
+ /**
+ * This method blocks until the stream pumper finishes.
+ *
+ * @see #isFinished()
+ */
+ public synchronized void waitFor() throws InterruptedException {
+ while (!isFinished()) {
+ wait();
+ }
+ }
+
+ /**
+ * Set the size in bytes of the read buffer.
+ *
+ * @param bufferSize the buffer size to use.
+ * @throws IllegalStateException if the StreamPumper is already running.
+ */
+ public synchronized void setBufferSize(final int bufferSize) {
+ if (started) {
+ throw new IllegalStateException("Cannot set buffer size on a running StreamPumper");
+ }
+
+ this.bufferSize = bufferSize;
+ }
+
+ /**
+ * Get the size in bytes of the read buffer.
+ *
+ * @return The size of the read buffer.
+ */
+ public synchronized int getBufferSize() {
+ return bufferSize;
+ }
+
+ /**
+ * Get the exception encountered, if any.
+ *
+ * @return The Exception encountered; or null if there was none.
+ */
+ public synchronized Exception getException() {
+ return exception;
+ }
+
+ /**
+ * Stop the pumper as soon as possible.
+ *
+ * Note that it may continue to block on the input stream
+ * but it will really stop the thread as soon as it gets EOF
+ * or any byte, and it will be marked as finished.
+ */
+ public synchronized void stop() {
+ finish = true;
+
+ notifyAll();
+ }
+}
\ No newline at end of file
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/java/org/apache/geronimo/gshell/commands/standard/util/StreamPumper.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties?rev=417278&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties Mon Jun 26 11:58:26 2006
@@ -0,0 +1,13 @@
+##
+## $Id$
+##
+
+class=org.apache.geronimo.gshell.commands.standard.ExecuteCommand
+
+name=exec
+
+aliases=run,system
+
+category=standard
+
+enable=true
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/META-INF/org.apache.geronimo.gshell.command/exec.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties?rev=417278&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties (added)
+++ geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties Mon Jun 26 11:58:26 2006
@@ -0,0 +1,17 @@
+##
+## $Id$
+##
+
+##
+## Option descriptions
+##
+
+cli.option.help=Display this help message
+
+##
+## Option usage (help)
+##
+
+cli.usage.description=Executes the specified command in a separate process.
+
+cli.usage.footer=
\ No newline at end of file
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: geronimo/sandbox/gshell/trunk/gshell-commands/gshell-standard-commands/src/main/resources/org/apache/geronimo/gshell/commands/standard/ExecuteCommandMessages.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain