package org.drools.guvnor;

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.Method;

/**
 *
 */
public class JettyLauncher {

    public void launch(String[] args) throws Exception {
        File jettyConf;
        if (args.length == 1) {
            jettyConf = new File(args[0]);
            if (!jettyConf.exists()) {
                throw new FileNotFoundException("Jetty config file " + args[1] + " not found!");
            }
        } else {
            jettyConf = new File("jetty.xml");
            if (!jettyConf.exists()) {
                throw new FileNotFoundException("jetty.xml must present in working directory!");
            }
        }

        try {
            Method mainMethod = getJettyServerClassName().getMethod("main", new Class[]{String[].class});
            mainMethod.invoke(null, new Object[]{new String[]{jettyConf.getAbsolutePath()}});
        }
        catch (ClassNotFoundException e) {
            System.err.println("You don't have Jetty in the classpath, cannot proceed!");
        }
    }

    private Class getJettyServerClassName() throws ClassNotFoundException {
        try {
            return Class.forName("org.mortbay.start.Main");// jetty 5
        }
        catch (ClassNotFoundException e) {
            return Class.forName("org.mortbay.jetty.Server");// jetty 4 and early versions
        }
    }

}

@@ -1142,32 +1142,351 @@
 %7B%0A        try %7B%0A
+            System.out.println(%22starting jetty6...%22);%0A            return Class.forName(%22org.mortbay.jetty.Main%22);// jetty 6%0A        %7D catch (ClassNotFoundException e1) %7B%0A            System.err.println(%22jetty6 failed: %22 + e1.getMessage());%0A            try %7B%0A                System.out.println(%22starting jetty5...%22);%0A    
             retu
@@ -1539,34 +1539,42 @@
 jetty 5%0A        
+   
+ 
 %7D%0A
+    
         catch (C
@@ -1588,32 +1588,33 @@
 FoundException e
+2
 ) %7B%0A            
@@ -1617,96 +1617,356 @@
     
-return Class.forName(%22org.mortbay.jetty.Server%22);// jetty 4 and early versions%0A        %7D
+    System.err.println(%22jetty5 failed: %22 + e1.getMessage());%0A                System.out.println(%22starting jetty4...%22);%0A                return Class.forName(%22org.mortbay.jetty.Server%22);// jetty 4 and early versions%0A            %7D%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        new JettyLauncher().launch(args);
 %0A   

support for jetty6