Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | import java.io.BufferedReader; |
| 20 | import java.io.IOException; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 21 | import java.net.Socket; |
| 22 | |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 23 | import org.apache.felix.shell.ShellService; |
Richard S. Hall | fd9c883 | 2010-09-20 19:28:38 +0000 | [diff] [blame] | 24 | import org.apache.felix.service.command.CommandProcessor; |
| 25 | import org.apache.felix.service.command.CommandSession; |
Felix Meschberger | d7c324d | 2008-08-19 14:10:48 +0000 | [diff] [blame] | 26 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Implements the shell. |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 29 | * <p> |
| 30 | * This class is instantiated by the {@link Listener} thread to handle a single |
| 31 | * remote connection in its own thread. The connection handler thread either |
| 32 | * terminates on request by the remote end or by the Remote Shell bundle being |
| 33 | * stopped. In the latter case, the {@link #terminate()} method is called, which |
| 34 | * closes the Socket used to handle the remote console. This causes a |
| 35 | * <code>SocketException</code> in the handler thread reading from the socket |
| 36 | * which in turn causes the {@link #run()} method to terminate and thus to |
| 37 | * end the handler thread. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 38 | */ |
| 39 | class Shell implements Runnable |
| 40 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 41 | private final Listener m_owner; |
| 42 | private final Socket m_socket; |
| 43 | private final AtomicInteger m_useCounter; |
| 44 | private volatile TerminalPrintStream m_out; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 45 | |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 46 | public Shell(Listener owner, Socket s, AtomicInteger counter) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 47 | { |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 48 | m_owner = owner; |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 49 | m_socket = s; |
| 50 | m_useCounter = counter; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 51 | }//constructor |
| 52 | |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 53 | void terminate() |
| 54 | { |
| 55 | // called by Listener.deactivate() to terminate this session |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 56 | exit("\r\nFelix Remote Shell Console Terminating"); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 57 | }//terminate |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 58 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Runs the shell. |
| 61 | */ |
| 62 | public void run() |
| 63 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 64 | m_owner.registerConnection(this); |
| 65 | |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 66 | String msg = null; |
| 67 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 68 | try |
| 69 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 70 | m_out = new TerminalPrintStream( |
| 71 | m_owner.getServices(), m_socket.getOutputStream()); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 72 | |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 73 | Object obj = null; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 74 | |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 75 | if ((obj = m_owner.getServices().getCommandProcessor(ServiceMediator.NO_WAIT)) |
| 76 | != null) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 77 | { |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 78 | CommandProcessor cp = (CommandProcessor) obj; |
| 79 | CommandSession session = |
| 80 | cp.createSession(m_socket.getInputStream(), m_out, m_out); |
| 81 | startGogoShell(session); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 82 | } |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 83 | else if ((obj = m_owner.getServices().getShellService(ServiceMediator.NO_WAIT)) |
| 84 | != null) |
| 85 | { |
| 86 | startFelixShell(); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | msg = "No shell services available...exiting."; |
| 91 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 92 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 93 | catch (IOException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 94 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 95 | m_owner.getServices().error("Shell::run()", ex); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 96 | } |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 97 | finally |
| 98 | { |
| 99 | // no need to clean up in/out, since exit does it all |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 100 | exit(msg); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 101 | } |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 102 | }//run |
| 103 | |
Richard S. Hall | 6f5381e | 2010-08-17 14:49:44 +0000 | [diff] [blame] | 104 | private void startGogoShell(CommandSession session) |
| 105 | { |
| 106 | try |
| 107 | { |
| 108 | session.execute("gosh --login --noshutdown"); |
| 109 | } |
| 110 | catch (Exception e) |
| 111 | { |
| 112 | e.printStackTrace(); |
| 113 | } |
| 114 | finally |
| 115 | { |
| 116 | session.close(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | private void startFelixShell() throws IOException |
| 121 | { |
| 122 | BufferedReader in = new BufferedReader( |
| 123 | new TerminalReader(m_socket.getInputStream(), m_out)); |
| 124 | ReentrantLock lock = new ReentrantLock(); |
| 125 | |
| 126 | // Print welcome banner. |
| 127 | m_out.println(); |
| 128 | m_out.println("Felix Remote Shell Console:"); |
| 129 | m_out.println("============================"); |
| 130 | m_out.println(""); |
| 131 | |
| 132 | do |
| 133 | { |
| 134 | String line = ""; |
| 135 | try |
| 136 | { |
| 137 | m_out.print("-> "); |
| 138 | line = in.readLine(); |
| 139 | //make sure to capture end of stream |
| 140 | if (line == null) |
| 141 | { |
| 142 | m_out.println("exit"); |
| 143 | return; |
| 144 | } |
| 145 | } |
| 146 | catch (Exception ex) |
| 147 | { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | line = line.trim(); |
| 152 | if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("disconnect")) |
| 153 | { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | ShellService shs = (ShellService) |
| 158 | m_owner.getServices().getShellService(ServiceMediator.NO_WAIT); |
| 159 | try |
| 160 | { |
| 161 | lock.acquire(); |
| 162 | shs.executeCommand(line, m_out, m_out); |
| 163 | } |
| 164 | catch (Exception ex) |
| 165 | { |
| 166 | m_owner.getServices().error("Shell::run()", ex); |
| 167 | } |
| 168 | finally |
| 169 | { |
| 170 | lock.release(); |
| 171 | } |
| 172 | } |
| 173 | while (true); |
| 174 | } |
| 175 | |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 176 | private void exit(String message) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 177 | { |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 178 | // farewell message |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 179 | if (message != null) |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 180 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 181 | m_out.println(message); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 182 | } |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 183 | m_out.println("Good Bye!"); |
| 184 | m_out.close(); |
Felix Meschberger | 73bf123 | 2009-02-02 08:28:42 +0000 | [diff] [blame] | 185 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 186 | try |
| 187 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 188 | m_socket.close(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 189 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 190 | catch (IOException ex) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 191 | { |
Richard S. Hall | 55900ec | 2009-04-25 16:57:58 +0000 | [diff] [blame] | 192 | m_owner.getServices().error("Shell::exit()", ex); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 193 | } |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 194 | m_owner.unregisterConnection(this); |
| 195 | m_useCounter.decrement(); |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 196 | }//exit |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 197 | }//class Shell |