Author: slotia
Date: 2012-08-13 15:09:14 -0700 (Mon, 13 Aug 2012)
New Revision: 30180

Added:
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalhostServerSocketFactoryImpl.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/ServerSocketFactory.java
Modified:
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/CyActivator.java
   
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalHttpServer.java
Log:
Abstracted away server socket creation

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/CyActivator.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/CyActivator.java
 2012-08-13 22:08:22 UTC (rev 30179)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/CyActivator.java
 2012-08-13 22:09:14 UTC (rev 30180)
@@ -8,6 +8,8 @@
 import org.cytoscape.app.internal.net.WebQuerier;
 import org.cytoscape.app.internal.net.server.AppGetResponder;
 import org.cytoscape.app.internal.net.server.LocalHttpServer;
+import org.cytoscape.app.internal.net.server.ServerSocketFactory;
+import org.cytoscape.app.internal.net.server.LocalhostServerSocketFactoryImpl;
 import org.cytoscape.app.internal.net.server.LocalHttpServer.Response;
 import org.cytoscape.app.swing.CySwingAppAdapter;
 import org.cytoscape.application.CyVersion;
@@ -375,7 +377,8 @@
                        
                        @Override
                        public void run() {
-                               server = new LocalHttpServer(2608, 
Executors.newSingleThreadExecutor());
+                final ServerSocketFactory serverSocketFactory = new 
LocalhostServerSocketFactoryImpl(2608);
+                               server = new 
LocalHttpServer(serverSocketFactory, Executors.newSingleThreadExecutor());
                                server.addGetResponder(new 
AppGetResponder(appManager));
                                
                                server.run();

Modified: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalHttpServer.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalHttpServer.java
  2012-08-13 22:08:22 UTC (rev 30179)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalHttpServer.java
  2012-08-13 22:09:14 UTC (rev 30180)
@@ -110,8 +110,8 @@
     final List<GetResponder> getResponders = new ArrayList<GetResponder>(); 
     final List<PostResponder> postResponders = new ArrayList<PostResponder>(); 
 
+    final ServerSocketFactory serverSocketFactory;
     final Executor connectionHandlerExecutor;
-    final int port;
 
     final HttpParams params;
     final HttpService service;
@@ -122,12 +122,13 @@
      * than 1024 if the server is being executed by a non-root user.
      * @param connectionHandlerExecutor executes a connection handler when an 
incoming socket connection is received
      */
-    public LocalHttpServer(final int port, final Executor 
connectionHandlerExecutor) {
-               if (port <= 0) {
-                   throw new IllegalArgumentException("port <= 0");
-               }
-               
-               this.port = port;
+    public LocalHttpServer(final ServerSocketFactory serverSocketFactory, 
final Executor connectionHandlerExecutor) {
+        if (serverSocketFactory == null) {
+            throw new IllegalArgumentException("serverSocketFactory == null");
+        }
+
+        this.serverSocketFactory = serverSocketFactory;
+
                if (connectionHandlerExecutor == null) {
                    throw new 
IllegalArgumentException("connectionHandlerExecutor == null");
                }
@@ -174,13 +175,13 @@
                // Create a server socket
                ServerSocket serverSocket = null;
                try {
-                   serverSocket = new ServerSocket(port, 0, 
InetAddress.getByName(null));
+                   serverSocket = serverSocketFactory.createServerSocket();
                } catch (IOException e) {
                    logger.error("Failed to create server socket", e);
                    return;
                }
        
-               logger.info("Server socket started on {}", 
String.format("%s:%d", serverSocket.getInetAddress().getHostAddress(), port));
+               logger.info("Server socket started on {}", 
String.format("%s:%d", serverSocket.getInetAddress().getHostAddress(), 
serverSocket.getLocalPort()));
                
                // Keep servicing incoming connections until this thread is 
flagged as interrupted
                while (!Thread.interrupted()) { // TODO: **interrupted is 
deprecated?

Added: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalhostServerSocketFactoryImpl.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalhostServerSocketFactoryImpl.java
                         (rev 0)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/LocalhostServerSocketFactoryImpl.java
 2012-08-13 22:09:14 UTC (rev 30180)
@@ -0,0 +1,29 @@
+package org.cytoscape.app.internal.net.server;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.net.InetAddress;
+
+/**
+ * An implementation of {@link ServerSocketFactory} that only accepts 
connections
+ * from localhost.
+ */
+public class LocalhostServerSocketFactoryImpl implements ServerSocketFactory
+{
+    final int port;
+
+    public LocalhostServerSocketFactoryImpl(int port)
+    {
+        if (port <= 0)
+            throw new IllegalArgumentException("port <= 0");
+        this.port = port;
+    }
+
+    /**
+     * Create a server socket with the given port and default backlog.
+     */
+    public ServerSocket createServerSocket() throws IOException
+    {
+        return new ServerSocket(port, 0, InetAddress.getByName(null));
+    }
+}

Added: 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/ServerSocketFactory.java
===================================================================
--- 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/ServerSocketFactory.java
                              (rev 0)
+++ 
core3/impl/trunk/app-impl/src/main/java/org/cytoscape/app/internal/net/server/ServerSocketFactory.java
      2012-08-13 22:09:14 UTC (rev 30180)
@@ -0,0 +1,22 @@
+package org.cytoscape.app.internal.net.server;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+/**
+ * Used by the http server to create {@link ServerSocket}s.
+ * You can control the creation of a {@code ServerSocket} that the
+ * http server will use to serve clients.
+ * By creating {@code ServerSocket}s yourself, you can control the
+ * port the http server listens to and the IP addresses the http server 
accepts.
+ * This is not to be confused with {@link javax.net.ServerSocketFactory}, which
+ * is just a list of convenience methods for creating {@code ServerSocket}s.
+ */
+public interface ServerSocketFactory
+{
+    /**
+     * Create a {@code ServerSocket}.
+     * @throws IOException if this method is unable to create a working server 
socket
+     */
+    public ServerSocket createServerSocket() throws IOException;
+}

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to