public void run()

in src/main/java/org/apache/sling/launchpad/app/ControlListener.java [177:301]


    public void run() {
        this.configure(false);

        final ServerSocket server;
        try {
            server = new ServerSocket();
            server.bind(this.socketAddress);
            writePortToConfigFile(getConfigFile(),
                new InetSocketAddress(server.getInetAddress(), server.getLocalPort()), this.secretKey);
            Thread.currentThread().setName(
                "Apache Sling Control Listener@" + server.getInetAddress() + ":" + server.getLocalPort());
            Main.info("Apache Sling Control Listener started", null);
        } catch (final IOException ioe) {
            Main.error("Failed to start Apache Sling Control Listener", ioe);
            return;
        }

        long delay = 0;

        try {
            while (true) {

                final Socket s;
                try {
                    s = server.accept();
                } catch (IOException ioe) {
                    // accept terminated, most probably due to Socket.close()
                    // just end the loop and exit
                    break;
                }

                // delay processing after unsuccessful attempts
                if (delay > 0) {
                    Main.info(s.getRemoteSocketAddress() + ": Delay: " + (delay / 1000), null);
                    try {
                        Thread.sleep(delay);
                    } catch (InterruptedException e) {
                    }
                }

                try {
                    String commandLine = null;
                    try {
                        commandLine = readLine(s);
                    } catch (SocketException ignore) {
                        Main.error("Failure in accessing a socket", ignore);
                        continue;
                    }

                    if (commandLine == null) {
                        final String msg = "ERR: missing command";
                        writeLine(s, msg);
                        continue;
                    }

                    final int blank = commandLine.indexOf(' ');
                    if (blank < 0) {
                        final String msg = "ERR: missing key";
                        writeLine(s, msg);
                        continue;
                    }

                    if (!secretKey.equals(commandLine.substring(0, blank))) {
                        final String msg = "ERR: wrong key";
                        writeLine(s, msg);
                        delay = (delay > 0) ? delay * 2 : 1000L;
                        continue;
                    }

                    final String command = commandLine.substring(blank + 1);
                    Main.info(s.getRemoteSocketAddress() + ">" + command, null);

                    if (COMMAND_STOP.equals(command)) {
                        if (this.shutdownThread != null) {
                            writeLine(s, RESPONSE_STOPPING);
                        } else {
                            this.shutdownThread = new Thread("Apache Sling Control Listener: Shutdown") {
                                @Override
                                public void run() {
                                    slingMain.doStop();
                                    try {
                                        server.close();
                                    } catch (final IOException ignore) {
                                    }
                                }
                            };
                            this.shutdownThread.start();
                            writeLine(s, RESPONSE_OK);
                        }

                    } else if (COMMAND_STATUS.equals(command)) {
                        writeLine(s, (this.shutdownThread == null) ? RESPONSE_OK : RESPONSE_STOPPING);

                    } else if (COMMAND_THREADS.equals(command)) {
                        dumpThreads(s);

                    } else {
                        final String msg = "ERR:" + command;
                        writeLine(s, msg);

                    }
                } finally {
                    try {
                        s.close();
                    } catch (IOException ignore) {
                    }
                }
            }
        } catch (final IOException ioe) {
            Main.error("Failure reading from client", ioe);
        } finally {
            try {
                server.close();
            } catch (final IOException ignore) {
            }
        }

        getConfigFile().delete();

        // everything has stopped and when this thread terminates,
        // the VM should stop. If there are still some non-daemon threads
        // active, this will not happen, so we force this here ...
        Main.info("Apache Sling terminated, exiting Java VM", null);
        this.slingMain.terminateVM(0);
    }