[jboss-cvs] JBossAS SVN: r62433 - branches/Branch_4_2/server/src/main/org/jboss/web.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Apr 19 17:22:17 EDT 2007


Author: dimitris at jboss.org
Date: 2007-04-19 17:22:16 -0400 (Thu, 19 Apr 2007)
New Revision: 62433

Modified:
   branches/Branch_4_2/server/src/main/org/jboss/web/WebServer.java
   branches/Branch_4_2/server/src/main/org/jboss/web/WebService.java
   branches/Branch_4_2/server/src/main/org/jboss/web/WebServiceMBean.java
Log:
JBAS-3325, Fix the handling of BindAddress and Host in org.jboss.web.WebService

Modified: branches/Branch_4_2/server/src/main/org/jboss/web/WebServer.java
===================================================================
--- branches/Branch_4_2/server/src/main/org/jboss/web/WebServer.java	2007-04-19 20:59:56 UTC (rev 62432)
+++ branches/Branch_4_2/server/src/main/org/jboss/web/WebServer.java	2007-04-19 21:22:16 UTC (rev 62433)
@@ -33,11 +33,9 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.URL;
-import java.net.UnknownHostException;
 import java.util.Properties;
 
 import org.jboss.logging.Logger;
-import org.jboss.util.StringPropertyReplacer;
 import org.jboss.util.threadpool.BasicThreadPool;
 import org.jboss.util.threadpool.BasicThreadPoolMBean;
 
@@ -122,9 +120,9 @@
    /**
     * Set the http listening port
     */
-   public void setPort(int p)
+   public void setPort(int port)
    {
-      port = p;
+      this.port = port;
    }
 
    /**
@@ -136,37 +134,25 @@
       return port;
    }
 
-   public String getBindAddress()
+   /**
+    * Set the http server bind address
+    * @param bindAddress
+    */
+   public void setBindAddress(InetAddress bindAddress)
    {
-      String address = null;
-      if (bindAddress != null)
-         address = bindAddress.getHostAddress();
+      this.bindAddress = bindAddress;
       
-      return address;
    }
-
-   public String getBindHostname()
+   
+   /**
+    * Get the address the http server binds to
+    * @return the http bind address
+    */
+   public InetAddress getBindAddress()
    {
-      return bindAddress.getHostName();
+      return bindAddress;
    }
 
-   public void setBindAddress(String host)
-   {
-      try
-      {
-         if (host != null)
-         {
-            String h = StringPropertyReplacer.replaceProperties(host);
-            bindAddress = InetAddress.getByName(h);
-         }
-      }
-      catch (UnknownHostException e)
-      {
-         String msg = "Invalid host address specified: " + host;
-         log.error(msg, e);
-      }
-   }
-
    /**
     * Get the server sockets listen queue depth
     * @return the listen queue depth
@@ -238,8 +224,7 @@
       try
       {
          server = new ServerSocket(port, backlog, bindAddress);
-         if (log.isDebugEnabled())
-            log.debug("Started server: " + server);
+         log.debug("Started server: " + server);
          
          listen();
       }
@@ -531,8 +516,7 @@
    protected byte[] getBytes(URL url) throws IOException
    {
       InputStream in = new BufferedInputStream(url.openStream());
-      if (log.isDebugEnabled())
-         log.debug("Retrieving " + url);
+      log.debug("Retrieving " + url);
       ByteArrayOutputStream out = new ByteArrayOutputStream();
       byte[] tmp = new byte[1024];
       int bytes;

Modified: branches/Branch_4_2/server/src/main/org/jboss/web/WebService.java
===================================================================
--- branches/Branch_4_2/server/src/main/org/jboss/web/WebService.java	2007-04-19 20:59:56 UTC (rev 62432)
+++ branches/Branch_4_2/server/src/main/org/jboss/web/WebService.java	2007-04-19 21:22:16 UTC (rev 62433)
@@ -21,7 +21,6 @@
  */
 package org.jboss.web;
 
-import java.io.IOException;
 import java.net.InetAddress;
 import java.net.URL;
 import java.net.UnknownHostException;
@@ -31,10 +30,9 @@
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
 
-import org.jboss.system.MissingAttributeException;
 import org.jboss.system.ServiceMBeanSupport;
+import org.jboss.system.server.ServerConfig;
 import org.jboss.system.server.ServerConfigUtil;
-import org.jboss.util.ThrowableHandler;
 import org.jboss.util.threadpool.BasicThreadPoolMBean;
 
 /**
@@ -46,11 +44,13 @@
  *      name="jboss:service=WebService"
  *
  * @version <tt>$Revision$</tt>
- * @author <a href="mailto:rickard.oberg at telkel.com">Rickard �berg</a>.
- * @author <a href="mailto:Scott.Stark at jboss.org">Scott Stark</a>.
+ * @author <a href="mailto:rickard.oberg at telkel.com">Rickard Oberg</a>
+ * @author <a href="mailto:Scott.Stark at jboss.org">Scott Stark</a>
  * @author <a href="mailto:jason at planet57.com">Jason Dillon</a>
+ * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
  */
-public class WebService extends ServiceMBeanSupport implements WebServiceMBean
+public class WebService extends ServiceMBeanSupport
+   implements WebServiceMBean
 {
    //
    // jason: WebService and WebServer classes should be merged into one
@@ -65,13 +65,15 @@
 
    /**
     * The web server instance which does the work.
-    *
     * <p>
     * Note: This value shadows the MBeanServer server value
     * from ServiceMBeanSupport.
     */
    private WebServer server = new WebServer();
 
+   /** The host portion of the RMI codebase URL */
+   private String host;
+   
    /**
     * @jmx:managed-constructor
     */
@@ -119,52 +121,58 @@
    }
 
    /**
-    * Get the name of the interface to use for the host portion of the
+    * Set the name of the interface to use for the host portion of the
     * RMI codebase URL.
     * 
     * @jmx:managed-attribute
     */
    public void setHost(final String hostname)
    {
-      server.setBindAddress(hostname);
+      this.host = ServerConfigUtil.fixRemoteAddress(hostname);
    }
 
    /**
-    * Set the name of the interface to use for the host portion of the
+    * Get the name of the interface to use for the host portion of the
     * RMI codebase URL.
     * 
     * @jmx:managed-attribute
     */
    public String getHost()
    {
-      return server.getBindHostname();
+      return host;
    }
 
    /**
-    * Get the specific address the WebService listens on.
+    * 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.
     *
     * @jmx:managed-attribute
     * 
-    * @return the interface name or IP address the WebService binds to.
+    * @param address  the interface name or IP address to bind. If host is null,
+    *                 connections on any/all local addresses will be allowed.
     */
-   public String getBindAddress()
+   public void setBindAddress(String address) throws UnknownHostException
    {
-      return server.getBindAddress();
+      InetAddress bindAddress = toInetAddress(address);
+      server.setBindAddress(bindAddress);
    }
-
+   
    /**
-    * 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.
+    * Get the specific address the WebService listens on.
     *
     * @jmx:managed-attribute
     * 
-    * @param host  the interface name or IP address to bind. If host is null,
-    *              connections on any/all local addresses will be allowed.
+    * @return the interface name or IP address the WebService binds to.
     */
-   public void setBindAddress(String host) throws UnknownHostException
+   public String getBindAddress()
    {
-      server.setBindAddress(host);
+      InetAddress bindAddress = server.getBindAddress();
+      
+      if (bindAddress != null)
+         return bindAddress.getHostAddress();
+      else
+         return null;
    }
 
    /**
@@ -245,6 +253,16 @@
       server.setDownloadResources(flag);
    }
 
+   /**
+    * The RMI codebase URL.
+    * 
+    * @jmx:managed-attribute
+    */
+   public String getCodebase()
+   {
+      return System.getProperty("java.rmi.server.codebase");
+   }
+   
    protected ObjectName getObjectName(MBeanServer server, ObjectName name)
       throws javax.management.MalformedObjectNameException
    {
@@ -273,52 +291,19 @@
          log.error("Failed to load org/jboss/web/mime.types; ignoring", e);
       }
 
