Apply Ed Schaller's feature patch to allow host address to be specified in configuration. See FELIX-3811 for details.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1423109 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
index ea5e997..d876f70 100644
--- a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
@@ -112,6 +112,8 @@
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,
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
index ad728c0..e75af10 100644
--- a/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
@@ -69,6 +69,10 @@
* 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 @@
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 @@
* <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 @@
// 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 @@
}
/**
+ * 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 @@
{
// 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 void setStopping() {
m_stopping = true;
}
-}
\ No newline at end of file
+}