protected void setupShutdown()

in plugins/org.apache.karaf.eik.app/src/main/java/org/apache/karaf/eik/app/internal/LockManager.java [199:310]


   protected void setupShutdown(Properties props) {
       try {
           String pidFile = props.getProperty(KARAF_SHUTDOWN_PID_FILE);
           if (pidFile != null) {
               RuntimeMXBean rtb = ManagementFactory.getRuntimeMXBean();
               String processName = rtb.getName();
               Pattern pattern = Pattern.compile("^([0-9]+)@.+$", Pattern.CASE_INSENSITIVE);
               Matcher matcher = pattern.matcher(processName);
               if (matcher.matches()) {
                   int pid = Integer.parseInt(matcher.group(1));
                   Writer w = new OutputStreamWriter(new FileOutputStream(pidFile));
                   w.write(Integer.toString(pid));
                   w.close();
               }
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
       try {
           int port = Integer.parseInt(props.getProperty(KARAF_SHUTDOWN_PORT, "0"));
           String host = props.getProperty(KARAF_SHUTDOWN_HOST, "localhost");
           String portFile = props.getProperty(KARAF_SHUTDOWN_PORT_FILE);
           final String shutdown = props.getProperty(KARAF_SHUTDOWN_COMMAND, DEFAULT_SHUTDOWN_COMMAND);
           if (port >= 0) {
               shutdownSocket = new ServerSocket(port, 1, InetAddress.getByName(host));
               if (port == 0) {
                   port = shutdownSocket.getLocalPort();
               }
               if (portFile != null) {
                   Writer w = new OutputStreamWriter(new FileOutputStream(portFile));
                   w.write(Integer.toString(port));
                   w.close();
               }
               Thread thread = new Thread() {
                   @Override
                public void run() {
                       try {
                           while (true) {
                               // Wait for the next connection
                               Socket socket = null;
                               InputStream stream = null;
                               try {
                                   socket = shutdownSocket.accept();
                                   socket.setSoTimeout(10 * 1000);  // Ten seconds
                                   stream = socket.getInputStream();
                               } catch (AccessControlException ace) {
                                   LOG.log(Level.WARNING, "Karaf shutdown socket: security exception: "
                                                      + ace.getMessage(), ace);
                                   continue;
                               } catch (IOException e) {
                                   LOG.log(Level.SEVERE, "Karaf shutdown socket: accept: ", e);
                                   System.exit(1);
                               }

                               // Read a set of characters from the socket
                               StringBuilder command = new StringBuilder();
                               int expected = 1024; // Cut off to avoid DoS attack
                               while (expected < shutdown.length()) {
                                   if (random == null) {
                                       random = new Random();
                                   }
                                   expected += (random.nextInt() % 1024);
                               }
                               while (expected > 0) {
                                   int ch = -1;
                                   try {
                                       ch = stream.read();
                                   } catch (IOException e) {
                                       LOG.log(Level.WARNING, "Karaf shutdown socket:  read: ", e);
                                       ch = -1;
                                   }
                                   if (ch < 32) {  // Control character or EOF terminates loop
                                       break;
                                   }
                                   command.append((char) ch);
                                   expected--;
                               }

                               // Close the socket now that we are done with it
                               try {
                                   socket.close();
                               } catch (IOException e) {
                                   // Ignore
                               }

                               // Match against our command string
                               boolean match = command.toString().equals(shutdown);
                               if (match) {
                                   bundleContext.getBundle(SYSTEM_BUNDLE_ID).stop();
                                   break;
                               } else {
                                   LOG.log(Level.WARNING, "Karaf shutdown socket:  Invalid command '" +
                                                      command.toString() + "' received");
                               }
                           }
                       } catch (Exception e) {
                           e.printStackTrace();
                       } finally {
                           try {
                               shutdownSocket.close();
                           } catch (IOException e) {
                           }
                       }
                   }
               };
               thread.setDaemon(true);
               thread.start();
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }