First let me apologize for the length email but I hope it has some juicy
details that may help get JettyLauncher working with Jetty6.  I pulled down
the source for the JettyLauncher plugin and tried to build it against Jetty6
and learned some things that may help get the launcher working with newer
versions of Jetty.  I'll continue trying to get it to work but someone with
more familiarity with the code might be able to do it faster so I thought
I'd share what I've found so far:

First, in Jetty6 there's no javax.servlet.jar and there's no
org.mortbay.jetty.jar.  Here are the details:

 1. javax.servlet.jar was renamed to servlet-api-2.5-6.0.2.jar (for Jetty
6.0.2, I imagine in Jetty 6.0.0 the JAR was called servlet-api-2.5-6.0.0.jar
).
 2. org.mortbay.jetty.jar was split up into two jars: jetty-6.0.2.jar and
jetty-util-6.0.2.jar.  Why versions were slapped directly onto the filename
is totally beyond me but I guess that's what we've got to live with.

Second, I tried creating a javax.servlet.jar and org.mortbay.jetty.jar to
see if I could hack the JettyLauncher into working with Jetty6 but fortunate
did not smile upon me.  It seems that the package org.mortbay.http was taken
out and this caused JettyLauncher's references to NCSARequestLog and
SocketListener to break.  More details:

 1. org.mortbay.http.NCSARequestLog is now org.mortbay.jetty.NCSARequestLog
 2. SocketListener was replaced with org.mortbay.jetty.bio.SocketConnector

Third, there seems to have been some other renaming and refactoring that
went on in Jetty6.  Namely:

 1. org.mortbay.jetty.servlet.WebApplicationContext is now
org.mortbay.jetty.webapp.WebAppContext
 2. Server.addWebApplication() no longer exists.  It looks like
WebAppContext ctx = new WebAppContext(server, contextPath,
webappContextRoot) may do the trick.
 3. The constructor Server(URL) no longer exists.  I stumbled upon
XmlConfiguration and it looks like you create an instance of
XmlConfiguration(URL) and then call xmlConfiguration.configure(server).
 4. The method Server.setRequestLog() no longer exists.  It looks like you
create a new RequestLogHandler(), then call handler.setRequestLog(new
NCSARequestLog()), and then call server.addHandler(handler).

I applied all of my recommended changes (all to PluginRunner.java) and all
of the compiler errors went away so now I can build the JettyLauncher plugin
against Jetty6 (although now the plugin won't work with earlier versions of
Jetty).  I get about 54 warnings but it's getting late so I thought I'd pass
along what I have so maybe someone else could help me work through this.
I've attached my version of PluginRunner.java.  You'll also need to update
your classpath so that the Jetty6 JAR files (replacements for
javax.servlet.jar and org.mortbay.jetty.jar described above) are found
before you can build the plugin.

I'll try to keep cranking away on this as I find time, please let me know if
you beat me to it!

Erik
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Jetty Launcher
 *
 * The Initial Developer of the Original Code is
 * Intelligent Works Incorporated.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * 
 *  [EMAIL PROTECTED]
 *
 * ***** END LICENSE BLOCK ***** */

package com.iw.plugins.jettyrunner;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.URL;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.mortbay.jetty.NCSARequestLog;
import org.mortbay.jetty.handler.RequestLogHandler;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.webapp.WebAppContext;
import org.mortbay.xml.XmlConfiguration;

/**
 *  Class that does the launching!
 * 
 * @author [EMAIL PROTECTED]
 * @version $Id: PluginRunner.java,v 1.16 2004/10/11 10:12:45 eelco12 Exp $
 */
public final class PluginRunner
{
        /** resource bundle for messages. */
        private static ResourceBundle resources;

        /** debug indicator. */
        private static boolean debug;

        /**
         * Pgm method.
         * @param args cmd args
         * @throws Exception
         */
        public static void main(String[] args)
        {
                // set xml parsing to non validating; will have problems 
otherwise with Jetty 5
                System.setProperty("org.mortbay.xml.XmlParser.NotValidating", 
"true");
                // if sys prop debugJettyPlugin == true, we show some extra 
debugging
                debug = Boolean.getBoolean("debugJettyPlugin");
                if (debug)
                        debugArguments(args);
                // parse the arguments and put them in a map
                Map argMap = getArgsAsMap(args);
                // do the launching
                launch(argMap);
        }

