User: starksm 
  Date: 01/08/20 15:12:53

  Modified:    src/main/org/jboss/web Tag: Branch_2_4 WebServer.java
                        WebService.java WebServiceMBean.java
  Log:
  Add support for binding to a specific IP address
  Use JBossCategory trace level logging
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.5.4.4   +87 -54    jboss/src/main/org/jboss/web/WebServer.java
  
  Index: WebServer.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/web/WebServer.java,v
  retrieving revision 1.5.4.3
  retrieving revision 1.5.4.4
  diff -u -r1.5.4.3 -r1.5.4.4
  --- WebServer.java    2001/07/21 08:55:46     1.5.4.3
  +++ WebServer.java    2001/08/20 22:12:53     1.5.4.4
  @@ -1,5 +1,5 @@
   /*
  - * JBoss, the OpenSource EJB server
  + * JBoss, the OpenSource J2EE webOS
    *
    * Distributable under LGPL license.
    * See terms of license at gnu.org.
  @@ -14,14 +14,17 @@
   import java.io.InputStream;
   import java.io.InputStreamReader;
   import java.io.IOException;
  +import java.net.InetAddress;
   import java.net.MalformedURLException;
   import java.net.Socket;
   import java.net.ServerSocket;
  +import java.net.UnknownHostException;
   import java.net.URL;
   import java.util.HashMap;
   import java.util.Properties;
   
  -import org.apache.log4j.Category;
  +import org.jboss.logging.log4j.JBossCategory;
  +
   /**
    *   A mini webserver that should be embedded in another application. It can server 
any file that is available from
    *   classloaders that are registered with it, including class-files.
  @@ -35,42 +38,49 @@
    *
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  - *   @author <a href="mailto:[EMAIL PROTECTED]";>Lennart Petersson</a>.
  - *   @version $Revision: 1.5.4.3 $
  + *   @version $Revision: 1.5.4.4 $
    *
    *   Revisions:
    *   
  + *   20010806 scott.stark: Allow binding of listening port to a specific IP address
  + *   20010619 scott.stark: Use log4j JBossCategory to enable trace level msgs
    *   20010618 scott.stark: Fixed extraction of mime-type from file extension in 
getMimeType
  - *   20010721 lennart.petersson: Implemented log4j partly of what Scott did in main 
branch.
  - *                               Since JBossCategory is not available in 2.4 both 
trace() and
  - *                               debug() are sent to category.debug().
  + *   20010627 scott.stark: Restore ability to download from the server classpath if 
no loader key is found
  +      amd downloadServerClasses is true
    */
   public class WebServer
        implements Runnable
   {
  -    // Constants -----------------------------------------------------
  +   // Constants -----------------------------------------------------
   
  -    // Attributes ----------------------------------------------------
  -   private static Category category = Category.getInstance(WebServer.class);
  -    /** The port the web server listens on */
  -    private int port = 8080;
  -    /** The map of class loaders registered with the web server */
  -    private HashMap loaderMap = new HashMap();
  -    /** The web server http listening socket */
  -    private ServerSocket server = null;
  -    /** A flag indicating if the server should attempt to download classes from
  -     thread context class loader when a request arrives that does not have a
  -     class loader key prefix.
  +   // Attributes ----------------------------------------------------
  +   private static JBossCategory category = 
(JBossCategory)JBossCategory.getInstance(WebServer.class);
  +   /** The port the web server listens on */
  +   private int port = 8083;
  +
  +   /** The interface to bind to. This is useful for multi-homed hosts
  +      that want control over which interfaces accept connections. */
  +   private InetAddress bindAddress;
  +   /** The serverSocket listen queue depth */
  +   private int backlog = 50;
  +
  +   /** The map of class loaders registered with the web server */
  +   private HashMap loaderMap = new HashMap();
  +   /** The web server http listening socket */
  +   private ServerSocket server = null;
  +   /** A flag indicating if the server should attempt to download classes from
  +    thread context class loader when a request arrives that does not have a
  +    class loader key prefix.
       */
  -    private boolean downloadServerClasses = true;
  -
  -    /** The class wide mapping of type suffixes(class, txt) to their mime
  -     type string used as the Content-Type header for the vended classes/resources */
  -    private static Properties mimeTypes = new Properties();
  -    /** The thread pool used to manage listening threads */
  -    private ThreadPool threadPool = new ThreadPool();
  +   private boolean downloadServerClasses = true;
  +   /** The class wide mapping of type suffixes(class, txt) to their mime
  +   type string used as the Content-Type header for the vended classes/resources */
  +   private static Properties mimeTypes = new Properties();
  +   /** The thread pool used to manage listening threads */
  +   private ThreadPool threadPool = new ThreadPool();
   
      // Public --------------------------------------------------------
  +
       /** Set the http listening port
        */
       public void setPort(int p) 
  @@ -85,6 +95,43 @@
           return port; 
       }
   
  +    public String getBindAddress()
  +    {
  +        String address = null;
  +        if( bindAddress != null )
  +            address = bindAddress.getHostAddress();
  +        return address;
  +    }
  +    public void setBindAddress(String host)
  +    {
  +        try
  +        {
  +            if (host != null)
  +                bindAddress = InetAddress.getByName(host);
  +        }
  +        catch(UnknownHostException e)
  +        {
  +            String msg = "Invalid host address specified: " + host;
  +            category.error(msg, e);
  +        }
  +    }
  +
  +    /** Get the server sockets listen queue depth
  +     @return the listen queue depth
  +     */
  +    public int getBacklog()
  +    {
  +        return backlog;
  +    }
  +    /** Set the server sockets listen queue depth
  +     */
  +    public void setBacklog(int backlog)
  +    {
  +        if( backlog <= 0 )
  +            backlog = 50;
  +        this.backlog = backlog;
  +    }
  +
      public boolean getDownloadServerClasses()
      {
         return downloadServerClasses;
  @@ -110,14 +157,12 @@
       {
           try
           {
  -            server = null;
  -            server = new ServerSocket(getPort());
  -            debug("Started on port " + getPort());
  +            server = new ServerSocket(port, backlog, bindAddress);
  +            category.debug("Started server: "+server);
               listen();
           }
           catch (IOException e)
           {
  -            debug("Could not start on port " + getPort());
               throw e;
           }
       }
  @@ -168,7 +213,7 @@
                   e.printStackTrace();
               }
           }
  -        trace("Added ClassLoader: "+cl+" URL: "+loaderURL);
  +        category.trace("Added ClassLoader: "+cl+" URL: "+loaderURL);
           return loaderURL;
       }
   
  @@ -202,9 +247,8 @@
           {
               // If the server is not null meaning we were not stopped report the err
               if( server != null )
  -               debug("DynaServer error: " + e.getMessage());
  +               category.error("Failed to accept connection", e);
               return;
  -
           }
   
           // Create a new thread to accept the next connection
  @@ -223,8 +267,8 @@
                   int separator = rawPath.indexOf('/');
                   String filePath = rawPath.substring(separator+1);
                   String loaderKey = rawPath.substring(0, separator+1);
  -                trace("loaderKey = "+loaderKey);
  -                trace("filePath = "+filePath);
  +                category.trace("loaderKey = "+loaderKey);
  +                category.trace("filePath = "+filePath);
                   ClassLoader loader = (ClassLoader) loaderMap.get(loaderKey);
                   /* If we did not find a class loader check to see if the raw path
                    begins with className + '@' + cl.hashCode() + '/' by looking for
  @@ -234,20 +278,19 @@
                   if( loader == null && rawPath.indexOf('@') < 0 )
                   {
                      filePath = rawPath;
  -                   trace("No loader, reset filePath = "+filePath);
  +                   category.trace("No loader, reset filePath = "+filePath);
                      loader = Thread.currentThread().getContextClassLoader();
                   }
  -
  -                trace("loader = "+loader);
  +                category.trace("loader = "+loader);
                   byte[] bytes;
                   if( filePath.endsWith(".class") )
                   {
                       // A request for a class file
                       String className = filePath.substring(0, 
filePath.length()-6).replace('/','.');
  -                    trace("loading className = "+className);
  +                    category.trace("loading className = "+className);
                       Class clazz = loader.loadClass(className);
                       URL clazzUrl = 
clazz.getProtectionDomain().getCodeSource().getLocation();
  -                    trace("clazzUrl = "+clazzUrl);
  +                    category.trace("clazzUrl = "+clazzUrl);
                       if (clazzUrl.getFile().endsWith(".jar"))
                          clazzUrl = new URL("jar:"+clazzUrl+"!/"+filePath);
                       else
  @@ -261,7 +304,7 @@
                   else // Resource
                   {
                       // Try getting resource
  -                    trace("loading resource = "+filePath);
  +                    category.trace("loading resource = "+filePath);
                       URL resourceUrl = loader.getResource(filePath);             
                       if (resourceUrl == null)
                           throw new FileNotFoundException("Resource not 
found:"+filePath);
  @@ -305,7 +348,7 @@
           {
               // eat exception (could log error to log file, but
               // write out to stdout for now).
  -            debug("error writing response: " + ex.getMessage());
  +            category.debug("error writing response: " + ex.getMessage());
               ex.printStackTrace();
           }
           finally 
  @@ -334,15 +377,6 @@
           return key;
       }
   
  -    protected void trace(String msg)
  -    {
  -        category.debug(msg);
  -    }
  -    protected void debug(String msg)
  -    {
  -        category.debug(msg);
  -    }
  -
       protected void listen()
       {
           threadPool.run(this);
  @@ -354,7 +388,7 @@
       protected String getPath(BufferedReader in) throws IOException
       {
           String line = in.readLine();
  -        trace("raw request="+line);
  +        category.trace("raw request="+line);
           // Find the request path by parsing the 'REQUEST_TYPE filePath 
HTTP_VERSION' string
           int start = line.indexOf(' ')+1;
           int end = line.indexOf(' ', start+1);
  @@ -368,7 +402,7 @@
       protected byte[] getBytes(URL url) throws IOException
       {
           InputStream in = new BufferedInputStream(url.openStream());
  -        debug("Retrieving "+url.toString());
  +        category.debug("Retrieving "+url.toString());
           ByteArrayOutputStream out = new ByteArrayOutputStream();
           byte[] tmp = new byte[1024];
           int bytes;
  @@ -398,4 +432,3 @@
       }
   
   }
  -
  
  
  
  1.4.4.2   +89 -27    jboss/src/main/org/jboss/web/WebService.java
  
  Index: WebService.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/web/WebService.java,v
  retrieving revision 1.4.4.1
  retrieving revision 1.4.4.2
  diff -u -r1.4.4.1 -r1.4.4.2
  --- WebService.java   2001/07/03 08:05:17     1.4.4.1
  +++ WebService.java   2001/08/20 22:12:53     1.4.4.2
  @@ -1,5 +1,5 @@
   /*
  - * JBoss, the OpenSource EJB server
  + * JBoss, the OpenSource J2EE webOS
    *
    * Distributable under LGPL license.
    * See terms of license at gnu.org.
  @@ -11,20 +11,20 @@
   import java.io.IOException;
   import java.net.URL;
   import java.net.InetAddress;
  +import java.net.UnknownHostException;
   import java.util.Properties;
   import java.util.Enumeration;
   
   import javax.management.*;
   
  -import org.jboss.logging.Log;
   import org.jboss.util.ServiceMBeanSupport;
   
   /** The WebService implementation. It configures a WebServer instance to
    perform dynamic class and resource loading.
   
  - *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @author [EMAIL PROTECTED]
  - *   @version $Revision: 1.4.4.1 $
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>.
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  + *   @version $Revision: 1.4.4.2 $
    */
   public class WebService
      extends ServiceMBeanSupport
  @@ -33,10 +33,9 @@
      // Constants -----------------------------------------------------
       
      // Attributes ----------------------------------------------------
  -   WebServer server;
  -     
  -     Log log = Log.createLog(getName());
  -   
  +   private WebServer server;
  +   private String host;
  +
      // Static --------------------------------------------------------
   
      // Constructors --------------------------------------------------
  @@ -57,12 +56,30 @@
                                String type = mimeTypes.getProperty(extension);
                                server.addMimeType(extension, type);
                        }
  -      } catch (IOException e)
  +      }
  +      catch (IOException e)
  +      {
  +         category.error("Failed to load mime.types", e);
  +      }
  +      // Get the public host name
  +      try
  +      {
  +         host = System.getProperty("java.rmi.server.hostname");
  +      }
  +      catch(SecurityException e)
  +      {
  +      }
  +      try
         {
  -         e.printStackTrace(System.err);
  +         if( host == null )
  +            host = InetAddress.getLocalHost().getHostName();
         }
  +      catch(IOException e)
  +      {
  +         category.error("Failed to get localhost name", e);
  +      }
      }
  -   
  +
      // Public --------------------------------------------------------
      public ObjectName getObjectName(MBeanServer server, ObjectName name)
         throws javax.management.MalformedObjectNameException
  @@ -76,30 +93,26 @@
      }
   
      /** Start the web server for dynamic downloading of classes and resources.
  -    This sets the system java.rmi.server.hostname property to the local hostname
  -    if it has not been set. The system java.rmi.server.codebase is also set to
  -    "http://"+java.rmi.server.hostname+":"+getPort()+"/" if the 
  -    java.rmi.server.codebase has not been set.
  +    The system java.rmi.server.codebase is also set to
  +    "http://"+getHost()":"+getPort()+"/" if the property has not been set.
       */
      public void startService()
         throws Exception
      {
  +      // Start the WebServer running
         server.start();
  -      // Set the rmi host and codebase if they are not already set
  -      String host = System.getProperty("java.rmi.server.hostname");
  -      if( host == null )
  -          host = InetAddress.getLocalHost().getHostName();
  -      
  +      category.info("Started webserver with address: " + server.getBindAddress() + 
" port: "+server.getPort());
  +
  +      // Set the rmi codebase if it is not already set
         String codebase = System.getProperty("java.rmi.server.codebase");
         if( codebase == null )
         {
           codebase = "http://"+host+":"+getPort()+"/";
           System.setProperty("java.rmi.server.codebase", codebase);
         }
  -      log.log("Codebase set to "+codebase);
  -      log.log("Started webserver on port "+server.getPort());
  +      category.info("Codebase set to: "+codebase);
      }
  -   
  +
      public void stopService()
      {
         server.stop();
  @@ -114,7 +127,7 @@
      {
         server.removeClassLoader(cl);
      }
  -     
  +
        public void setPort(int port)
        {
                server.setPort(port);
  @@ -124,6 +137,56 @@
      {
        return server.getPort();
      }
  +
  +     public void setHost(String host)
  +     {
  +             this.host = host;
  +     }
  +     
  +   public String getHost()
  +   {
  +     return host;
  +   }
  +
  +   /** Get the specific address the WebService listens on.t
  +    @return the interface name or IP address the WebService binds to.
  +    */
  +   public String getBindAddress()
  +   {
  +      return server.getBindAddress();
  +   }
  +   /** Set the specific address the WebService listens on.  This can be used on
  +    a multi-homed host for a ServerSocket that will only accept connect requests
  +    to one of its addresses.
  +    @param host, the interface name or IP address to bind. If host is null,
  +    connections on any/all local addresses will be allowed.
  +    */
  +   public void setBindAddress(String host) throws UnknownHostException
  +   {
  +      server.setBindAddress(host);
  +   }
  +
  +   /** Get the WebService listen queue backlog limit. The maximum queue length
  +    for incoming connection indications (a request to connect) is set to the
  +    backlog parameter. If a connection indication arrives when the queue is
  +    full, the connection is refused. 
  +    @return the queue backlog limit. 
  +    */
  +   public int getBacklog()
  +   {
  +      return server.getBacklog();
  +   }
  +   /** Set the WebService listen queue backlog limit. The maximum queue length
  +    for incoming connection indications (a request to connect) is set to the
  +    backlog parameter. If a connection indication arrives when the queue is
  +    full, the connection is refused. 
  +    @param backlog, the queue backlog limit. 
  +    */
  +   public void setBacklog(int backlog)
  +   {
  +      server.setBacklog(backlog);
  +   }
  +
      /** A flag indicating if the server should attempt to download classes from
       * thread context class loader when a request arrives that does not have a
       * class loader key prefix.
  @@ -136,7 +199,6 @@
      {
         server.setDownloadServerClasses(flag);
      }
  +   
      // Protected -----------------------------------------------------
   }
  -
  -
  
  
  
  1.3.4.2   +50 -9     jboss/src/main/org/jboss/web/WebServiceMBean.java
  
  Index: WebServiceMBean.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/web/WebServiceMBean.java,v
  retrieving revision 1.3.4.1
  retrieving revision 1.3.4.2
  diff -u -r1.3.4.1 -r1.3.4.2
  --- WebServiceMBean.java      2001/07/03 08:04:20     1.3.4.1
  +++ WebServiceMBean.java      2001/08/20 22:12:53     1.3.4.2
  @@ -1,20 +1,19 @@
   /*
  - * JBoss, the OpenSource EJB server
  + * JBoss, the OpenSource J2EE webOS
    *
    * Distributable under LGPL license.
    * See terms of license at gnu.org.
    */
   package org.jboss.web;
   
  +import java.net.UnknownHostException;
   import java.net.URL;
   
  -/**
  - *   <description> 
  +/** An mbean for configuring the classloader web service.
    *      
  - *   @see <related>
  - *   @author Rickard �berg ([EMAIL PROTECTED])
  - *   @author [EMAIL PROTECTED]
  - *   @version $Revision: 1.3.4.1 $
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Rickard �berg</a>.
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  + *   @version $Revision: 1.3.4.2 $
    */
   public interface WebServiceMBean
      extends org.jboss.util.ServiceMBean
  @@ -27,9 +26,51 @@
      
      public void removeClassLoader(ClassLoader cl);
   
  -   public void setPort(int port);
  -     
  +   /** Set the WebService listening port.
  +    @param part, the listening port, 0 == Anonymous.
  +    */
  +     public void setPort(int port);
  +   /** Get the WebService listening port.
  +    @return the WebService listening port, 0 == Anonymous.
  +    */
      public int getPort();
  +
  +   /** Get the name of the public interface to use for the host portion of the
  +    RMI codebase URL.
  +    */
  +   public String getHost();
  +   /** Set the name of the public interface to use for the host portion of the
  +    RMI codebase URL.
  +    */
  +   public void setHost(String host);
  +
  +   /** Get the specific address the WebService listens on.t
  +    @return the interface name or IP address the WebService binds to.
  +    */
  +   public String getBindAddress();
  +   /** Set the specific address the WebService listens on.  This can be used on
  +    a multi-homed host for a ServerSocket that will only accept connect requests
  +    to one of its addresses.
  +    @param host, the interface name or IP address to bind. If host is null,
  +    connections on any/all local addresses will be allowed.
  +    */
  +   public void setBindAddress(String host) throws UnknownHostException;
  +
  +   /** Get the WebService listen queue backlog limit. The maximum queue length
  +    for incoming connection indications (a request to connect) is set to the
  +    backlog parameter. If a connection indication arrives when the queue is
  +    full, the connection is refused. 
  +    @return the queue backlog limit. 
  +    */
  +   public int getBacklog();
  +   /** Set the WebService listen queue backlog limit. The maximum queue length
  +    for incoming connection indications (a request to connect) is set to the
  +    backlog parameter. If a connection indication arrives when the queue is
  +    full, the connection is refused. 
  +    @param backlog, the queue backlog limit. 
  +    */
  +   public void setBacklog(int backlog);
  +
      /** A flag indicating if the server should attempt to download classes from
       thread context class loader when a request arrives that does not have a
       class loader key prefix.
  
  
  

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

Reply via email to