User: jules   
  Date: 00/11/09 17:10:15

  Added:       jetty/src/main/org/jboss/jetty JBossLogSink.java
                        JettyService.java JettyServiceMBean.java
  Log:
  JBoss/Jetty integration module
  
  Revision  Changes    Path
  1.1                  contrib/jetty/src/main/org/jboss/jetty/JBossLogSink.java
  
  Index: JBossLogSink.java
  ===================================================================
  // ========================================================================
  // $Id: JBossLogSink.java,v 1.1 2000/11/10 01:10:13 jules Exp $
  // ========================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.jetty;
  
  /* ------------------------------------------------------------ */
  /** A Log sink.
   * This class represents both a concrete or abstract sink of
   * Log data.  The default implementation logs to a PrintWriter, but
   * derived implementations may log to files, syslog, or other
   * logging APIs.
   *
   * @see
   * @version $Id: JBossLogSink.java,v 1.1 2000/11/10 01:10:13 jules Exp $
   * @author Jules Gosnell (jules)
   */
  public class JBossLogSink
    implements com.mortbay.Util.LogSink
  {
    org.jboss.logging.Log _log;
    boolean               _started = false;
  
    // 'LifeCycle' interface
    public void
      initialize(Object log)
      throws InterruptedException
    {
      _log = (org.jboss.logging.Log) log;
    }
  
    public void
      start()
    {
      _started = true;
    }
  
    public void
      stop()
      throws InterruptedException
    {
      _started = false;
    }
  
    public void
      destroy()
    {
      _log = null;
    }
  
    public boolean
      isStarted()
    {
      return _started;
    }
  
    public boolean
      isDestroyed()
    {
      return (_log==null);
    }
  
      public void
        setOptions(String dateFormat,
                 String timezone,
                 boolean logTimeStamps,
                 boolean logLabels,
                 boolean logTags,
                 boolean logStackSize,
                 boolean logStackTrace,
                 boolean logOneLine)
    {
      // is it possible to translate these into JBoss logging options...?
    }
    
    /* ------------------------------------------------------------ */
    /** Log a message.
     * This method formats the log information as a string and calls
     * log(String).  It should only be specialized by a derived
     * implementation if the format of the logged messages is to be changed.
     *
     * @param tag Tag for type of log
     * @param msg The message
     * @param frame The frame that generated the message.
     * @param time The time stamp of the message.
     */
    public void log(String tag,
                  Object msg,
                  com.mortbay.Util.Frame frame,
                  long time)
    {
      if (tag.equals(com.mortbay.Util.Log.WARN))
      {
        _log.warning(msg+(com.mortbay.Util.Code.debug()?", "+frame:""));
        return;
      }
      else if (tag.equals(com.mortbay.Util.Log.EVENT))
      {
        _log.log(msg+(com.mortbay.Util.Code.debug()?", "+frame:""));
        return;
      }
      else if (tag.equals(com.mortbay.Util.Log.DEBUG))
      {
        _log.debug(msg+(com.mortbay.Util.Code.debug()?", "+frame:""));
        return;
      }
      else if (tag.equals(com.mortbay.Util.Log.ASSERT))
      {
        _log.error(msg+(com.mortbay.Util.Code.debug()?", "+frame:""));
        return;
      }
      else if (tag.equals(com.mortbay.Util.Log.FAIL))
      {
        _log.error(msg+(com.mortbay.Util.Code.debug()?", "+frame:""));
        return;
      }
  
      log(msg+" - "+tag+(com.mortbay.Util.Code.debug()?", "+frame:""));
    }
      
    /* ------------------------------------------------------------ */
    /** Log a message.
     * The formatted log string is written to the log sink. The default
     * implementation writes the message to a PrintWriter.
     * @param formattedLog 
     */
    public synchronized void
      log(String formattedLog)
    {
      _log.log(formattedLog);
    }
  };
  
  
  
  
  
  
  
  
  
  
  
  1.1                  contrib/jetty/src/main/org/jboss/jetty/JettyService.java
  
  Index: JettyService.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
  package org.jboss.jetty;
  
  import java.net.URL;
  import java.util.Hashtable;
  
  import javax.management.*;
  
  import org.jboss.logging.Log;
  import org.jboss.logging.Logger;
  import org.jboss.util.ServiceMBeanSupport;
  import org.jboss.ejb.DeploymentException;
  
  import com.mortbay.HTTP.HttpServer;
  import com.mortbay.HTTP.HandlerContext;
  import com.mortbay.HTTP.WebApplicationContext;
  import com.mortbay.Util.XmlConfiguration;
  
  /**
   *   A service to launch jetty from JMX.
   *      
   *   @see <related>
   *   @author <a href="mailto:[EMAIL PROTECTED]">Julian Gosnell</a>
   *   @version $Revision: 1.1 $
   */
  public class JettyService extends ServiceMBeanSupport
    implements JettyServiceMBean, MBeanRegistration
  {
    public static final String NAME = "Jetty";
  
    Log          _log        = new Log(getName());
    JBossLogSink _logSink    = new JBossLogSink();
    HttpServer   _server     = null;
    String       _home       = null;
    String       _config     = null;
    Hashtable    _deployed   = new Hashtable();
  
    protected void
      ensureService()
      throws Exception
    {
      Log.setLog(_log);
      Logger.log("Testing if Jetty is present....");
                
      try
      {
        // get hold of JETTY_HOME...
        _home = System.getProperty("jetty.home");
        if (_home == null)
        {
        Logger.log("failed");
        Logger.log("System property jetty.home not found. Be sure to set JETTY_HOME to 
the home of Jetty 3+");
        throw new Exception("start failed");
        }                          
                        
        // connect Jetty and JBoss log models...
        try{_logSink.initialize(_log);}catch(Exception e){e.printStackTrace();}
        _logSink.start();
        com.mortbay.Util.Log.instance().add(_logSink);
        
        try
        {
        // start a Jetty...
        _server = new HttpServer();
        }
        catch (NoClassDefFoundError 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...
        XmlConfiguration xc=new XmlConfiguration(new URL(_config));
        xc.configure(_server);
        }
        catch(Exception e)
        {
        e.printStackTrace();
        }
      }
      finally
      {
        // unset log for the main thread.
        // tomcat's child threads have a copy of it anyway.
        Log.unsetLog();
      }
    }
  
    //----------------------------------------------------------------------------
  
    public
      JettyService(String config)
    {
      _config=config;
    }
  
    //----------------------------------------------------------------------------
    // 'name' interface
    //----------------------------------------------------------------------------
  
    public ObjectName
      getObjectName(MBeanServer server, ObjectName name)
      throws javax.management.MalformedObjectNameException
    {
      return new ObjectName(OBJECT_NAME);
    }
    
    public String
      getName()
    {
      return NAME;
    }
  
    //----------------------------------------------------------------------------
    // 'service' interface
    //----------------------------------------------------------------------------
  
    public void
      startService()
      throws Exception
    {
      ensureService();          // lazy construction
      // should check whether already running ? TODO
      _server.start();
    }
     
    public void
      stopService()
    {
      // should check whether already stopped ? TODO
  
      System.out.println("Stopping Jetty - if using sun jdk 1.3 on linux, waits 1min 
for sun bug 4386498... ");
      try {_server.stop();} catch (Exception e) {e.printStackTrace();}
      System.out.println("Jetty Stopped");
    }
  
    //----------------------------------------------------------------------------
    // 'deploy' interface
    //----------------------------------------------------------------------------
  
    public void
      deploy(String path, String warUrl)
      throws DeploymentException
    {
      Log.setLog(log);
                
      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
        String defaultResource=_home+"/etc/webdefault.xml";
  
        System.out.println("About to deploy - "+contextPath+" maps to "+warDir);
        WebApplicationContext app = _server.addWebApplication(contextPath,
                                                            warDir,
                                                            defaultResource);
        
        // 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();
        System.out.println("using ClassLoader - "+cl);
        app.setClassLoader(cl);
  
        // finally - start the WebApp...
        app.start();
        
        // keep track of deployed contexts for undeployment
        _deployed.put(warUrl, app);
      }
      catch (Exception e)
      {
        e.printStackTrace();
        throw new DeploymentException(e.getMessage());
      }
      finally
      {
        Log.unsetLog();
      }
    }
        
    public void
      undeploy(String warUrl)
      throws DeploymentException
    {
      Log.setLog(log);
                
      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);
          System.out.println("trying - "+app.getContextPath()+"/*"+" - "+hc);
        }
        while (hc!=null && hc!= app);
        
        if (hc==app)
        {
          System.out.println("found WebApp for removal : "+i);
          _server.removeContext(null, app.getContextPath()+"/*", i);
        }
        else
        {
          System.out.println("could not remove WebApp Context : "+i);
        }
        }
        
        _deployed.remove(app);
      }
      catch (Exception e)
      {
        throw new DeploymentException(e.getMessage());
      }
      finally
      {
        Log.unsetLog();
      }
    }
        
    public boolean
      isDeployed(String warUrl)
    {
      return _deployed.containsKey(warUrl);
    }
  }
  
  
  
  1.1                  contrib/jetty/src/main/org/jboss/jetty/JettyServiceMBean.java
  
  Index: JettyServiceMBean.java
  ===================================================================
  /*
   * jBoss, the OpenSource EJB server
   *
   * Distributable under GPL license.
   * See terms of license at gnu.org.
   */
   
   
  package org.jboss.jetty;
  
  import org.jboss.ejb.DeploymentException;
  
  
  /**
   *   <description> 
   *      
   *   @see <related>
   *   @author <a href="mailto:[EMAIL PROTECTED]">Sebastien Alborini</a>
   *   @version $Revision: 1.1 $
   */
  public interface JettyServiceMBean
    extends org.jboss.util.ServiceMBean
  {
    
    // Constants -----------------------------------------------------
    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);
  
  }
  
  
  

Reply via email to