/*
 * 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 org.apache.karaf.main;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * Main class used to stop the root Karaf instance
 */
public class Stop {

    /**
     * Sends the shutdown command to the running karaf instance. Uses either a shut down port configured in config.properties or
     * the port from the shutdown port file.
     * 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ConfigProperties config = new ConfigProperties();
        if (config.shutdownPort == 0 && config.portFile != null) {
            config.shutdownPort = getPortFromShutdownPortFile(config.portFile);
        }
        if (config.shutdownPort > 0) {
            Socket s = new Socket(config.shutdownHost, config.shutdownPort);
            s.getOutputStream().write(config.shutdownCommand.getBytes());
            s.close();
        } else {
            System.err.println("Unable to find port...");
        }

    }

    private static int getPortFromShutdownPortFile(String portFile) throws FileNotFoundException, IOException {
        int port;
        BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(portFile)));
        String portStr = r.readLine();
        port = Integer.parseInt(portStr);
        r.close();
        return port;
    }

}

@@ -988,32 +988,66 @@
 utStreamReader;%0A
+import java.net.ConnectException;%0A
 import java.net.
@@ -1323,17 +1323,16 @@
 .%0A     *
- 
 %0A     * 
@@ -1559,24 +1559,46 @@
  != null) %7B%0A
+            try %7B%0A    
             
@@ -1665,16 +1665,403 @@
 tFile);%0A
+            %7D catch (FileNotFoundException fnfe) %7B%0A                System.err.println(config.portFile + %22 port file doesn't exist. The container is not running.%22);%0A                System.exit(3);%0A            %7D catch (IOException ioe) %7B%0A                System.err.println(%22Can't read %22 + config.portFile + %22 port file: %22 + ioe.getMessage());%0A                System.exit(4);%0A            %7D%0A
         
@@ -2123,16 +2123,60 @@
 cket s =
+ null;%0A            try %7B%0A                s =
  new Soc
@@ -2234,16 +2234,20 @@
         
+ 
+   
 s.getOut
@@ -2316,18 +2316,338 @@
     
-s.close();
+    System.exit(0);%0A            %7D catch (ConnectException connectException) %7B%0A                System.err.println(%22Can't connect to the container. The container is not running.%22);%0A                System.exit(1);%0A            %7D finally %7B%0A                if (s != null) %7B%0A                    s.close();%0A                %7D%0A            %7D
 %0A   
@@ -2714,24 +2714,52 @@
  port...%22);%0A
+            System.exit(2);%0A
         %7D%0A%0A 

Improve stop script with exit code and better messages