        /**
         * Launch the proper version of Jetty.
         * @param argMap arguments
         */
        private static void launch(Map argMap)
        {
                // should we use JettyPlus or the plain version of Jetty
                boolean useJettyPlus = Boolean.valueOf((String) 
argMap.get("-use_jetty_plus"))
                                .booleanValue();
                if (argMap.containsKey("-xml")) // if so, start up using an xml 
config file
                {
                        try
                        {
                                launchWithXML((String) argMap.get("-xml"), 
useJettyPlus);
                        }
                        catch (Exception e)
                        {
                                e.printStackTrace();
                        }
                }
                else if (argMap.containsKey("-context") && 
argMap.containsKey("-port"))
                { // otherwise startup plain
                        boolean useNCSALogging = Boolean.valueOf(
                                        (String) 
argMap.get("-use_ncsa_logging")).booleanValue();
                        try
                        {
                                launch((String) argMap.get("-host"), (String) 
argMap.get("-context"),
                                                (String) argMap.get("-port"), 
(String) argMap.get("-wContext"),
                                                useNCSALogging, (String) 
argMap.get("-web_defaults"));
                        }
                        catch (Exception e)
                        {
                                e.printStackTrace();
                        }
                }
                else
                // wrong argument set
                {
                        
System.err.println(getString("jetty-runner-invalid-args", argMap));
                        System.err.println(getString("jetty-runner-usage"));
                }
        }

        /**
         * Get command line args and put them in a map (args that start with 
'-' are
         *      interpreted as a key, everything else as their values.
         * @param args cmd arguments
         * @return Map with arguments as key/ value pairs
         */
        private static Map getArgsAsMap(String[] args)
        {
                Map argMap = new HashMap();
                int pos = 0;
                while (pos < args.length)
                {
                        String key = args[pos];
                        if (debug)
                                System.out.println("arg: " + key);
                        if (pos + 1 < args.length)
                        {
                                int peek = pos + 1; // peek position
                                int nbrOfValues = 1; // counter for number of 
values (we need it to add spaces)
                                StringBuffer val = new StringBuffer("");
                                // peek; we need this for arguments with spaces 
in them
                                while (peek < args.length)
                                {
                                        String temp = args[peek++];
                                        if (debug)
                                                System.out.println("peek: " + 
temp);
                                        if (!temp.startsWith("-"))
                                        {
                                                if (nbrOfValues > 1)
                                                {
                                                        val.append(" "); // add 
the original space
                                                }
                                                val.append(temp);
                                                nbrOfValues++;
                                        }
                                        else
                                        // next argument
                                        {
                                                break;
                                        }
                                }
                                if (debug)
                                        System.out.println("using arg: " + key 
+ " == " + val.toString());
                                argMap.put(key, val.toString());
                                pos = peek - 1;
                        }
                        else
                        { //there's an arg without a value; probably -wContext
                                pos++; // same as break
                                if (debug)
                                        System.out.println("- no value - for " 
+ key);
                        }
                }
                return argMap;
        }

        /**
         * Print args as one string.
         * @param args arguments
         */
        private static void debugArguments(String[] args)
        {
                String line = "";
                for (int i = 0; i < args.length; i++)
                {
                        line += " " + args[i];
                }
                System.out.println("cmd args:" + line);
        }

