Skip to content

Commit

Permalink
fixes #212 -- added GatewayServerBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartdag committed May 29, 2016
1 parent dcf3917 commit c2116bf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
81 changes: 81 additions & 0 deletions py4j-java/src/main/java/py4j/GatewayServer.java
Expand Up @@ -787,4 +787,85 @@ public List<Class<? extends Command>> getCustomCommands() {
public List<GatewayServerListener> getListeners() {
return Collections.unmodifiableList(listeners);
}

/**
* Helper class to make it easier and self-documenting how a
* {@link GatewayServer} is constructed.
*/
public static class GatewayServerBuilder {
private int javaPort;
private InetAddress javaAddress;
private int connectTimeout;
private int readTimeout;
private ServerSocketFactory serverSocketFactory;
private Object entryPoint;
private Py4JPythonClient callbackClient;
private List<Class<? extends Command>> customCommands;

public GatewayServerBuilder() {
this(null);
}

public GatewayServerBuilder(Object entryPoint) {
javaPort = GatewayServer.DEFAULT_PORT;
javaAddress = GatewayServer.defaultAddress();
connectTimeout = GatewayServer.DEFAULT_CONNECT_TIMEOUT;
readTimeout = GatewayServer.DEFAULT_READ_TIMEOUT;
serverSocketFactory = ServerSocketFactory.getDefault();
this.entryPoint = entryPoint;
}

public GatewayServer build() {
if (callbackClient == null) {
callbackClient = new CallbackClient(GatewayServer.DEFAULT_PYTHON_PORT);
}
return new GatewayServer(entryPoint, javaPort, javaAddress, connectTimeout, readTimeout, customCommands,
callbackClient, serverSocketFactory);
}

public GatewayServerBuilder javaPort(int javaPort) {
this.javaPort = javaPort;
return this;
}

public GatewayServerBuilder javaAddress(InetAddress javaAddress) {
this.javaAddress = javaAddress;
return this;
}

public GatewayServerBuilder callbackClient(int pythonPort, InetAddress pythonAddress) {
callbackClient = new CallbackClient(pythonPort, pythonAddress);
return this;
}

public GatewayServerBuilder callbackClient(CallbackClient callbackClient) {
this.callbackClient = callbackClient;
return this;
}

public GatewayServerBuilder connectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

public GatewayServerBuilder readTimeout(int readTimeout) {
this.readTimeout = readTimeout;
return this;
}

public GatewayServerBuilder serverSocketFactory(ServerSocketFactory serverSocketFactory) {
this.serverSocketFactory = serverSocketFactory;
return this;
}

public GatewayServerBuilder entryPoint(Object entryPoint) {
this.entryPoint = entryPoint;
return this;
}

public GatewayServerBuilder customCommands(List<Class<? extends Command>> customCommands) {
this.customCommands = customCommands;
return this;
}
}
}
4 changes: 2 additions & 2 deletions py4j-java/src/test/java/py4j/GatewayServerTest.java
Expand Up @@ -42,8 +42,8 @@ public class GatewayServerTest {

@Test
public void testDoubleListen() {
GatewayServer server1 = new GatewayServer(null);
GatewayServer server2 = new GatewayServer(null);
GatewayServer server1 = new GatewayServer.GatewayServerBuilder().entryPoint(null).build();
GatewayServer server2 = new GatewayServer.GatewayServerBuilder().entryPoint(null).build();
boolean valid = false;

try {
Expand Down

0 comments on commit c2116bf

Please sign in to comment.