-      String hostname = server.getBindAddress();
-
-      // If not user specified hostname given, then try to determine what it should be
-      if (hostname == null)
-      {
-         // First look for the rmi server name
-         try
-         {
-            hostname = System.getProperty("java.rmi.server.hostname");
-         }
-         catch (SecurityException e)
-         {
-            // ignore, but don't be silent
-            ThrowableHandler.addWarning(e);
-         }
-
-         // else use the localhost name
-         if (hostname == null)
-         {
-            try
-            {
-               hostname = InetAddress.getLocalHost().getHostName();
-            }
-            catch (IOException e)
-            {
-               log.error("Failed to get localhost name; ignoring", e);
-            }
-         }
-
-         if (hostname != null)
-         {
-            setHost(hostname);
-         }
-      }
-      // Host must be set to continue (either by user or detection)
-      String address = getHost();
-      if (address == null)
-         throw new MissingAttributeException("Host");
-
+      // if no override has been specified, default to the jboss bind address
+      if (getBindAddress() == null)
+         setBindAddress(System.getProperty(ServerConfig.SERVER_BIND_ADDRESS));
+      
+      // if no host specified, default to the java.rmi.server.hostname property value
+      if (getHost() == null)
+         setHost(System.getProperty("java.rmi.server.hostname"));
+      
       // Set the rmi codebase if it is not already set
-      String codebase = System.getProperty("java.rmi.server.codebase");
-      if (codebase == null)
+      String codebase = getCodebase();
+      if (codebase == null);
       {
-         address = ServerConfigUtil.fixRemoteAddress(address);
-
-         codebase = "http://" + address + ":" + getPort() + "/";
+         codebase = "http://" + getHost() + ":" + getPort() + "/";
          System.setProperty("java.rmi.server.codebase", codebase);
       }
       log.info("Using RMI server codebase: " + codebase);
@@ -341,5 +326,17 @@
    protected void stopService() throws Exception
    {
       server.stop();
+      log.debug("Stopped WebServer with address: " + server.getBindAddress() + ":" + getPort());
    }
+   
+   /**
+    * Safely convert a host string to InetAddress or null
+    */
+   private InetAddress toInetAddress(String host) throws UnknownHostException
+   {
+      if (host == null || host.length() == 0)
+         return null;
+      else
+         return InetAddress.getByName(host);
+   }
 }

Modified: branches/Branch_4_2/server/src/main/org/jboss/web/WebServiceMBean.java
===================================================================
--- branches/Branch_4_2/server/src/main/org/jboss/web/WebServiceMBean.java	2007-04-19 20:59:56 UTC (rev 62432)
+++ branches/Branch_4_2/server/src/main/org/jboss/web/WebServiceMBean.java	2007-04-19 21:22:16 UTC (rev 62433)
@@ -43,7 +43,7 @@
    // Attributes ----------------------------------------------------
    
    /** The specific address the WebService listens on. */
-   void setBindAddress(String host) throws UnknownHostException;
+   void setBindAddress(String bindAddress) throws UnknownHostException;
    String getBindAddress();
    
    /** The WebService listening port, 0 for anonymous. */
@@ -53,7 +53,7 @@
    /** The name of the interface to use for the host portion of the RMI codebase URL. */
    void setHost(String hostname);
    String getHost();
-
+   
    /** The WebService listen queue backlog limit. */
    void setBacklog(int backlog);   
    int getBacklog();
@@ -70,6 +70,9 @@
    /** The thread pool used for the WebServer class loading. */
    void setThreadPool(BasicThreadPoolMBean threadPool);
    
+   /** The RMI codebase URL. */
+   String getCodebase();
+   
    // Operations ----------------------------------------------------
    
    URL addClassLoader(ClassLoader cl);




More information about the jboss-cvs-commits mailing list