[karaf] rename gshell to shell and rename submodules to avoid having the parent module name in their names

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@816769 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/karaf/shell/ssh/pom.xml b/karaf/shell/ssh/pom.xml
new file mode 100644
index 0000000..3166527
--- /dev/null
+++ b/karaf/shell/ssh/pom.xml
@@ -0,0 +1,102 @@
+<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.felix.karaf.shell</groupId>
+        <artifactId>shell</artifactId>
+        <version>0.9.0-SNAPSHOT</version>
+    </parent>
+
+    <groupId>org.apache.felix.karaf.shell</groupId>
+    <artifactId>org.apache.felix.karaf.shell.ssh</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.9.0-SNAPSHOT</version>
+    <name>Apache Felix Karaf :: Shell SSH</name>
+
+    <description>
+        Provides SSH support to the console
+    </description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix.karaf.shell</groupId>
+            <artifactId>org.apache.felix.karaf.shell.console</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.servicemix.bundles</groupId>
+            <artifactId>org.apache.servicemix.bundles.junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.sshd</groupId>
+            <artifactId>sshd-core</artifactId>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <resources>
+            <resource>
+                <directory>${pom.basedir}/src/main/resources</directory>
+                <includes>
+                    <include>**/*</include>
+                </includes>
+            </resource>
+        </resources>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <configuration>
+                    <instructions>
+                        <Bundle-SymbolicName>${artifactId}</Bundle-SymbolicName>
+                        <Export-Package>${pom.artifactId}*;version=${project.version}</Export-Package>
+                        <Import-Package>
+                            org.osgi.service.command,
+                            org.apache.felix.gogo.commands,
+                            org.apache.felix.karaf.shell.console,
+                            org.apache.sshd.server.keyprovider,
+                            org.apache.sshd.server.jaas,
+                            *
+                        </Import-Package>
+                        <Private-Package>org.apache.felix.karaf.jpm.*</Private-Package>
+                        <_versionpolicy>${bnd.version.policy}</_versionpolicy>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellCommandFactory.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellCommandFactory.java
new file mode 100644
index 0000000..4e0c290
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellCommandFactory.java
@@ -0,0 +1,94 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import org.apache.sshd.server.CommandFactory;
+import org.osgi.service.command.CommandProcessor;
+import org.osgi.service.command.CommandSession;
+
+public class ShellCommandFactory implements CommandFactory {
+
+    private CommandProcessor commandProcessor;
+
+    public void setCommandProcessor(CommandProcessor commandProcessor) {
+        this.commandProcessor = commandProcessor;
+    }
+
+    public Command createCommand(String command) {
+        return new ShellCommand(command);
+    }
+
+    public class ShellCommand implements Command {
+
+        private String command;
+        private InputStream in;
+        private OutputStream out;
+        private OutputStream err;
+        private ExitCallback callback;
+
+        public ShellCommand(String command) {
+            this.command = command;
+        }
+
+        public void setInputStream(InputStream in) {
+            this.in = in;
+        }
+
+        public void setOutputStream(OutputStream out) {
+            this.out = out;
+        }
+
+        public void setErrorStream(OutputStream err) {
+            this.err = err;
+        }
+
+        public void setExitCallback(ExitCallback callback) {
+            this.callback = callback;
+        }
+
+        public void start() throws IOException {
+            try {
+                CommandSession session = commandProcessor.createSession(in, new PrintStream(out), new PrintStream(err));
+                session.execute(command);
+            } catch (Exception e) {
+                throw (IOException) new IOException("Unable to start shell").initCause(e);
+            } finally {
+                close(in, out, err);
+                callback.onExit(0);
+            }
+        }
+
+    }
+
+    private static void close(Closeable... closeables) {
+        for (Closeable c : closeables) {
+            try {
+                c.close();
+            } catch (IOException e) {
+                // Ignore
+            }
+        }
+    }
+}
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java
new file mode 100644
index 0000000..f38cc1b
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/ShellFactoryImpl.java
@@ -0,0 +1,131 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.felix.karaf.shell.console.Completer;
+import org.apache.felix.karaf.shell.console.completer.AggregateCompleter;
+import org.apache.felix.karaf.shell.console.jline.Console;
+import org.apache.sshd.server.ShellFactory;
+import org.osgi.service.command.CommandProcessor;
+import org.osgi.service.command.CommandSession;
+
+/**
+ * SSHD {@link org.apache.sshd.server.ShellFactory} which provides access to Shell.
+ *
+ * @version $Rev: 731517 $ $Date: 2009-01-05 11:25:19 +0100 (Mon, 05 Jan 2009) $
+ */
+public class ShellFactoryImpl implements ShellFactory
+{
+    private CommandProcessor commandProcessor;
+    private List<Completer> completers;
+
+    public void setCommandProcessor(CommandProcessor commandProcessor) {
+        this.commandProcessor = commandProcessor;
+    }
+
+    public void setCompleters(List<Completer> completers) {
+        this.completers = completers;
+    }
+
+    public Shell createShell() {
+        return new ShellImpl();
+    }
+
+    public class ShellImpl implements Shell
+    {
+        private InputStream in;
+
+        private OutputStream out;
+
+        private OutputStream err;
+
+        private ExitCallback callback;
+
+        private boolean closed;
+
+        public void setInputStream(final InputStream in) {
+            this.in = in;
+        }
+
+        public void setOutputStream(final OutputStream out) {
+            this.out = out;
+        }
+
+        public void setErrorStream(final OutputStream err) {
+            this.err = err;
+        }
+
+        public void setExitCallback(ExitCallback callback) {
+            this.callback = callback;
+        }
+
+        public void start(final Environment env) throws IOException {
+            try {
+                Console console = new Console(commandProcessor,
+                                              in,
+                                              new PrintStream(out),
+                                              new PrintStream(err),
+                                              new SshTerminal(env),
+                                              new AggregateCompleter(completers),
+                                              new Runnable() {
+                                                  public void run() {
+                                                      destroy();
+                                                  }
+                                              });
+                CommandSession session = console.getSession();
+                session.put("APPLICATION", System.getProperty("karaf.name", "root"));
+                for (Map.Entry<String,String> e : env.getEnv().entrySet()) {
+                    session.put(e.getKey(), e.getValue());
+                }
+                new Thread(console).start();
+            } catch (Exception e) {
+                throw (IOException) new IOException("Unable to start shell").initCause(e);
+            }
+        }
+
+        public void destroy() {
+            if (!closed) {
+                closed = true;
+                ShellFactoryImpl.close(in, out, err);
+                callback.onExit(0);
+            }
+        }
+
+    }
+
+    private static void close(Closeable... closeables) {
+        for (Closeable c : closeables) {
+            try {
+                c.close();
+            } catch (IOException e) {
+                // Ignore
+            }
+        }
+    }
+
+}
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshAction.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshAction.java
new file mode 100644
index 0000000..a9e165f
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshAction.java
@@ -0,0 +1,149 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import java.io.IOException;
+
+import org.apache.sshd.ClientChannel;
+import org.apache.sshd.ClientSession;
+import org.apache.sshd.SshClient;
+import org.apache.sshd.client.future.ConnectFuture;
+import org.apache.sshd.common.util.NoCloseInputStream;
+import org.apache.sshd.common.util.NoCloseOutputStream;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.apache.felix.karaf.shell.console.BlueprintContainerAware;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+
+/**
+ * Connect to a SSH server.
+ *
+ * @version $Rev: 721244 $ $Date: 2008-11-27 18:19:56 +0100 (Thu, 27 Nov 2008) $
+ */
+@Command(scope = "ssh", name = "ssh", description = "Connect to a remote SSH server")
+public class SshAction
+    extends OsgiCommandSupport implements BlueprintContainerAware
+{
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Option(name="-l", aliases={"--username"}, description = "Username")
+    private String username;
+
+    @Option(name="-P", aliases={"--password"}, description = "Password")
+    private String password;
+
+    @Argument(required=true, description = "Host")
+    private String hostname;
+
+    @Option(name="-p", aliases={"--port"}, description = "Port")
+    private int port = 22;
+
+    private BlueprintContainer container;
+
+	private ClientSession session;
+    private String sshClientId;
+
+    public void setBlueprintContainer(final BlueprintContainer container) {
+        assert container != null;
+        this.container = container;
+    }
+
+    public void setSshClientId(String sshClientId) {
+        this.sshClientId = sshClientId;
+    }
+
+    @Override
+    protected Object doExecute() throws Exception {
+
+        //
+        // TODO: Parse hostname for <username>@<hostname>
+        //
+
+        System.out.println("Connecting to host " + hostname + " on port " + port);
+
+        // If the username/password was not configured via cli, then prompt the user for the values
+        if (username == null || password == null) {
+            log.debug("Prompting user for credentials");
+            if (username == null) {
+                username = readLine("Login: ");
+            }
+            if (password == null) {
+                password = readLine("Password: ");
+            }
+        }
+
+        // Create the client from prototype
+        SshClient client = (SshClient) container.getComponentInstance(sshClientId);
+        log.debug("Created client: {}", client);
+        client.start();
+
+        try {
+            ConnectFuture future = client.connect(hostname, port);
+            future.await();
+            session = future.getSession();
+            try {
+                System.out.println("Connected");
+
+                session.authPassword(username, password);
+                int ret = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
+                if ((ret & ClientSession.AUTHED) == 0) {
+                    System.err.println("Authentication failed");
+                    return null;
+                }
+
+                ClientChannel channel = session.createChannel("shell");
+                channel.setIn(new NoCloseInputStream(System.in));
+                channel.setOut(new NoCloseOutputStream(System.out));
+                channel.setErr(new NoCloseOutputStream(System.err));
+                channel.open();
+                channel.waitFor(ClientChannel.CLOSED, 0);
+            } finally {
+                session.close(false);
+            }
+        } finally {
+            client.stop();
+        }
+
+        return null;
+    }
+
+    public String readLine(String msg) throws IOException {
+        StringBuffer sb = new StringBuffer();
+        System.err.print(msg);
+        System.err.flush();
+        for (;;) {
+            int c = super.session.getKeyboard().read();
+            if (c < 0) {
+                return null;
+            }
+            System.err.print((char) c);
+            if (c == '\r' || c == '\n') {
+                break;
+            }
+            sb.append((char) c);
+        }
+        return sb.toString();
+    }
+
+}
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerAction.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerAction.java
new file mode 100644
index 0000000..a67eaf7
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerAction.java
@@ -0,0 +1,83 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import org.apache.sshd.SshServer;
+import org.apache.felix.karaf.shell.console.BlueprintContainerAware;
+import org.apache.felix.karaf.shell.console.OsgiCommandSupport;
+import org.apache.felix.gogo.commands.Option;
+import org.apache.felix.gogo.commands.Command;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+
+/**
+ * Start a SSH server.
+ *
+ * @version $Rev: 720411 $ $Date: 2008-11-25 05:32:43 +0100 (Tue, 25 Nov 2008) $
+ */
+@Command(scope = "ssh", name = "sshd", description = "Create an SSH server")
+public class SshServerAction extends OsgiCommandSupport implements BlueprintContainerAware
+{
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Option(name="-p", aliases={ "--port" }, description = "Port")
+    private int port = 8101;
+
+    @Option(name="-b", aliases={ "--background"}, description = "Background")
+    private boolean background = true;
+
+    private BlueprintContainer container;
+
+    private String sshServerId;
+
+    public void setBlueprintContainer(final BlueprintContainer container) {
+        assert container != null;
+        this.container = container;
+    }
+
+    public void setSshServerId(String sshServerId) {
+        this.sshServerId = sshServerId;
+    }
+
+    protected Object doExecute() throws Exception {
+        SshServer server = (SshServer) container.getComponentInstance(sshServerId);
+
+        log.debug("Created server: {}", server);
+
+        server.setPort(port);
+
+        server.start();
+
+        System.out.println("SSH server listening on port " + port);
+
+        if (!background) {
+            synchronized (this) {
+                log.debug("Waiting for server to shutdown");
+
+                wait();
+            }
+
+            server.stop();
+        }
+
+        return null;
+    }
+}
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerFactory.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerFactory.java
new file mode 100644
index 0000000..150da1f
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshServerFactory.java
@@ -0,0 +1,58 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import org.apache.sshd.SshServer;
+
+public class SshServerFactory {
+
+    private SshServer server;
+
+    private boolean start;
+
+    public SshServerFactory(SshServer server) {
+        this.server = server;
+    }
+
+    public boolean isStart() {
+        return start;
+    }
+
+    public void setStart(boolean start) {
+        this.start = start;
+    }
+
+    public void start() throws Exception {
+        if (start) {
+            try {
+                server.start();
+            } catch (Exception e) {
+                e.printStackTrace();
+                throw e;
+            }
+        }
+    }
+
+    public void stop() throws Exception {
+        if (start) {
+            server.stop();
+        }
+    }
+
+}
diff --git a/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshTerminal.java b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshTerminal.java
new file mode 100644
index 0000000..b8235af
--- /dev/null
+++ b/karaf/shell/ssh/src/main/java/org/apache/felix/karaf/shell/ssh/SshTerminal.java
@@ -0,0 +1,219 @@
+/*
+ * 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.felix.karaf.shell.ssh;
+
+import java.io.InputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+
+import jline.Terminal;
+import org.apache.sshd.server.ShellFactory;
+
+public class SshTerminal extends Terminal implements ShellFactory.SignalListener {
+
+    public static final short ARROW_START = 27;
+    public static final short ARROW_PREFIX = 91;
+    public static final short ARROW_LEFT = 68;
+    public static final short ARROW_RIGHT = 67;
+    public static final short ARROW_UP = 65;
+    public static final short ARROW_DOWN = 66;
+    public static final short O_PREFIX = 79;
+    public static final short HOME_CODE = 72;
+    public static final short END_CODE = 70;
+
+    public static final short DEL_THIRD = 51;
+    public static final short DEL_SECOND = 126;
+
+    private ShellFactory.Environment environment;
+    private boolean backspaceDeleteSwitched = false;
+    private String encoding = System.getProperty("input.encoding", "UTF-8");
+    private ReplayPrefixOneCharInputStream replayStream = new ReplayPrefixOneCharInputStream(encoding);
+    private InputStreamReader replayReader;
+
+    public SshTerminal(ShellFactory.Environment environment) {
+        this.environment = environment;
+        this.environment.addSignalListener(this);
+        try {
+            replayReader = new InputStreamReader(replayStream, encoding);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public void initializeTerminal() throws Exception {
+    }
+
+    public void restoreTerminal() throws Exception {
+    }
+
+    public int getTerminalWidth() {
+        return Integer.valueOf(this.environment.getEnv().get("COLUMNS"));
+    }
+
+    public int getTerminalHeight() {
+        return Integer.valueOf(this.environment.getEnv().get("LINES"));
+    }
+
+    public boolean isSupported() {
+        return true;
+    }
+
+    public boolean getEcho() {
+        return false;
+    }
+
+    public boolean isEchoEnabled() {
+        return false;
+    }
+
+    public void enableEcho() {
+    }
+
+    public void disableEcho() {
+    }
+
+    public void signal(int signal) {
+
+    }
+
+    public int readVirtualKey(InputStream in) throws IOException {
+        int c = readCharacter(in);
+
+        if (backspaceDeleteSwitched)
+            if (c == DELETE)
+                c = '\b';
+            else if (c == '\b')
+                c = DELETE;
+
+        // in Unix terminals, arrow keys are represented by
+        // a sequence of 3 characters. E.g., the up arrow
+        // key yields 27, 91, 68
+        if (c == ARROW_START) {
+		//also the escape key is 27
+		//thats why we read until we
+		//have something different than 27
+		//this is a bugfix, because otherwise
+		//pressing escape and than an arrow key
+		//was an undefined state
+		while (c == ARROW_START)
+            		c = readCharacter(in);
+            if (c == ARROW_PREFIX || c == O_PREFIX) {
+                c = readCharacter(in);
+                if (c == ARROW_UP) {
+                    return CTRL_P;
+                } else if (c == ARROW_DOWN) {
+                    return CTRL_N;
+                } else if (c == ARROW_LEFT) {
+                    return CTRL_B;
+                } else if (c == ARROW_RIGHT) {
+                    return CTRL_F;
+                } else if (c == HOME_CODE) {
+                    return CTRL_A;
+                } else if (c == END_CODE) {
+                    return CTRL_E;
+                } else if (c == DEL_THIRD) {
+                    c = readCharacter(in); // read 4th
+                    return DELETE;
+                }
+            }
+        }
+        // handle unicode characters, thanks for a patch from amyi@inf.ed.ac.uk
+        if (c > 128) {
+          // handle unicode characters longer than 2 bytes,
+          // thanks to Marc.Herbert@continuent.com
+            replayStream.setInput(c, in);
+//            replayReader = new InputStreamReader(replayStream, encoding);
+            c = replayReader.read();
+
+        }
+
+        return c;
+    }
+
+    /**
+     * This is awkward and inefficient, but probably the minimal way to add
+     * UTF-8 support to JLine
+     *
+     * @author <a href="mailto:Marc.Herbert@continuent.com">Marc Herbert</a>
+     */
+    static class ReplayPrefixOneCharInputStream extends InputStream {
+        byte firstByte;
+        int byteLength;
+        InputStream wrappedStream;
+        int byteRead;
+
+        final String encoding;
+
+        public ReplayPrefixOneCharInputStream(String encoding) {
+            this.encoding = encoding;
+        }
+
+        public void setInput(int recorded, InputStream wrapped) throws IOException {
+            this.byteRead = 0;
+            this.firstByte = (byte) recorded;
+            this.wrappedStream = wrapped;
+
+            byteLength = 1;
+            if (encoding.equalsIgnoreCase("UTF-8"))
+                setInputUTF8(recorded, wrapped);
+            else if (encoding.equalsIgnoreCase("UTF-16"))
+                byteLength = 2;
+            else if (encoding.equalsIgnoreCase("UTF-32"))
+                byteLength = 4;
+        }
+
+
+        public void setInputUTF8(int recorded, InputStream wrapped) throws IOException {
+            // 110yyyyy 10zzzzzz
+            if ((firstByte & (byte) 0xE0) == (byte) 0xC0)
+                this.byteLength = 2;
+            // 1110xxxx 10yyyyyy 10zzzzzz
+            else if ((firstByte & (byte) 0xF0) == (byte) 0xE0)
+                this.byteLength = 3;
+            // 11110www 10xxxxxx 10yyyyyy 10zzzzzz
+            else if ((firstByte & (byte) 0xF8) == (byte) 0xF0)
+                this.byteLength = 4;
+            else
+                throw new IOException("invalid UTF-8 first byte: " + firstByte);
+        }
+
+        public int read() throws IOException {
+            if (available() == 0)
+                return -1;
+
+            byteRead++;
+
+            if (byteRead == 1)
+                return firstByte;
+
+            return wrappedStream.read();
+        }
+
+        /**
+        * InputStreamReader is greedy and will try to read bytes in advance. We
+        * do NOT want this to happen since we use a temporary/"losing bytes"
+        * InputStreamReader above, that's why we hide the real
+        * wrappedStream.available() here.
+        */
+        public int available() {
+            return byteLength - byteRead;
+        }
+    }
+
+}
diff --git a/karaf/shell/ssh/src/main/resources/OSGI-INF/blueprint/shell-ssh.xml b/karaf/shell/ssh/src/main/resources/OSGI-INF/blueprint/shell-ssh.xml
new file mode 100644
index 0000000..10c0439
--- /dev/null
+++ b/karaf/shell/ssh/src/main/resources/OSGI-INF/blueprint/shell-ssh.xml
@@ -0,0 +1,104 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:cm="http://geronimo.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
+           xmlns:ext="http://geronimo.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0"
+           default-activation="lazy">
+
+    <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]"/>
+
+    <cm:property-placeholder persistent-id="org.apache.felix.karaf.shell">
+        <cm:default-properties>
+            <cm:property name="sshPort" value="8101"/>
+            <cm:property name="sshRealm" value="karaf"/>
+            <cm:property name="hostKey" value="${karaf.base}/etc/host.key"/>
+        </cm:default-properties>
+    </cm:property-placeholder>
+
+    <command-bundle xmlns="http://felix.apache.org/karaf/xmlns/shell/v1.0.0">
+        <command name="ssh/ssh">
+            <action class="org.apache.felix.karaf.shell.ssh.SshAction">
+                <property name="sshClientId">
+                    <bp:idref component-id="sshClient"/>
+                </property>
+            </action>
+        </command>
+        <command name="ssh/sshd">
+            <action class="org.apache.felix.karaf.shell.ssh.SshServerAction">
+                <property name="sshServerId">
+                    <bp:idref component-id="sshServer"/>
+                </property>
+            </action>
+        </command>
+    </command-bundle>
+
+    <bean id="sshClient" class="org.apache.sshd.SshClient" factory-method="setUpDefaultClient" scope="prototype">
+    </bean>
+
+    <bean id="sshServer" class="org.apache.sshd.SshServer" factory-method="setUpDefaultServer" scope="prototype">
+        <property name="port" value="${sshPort}"/>
+        <property name="shellFactory">
+            <bean class="org.apache.felix.karaf.shell.ssh.ShellFactoryImpl">
+                <property name="completers">
+                    <list>
+                        <ref component-id="commandCompleter"/>
+                    </list>
+                </property>
+                <property name="commandProcessor" ref="commandProcessor"/>
+            </bean>
+        </property>
+        <property name="commandFactory">
+            <bean class="org.apache.felix.karaf.shell.ssh.ShellCommandFactory">
+                <property name="commandProcessor" ref="commandProcessor"/>
+            </bean>
+        </property>
+        <property name="keyPairProvider" ref="keyPairProvider"/>
+        <property name="passwordAuthenticator" ref="passwordAuthenticator"/>
+    </bean>
+
+    <bean id="keyPairProvider" class="org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider">
+        <property name="path" value="${hostKey}"/>
+    </bean>
+    <bean id="passwordAuthenticator" class="org.apache.sshd.server.jaas.JaasPasswordAuthenticator">
+        <property name="domain" value="${sshRealm}"/>
+    </bean>
+
+    <bean id="sshServerFactory" class="org.apache.felix.karaf.shell.ssh.SshServerFactory" init-method="start"
+          destroy-method="stop" activation="eager">
+        <argument ref="sshServer"/>
+        <property name="start" value="$[karaf.startRemoteShell]"/>
+    </bean>
+
+    <reference id="commandProcessor" interface="org.osgi.service.command.CommandProcessor">
+    </reference>
+
+    <reference-list id="functions" filter="(&amp;(osgi.command.scope=*)(osgi.command.function=*))"
+                    availability="optional" activation="eager">
+        <reference-listener ref="commandCompleter"
+                            bind-method="register"
+                            unbind-method="unregister"/>
+    </reference-list>
+
+    <bean id="commandCompleter" class="org.apache.felix.karaf.shell.console.completer.CommandsCompleter">
+        <property name="bundleContext" ref="blueprintBundleContext" />
+    </bean>
+
+</blueprint>