User: jules_gosnell
  Date: 01/04/30 15:44:49

  Modified:    jetty/src/main/org/jboss/jetty JettyService.java
                        JettyServiceMBean.java
  Added:       jetty/src/main/org/jboss/jetty Jetty.java
  Log:
  integrate Jetty's new JMX support
  begin working on support for ejb-ref
  
  Revision  Changes    Path
  1.8       +168 -189  contrib/jetty/src/main/org/jboss/jetty/JettyService.java
  
  Index: JettyService.java
  ===================================================================
  RCS file: /cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/JettyService.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JettyService.java 2000/12/16 15:29:59     1.7
  +++ JettyService.java 2001/04/30 22:44:49     1.8
  @@ -26,26 +26,44 @@
   import com.mortbay.HTTP.WebApplicationContext;
   import com.mortbay.Util.XmlConfiguration;
   
  +import com.mortbay.Jetty.JMX.HttpServerMBean;
  +
   /**
    *   A service to launch jetty from JMX.
    *      
    *   @see <related>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Julian Gosnell</a>
  - *   @version $Revision: 1.7 $
  + *   @version $Revision: 1.8 $
    */
  +
  +class JettyMBean extends HttpServerMBean
  +{
  +  public JettyMBean(Jetty jetty)
  +    throws MBeanException, InstanceNotFoundException
  +  {
  +    super(jetty);
  +  }
  +
  +  protected String
  +    newObjectName(String prefix, MBeanServer server)
  +  {
  +    return uniqueObjectName(server, prefix);
  +  }
  +}
  +
   public class JettyService extends ServiceMBeanSupport
     implements JettyServiceMBean, MBeanRegistration
   {
     class JettyLog extends Log
     {
       Object _source=null;     // should really be in same package as Log
  -
  +    
       public
         JettyLog()
       {
         super();
       }
  -
  +    
       public
         JettyLog(Object source)
       {
  @@ -61,103 +79,89 @@
     }
     
     public static final String NAME = "Jetty";
  -
  -  Log          _log        = new JettyLog(getName());
  -  JBossLogSink _logSink    = new JBossLogSink();
  -  HttpServer   _server     = null;
  -  String       _home       = null;
  -  Vector       _configs    = new Vector();
  -  Hashtable    _deployed   = new Hashtable();
  -  boolean      _unpackWars = false;
  -
  +  
  +  Log          _log     = new JettyLog(getName());
  +  JBossLogSink _logSink = new JBossLogSink();
  +  JettyMBean   _mbean   = null;
  +  Jetty        _jetty  = null;
  +  
     protected void
       ensureService()
       throws Exception
     {
  -    Log.setLog(_log);
  -    Logger.log("Testing if Jetty is present....");
  -             
  -    try
       {
  -      // get hold of JETTY_HOME...
  -      Class jettyClass;
  -      try {      
  -         jettyClass = Class.forName("com.mortbay.HTTP.HttpServer");
  -         
  -      } catch (Exception e) {
  -         
  -         Logger.log("failed");
  -         Logger.log("Jetty not found.  You need jetty 3+");
  -         throw new Exception("start failed");
  -      }
  -               
  -      URL jettyUrl = jettyClass.getProtectionDomain().getCodeSource().getLocation();
  -      _home = new File(new File(jettyUrl.getFile()).getParent()).getParent();
  -      System.setProperty("jetty.home", _home);
  -
  -      // tell Jetty to use JBoss' XML Parser - Yeugghhhhhh !
  -      //      System.setProperty("org.xml.sax.parser", "com.sun.xml.parser.Parser");
  -      
  -      // connect Jetty and JBoss log models...
         try{_logSink.initialize(_log);}catch(Exception e){e.printStackTrace();}
         _logSink.start();
  +      com.mortbay.Util.Log.instance().disableLog(); // remove default logger
         com.mortbay.Util.Log.instance().add(_logSink);
  -      
  -      // load properties  (xml parser)
  +    
  +      com.mortbay.Util.Log.event("JBoss and Jetty Log models connected");
  +    }
  +     
  +    {
         InputStream propertiesIn = 
getClass().getClassLoader().getResourceAsStream("jetty.properties");
  -
  -      if ( propertiesIn == null ) {
  -
  -          throw new IOException("jetty.properties missing");
  -      }
  -
  +      
  +      if (propertiesIn == null)
  +     throw new IOException("failed to load jetty.properties");
  +      
         System.getProperties().load(propertiesIn);
  +    
  +      com.mortbay.Util.Log.event("successfully loaded properties");
  +    }
   
  -      
  +    try
  +    {
  +      // make a Jetty...
  +      _jetty = new Jetty();
  +      _jetty.setJettyHome(_getJettyHome());
  +      _jetty.setWebDefault(_jetty.getJettyHome()+"/etc/webdefault.xml");
  +
  +      com.mortbay.Util.Log.event("Jetty instantiated and initialised");
  +    }
  +    catch (NoClassDefFoundError e)
  +    {
  +      com.mortbay.Util.Log.event("failed to instantiate Jetty");
  +      throw e;
  +    } 
  +    finally
  +    {
  +      // unset log for the main thread.
  +      // tomcat's child threads have a copy of it anyway.
  +      Log.unsetLog();
  +    }
  +
         try
         {
  -     // start a Jetty...
  -     _server = new HttpServer();
  +     _mbean  = new JettyMBean(_jetty);
  +     MBeanServer server = 
(MBeanServer)javax.management.MBeanServerFactory.findMBeanServer(null).get(0);
  +     //      MBeanServer server=MBeanServerFactory.newMBeanServer("Jetty");
  +     server.registerMBean(_mbean, new 
ObjectName(_mbean.newObjectName(OBJECT_NAME,server)));
         }
  -      catch (NoClassDefFoundError e)
  +      catch (Exception e)
         {
  -     Logger.log("failed");
  -     Logger.log("com.mortbay.HTTP.HttpServer wasn't found. Be sure to have your 
CLASSPATH correctly set");
  -     Logger.log("You need Jetty 3+ to use this service");
  -     throw e;
  -      } 
  -                     
  -      try
  -      {
  -     // now add server config...
  -     for (int i=0; i<_configs.size(); i++)
  -     {
  -       String config=(String)_configs.elementAt(i);
  -       URL url=new URL(config);
  -       XmlConfiguration xc=new XmlConfiguration(url);
  -       xc.configure(_server);
  -     }
  +     e.printStackTrace();
         }
  -      catch(Exception e)
  +      catch (Error e)
         {
        e.printStackTrace();
         }
  -    }
  -    finally
  -    {
  -      // unset log for the main thread.
  -      // tomcat's child threads have a copy of it anyway.
  -      Log.unsetLog();
  -    }
     }
  -
  +  
     //----------------------------------------------------------------------------
  -
  +  
     public
       JettyService()
     {
  +    try
  +    {
  +      ensureService();
  +    }
  +    catch (Exception e)
  +    {
  +      e.printStackTrace();
  +    }
     }
  -
  +  
     //----------------------------------------------------------------------------
     // 'name' interface
     //----------------------------------------------------------------------------
  @@ -184,11 +188,10 @@
       throws Exception
     {
       Log.setLog(_log);
  -    Logger.log("Starting (jetty.mortbay.com)");
  -    ensureService();         // lazy construction
  +    com.mortbay.Util.Log.event("Starting (jetty.mortbay.com)");
       // should check whether already running ? TODO
  -    _server.start();
  -    Logger.log("Started successfully");
  +    _jetty.start();
  +    com.mortbay.Util.Log.event("Started successfully");
       Log.unsetLog();
     }
      
  @@ -196,9 +199,9 @@
       stopService()
     {
       // should check whether already stopped ? TODO
  -    Logger.log("Stopping: if using sun jdk 1.3 on linux, waits 1 minute for Sun bug 
4386498... ");
  -    try {_server.stop();} catch (Exception e) {e.printStackTrace();}
  -    Logger.log("Stopped Successfully");
  +    com.mortbay.Util.Log.event("Stopping: if using sun jdk 1.3 on linux, waits 1 
minute for Sun bug 4386498... ");
  +    try {_jetty.stop();} catch (Exception e) {e.printStackTrace();}
  +    com.mortbay.Util.Log.event("Stopped Successfully");
     }
   
     //----------------------------------------------------------------------------
  @@ -209,134 +212,110 @@
       deploy(String path, String warUrl)
       throws DeploymentException
     {
  -    Log.setLog(log);
  -    Logger.log("Deploying: context="+path+" webapp="+warUrl+" 
unpackWars="+_unpackWars);
  -    
  -    try
  -    {
  -      // deploy the WebApp
  -      // prevent Jetty moaning about old-style context paths
  -      String contextPath=path+"/*";
  -      // force Jetty to recognise this as a dir and not an archive
  -      String warDir=warUrl+(warUrl.endsWith("/")?"":"/");
  -      // give Jetty it's correct default resource file - should be parameterised...
  -      String defaultResource=_home+"/etc/webdefault.xml";
  -      
  -      WebApplicationContext app = _server.addWebApplication(contextPath,
  -                                                         warDir,
  -                                                         defaultResource,
  -                                                         _unpackWars);
  -      
  -      // Use the same ClassLoader as JBoss - this allows optimisation
  -      // of calls to EJBs...
  -      
  -      // Greg reckons that as long as we set the ClassLoader before
  -      // start()-ing it should do the business...
  -      ClassLoader cl=Thread.currentThread().getContextClassLoader();
  -      app.setClassLoader(cl);
  -      
  -      // finally - start the WebApp...
  -      app.start();
  -      
  -      // keep track of deployed contexts for undeployment
  -      _deployed.put(warUrl, app);
  -      
  -      Logger.log("Deployed successfully");
  -    }
  -    catch (Exception e)
  -    {
  -      e.printStackTrace();
  -      throw new DeploymentException(e.getMessage());
  -    }
  -    finally
  -    {
  -      Log.unsetLog();
  -    }
  +    _jetty.deploy(path, warUrl);
     }
        
     public void
       undeploy(String warUrl)
       throws DeploymentException
     {
  -    Log.setLog(log);
  -    Logger.log("Undeploying: webapp="+warUrl);
  -             
  -    try
  -    {
  -      // find the WebApp Context in the repository
  -      WebApplicationContext app = (WebApplicationContext)_deployed.get(warUrl);
  -                     
  -      if (app == null) 
  -     throw new DeploymentException("URL " + warUrl + " is not deployed");
  -                     
  -      app.stop();
  -
  -      // remove the app - NYI
  -      {
  -     // yeughhhhhhh ! - Jetty's API for this needs work !
  -     int i=-1;
  -     HandlerContext hc=null;
  -     do
  -     {
  -       i++;
  -       hc=_server.getContext(null, app.getContextPath()+"/*", i);
  -     }
  -     while (hc!=null && hc!= app);
  -     
  -     if (hc==app)
  -     {
  -       _server.removeContext(null, app.getContextPath()+"/*", i);
  -     }
  -     else
  -     {
  -       Logger.log("could not remove WebApp Context : "+i);
  -     }
  -      }
  -      
  -      _deployed.remove(app);
  -      Logger.log("Undeployed successfully");
  -    }
  -    catch (Exception e)
  -    {
  -      throw new DeploymentException(e.getMessage());
  -    }
  -    finally
  -    {
  -      Log.unsetLog();
  -    }
  +    _jetty.undeploy(warUrl);
     }
  -     
  +  
     public boolean
       isDeployed(String warUrl)
  +  {
  +    return _jetty.isDeployed(warUrl);
  +  }
  +  
  +  public boolean
  +    getUnpackWars()
     {
  -    return _deployed.containsKey(warUrl);
  +    return _jetty.getUnpackWars();
     }
   
  -  // I want multiple configs - but we'll make do with one for the
  -  // moment...
     public void
  +    setUnpackWars(boolean unpackWars)
  +  {
  +    _jetty.setUnpackWars(unpackWars);
  +  }
  +
  +  public String
  +    getWebDefault()
  +  {
  +    return _jetty.getWebDefault();
  +  }
  +
  +  public void
  +    setWebDefault(String webDefault)
  +  {
  +    _jetty.setWebDefault(webDefault);
  +  }
  +
  +  //deprecated
  +  public void
       setConfiguration(String configUrl)
     {
  -    Log.setLog(_log);
  -    Logger.log("Adding configuration: URL="+configUrl);
  -    _configs.addElement(configUrl);
  -    Logger.log("Added successfully");
  -    Log.unsetLog();
  +    com.mortbay.Util.Log.event("setConfiguration() is deprecated - use 
addConfiguration()");
  +    _jetty.addConfiguration(configUrl);
     }
   
     public void
  -    setUnpackWars(boolean unpackWars)
  +    addConfiguration(String configUrl)
     {
  -    Log.setLog(_log);
  -    Logger.log("Setting unpackWars="+unpackWars);
  -    _unpackWars=unpackWars;
  -    Logger.log("Set successfully");
  -    Log.unsetLog();
  +    com.mortbay.Util.Log.event("addConfiguration() - going for it...");
  +    _jetty.addConfiguration(configUrl);
     }
   
  -  public boolean
  -    getUnpackWars()
  +  public String
  +    getJettyHome()
     {
  -    return _unpackWars;
  +    return _jetty.getJettyHome();
     }
  +
  +  //----------------------------------------
  +  // hack city...
  +  //----------------------------------------
  +
  +  static public String
  +    _getJettyHome()
  +  {
  +    Class jettyClass;
  +    try
  +    {      
  +      jettyClass = Class.forName("com.mortbay.HTTP.HttpServer");
  +      URL jettyUrl = jettyClass.getProtectionDomain().getCodeSource().getLocation();
  +      String jettyHome = new File(new 
File(jettyUrl.getFile()).getParent()).getParent();
  +
  +      return jettyHome;
  +    }
  +    catch (Exception e)
  +    {
  +      //      throw new Exception("start failed");
  +    }
  +    
  +    return null;
  +  }
  +
  +//   // bind Jetty3Extra MBean into our own lifecycle...
  +  
  +//   public void
  +//     start()
  +//     throws Exception
  +//   {
  +//     super.start();
  +//     //    _mbean.;
  +//   }
  +
  +//   public void
  +//     stop()
  +//   {
  +//     super.stop();
  +//   }
  +
  +//   public void
  +//     destroy()
  +//   {
  +//     super.destroy();
  +//   }
   }
  
  
  
  1.4       +9 -4      contrib/jetty/src/main/org/jboss/jetty/JettyServiceMBean.java
  
  Index: JettyServiceMBean.java
  ===================================================================
  RCS file: 
/cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/JettyServiceMBean.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- JettyServiceMBean.java    2000/12/16 15:29:59     1.3
  +++ JettyServiceMBean.java    2001/04/30 22:44:49     1.4
  @@ -16,7 +16,7 @@
    *      
    *   @see <related>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Sebastien Alborini</a>
  - *   @version $Revision: 1.3 $
  + *   @version $Revision: 1.4 $
    */
   public interface JettyServiceMBean
     extends org.jboss.util.ServiceMBean
  @@ -26,14 +26,19 @@
     public static final String OBJECT_NAME = ":service=Jetty";
     
     // Public --------------------------------------------------------
  +
     public void deploy(String ctxPath, String warUrl) throws DeploymentException;
  -     
     public void undeploy(String warUrl) throws DeploymentException;
  -     
     public boolean isDeployed(String warUrl);
   
  -  public void setConfiguration(String configUrl);
  +  public void setConfiguration(String configUrl); // deprecated
  +  public void addConfiguration(String configUrl);
   
     public boolean getUnpackWars();
     public void setUnpackWars(boolean unpackWars);
  +
  +  public String getWebDefault();
  +  public void setWebDefault(String webDefault);
  +
  +  public String getJettyHome();
   }
  
  
  
  1.1                  contrib/jetty/src/main/org/jboss/jetty/Jetty.java
  
  Index: Jetty.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
  
  // A Jetty HttpServer with the interface expected by JBoss'
  // J2EEDeployer...
   
  package org.jboss.jetty;
  
  import java.util.Hashtable;
  import java.util.Vector;
  import java.net.URL;
  import org.jboss.ejb.DeploymentException;
  import com.mortbay.HTTP.WebApplicationContext;
  import com.mortbay.HTTP.HandlerContext;
  import com.mortbay.Util.Code;
  import com.mortbay.Util.Log;
  import com.mortbay.Util.XmlConfiguration;
  import com.mortbay.Util.XmlParser;
  
  import org.xml.sax.SAXException;
  import org.xml.sax.Attributes;
  
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import java.lang.reflect.Method;
  
  abstract class JndiHandler
    extends org.xml.sax.helpers.DefaultHandler
  {
    String _tmp;                        // temporary buffer
  
    public void
      startElement (String uri, String localName, String qName, Attributes attrs)
      throws SAXException
    {
      _tmp="";
    }
  
    // is this how it should be done ?
    public void
      characters (char[] buf, int offset, int len)
      throws SAXException
    {
      _tmp+=new String(buf,offset,len);
    }
  
    public void
      endElement(String uri, String localName, String qName)
      throws SAXException
    {
      
      String methodName="set_"+localName.replace('-','_').toLowerCase();
      Class[] params={_tmp.getClass()};
      
      try
      {
        Method method=getClass().getMethod(methodName,params);
        Object[] args={_tmp};
        method.invoke(this, args);
      }
      catch(NoSuchMethodException e)
      {
        Log.warning("NYI: tag - "+localName+" ("+methodName+")");
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
    }
    
    public void
      doBinding(String from, String to)
    {
      Log.event("binding: "+from+" to "+to);
    }
    
    static
    {
      // ensure comp/env context exists...
      try
      {
        Context context = (Context)(new InitialContext().lookup("java:comp"));
        context = context.createSubcontext("env");
  
        Log.event("Ensured existence of java:comp/env subcontext");
      }
      catch(Exception exception)
      {
        exception.printStackTrace();
      }
    }
  }
  
  // The ejb-ref element is used to declare a reference to an enterprise
  // bean.
  
  class EJBRefHandler extends JndiHandler
  {
    // The ejb-ref-name element contains the name of an EJB
    // reference. This is the JNDI name that the servlet code uses to
    // get a reference to the enterprise bean.
    String _ejbRefName;
    // The ejb-ref-type element contains the expected java class type of
    // the referenced EJB.
    String _ejbRefType;
    // The ejb-home element contains the fully qualified name of the
    // EJB's home interface.
    String _home;
    //  The ejb-remote element contains the fully qualified name of the
    //  EJB's remote interface.
    String _remote;
    // The ejb-link element is used in the ejb-ref element to specify
    // that an EJB reference is linked to an EJB in an encompassing
    // Java2 Enterprise Edition (J2EE) application package. The value of
    // the ejb-link element must be the ejb-name of an EJB in the J2EE
    // application package.
    String _ejbLink;
  
    // I could do some verification after each of these atributes is
    // set....?
  
    public void set_ejb_ref_name (String name)   {_ejbRefName =name;}
    public void set_ejb_ref_type (String type)   {_ejbRefType =type;} 
    public void set_home         (String home)   {_home       =home;}
    public void set_remote       (String remote) {_remote     =remote;} 
    public void set_ejb_link     (String name)   {_ejbLink    =name;}
  
    public void
      set_ejb_ref(String dummy)
    {
      doBinding(_ejbRefName, _ejbLink);
    }
  
    public String
      toString()
    {
      return "<EJBRefHandler:"+
        "name="+_ejbRefName+","+
        "type="+_ejbRefType+","+
        "home="+_home+","+
        "remote="+_remote+
        "link="+_ejbLink+
        ">";
    }
  }
  
  class Jetty extends com.mortbay.Jetty.Server
  {
    Jetty()
    {
      super();
    }
  
    //----------------------------------------
    // jettyHome property
    //----------------------------------------
  
    void
      setJettyHome(String jettyHome) // not public
    {
      System.setProperty("jetty.home", jettyHome);
      Log.event("setting jettyHome to "+jettyHome);
    }
  
    public String
      getJettyHome()
    {
      return System.getProperty("jetty.home");
    }
  
    //----------------------------------------
    // unpackWars property
    //----------------------------------------
  
    boolean _unpackWars=false;
  
    public void
      setUnpackWars(boolean unpackWars)
    {
      _unpackWars=unpackWars;
      Log.event("setting unpackWars to "+_unpackWars);
    }
    
    public boolean
      getUnpackWars()
    {
      return _unpackWars;
    }
  
    //----------------------------------------
    // webDefault property
    //----------------------------------------
  
    String _webDefault;
  
    public void
      setWebDefault(String webDefault)
    {
      _webDefault=webDefault;
      Log.event("setting webDefault to "+_webDefault);
    }
    
    public String
      getWebDefault()
    {
      return _webDefault;
    }
  
    //----------------------------------------
  
    public void
      addConfiguration(String configUrl)
    {
      try
      {
        new XmlConfiguration(new URL(configUrl)).configure(this);
        Log.event("successfully added configuration: "+configUrl);
      }
      catch (Exception e)
      {
        Log.event("problem adding configuration: "+configUrl);
        e.printStackTrace();
      }
    }
    
    //----------------------------------------------------------------------------
    // 'deploy' interface
    //----------------------------------------------------------------------------
  
    Hashtable _deployed = new Hashtable();
  
    public void
      deploy(String path, String warUrl)
      throws DeploymentException
    {
      try
      {
        // deploy the WebApp
        WebApplicationContext app=new WebApplicationContext(this, path+"/*");
  
        // Use the same ClassLoader as JBoss - this allows optimisation
        // of calls to EJBs...
        app.setClassLoader(Thread.currentThread().getContextClassLoader());
  
        // set up ENC stuff
        XmlParser parser=app.getXmlParser();
        parser.addContentHandler("ejb-ref", new EJBRefHandler());
  
        app.initialize(warUrl+(warUrl.endsWith("/")?"":"/"),
                     getWebDefault(),
                     getUnpackWars());
        
        // finally - start the WebApp...
        app.start();
        
        // keep track of deployed contexts for undeployment
        _deployed.put(warUrl, app);
  
        Log.event("successfully deployed "+warUrl+" to "+path);
      }
      catch (Exception e)
      {
        Log.event("problem deploying "+warUrl+" to "+path);
        e.printStackTrace();
        throw new DeploymentException(e.getMessage());
      }
    }
    
    public void
      undeploy(String warUrl)
      throws DeploymentException
    {
      // find the WebApp Context in the repository
      WebApplicationContext app = (WebApplicationContext)_deployed.get(warUrl);
      
      try
      {
        app.stop();
        removeContext(app);
        _deployed.remove(app);
        Log.event("successfully undeployed "+warUrl);
      }
      catch (Exception e)
      {
        Log.event("problem undeploying "+warUrl);
        throw new DeploymentException(e.getMessage());
      }
    }
    
    public boolean
      isDeployed(String warUrl)
    {
      return _deployed.containsKey(warUrl);
    }
  }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to