        /**
         * Launch Jetty using an XML file.
         * @param xmlFile the xml file location (absolute path)
         * @param useJettyPlus whether to use JettyPlus
         * @throws Exception
         */
        private static void launchWithXML(String xmlFile, boolean useJettyPlus)
                        throws Exception
        {
                File file = new File(xmlFile);
                if(file == null || (!file.isFile()))
                {
                    throw new Exception(xmlFile + " is not a file; unable to 
start Jetty!");
                }

                Server server = null;
                if (useJettyPlus)
                {
                        
System.out.println(getString("jetty-runner-launch-xml-plus", xmlFile));
                        // see if JNDI is properly configured
                        if ((System.getProperty("java.naming.factory.url.pkgs") 
== null)
                                        && 
(System.getProperty("java.naming.factory.initial") == null))
                        {
                                
System.out.println(getString("jetty-runner-setting-jndi"));
                                
System.setProperty("java.naming.factory.url.pkgs", "org.mortbay.jndi");
                                
System.setProperty("java.naming.factory.initial",
                                                
"org.mortbay.jndi.InitialContextFactory");
                        }
                        // dynamically load the class to avoid dependency 
loading problem with the runner
                        Class clazz = 
Class.forName("org.mortbay.jetty.plus.Server");
                        Constructor constructor = clazz.getConstructor(new 
Class[] {URL.class});
                        server = (Server) constructor.newInstance(new Object[] 
{});
                }
                else
                {
                        System.out.println(getString("jetty-runner-launch-xml", 
xmlFile));
                        server = new Server();
                }


                XmlConfiguration xmlConfig = new XmlConfiguration(file.toURL());
                xmlConfig.configure(server);
                
                // start server
                server.start();
        }

        /**
         * Launch without XML, using the given arguments.
         * @param theHost to use
         * @param webappContextRoot (project relative) path to use as webapp 
context root
         * @param port port to use
         * @param contextPath webapp context path
         * @param useNCSALogging whether to use NCSA logging
         * @param webDefaults location of web defaults file or null if none 
should be used
         * @throws Exception
         */
        private static void launch(String theHost, String webappContextRoot, 
String port,
                        String contextPath, boolean useNCSALogging, String 
webDefaults)
                        throws Exception
        {
                int iPort = -1;
                String host = theHost;
                if (host == null || host.trim().length() == 0)
                {
                        host = "0.0.0.0";
                }
                try
                {
                        iPort = Integer.parseInt(port);
                }
                catch (NumberFormatException e)
                {
                        
System.err.println(getString("jetty-runner-invalid-port", port));
                }
                if (iPort >= 0)
                {
                        if (contextPath == null || 
contextPath.trim().equals(""))
                        {
                                contextPath = "/";
                        }

                        Server server = null;
                        
System.out.println(getString("jetty-runner-launch-context",
                                        new String[]{webappContextRoot, host, 
port, contextPath,}));
                        server = new Server();

                        addLog(server, useNCSALogging);
                        addListener(host, iPort, server);
                        WebAppContext wctx = new WebAppContext(server, 
contextPath, webappContextRoot);
                        if (webDefaults != null)
                        {
                                System.out.println("using webdefaults: " + 
webDefaults);
                                wctx.setDefaultsDescriptor(webDefaults);
                        }
                        // start server
                        server.start();
                }
        }

        /**
         * Add HTTP listener.
         * @param port port
         * @param server server to add http listener to
         */
        private static void addListener(String host, int port, Server server)
        {
                SocketConnector listener = new SocketConnector();
                if (host == null || host.trim().length() == 0)
                {
                        listener.setHost("0.0.0.0");
                }
                else
                {
                        listener.setHost(host);
                }

                listener.setPort(port);
                listener.setAcceptors(10);
                listener.setMaxIdleTime(30000);
                listener.setLowResourceMaxIdleTime(2000);
                listener.setConfidentialPort(8443);
                server.addConnector(listener);
        }

        /**
         * add log.
         * @param server server instance
         * @param useNCSALogging whether to use ncsa logging
         */
        private static void addLog(Server server, boolean useNCSALogging) 
throws Exception
        {
                addLogAndSink();
                if (useNCSALogging)
                {
                        RequestLogHandler handler = new RequestLogHandler();
                        handler.setRequestLog(new NCSARequestLog());
                        server.addHandler(handler);
                }
        }

        /**
         * add log and sink.
         * @throws Exception
         */
        private static void addLogAndSink() throws Exception
        {
                int jettyVersion = 4;
                Object log = null;
                try
                {
                        log = getJettyUtilLog();
                }
                catch (Exception e)
                {
                        // try Jetty 5 (via commons logging
                        try
                        {
                                jettyVersion = 5;
                                log = getJettyCommonsLog();
                        }
                        catch (Exception e1)
                        {
                                e1.printStackTrace();
                                jettyVersion = -1;
                        }
                }
                Object logSink = getOutputStreamLogSink(jettyVersion);
                Class logSinkClass = logSink.getClass();
                Class logClass = log.getClass();
                // call start method
                Method startM = logSinkClass.getMethod("start", new Class[0]);
                startM.invoke(logSink, new Object[0]);
                // add log
                Method addM = logClass.getMethod("add", new Class[] 
{String.class});
                addM.invoke(log, new Object[] {logSinkClass.getName()});
        }

        /**
         * Get the log sink for the given version.
         * @param jettyVersion
         * @return Object log sink for the given version
         * @throws Exception
         */
        private static Object getOutputStreamLogSink(int jettyVersion) throws 
Exception
        {
                String className = null;
                if (jettyVersion == 4)
                {
                        className = "org.mortbay.util.OutputStreamLogSink";
                }
                else if (jettyVersion == 5)
                {
                        className = "org.mortbay.log.OutputStreamLogSink";
                }
                else
                {
                        throw new Exception("unsupported Jetty version");
                }
                Class sinkClass = Class.forName(className);
                Object sink = sinkClass.newInstance();
                return sink;
        }

        /**
         * Get Jetty Util (Jetty 4) log.
         * @return Object Log instance
         * @throws Exception
         */
        private static Object getJettyUtilLog() throws Exception
        {
                Class logClass = Class.forName("org.mortbay.util.Log");
                Method instanceM = logClass.getMethod("instance", new Class[0]);
                Object log = instanceM.invoke(null, new Object[0]);
                return log;
        }

        /**
         * Get and configure Commons (Jetty 5) log.
         * @return Object Log instance
         * @throws Exception
         */
        private static Object getJettyCommonsLog() throws Exception
        {
                // release current factories
                // LogFactory.releaseAll();
                Class lfClass = 
Class.forName("org.apache.commons.logging.LogFactory");
                Method releaseAllM = lfClass.getMethod("releaseAll", new 
Class[0]);
                releaseAllM.invoke(null, new Object[0]);
                // set factory property
                System.setProperty("org.apache.commons.logging.LogFactory",
                                "org.mortbay.log.Factory");
                // let commons logging instantiate the factory
                // LogFactory logFactory = LogFactory.getFactory();
                Method getFactoryM = lfClass.getMethod("getFactory", new 
Class[0]);
                Object logFactory = getFactoryM.invoke(null, new Object[0]);
                Method getInstanceM = 
logFactory.getClass().getMethod("getInstance",
                                new Class[] {String.class});
                //Log log = 
logFactory.getInstance("com.iw.plugins.jettylauncher.JettyPlus");
                Object log = getInstanceM.invoke(logFactory,
                                new Object[] 
{"com.iw.plugins.jettylauncher.JettyPlus"});
                // call reset method
                Method resetM = log.getClass().getMethod("reset", new Class[0]);
                resetM.invoke(log, new Object[0]);
                return log;
        }

        /**
         * Get string from messagebundle.
         * @param key key messagebundle
         * @return String message
         */
        public static String getString(String key)
        {
                return getString(key, null);
        }

        /**
         * Get string from messagebundle.
         * @param key key messagebundle
         * @param arg arg for {0}
         * @return String message
         */
        public static String getString(String key, Object arg)
        {
                return getString(key, new Object[] {arg});
        }

        /**
         * Get string from messagebundle.
         * @param key key messagebundle
         * @param arg1 arg for {0}
         * @param arg2 arg for {1}
         * @return String message
         */
        public static String getString(String key, Object arg1, Object arg2)
        {
                return getString(key, new Object[] {arg1, arg2});
        }

        /**
         * Get string from messagebundle.
         * @param key key messagebundle
         * @param arg1 arg for {0}
         * @param arg2 arg for {1}
         * @param arg3 arg for {2}
         * @return String message
         */
        public static String getString(String key, Object arg1, Object arg2, 
Object arg3)
        {
                return getString(key, new Object[] {arg1, arg2, arg3});
        }

        /**
         * Get string from messagebundle.
         * @param key key messagebundle
         * @param args args for {0..(args.length - 1)}
         * @return String message
         */
        public static String getString(String key, Object[] args)
        {
                if (resources == null)
                        resources = 
ResourceBundle.getBundle("com.iw.plugins.jettyrunner.resources");
                try
                {
                        String pattern = resources.getString(key);
                        if (args == null)
                                return pattern;

                        return MessageFormat.format(pattern, args);
                }
                catch (MissingResourceException e)
                {
                        return "!" + key + "!";
                }
        }
}
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Jettylauncher-plugin mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jettylauncher-plugin

Reply via email to