/*
 * 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.geronimo.gshell.bootstrap;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * Bootstrap launcher.
 *
 * @version $Rev$ $Date$
 */
public class Launcher
{
    private static final int SUCCESS_EXIT_CODE = 0;

    private static final int FAILURE_EXIT_CODE = 100;

    private static final String MAIN_CLASS = "org.apache.geronimo.gshell.cli.Main";

    public static void main(final String[] args) {
        assert args != null;

        Configuration config = new Configuration();

        try {
            Log.debug("Configuring");
            
            config.configure();

            ClassLoader cl = config.getClassLoader();
            Class type = cl.loadClass(MAIN_CLASS);
            Method method = getMainMethod(type);

            Thread.currentThread().setContextClassLoader(cl);

            if (Log.DEBUG) {
                Log.debug("Launching: " + method);
            }

            method.invoke(null, new Object[] { args });

            Log.debug("Exiting");

            System.exit(SUCCESS_EXIT_CODE);
        }
        catch (Throwable t) {
            t.printStackTrace(System.err);
            System.err.flush();
            System.exit(FAILURE_EXIT_CODE);
        }
    }

    private static Method getMainMethod(final Class type) throws Exception {
        assert type != null;

        Method method = type.getMethod("main", String[].class);
        int modifiers = method.getModifiers();

        if (Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers)) {
            Class returns = method.getReturnType();
            if (returns == Integer.TYPE || returns == Void.TYPE) {
                return method;
            }
        }

        throw new NoSuchMethodException("public static void main(String[] args) in " + type);
    }
}

@@ -1286,72 +1286,424 @@
     
-Configuration config = new Configuration();%0A%0A        try
+try %7B%0A            launch(args);%0A%0A            Log.debug(%22Exiting%22);%0A%0A            System.exit(SUCCESS_EXIT_CODE);%0A        %7D%0A        catch (Throwable t) %7B%0A            Log.debug(%22Failure: %22 + t);%0A            %0A            t.printStackTrace(System.err);%0A            System.err.flush();%0A            System.exit(FAILURE_EXIT_CODE);%0A        %7D%0A    %7D%0A%0A    public static void launch(final String%5B%5D args) throws Exception
  %7B%0A     
     
@@ -1690,39 +1690,65 @@
 xception %7B%0A     
+  
+ assert args != null;%0A%0A
        
+ 
 Log.debug(%22Confi
@@ -1757,24 +1757,25 @@
 ring%22);%0A
+%0A
         
     %0A   
@@ -1762,33 +1762,68 @@
 );%0A%0A        
-    %0A    
+Configuration config = new Configuration();%0A
         conf
@@ -1843,28 +1843,24 @@
 );%0A%0A        
-  
-  
 ClassLoader 
@@ -1885,28 +1885,24 @@
 ssLoader();%0A
-    
         Clas
@@ -1940,28 +1940,24 @@
 S);%0A        
-    
 Method metho
@@ -1990,20 +1990,16 @@
         
-  
-  
 Thread.c
@@ -2037,28 +2037,24 @@
 oader(cl);%0A%0A
-    
         if (
@@ -2070,36 +2070,32 @@
 ) %7B%0A            
-    
 Log.debug(%22Launc
@@ -2117,35 +2117,27 @@
 d);%0A        
-  
-  
 %7D%0A%0A
-    
         meth
@@ -2179,257 +2179,8 @@
  %7D);
-%0A%0A            Log.debug(%22Exiting%22);%0A%0A            System.exit(SUCCESS_EXIT_CODE);%0A        %7D%0A        catch (Throwable t) %7B%0A            t.printStackTrace(System.err);%0A            System.err.flush();%0A            System.exit(FAILURE_EXIT_CODE);%0A        %7D
 %0A   

Allow System.exit() to be skipped (for perf tests)