User: starksm 
  Date: 01/08/06 09:08:35

  Modified:    src/main/org/jboss/web WebServer.java WebService.java
                        WebServiceMBean.java
  Log:
  Allow for binding the listening port to a specific IP address.
  Add host attribute for setting the host portion of the RMI codebase URL.
  
  Revision  Changes    Path
  1.12      +64 -27    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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- WebServer.java    2001/08/03 17:15:58     1.11
  +++ WebServer.java    2001/08/06 16:08:35     1.12
  @@ -14,9 +14,11 @@
   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;
  @@ -36,10 +38,11 @@
    *
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Marc Fleury</a>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  - *   @version $Revision: 1.11 $
  + *   @version $Revision: 1.12 $
    *
    *   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
    *   20010627 scott.stark: Restore ability to download from the server classpath if 
no loader key is found
  @@ -53,7 +56,14 @@
      // Attributes ----------------------------------------------------
      private static JBossCategory category = 
(JBossCategory)JBossCategory.getInstance(WebServer.class);
      /** The port the web server listens on */
  -   private int port = 8080;
  +   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 */
  @@ -70,6 +80,7 @@
      private ThreadPool threadPool = new ThreadPool();
   
      // Public --------------------------------------------------------
  +
       /** Set the http listening port
        */
       public void setPort(int p) 
  @@ -84,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;
  @@ -109,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;
           }
       }
  @@ -167,7 +213,7 @@
                   e.printStackTrace();
               }
           }
  -        trace("Added ClassLoader: "+cl+" URL: "+loaderURL);
  +        category.trace("Added ClassLoader: "+cl+" URL: "+loaderURL);
           return loaderURL;
       }
   
  @@ -201,7 +247,7 @@
           {
               // 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;
           }
   
  @@ -221,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
  @@ -232,19 +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
  @@ -258,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);
  @@ -302,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 
  @@ -331,15 +377,6 @@
           return key;
       }
   
  -    protected void trace(String msg)
  -    {
  -        category.trace(msg);
  -    }
  -    protected void debug(String msg)
  -    {
  -        category.debug(msg);
  -    }
  -
       protected void listen()
       {
           threadPool.run(this);
  @@ -351,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);
  @@ -365,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;
  
  
  
  1.9       +86 -24    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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- WebService.java   2001/08/03 17:15:58     1.8
  +++ WebService.java   2001/08/06 16:08:35     1.9
  @@ -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 <a href="mailto:[EMAIL PROTECTED]";>Rickard Öberg</a>.
  - *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  - *   @version $Revision: 1.8 $
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  + *   @version $Revision: 1.9 $
    */
   public class WebService
      extends ServiceMBeanSupport
  @@ -33,10 +33,9 @@
      // Constants -----------------------------------------------------
       
      // Attributes ----------------------------------------------------
  -   WebServer server;
  -     
  -   Log log = Log.createLog(this.getClass().getName() + "#" + 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)
         {
  -         e.printStackTrace(System.err);
  +         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
  +      {
  +         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,7 +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.
  
  
  
  1.7       +47 -6     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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- WebServiceMBean.java      2001/08/03 17:15:58     1.6
  +++ WebServiceMBean.java      2001/08/06 16:08:35     1.7
  @@ -6,15 +6,14 @@
    */
   package org.jboss.web;
   
  +import java.net.UnknownHostException;
   import java.net.URL;
   
  -/**
  - *   <description> 
  +/** An mbean for configuring the classloader web service.
    *      
  - *   @see <related>
    *   @author <a href="mailto:[EMAIL PROTECTED]";>Rickard Öberg</a>.
  - *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  - *   @version $Revision: 1.6 $
  + *   @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  + *   @version $Revision: 1.7 $
    */
   public interface WebServiceMBean
      extends org.jboss.util.ServiceMBean
  @@ -27,9 +26,51 @@
      
      public void removeClassLoader(ClassLoader cl);
   
  +   /** 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