Browse Source

ZOOKEEPER-2221: Zookeeper JettyAdminServer server should start on configured IP
(Surendra Singh Lilhore via rgs)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1688505 13f79535-47bb-0310-9956-ffa450edef68

Raúl Gutiérrez Segalés 10 years ago
parent
commit
a61b4e1b52

+ 3 - 0
CHANGES.txt

@@ -140,6 +140,9 @@ BUGFIXES:
   ZOOKEEPER-2193: reconfig command completes even if parameter is wrong obviously
   (Yasuhito Fukuda via rgs)
 
+  ZOOKEEPER-2221: Zookeeper JettyAdminServer server should start on configured IP
+  (Surendra Singh Lilhore via rgs)
+
 IMPROVEMENTS:
   ZOOKEEPER-1660 Documentation for Dynamic Reconfiguration (Reed Wanderman-Milne via shralex)  
 

+ 11 - 0
src/docs/src/documentation/content/xdocs/zookeeperAdmin.xml

@@ -1473,6 +1473,17 @@ server.3=zoo3:2888:3888</programlisting>
             </listitem>
           </varlistentry>
 
+          <varlistentry>
+            <term>admin.serverAddress</term>
+
+            <listitem>
+              <para>(Java system property: <emphasis
+              role="bold">zookeeper.admin.serverAddress</emphasis>)</para>
+
+              <para>The address the embedded Jetty server listens on. Defaults to 0.0.0.0.</para>
+            </listitem>
+          </varlistentry>
+
           <varlistentry>
             <term>admin.serverPort</term>
 

+ 26 - 13
src/java/main/org/apache/zookeeper/server/admin/JettyAdminServer.java

@@ -32,6 +32,7 @@ import javax.servlet.http.HttpServletResponse;
 
 import org.apache.zookeeper.server.ZooKeeperServer;
 import org.mortbay.jetty.Server;
+import org.mortbay.jetty.nio.SelectChannelConnector;
 import org.mortbay.jetty.servlet.Context;
 import org.mortbay.jetty.servlet.ServletHolder;
 import org.slf4j.Logger;
@@ -54,26 +55,36 @@ public class JettyAdminServer implements AdminServer {
     static final Logger LOG = LoggerFactory.getLogger(JettyAdminServer.class);
 
     public static final int DEFAULT_PORT = 8080;
+    private static final String DEFAULT_ADDRESS = "0.0.0.0";
     public static final String DEFAULT_COMMAND_URL = "/commands";
 
     private final Server server;
     private ZooKeeperServer zkServer;
     private final int port;
+    private String address;
     private final String commandUrl;
 
     public JettyAdminServer() throws AdminServerException {
-        this(Integer.getInteger("zookeeper.admin.serverPort", DEFAULT_PORT),
-             System.getProperty("zookeeper.admin.commandURL", DEFAULT_COMMAND_URL));
+        this(System.getProperty("zookeeper.admin.serverAddress",
+                DEFAULT_ADDRESS), Integer.getInteger(
+                "zookeeper.admin.serverPort", DEFAULT_PORT), System
+                .getProperty("zookeeper.admin.commandURL", DEFAULT_COMMAND_URL));
     }
 
-    public JettyAdminServer(int port, String commandUrl) {
+    public JettyAdminServer(String address, int port, String commandUrl) {
         this.port = port;
         this.commandUrl = commandUrl;
+        this.address = address;
 
-        server = new Server(port);
+        server = new Server();
+        SelectChannelConnector connector = new SelectChannelConnector();
+        connector.setHost(address);
+        connector.setPort(port);
+        server.addConnector(connector);
         Context context = new Context(server, "/");
         server.setHandler(context);
-        context.addServlet(new ServletHolder(new CommandServlet()), commandUrl + "/*");
+        context.addServlet(new ServletHolder(new CommandServlet()), commandUrl
+                + "/*");
     }
 
     /**
@@ -86,12 +97,13 @@ public class JettyAdminServer implements AdminServer {
         } catch (Exception e) {
             // Server.start() only throws Exception, so let's at least wrap it
             // in an identifiable subclass
-            throw new AdminServerException(
-                    String.format("Problem starting AdminServer on port %d, command URL %s",
-                                  port, commandUrl), e);
+            throw new AdminServerException(String.format(
+                    "Problem starting AdminServer on address %s,"
+                            + " port %d and command URL %s", address, port,
+                    commandUrl), e);
         }
-        LOG.info(String.format("Started AdminServer on port %d, command URL %s",
-                               port, commandUrl));
+        LOG.info(String.format("Started AdminServer on address %s, port %d"
+                + " and command URL %s", address, port, commandUrl));
     }
 
     /**
@@ -106,9 +118,10 @@ public class JettyAdminServer implements AdminServer {
         try {
             server.stop();
         } catch (Exception e) {
-            throw new AdminServerException(
-                    String.format("Problem stopping AdminServer on port %d, command URL %s",
-                                  port, commandUrl), e);
+            throw new AdminServerException(String.format(
+                    "Problem stopping AdminServer on address %s,"
+                            + " port %d and command URL %s", address, port, commandUrl),
+                    e);
         }
     }