Author: kgilmer
Date: Mon Dec 17 19:41:13 2012
New Revision: 1423109

URL: http://svn.apache.org/viewvc?rev=1423109&view=rev
Log:
Apply Ed Schaller's feature patch to allow host address to be specified in 
configuration.  See FELIX-3811 for details.

Modified:
    
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
    
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java

Modified: 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java?rev=1423109&r1=1423108&r2=1423109&view=diff
==============================================================================
--- 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
 (original)
+++ 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
 Mon Dec 17 19:41:13 2012
@@ -112,6 +112,8 @@ public class Activator implements Bundle
 
         config.put(Server.CONFIG_PROPERTY_HTTP_PORT,
             context.getProperty(Server.CONFIG_PROPERTY_HTTP_PORT));
+        config.put(Server.CONFIG_PROPERTY_HTTP_HOST,
+            context.getProperty(Server.CONFIG_PROPERTY_HTTP_HOST));
         config.put(Server.CONFIG_PROPERTY_HTTP_ENABLE,
             context.getProperty(Server.CONFIG_PROPERTY_HTTP_ENABLE));
         config.put(Server.CONFIG_PROPERTY_HTTPS_ENABLE,

Modified: 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java?rev=1423109&r1=1423108&r2=1423109&view=diff
==============================================================================
--- 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
 (original)
+++ 
felix/trunk/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
 Mon Dec 17 19:41:13 2012
@@ -69,6 +69,10 @@ public class Server
      * The port used for servlets and resources available via HTTP. The 
default is 8080. A negative port number has the same effect as setting 
org.apache.felix.http.enable to false.
      */
     public static final String CONFIG_PROPERTY_HTTP_PORT = 
"org.osgi.service.http.port";
+    /**
+     * The address of the host interface to bind http to. The default is to 
bind to all interfaces.
+     */
+    public static final String CONFIG_PROPERTY_HTTP_HOST = 
"org.apache.felix.http.host"; 
 
     /**
      * Default HTTP port to listen on.
@@ -94,6 +98,7 @@ public class Server
 
     private String m_hostname;
     private final int m_port;
+    private InetAddress m_bindAddr;
 
     private int m_state;
     private ThreadGate m_shutdownGate;
@@ -119,6 +124,9 @@ public class Server
      *   <li><tt>org.osgi.service.http.port</tt> - the port on which it 
listens for connections;
      *       the default is 8080.
      *   </li>   
+     *   <li><tt>org.apache.felix.http.host</tt> - the address of the host 
interface which is bound;
+     *       the default is all addresses.
+     *   </li>
      *   <li><tt>org.apache.felix.http.threadpool.limit</tt> - the maximum 
number of threads in the
      *       thread pool; the default value is 10.
      *   </li>
@@ -149,6 +157,7 @@ public class Server
 
         // Read in the configured properties or their default values.
         m_port = getConfiguredPort(configMap);
+        m_bindAddr = getConfiguredBindAddr(configMap);
         int threadLimit = 
(configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP) == null) ? 
DEFAULT_THREADPOOL_LIMIT
             : Integer.parseInt((String) 
configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP));
         int threadTimeout = 
(configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP) == null) ? 
ThreadPool.DEFAULT_THREAD_TIMEOUT
@@ -173,6 +182,27 @@ public class Server
     }
 
     /**
+     * Get the address of the interface the HTTP server listens on based on 
configuration map or default value.
+     * 
+     * @param configMap
+     * @return Address of the interface to bind to or null if no interface is 
specified.
+     */
+    public InetAddress getConfiguredBindAddr(Map configMap)
+    {
+        try
+        {
+            return (configMap.get(Server.CONFIG_PROPERTY_HTTP_HOST) == null) ? 
null
+                : InetAddress.getByName((String) 
configMap.get(Server.CONFIG_PROPERTY_HTTP_HOST));
+        }
+        catch(UnknownHostException ex)
+        {
+            m_logger.log(Logger.LOG_ERROR,
+                "Unable to resolve " + 
configMap.get(Server.CONFIG_PROPERTY_HTTP_HOST) + " to address of interface to 
bind to. Binding to all interfaces.", ex);
+           return null;
+        }
+    }
+
+    /**
      * This method returns the current state of the web server, which is one
      * of the following values:
      * <ul>
@@ -240,7 +270,12 @@ public class Server
         {
             // If inactive, then create server socket, server thread, and
             // set state to active.
-            m_serverSocket = new ServerSocket(m_port);
+            if(m_bindAddr == null)
+                m_serverSocket = new ServerSocket(m_port);
+            else
+                m_serverSocket = new ServerSocket(m_port, 0, m_bindAddr);
+            
+
             m_serverThread = new Thread(new Runnable()
             {
                                public void run()
@@ -404,4 +439,4 @@ public class Server
        public void setStopping() {             
                m_stopping  = true;
        }
-}
\ No newline at end of file
+}


Reply via email to