Initial commit of OSGi Shell contribution. (FELIX-946)
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@783826 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/src/aQute/shell/telnet/TelnetShell.java b/gogo/src/aQute/shell/telnet/TelnetShell.java
new file mode 100644
index 0000000..6e66de6
--- /dev/null
+++ b/gogo/src/aQute/shell/telnet/TelnetShell.java
@@ -0,0 +1,104 @@
+/*
+ * 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 aQute.shell.telnet;
+
+import java.io.*;
+import java.net.*;
+import java.util.*;
+
+import org.osgi.service.command.*;
+import org.osgi.service.component.*;
+
+public class TelnetShell extends Thread {
+ boolean quit;
+ CommandProcessor processor;
+ ServerSocket server;
+ int port = 2019;
+ List<Handler> handlers = new ArrayList<Handler>();
+
+ protected void activate(ComponentContext context) {
+ String s = (String) context.getProperties().get("port");
+ if (s != null)
+ port = Integer.parseInt(s);
+ System.out.println("Telnet Listener at port " + port);
+ start();
+ }
+
+ protected void deactivate(ComponentContext ctx) throws Exception {
+ try {
+ quit = true;
+ server.close();
+ interrupt();
+ } catch (Exception e) {
+ // Ignore
+ }
+ }
+
+ public void run() {
+ int delay = 0;
+ try {
+ while (!quit)
+ try {
+ server = new ServerSocket(port);
+ delay = 5;
+ while (!quit) {
+ Socket socket = server.accept();
+ CommandSession session = processor.createSession(socket
+ .getInputStream(), new PrintStream(socket
+ .getOutputStream()), System.err);
+ Handler handler = new Handler(this, session, socket);
+ handlers.add(handler);
+ handler.start();
+ }
+ } catch (BindException be) {
+ delay += 5;
+ System.err.println("Can not bind to port " + port);
+ try {
+ Thread.sleep(delay * 1000);
+ } catch (InterruptedException e) {
+ // who cares?
+ }
+ } catch (Exception e) {
+ if (!quit)
+ e.printStackTrace();
+ } finally {
+ try {
+ server.close();
+ Thread.sleep(2000);
+ } catch (Exception ie) {
+ //
+ }
+ }
+
+ } finally {
+ try {
+ if (server != null)
+ server.close();
+ } catch (IOException e) {
+ //
+ }
+ for (Handler handler : handlers)
+ handler.close();
+ }
+ }
+
+ public void setProcessor(CommandProcessor processor) {
+ this.processor = processor;
+ }
+}