[jboss-cvs] JBossAS SVN: r62435 - trunk/server/src/main/org/jboss/web.

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


Author: dimitris at jboss.org
Date: 2007-04-19 17:25:31 -0400 (Thu, 19 Apr 2007)
New Revision: 62435

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

Modified: trunk/server/src/main/org/jboss/web/WebServer.java
===================================================================
--- trunk/server/src/main/org/jboss/web/WebServer.java	2007-04-19 21:22:50 UTC (rev 62434)
+++ trunk/server/src/main/org/jboss/web/WebServer.java	2007-04-19 21:25:31 UTC (rev 62435)
@@ -1,24 +1,24 @@
 /*
-* JBoss, Home of Professional Open Source
-* Copyright 2005, JBoss Inc., and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-*/
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 package org.jboss.web;
 
 import java.io.BufferedInputStream;
@@ -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;
 
@@ -54,7 +52,8 @@
  *
  * It is configured by calling any methods programmatically prior to startup.
  * @author <a href="mailto:marc at jboss.org">Marc Fleury</a>
- * @author <a href="mailto:Scott.Stark at org.jboss">Scott Stark</a>.
+ * @author <a href="mailto:Scott.Stark at org.jboss">Scott Stark</a>
+ * @authro <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
  * @version $Revision$
  * @see WebClassLoader
  */
@@ -65,6 +64,7 @@
 
    // Attributes ----------------------------------------------------
    private static Logger log = Logger.getLogger(WebServer.class);
+   
    /**
     * The port the web server listens on
     */
@@ -75,6 +75,7 @@
     * control over which interfaces accept connections.
     */
    private InetAddress bindAddress;
+   
    /**
     * The serverSocket listen queue depth
     */
@@ -84,26 +85,31 @@
     * The map of class loaders registered with the web server
     */
    private final ConcurrentReaderHashMap loaderMap = new ConcurrentReaderHashMap();
+   
    /**
     * 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;
+   
    /**
     * A flag indicating if the server should attempt to download resources,
     * i.e. resource paths that don't end in .class
     */
    private boolean downloadResources = false;
+   
    /**
     * 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 final Properties mimeTypes = new Properties();
+   
    /**
     * The thread pool used to manage listening threads
     */
@@ -114,9 +120,9 @@
    /**
     * Set the http listening port
     */
-   public void setPort(int p)
+   public void setPort(int port)
    {
-      port = p;
+      this.port = port;
    }
 
    /**
@@ -128,36 +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();
-      return address;
+      this.bindAddress = bindAddress;
+      
    }
-
-   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
@@ -174,6 +169,7 @@
    {
       if (backlog <= 0)
          backlog = 50;
+      
       this.backlog = backlog;
    }
 
@@ -186,7 +182,7 @@
    {
       downloadServerClasses = flag;
    }
-   
+
    public boolean getDownloadResources()
    {
       return downloadResources;
@@ -197,7 +193,6 @@
       downloadResources = flag;
    }
 
-
    public BasicThreadPoolMBean getThreadPool()
    {
       return threadPool;
@@ -225,15 +220,17 @@
    {
       if (threadPool == null)
          threadPool = new BasicThreadPool("ClassLoadingPool");
+      
       try
       {
          server = new ServerSocket(port, backlog, bindAddress);
          log.debug("Started server: " + server);
+         
          listen();
       }
       catch(java.net.BindException be)
       {
-          throw new Exception("Port "+port+" already in use.",be);
+           throw new Exception("Port "+port+" already in use.",be);
       }
       catch (IOException e)
       {
@@ -252,9 +249,7 @@
          server = null;
          srv.close();
       }
-      catch (Exception e)
-      {
-      }
+      catch (Exception ignore) {}
    }
 
    /**

Modified: trunk/server/src/main/org/jboss/web/WebService.java
===================================================================
--- trunk/server/src/main/org/jboss/web/WebService.java	2007-04-19 21:22:50 UTC (rev 62434)
+++ trunk/server/src/main/org/jboss/web/WebService.java	2007-04-19 21:25:31 UTC (rev 62435)
@@ -1,27 +1,26 @@
 /*
-* JBoss, Home of Professional Open Source
-* Copyright 2005, JBoss Inc., and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-*/
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
 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: trunk/server/src/main/org/jboss/web/WebServiceMBean.java
===================================================================
--- trunk/server/src/main/org/jboss/web/WebServiceMBean.java	2007-04-19 21:22:50 UTC (rev 62434)
+++ trunk/server/src/main/org/jboss/web/WebServiceMBean.java	2007-04-19 21:25:31 UTC (rev 62435)
@@ -1,8 +1,8 @@
 /*
- * JBoss, Home of Professional Open Source
- * Copyright 2005, JBoss Inc., and individual contributors as indicated
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
@@ -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