You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/03/19 15:30:46 UTC

svn commit: r638842 - in /servicemix/smx4/kernel/trunk/gshell/gshell-core: ./ src/main/java/org/apache/geronimo/gshell/commands/builtins/ src/main/java/org/apache/geronimo/gshell/commands/utils/ src/main/java/org/apache/geronimo/gshell/remote/server/ha...

Author: gnodet
Date: Wed Mar 19 07:30:39 2008
New Revision: 638842

URL: http://svn.apache.org/viewvc?rev=638842&view=rev
Log:
SMX4KNL-21, SMX4KNL-22, SMX4KNL-23

Added:
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseInputStream.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseOutputStream.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ProxyIO.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/TerminalFactoryBean.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/test/java/org/apache/geronimo/gshell/spring/NoCloseInputStreamTest.java
Removed:
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/IOTargetSource.java
Modified:
    servicemix/smx4/kernel/trunk/gshell/gshell-core/pom.xml
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/builtins/EchoCommand.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/remote/server/handler/SpringExecuteHandler.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/EnvironmentTargetSource.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/GShell.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/OsgiSubShell.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/Prompter.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandExecutor.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringRshServer.java
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
    servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell.xml

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/pom.xml?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/pom.xml (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/pom.xml Wed Mar 19 07:30:39 2008
@@ -148,7 +148,7 @@
         <dependency>
             <groupId>jline</groupId>
             <artifactId>jline</artifactId>
-            <version>0.9.91</version>
+            <version>${jline.version}</version>
         </dependency>
         <dependency>
             <groupId>org.codehaus.plexus</groupId>

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/builtins/EchoCommand.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/builtins/EchoCommand.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/builtins/EchoCommand.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/builtins/EchoCommand.java Wed Mar 19 07:30:39 2008
@@ -23,8 +23,8 @@
 
 import org.apache.geronimo.gshell.clp.Argument;
 import org.apache.geronimo.gshell.clp.Option;
-import org.apache.geronimo.gshell.command.CommandSupport;
 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
 
 /**
  * A simple command to <em>echo</em> all given arguments to the commands standard output.
@@ -33,7 +33,7 @@
  */
 @CommandComponent(id="gshell-builtins:echo", description="Echo or print arguments to STDOUT")
 public class EchoCommand
-    extends CommandSupport
+    extends OsgiCommandSupport
 {
     @Option(name="-n", description="Do not print the trailing newline character")
     private boolean trailingNewline = true;

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/commands/utils/GrepCommand.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,49 @@
+package org.apache.geronimo.gshell.commands.utils;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.regex.Pattern;
+
+import org.apache.geronimo.gshell.clp.Argument;
+import org.apache.geronimo.gshell.clp.Option;
+import org.apache.geronimo.gshell.command.annotation.CommandComponent;
+import org.apache.geronimo.gshell.support.OsgiCommandSupport;
+
+@CommandComponent(id="utils:grep", description="Grep")
+public class GrepCommand extends OsgiCommandSupport {
+
+    @Argument(required=true, description="Regular expression")
+    private String regex;
+
+    @Option(name = "-v", description = "Inverse matching")
+    private boolean inverse;
+
+    protected Object doExecute() throws Exception {
+        Pattern p = Pattern.compile(regex);
+        try {
+            while (true) {
+                String line = readLine(io.in);
+                if (p.matcher(line).matches() ^ inverse) {
+                    io.out.println(line);
+                }
+            }
+        } catch (IOException e) {
+        }
+        return null;
+    }
+
+    private String readLine(Reader in) throws IOException {
+        StringBuffer buf = new StringBuffer();
+        while (true) {
+            int i = in.read();
+            if (i == -1 && buf.length() == 0) {
+                throw new IOException("break");
+            }
+            if (i == -1 || i == '\n' || i == '\r') {
+                return buf.toString();
+            }
+            buf.append((char) i);
+        }
+    }
+
+}

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/remote/server/handler/SpringExecuteHandler.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/remote/server/handler/SpringExecuteHandler.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/remote/server/handler/SpringExecuteHandler.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/remote/server/handler/SpringExecuteHandler.java Wed Mar 19 07:30:39 2008
@@ -16,21 +16,12 @@
  */
 package org.apache.geronimo.gshell.remote.server.handler;
 
-import org.apache.geronimo.gshell.remote.message.ExecuteMessage;
-import org.apache.geronimo.gshell.whisper.transport.Session;
-import org.apache.geronimo.gshell.lookup.IOLookup;
-import org.apache.geronimo.gshell.lookup.EnvironmentLookup;
 import org.apache.geronimo.gshell.common.Notification;
-import org.apache.geronimo.gshell.spring.IOTargetSource;
+import org.apache.geronimo.gshell.remote.message.ExecuteMessage;
 import org.apache.geronimo.gshell.spring.EnvironmentTargetSource;
+import org.apache.geronimo.gshell.spring.ProxyIO;
+import org.apache.geronimo.gshell.whisper.transport.Session;
 
-/**
- * Created by IntelliJ IDEA.
- * User: gnodet
- * Date: Dec 5, 2007
- * Time: 5:14:29 PM
- * To change this template use File | Settings | File Templates.
- */
 public class SpringExecuteHandler extends ServerMessageHandlerSupport<ExecuteMessage>
 {
     public SpringExecuteHandler() {
@@ -39,7 +30,7 @@
 
     public void handle(final Session session, final ServerSessionContext context, final ExecuteMessage message) throws Exception {
         // Need to make sure that the execuing thread has the right I/O and environment in context
-        IOTargetSource.setIO(context.io);
+        ProxyIO.setIO(context.io);
         EnvironmentTargetSource.setEnvironment(context.env);
 
         ExecuteMessage.Result reply;

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/EnvironmentTargetSource.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/EnvironmentTargetSource.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/EnvironmentTargetSource.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/EnvironmentTargetSource.java Wed Mar 19 07:30:39 2008
@@ -28,6 +28,10 @@
 
     private static ThreadLocal<Environment> tls = new ThreadLocal<Environment>();
 
+    public static Environment getEnvironment() {
+        return tls.get();
+    }
+
     public static void setEnvironment(Environment env) {
         tls.set(env);
     }

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/GShell.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/GShell.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/GShell.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/GShell.java Wed Mar 19 07:30:39 2008
@@ -16,13 +16,16 @@
  */
 package org.apache.geronimo.gshell.spring;
 
+import java.io.IOException;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.geronimo.gshell.DefaultEnvironment;
+import org.apache.geronimo.gshell.DefaultShell;
 import org.apache.geronimo.gshell.ExitNotification;
 import org.apache.geronimo.gshell.command.IO;
 import org.apache.geronimo.gshell.common.Arguments;
+import org.apache.geronimo.gshell.console.Console;
 import org.apache.geronimo.gshell.shell.Environment;
 import org.apache.geronimo.gshell.shell.InteractiveShell;
 import org.apache.servicemix.kernel.main.spi.MainService;
@@ -32,6 +35,7 @@
 import org.osgi.framework.FrameworkListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.DisposableBean;
 import org.springframework.osgi.context.BundleContextAware;
 
 /**
@@ -51,11 +55,18 @@
     private MainService mainService;
     private BundleContext bundleContext;
     private CountDownLatch frameworkStarted;
+    private volatile boolean closed;
 
-    public GShell(InteractiveShell shell) {
+    public GShell(InteractiveShell shell) throws IOException {
         this.shell = shell;
-        this.io = new IO(System.in, System.out, System.err);
-        this.env = new DefaultEnvironment(io);
+        if (shell instanceof DefaultShell) {
+            DefaultShell sh = (DefaultShell) shell;
+            sh.setErrorHandler(wrapErrorHandler(sh.getErrorHandler()));
+        }
+        this.io = new IO(new NoCloseInputStream(System.in),
+                         new NoCloseOutputStream(System.out),
+                         new NoCloseOutputStream(System.err));
+        this.env = new DefaultEnvironment(new ProxyIO());
     }
 
     public void setStart(boolean start) {
@@ -78,7 +89,9 @@
         }
     }
 
-    public void stop() throws InterruptedException {
+    public void stop() throws Exception {
+        closed = true;
+        io.close();
         if (thread != null) {
             frameworkStarted.countDown();
             thread.interrupt();
@@ -90,7 +103,7 @@
 
     public void run() {
         try {
-            IOTargetSource.setIO(io);
+            ProxyIO.setIO(io);
             EnvironmentTargetSource.setEnvironment(env);
 
             String[] args = null;
@@ -113,26 +126,28 @@
             } else {
                 // Otherwise go into a command shell.
                 shell.run();
+            }
+
+        } catch (Throwable e) {
+            if (e instanceof ExitNotification) {
+                if (closed) {
+                    // Ignore notifications coming because the spring app has been destroyed
+                    return;
+                }
                 if (mainService != null) {
                     mainService.setExitCode(0);
                 }
+                log.info("Exiting shell due received exit notification");
+            } else {
+                if (mainService != null) {
+                    mainService.setExitCode(-1);
+                }
+                log.info("Exiting shell due to caught exception " + e, e);
             }
-
-        } catch (ExitNotification e) {
-            if (mainService != null) {
-                mainService.setExitCode(0);
-            }
-            log.info("Exiting shell due received exit notification");
-        } catch (Exception e) {
-            if (mainService != null) {
-                mainService.setExitCode(-1);
-            }
-            log.info("Exiting shell due to caught exception " + e, e);
-        } finally {
             try {
                 getBundleContext().getBundle(0).stop();
-            } catch (BundleException e) {
-                log.info("Caught exception while shutting down framework: " + e, e);
+            } catch (BundleException e2) {
+                log.info("Caught exception while shutting down framework: " + e2, e2);
             }
         }
     }
@@ -166,5 +181,17 @@
     public BundleContext getBundleContext() {
         return bundleContext;
 	}
+
+    protected Console.ErrorHandler wrapErrorHandler(final Console.ErrorHandler handler) {
+        return new Console.ErrorHandler() {
+            public Result handleError(Throwable error) {
+                if (closed) {
+                    throw new ExitNotification();
+                }
+                return handler.handleError(error);
+            }
+        };
+    }
+
 
 }

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseInputStream.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseInputStream.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseInputStream.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseInputStream.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,93 @@
+/*
+ * 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.geronimo.gshell.spring;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+/**
+ * Wrap an InputStream in a complex structure so that it can
+ * be reused and closed even when a read() method is blocking.
+ *
+ * This stream uses a PipedInputStream / PipedOutputStream to
+ * decouple the read InputStream.  When the close() method is
+ * called, the pipe will be broken, and a single character will
+ * be eaten from the reader thread.
+ */
+public class NoCloseInputStream extends PipedInputStream {
+
+    private final InputStream in;
+    private final PipedOutputStream pos;
+    private final Thread thread;
+    private IOException exception;
+
+    public NoCloseInputStream(InputStream in) throws IOException {
+        this.in = in;
+        pos = new PipedOutputStream(this);
+        thread = new Thread() {
+            public void run() {
+                doRead();
+            }
+        };
+        thread.start();
+    }
+
+    public synchronized int read() throws IOException {
+        if (exception != null) {
+            throw exception;
+        }
+        return super.read();
+    }
+
+    public synchronized int read(byte b[], int off, int len) throws IOException {
+        if (exception != null) {
+            throw exception;
+        }
+        return super.read(b, off, len);
+    }
+
+    public void close() throws IOException {
+        super.close();
+        pos.close();
+        thread.interrupt();
+    }
+
+    protected void doRead() {
+        try {
+            int c;
+            while ((c = in.read()) != -1) {
+                pos.write(c);
+                // Need to notify, else there is a 1 sec lag for the
+                // echo to be displayed on the terminal.  The notify
+                // will unblock the reader thread.
+                synchronized (this) {
+                    this.notifyAll();
+                }
+            }
+        } catch (IOException e) {
+            exception = e;
+            try {
+                pos.close();
+            } catch (Exception e2) {
+                // ignore
+            }
+        }
+    }
+
+}

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseOutputStream.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseOutputStream.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseOutputStream.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/NoCloseOutputStream.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,41 @@
+/*
+ * 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.geronimo.gshell.spring;
+
+import java.io.FilterOutputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
+/**
+ * Simple OutputStream, that do not close the underlying
+ * stream when close() is called.
+ */
+public class NoCloseOutputStream extends FilterOutputStream {
+
+    public NoCloseOutputStream(OutputStream out) {
+        super(out);
+    }
+
+    public void close() throws IOException {
+        try {
+            flush();
+        } catch (IOException ignored) {
+        } finally {
+            out = null;
+        }
+    }
+}

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/OsgiSubShell.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/OsgiSubShell.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/OsgiSubShell.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/OsgiSubShell.java Wed Mar 19 07:30:39 2008
@@ -54,7 +54,7 @@
 			// that the DefaultEnvironment tries to overwrite.
 			Environment environment = new DefaultEnvironment(ctx.getIo(), new DefaultVariables());
 			ctx.setEnvironment(environment);
-	        IOTargetSource.setIO(ctx.getIo());
+	        ProxyIO.setIO(ctx.getIo());
 	        EnvironmentTargetSource.setEnvironment(environment);
 			InteractiveShell interactiveShell = createInteractiveShell(ctx);
 			if( args!=null && args.length>0 ) {

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/Prompter.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/Prompter.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/Prompter.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/Prompter.java Wed Mar 19 07:30:39 2008
@@ -23,6 +23,9 @@
 import org.apache.geronimo.gshell.shell.Environment;
 import org.apache.geronimo.gshell.shell.ShellInfo;
 
+/**
+ * A prompter that displays the current sub-shell.
+ */
 public class Prompter implements Console.Prompter {
 
     private Renderer renderer = new Renderer();

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ProxyIO.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ProxyIO.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ProxyIO.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/ProxyIO.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,117 @@
+/*
+ * 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.geronimo.gshell.spring;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+
+import org.apache.geronimo.gshell.command.IO;
+
+/**
+ * An IO implementation that delegates the Input and Output
+ * stream to another IO stored in a ThreadLocal.
+ * The reason for this class is that Spring AOP can not proxy
+ * fields and GShell always access the in, out and err fields
+ * directly, hence the need to wrap these using delegates. 
+ */
+public class ProxyIO extends IO {
+
+    private static final ThreadLocal<IO> TLS_IO = new ThreadLocal<IO>();
+
+    public ProxyIO() {
+        super(new ProxyInputStream() {
+            protected InputStream getIn() {
+                return TLS_IO.get().inputStream;
+            }
+        }, new ProxyOutputStream() {
+            protected OutputStream getOut() {
+                return TLS_IO.get().outputStream;
+            }
+        }, new ProxyOutputStream() {
+            protected OutputStream getOut() {
+                return TLS_IO.get().errorStream;
+            }
+        });
+    }
+
+    public static void setIO(IO io) {
+        TLS_IO.set(io);
+    }
+
+    public static IO getIO() {
+        return TLS_IO.get();
+    }
+
+    protected static abstract class ProxyInputStream extends InputStream {
+        public int read() throws IOException {
+            return getIn().read();
+        }
+        public int read(byte b[]) throws IOException {
+            return read(b, 0, b.length);
+        }
+        public int read(byte b[], int off, int len) throws IOException {
+            return getIn().read(b, off, len);
+        }
+        public long skip(long n) throws IOException {
+            return getIn().skip(n);
+        }
+        public int available() throws IOException {
+            return getIn().available();
+        }
+        public void close() throws IOException {
+            getIn().close();
+        }
+        public synchronized void mark(int readlimit) {
+            getIn().mark(readlimit);
+        }
+        public synchronized void reset() throws IOException {
+            getIn().reset();
+        }
+        public boolean markSupported() {
+            return getIn().markSupported();
+        }
+        protected abstract InputStream getIn();
+    }
+
+    protected static abstract class ProxyOutputStream extends OutputStream {
+        public void write(int b) throws IOException {
+            getOut().write(b);
+        }
+        public void write(byte b[]) throws IOException {
+            write(b, 0, b.length);
+        }
+        public void write(byte b[], int off, int len) throws IOException {
+            if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
+                throw new IndexOutOfBoundsException();
+            for (int i = 0 ; i < len ; i++) {
+                write(b[off + i]);
+            }
+        }
+        public void flush() throws IOException {
+            getOut().flush();
+        }
+        public void close() throws IOException {
+            try {
+                flush();
+            } catch (IOException ignored) {
+            }
+            getOut().close();
+        }
+        protected abstract OutputStream getOut();
+    }
+}

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandExecutor.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandExecutor.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandExecutor.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringCommandExecutor.java Wed Mar 19 07:30:39 2008
@@ -20,6 +20,7 @@
 import org.apache.geronimo.gshell.DefaultCommandExecutor;
 import org.apache.geronimo.gshell.registry.CommandRegistry;
 import org.apache.geronimo.gshell.command.CommandExecutor;
+import org.apache.geronimo.gshell.command.IO;
 import org.apache.geronimo.gshell.layout.LayoutManager;
 import org.apache.geronimo.gshell.shell.Environment;
 
@@ -53,7 +54,22 @@
     }
     
     public void init() {
-        executor = new DefaultCommandExecutor(layoutManager, commandRegistry, commandLineBuilder, env);
+        executor = new DefaultCommandExecutor(layoutManager, commandRegistry, commandLineBuilder, env) {
+            @Override
+            protected Thread createThread(final Runnable run) {
+                final IO proxyio = ProxyIO.getIO();
+                final Environment env = EnvironmentTargetSource.getEnvironment();
+                return new Thread() {
+                    @Override
+                    public void run() {
+                        EnvironmentTargetSource.setEnvironment(env);
+                        ProxyIO.setIO(proxyio);
+                        run.run();
+                    }
+                };
+            }
+
+        };
     }
 
     public Object execute(String s) throws Exception {
@@ -65,6 +81,10 @@
     }
 
     public Object execute(Object... objects) throws Exception {
+        return executor.execute(objects);
+    }
+
+    public Object execute(Object[][] objects) throws Exception {
         return executor.execute(objects);
     }
 }

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringRshServer.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringRshServer.java?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringRshServer.java (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/SpringRshServer.java Wed Mar 19 07:30:39 2008
@@ -20,13 +20,6 @@
 
 import org.apache.geronimo.gshell.remote.server.RshServer;
 
-/**
- * Created by IntelliJ IDEA.
- * User: gnodet
- * Date: Dec 5, 2007
- * Time: 8:34:40 PM
- * To change this template use File | Settings | File Templates.
- */
 public class SpringRshServer {
 
     private RshServer server;

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/TerminalFactoryBean.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/TerminalFactoryBean.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/TerminalFactoryBean.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/java/org/apache/geronimo/gshell/spring/TerminalFactoryBean.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,57 @@
+/*
+ * 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.geronimo.gshell.spring;
+
+import jline.Terminal;
+import jline.UnixTerminal;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.FactoryBean;
+
+/**
+ * Spring factory bean for JLine terminal.
+ * The main purpose of this factory is to destroy the terminal when
+ * the Spring application is terminated, so that the terminal is
+ * restored to its normal state.
+ */
+public class TerminalFactoryBean implements FactoryBean, DisposableBean {
+
+    private Terminal terminal;
+
+    public synchronized Object getObject() throws Exception {
+        if (terminal == null) {
+            terminal = Terminal.getTerminal();
+        }
+        return terminal;
+    }
+
+    public Class getObjectType() {
+        return Terminal.class;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    public synchronized void destroy() throws Exception {
+        if (terminal != null) {
+            if (terminal instanceof UnixTerminal) {
+                ((UnixTerminal) terminal).restoreTerminal();
+            }
+            terminal = null;
+        }
+    }
+}

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-commands.xml Wed Mar 19 07:30:39 2008
@@ -52,4 +52,6 @@
 
     <bean id="create" class="org.apache.geronimo.gshell.spring.CreateCommand" />
 
+    <bean id="grep" class="org.apache.geronimo.gshell.commands.utils.GrepCommand" />
+
 </beans>

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell-osgi.xml Wed Mar 19 07:30:39 2008
@@ -32,7 +32,7 @@
   http://www.springframework.org/schema/osgi-compendium
   http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd">
 
-    <osgi:list id="commands" interface="org.apache.geronimo.gshell.command.Command">
+    <osgi:list id="commands" interface="org.apache.geronimo.gshell.command.Command" cardinality="0..N">
         <osgi:listener ref="commandRegistry" bind-method="register" unbind-method="unregister" />
     </osgi:list>
 
@@ -92,6 +92,13 @@
         <osgi:service-properties>
             <entry key="shell" value="admin"/>
         	<entry key="alias" value="create"/>
+        </osgi:service-properties>
+    </osgi:service>
+
+    <osgi:service ref="grep" interface="org.apache.geronimo.gshell.command.Command">
+        <osgi:service-properties>
+            <entry key="shell" value="utils"/>
+        	<entry key="alias" value="grep"/>
         </osgi:service-properties>
     </osgi:service>
 

Modified: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell.xml
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell.xml?rev=638842&r1=638841&r2=638842&view=diff
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell.xml (original)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/main/resources/META-INF/spring/gshell.xml Wed Mar 19 07:30:39 2008
@@ -35,7 +35,7 @@
         <constructor-arg value="servicemix-version.properties" />
     </bean>
 
-    <bean id="terminal" class="jline.Terminal" factory-method="getTerminal" />
+    <bean id="terminal" class="org.apache.geronimo.gshell.spring.TerminalFactoryBean" />
 
     <bean id="promptReader" class="org.apache.geronimo.gshell.console.PromptReader" init-method="initialize">
         <constructor-arg ref="terminal" />
@@ -78,15 +78,11 @@
       </property>
     </bean>
 
-    <bean id="io" class="org.springframework.aop.framework.ProxyFactoryBean">
-      <property name="targetSource">
-          <bean class="org.apache.geronimo.gshell.spring.IOTargetSource" />
-      </property>
-    </bean>
+    <bean id="io" class="org.apache.geronimo.gshell.spring.ProxyIO" />
 
     <bean id="prompter" class="org.apache.geronimo.gshell.spring.Prompter">
         <constructor-arg ref="shellInfo" />
         <constructor-arg ref="environment" />
     </bean>
-    
+
 </beans>

Added: servicemix/smx4/kernel/trunk/gshell/gshell-core/src/test/java/org/apache/geronimo/gshell/spring/NoCloseInputStreamTest.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/gshell/gshell-core/src/test/java/org/apache/geronimo/gshell/spring/NoCloseInputStreamTest.java?rev=638842&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/gshell/gshell-core/src/test/java/org/apache/geronimo/gshell/spring/NoCloseInputStreamTest.java (added)
+++ servicemix/smx4/kernel/trunk/gshell/gshell-core/src/test/java/org/apache/geronimo/gshell/spring/NoCloseInputStreamTest.java Wed Mar 19 07:30:39 2008
@@ -0,0 +1,43 @@
+package org.apache.geronimo.gshell.spring;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+import junit.framework.TestCase;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: gnodet
+ * Date: Mar 18, 2008
+ * Time: 4:35:05 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class NoCloseInputStreamTest extends TestCase {
+
+    public void testStream() throws Exception {
+        final InputStream in = new NoCloseInputStream(System.in);
+        new Thread() {
+            public void run() {
+                try {
+                    int c;
+                    System.err.println("Reading from in...");
+                    while ((c = in.read()) != -1) {
+                        System.err.println("Read from in: " + c);
+                    }
+                    System.err.println("Exiting thread...");
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }.start();
+
+        Thread.sleep(2000);
+
+        in.close();
+
+        Thread.sleep(2000);
+
+    }
+}