| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| TelnetDaemon |
|
| 3.5;3.5 | ||||
| TelnetDaemon$1 |
|
| 3.5;3.5 |
| 1 | /** | |
| 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 | */ | |
| 17 | package org.apache.xbean.terminal.telnet; | |
| 18 | ||
| 19 | import java.io.IOException; | |
| 20 | import java.net.ServerSocket; | |
| 21 | import java.net.Socket; | |
| 22 | ||
| 23 | 0 | public class TelnetDaemon implements Runnable { |
| 24 | ||
| 25 | private final TelnetShell shell; | |
| 26 | private final int port; | |
| 27 | ||
| 28 | private ServerSocket serverSocket; | |
| 29 | ||
| 30 | /** | |
| 31 | * We start out in a "stopped" state until someone calls the start method. | |
| 32 | */ | |
| 33 | 0 | private boolean stop = true; |
| 34 | ||
| 35 | 0 | public TelnetDaemon(String serverName, int port) { |
| 36 | 0 | this.port = port; |
| 37 | 0 | this.shell = new TelnetShell(serverName); |
| 38 | 0 | } |
| 39 | ||
| 40 | public void start() throws Exception { | |
| 41 | 0 | synchronized (this) { |
| 42 | // Don't bother if we are already started/starting | |
| 43 | 0 | if (!stop) |
| 44 | 0 | return; |
| 45 | ||
| 46 | 0 | stop = false; |
| 47 | ||
| 48 | // Do our stuff | |
| 49 | try { | |
| 50 | 0 | serverSocket = new ServerSocket(port, 20); |
| 51 | 0 | Thread d = new Thread(this); |
| 52 | 0 | d.setName("service.shell@" + d.hashCode()); |
| 53 | 0 | d.setDaemon(true); |
| 54 | 0 | d.start(); |
| 55 | 0 | } catch (Exception e) { |
| 56 | 0 | throw new Exception("Service failed to start.", e); |
| 57 | 0 | } |
| 58 | 0 | } |
| 59 | 0 | } |
| 60 | ||
| 61 | public void stop() throws Exception { | |
| 62 | 0 | synchronized (this) { |
| 63 | 0 | if (stop) { |
| 64 | 0 | return; |
| 65 | } | |
| 66 | 0 | stop = true; |
| 67 | try { | |
| 68 | 0 | this.notifyAll(); |
| 69 | 0 | } catch (Throwable t) { |
| 70 | 0 | t.printStackTrace(); |
| 71 | 0 | } |
| 72 | 0 | } |
| 73 | 0 | } |
| 74 | ||
| 75 | public synchronized void service(final Socket socket) throws IOException { | |
| 76 | 0 | Thread d = new Thread(new Runnable() { |
| 77 | public void run() { | |
| 78 | try { | |
| 79 | 0 | shell.service(socket); |
| 80 | 0 | } catch (SecurityException e) { |
| 81 | 0 | } catch (Throwable e) { |
| 82 | } finally { | |
| 83 | 0 | try { |
| 84 | 0 | if (socket != null) |
| 85 | 0 | socket.close(); |
| 86 | 0 | } catch (Throwable t) { |
| 87 | 0 | } |
| 88 | 0 | } |
| 89 | 0 | } |
| 90 | }); | |
| 91 | 0 | d.setDaemon(true); |
| 92 | 0 | d.start(); |
| 93 | 0 | } |
| 94 | ||
| 95 | public void run() { | |
| 96 | ||
| 97 | 0 | Socket socket = null; |
| 98 | ||
| 99 | 0 | while (!stop) { |
| 100 | try { | |
| 101 | 0 | socket = serverSocket.accept(); |
| 102 | 0 | socket.setTcpNoDelay(true); |
| 103 | 0 | if (!stop) service(socket); |
| 104 | 0 | } catch (SecurityException e) { |
| 105 | 0 | } catch (Throwable e) { |
| 106 | 0 | } |
| 107 | } | |
| 108 | 0 | } |
| 109 | ||
| 110 | } |