User: jules_gosnell
  Date: 01/05/09 15:46:01

  Modified:    jetty/src/main/org/jboss/jetty JettyService.java
  Log:
  hold off on allocating a Jetty instance until initService() is called
  do the correct thing in destroyService()
  
  Revision  Changes    Path
  1.11      +199 -125  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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JettyService.java 2001/05/08 23:18:40     1.10
  +++ JettyService.java 2001/05/09 22:45:59     1.11
  @@ -33,7 +33,7 @@
    *      
    *   @see <related>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Julian Gosnell</a>
  - *   @version $Revision: 1.10 $
  + *   @version $Revision: 1.11 $
    */
   
   class JettyMBean extends HttpServerMBean
  @@ -44,52 +44,80 @@
       super(jetty);
     }
   
  -    protected String newObjectName(MBeanServer server)
  +  protected String
  +    newObjectName(MBeanServer server)
     {
       return super.newObjectName(server);
     }
  -//   protected String
  -//     newObjectName(String prefix, MBeanServer server)
  -//   {
  -//     return uniqueObjectName(server, prefix);
  -//   }
  +  //   protected String
  +  //     newObjectName(String prefix, MBeanServer server)
  +  //   {
  +  //     return uniqueObjectName(server, prefix);
  +  //   }
   }
   
  -public class JettyService extends ServiceMBeanSupport
  -  implements JettyServiceMBean, MBeanRegistration
  +class JettyLog extends Log
   {
  -  class JettyLog extends Log
  -  {
  -    Object _source=null;     // should really be in same package as Log
  +  Object _source=null;       // should really be in same package as Log
       
  -    public
  -      JettyLog()
  -    {
  -      super();
  -    }
  +  public
  +    JettyLog()
  +  {
  +    super();
  +  }
       
  -    public
  -      JettyLog(Object source)
  -    {
  -      super(source);
  -      _source=source;                // hack
  -    }
  +  public
  +    JettyLog(Object source)
  +  {
  +    super(source);
  +    _source=source;          // hack
  +  }
       
  -    public void
  -      log(String type, String message)
  -    {
  -      Logger.getLogger().fireNotification(type, _source, message);
  -    }
  +  public void
  +    log(String type, String message)
  +  {
  +    Logger.getLogger().fireNotification(type, _source, message);
     }
  +}
     
  +public class JettyService extends ServiceMBeanSupport
  +  implements JettyServiceMBean, MBeanRegistration
  +{
     public static final String NAME = "Jetty";
     
     Log          _log     = new JettyLog(getName());
     JBossLogSink _logSink = new JBossLogSink();
     JettyMBean   _mbean   = null;
  -  Jetty        _jetty  = null;
  -  MBeanServer  _server =null;
  +  Jetty        _jetty   = null;
  +  MBeanServer  _server  = null;
  +
  +  //----------------------------------------
  +  // hack city...
  +  //----------------------------------------
  +
  +  protected static String _jettyHome=_getJettyHome();
  +
  +  static public String
  +    _getJettyHome()
  +  {
  +    try
  +    {      
  +      Class 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)
  +    {
  +      com.mortbay.Util.Log.event("Could not figure out JBOSS_HOME - 
com.mortbay.HTTP.HttpServer displaced");
  +    }
  +    
  +    return null;
  +  }
  +
  +  //----------------------------------------------------------------------------
  +
     public ObjectName
       preRegister(MBeanServer server, ObjectName name)
       throws java.lang.Exception
  @@ -97,56 +125,54 @@
       name = getObjectName(server, name);
       _server = server;
       
  -    try {ensureService();} catch (Exception e) {e.printStackTrace();}
  -
       return name;
     }
  +
  +  //----------------------------------------------------------------------------
     
     protected void
  -    ensureService()
  -    throws Exception
  +    ensureLogging()
     {
  -    {
  -      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);
  +    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);
       
  -      com.mortbay.Util.Log.event("JBoss and Jetty Log models connected");
  -    }
  -     
  -    {
  -      InputStream propertiesIn = 
getClass().getClassLoader().getResourceAsStream("jetty.properties");
  -      
  -      if (propertiesIn == null)
  -     throw new IOException("failed to load jetty.properties");
  -      
  -      System.getProperties().load(propertiesIn);
  +    com.mortbay.Util.Log.event("JBoss and Jetty Log models connected");
  +  }
  +  
  +  protected void
  +    ensureProperties()
  +    throws IOException
  +  {
  +    InputStream propertiesIn = 
getClass().getClassLoader().getResourceAsStream("jetty.properties");
       
  -      com.mortbay.Util.Log.event("successfully loaded properties");
  -    }
  +    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");
  +  protected void
  +    ensureJetty()
  +  {
  +    // make a Jetty...
  +    _jetty = new Jetty();
  +    _jetty.setJettyHome(_jettyHome);
  +    _jetty.setWebDefault(_webDefault);
   
  -      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();
  -    }
  +    com.mortbay.Util.Log.event("Jetty instantiated and initialised");
  +
  +    // unset log for the main thread.
  +    // tomcat's child threads have a copy of it anyway.
  +    Log.unsetLog();
  +  }
   
  +  protected void
  +    ensureMBean()
  +  {
       try
       {
         _mbean  = new JettyMBean(_jetty);
  @@ -160,13 +186,28 @@
       {
         e.printStackTrace();
       }
  +
  +    for (int i=0; i<_configuration.size(); i++)
  +    {
  +      try
  +      {
  +     _jetty.configure((String)_configuration.elementAt(i));
  +      }
  +      catch(IOException e)
  +      {
  +     e.printStackTrace();
  +      }
  +    }
     }
  -  
  -  //----------------------------------------------------------------------------
  -  
  -  public
  -    JettyService()
  +
  +  protected void
  +    ensureService()
  +    throws Exception
     {
  +    ensureLogging();
  +    ensureProperties();
  +    ensureJetty();
  +    ensureMBean();
     }
     
     //----------------------------------------------------------------------------
  @@ -188,12 +229,36 @@
   
     //----------------------------------------------------------------------------
     // 'service' interface
  +  
  +  // called by lifecycle methods as defined in ServiceMBeanSupport.
  +
  +
  +  // we should probably bind our instance of Jetty directly into our
  +  // own lifecycle, so that when init(), start(), stop() and destroy()
  +  // are called on us, the equivalent is done to our delegate.
  +
  +  // How should we do it ?
  +  
  +  // a). We delegate to the JettyMBean which delegates to Jetty
  +  // b). We do it directly to Jetty and the MBean recieves notification if necessary
  +  
     //----------------------------------------------------------------------------
   
     public void
  +    initService()
  +    throws Exception
  +  {
  +    super.initService();
  +
  +    try {ensureService();} catch (Exception e) {e.printStackTrace();}
  +  }
  +
  +  public void
       startService()
       throws Exception
     {
  +    super.startService();
  +
       Log.setLog(_log);
       com.mortbay.Util.Log.event("Starting (jetty.mortbay.com)");
       // should check whether already running ? TODO
  @@ -205,12 +270,26 @@
     public void
       stopService()
     {
  +    super.stopService();
  +
       // should check whether already stopped ? TODO
       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");
     }
   
  +  public void
  +    destroyService()
  +  {
  +    super.destroyService();
  +
  +    Log.setLog(_log);
  +    com.mortbay.Util.Log.event("Destroying (jetty.mortbay.com)");
  +    _jetty.destroy();
  +    com.mortbay.Util.Log.event("Destroyed successfully");
  +    Log.unsetLog();
  +  }
  +
     //----------------------------------------------------------------------------
     // 'deploy' interface
     //----------------------------------------------------------------------------
  @@ -234,23 +313,49 @@
     {
       return _jetty.isDeployed(warUrl);
     }
  +
  +  //----------------------------------------------------------------------------
  +
  +  protected boolean
  +    isInitialised()
  +  {
  +    return (_jetty!=null);
  +  }
     
  +  //----------------------------------------------------------------------------
  +
  +
  +  protected boolean _unpackWars=false;
  +
     public boolean
       getUnpackWars()
     {
  -    return _jetty.getUnpackWars();
  +    if (isInitialised())
  +      return _jetty.getUnpackWars();
  +    else
  +      return _unpackWars;
     }
   
     public void
       setUnpackWars(boolean unpackWars)
     {
  -    _jetty.setUnpackWars(unpackWars);
  +    if (isInitialised())
  +      _jetty.setUnpackWars(unpackWars);
  +    else
  +      _unpackWars=unpackWars;
     }
   
  +  //----------------------------------------------------------------------------
  +  
  +  protected String _webDefault=_jettyHome+"/etc/webdefault.xml";
  +
     public String
       getWebDefault()
     {
  -    return _jetty.getWebDefault();
  +    if (isInitialised())
  +      return _jetty.getWebDefault();
  +    else
  +      return _webDefault;
     }
   
     public void
  @@ -259,6 +364,10 @@
       _jetty.setWebDefault(webDefault);
     }
   
  +  //----------------------------------------------------------------------------
  +  
  +  protected Vector _configuration=new Vector();
  +
     //deprecated
     public void
       setConfiguration(String configUrl)
  @@ -271,58 +380,23 @@
       addConfiguration(String configUrl)
     {
       com.mortbay.Util.Log.event("adding Configuration "+configUrl);
  -    _jetty.addConfiguration(configUrl);
  +
  +    if (isInitialised())
  +      _jetty.addConfiguration(configUrl);
  +    else
  +      _configuration.add(configUrl);
     }
   
  +  //----------------------------------------------------------------------------
  +  // TODO
  +
     public String
       getJettyHome()
     {
  -    return _jetty.getJettyHome();
  +    if (isInitialised())
  +      return _jetty.getJettyHome();
  +    else
  +      return _jettyHome;
     }
   
  -  //----------------------------------------
  -  // 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();
  -  //   }
   }
  
  
  

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

Reply via email to