Richard S. Hall | fbd735b | 2009-06-11 16:07:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package aQute.shell.telnet; |
| 20 | |
| 21 | import java.io.*; |
| 22 | import java.net.*; |
| 23 | import java.util.*; |
| 24 | |
| 25 | import org.osgi.service.command.*; |
| 26 | import org.osgi.service.component.*; |
| 27 | |
| 28 | public class TelnetShell extends Thread { |
| 29 | boolean quit; |
| 30 | CommandProcessor processor; |
| 31 | ServerSocket server; |
| 32 | int port = 2019; |
| 33 | List<Handler> handlers = new ArrayList<Handler>(); |
| 34 | |
| 35 | protected void activate(ComponentContext context) { |
| 36 | String s = (String) context.getProperties().get("port"); |
| 37 | if (s != null) |
| 38 | port = Integer.parseInt(s); |
| 39 | System.out.println("Telnet Listener at port " + port); |
| 40 | start(); |
| 41 | } |
| 42 | |
| 43 | protected void deactivate(ComponentContext ctx) throws Exception { |
| 44 | try { |
| 45 | quit = true; |
| 46 | server.close(); |
| 47 | interrupt(); |
| 48 | } catch (Exception e) { |
| 49 | // Ignore |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void run() { |
| 54 | int delay = 0; |
| 55 | try { |
| 56 | while (!quit) |
| 57 | try { |
| 58 | server = new ServerSocket(port); |
| 59 | delay = 5; |
| 60 | while (!quit) { |
| 61 | Socket socket = server.accept(); |
| 62 | CommandSession session = processor.createSession(socket |
| 63 | .getInputStream(), new PrintStream(socket |
| 64 | .getOutputStream()), System.err); |
| 65 | Handler handler = new Handler(this, session, socket); |
| 66 | handlers.add(handler); |
| 67 | handler.start(); |
| 68 | } |
| 69 | } catch (BindException be) { |
| 70 | delay += 5; |
| 71 | System.err.println("Can not bind to port " + port); |
| 72 | try { |
| 73 | Thread.sleep(delay * 1000); |
| 74 | } catch (InterruptedException e) { |
| 75 | // who cares? |
| 76 | } |
| 77 | } catch (Exception e) { |
| 78 | if (!quit) |
| 79 | e.printStackTrace(); |
| 80 | } finally { |
| 81 | try { |
| 82 | server.close(); |
| 83 | Thread.sleep(2000); |
| 84 | } catch (Exception ie) { |
| 85 | // |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | } finally { |
| 90 | try { |
| 91 | if (server != null) |
| 92 | server.close(); |
| 93 | } catch (IOException e) { |
| 94 | // |
| 95 | } |
| 96 | for (Handler handler : handlers) |
| 97 | handler.close(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public void setProcessor(CommandProcessor processor) { |
| 102 | this.processor = processor; |
| 103 | } |
| 104 | } |