Skip to content

Commit

Permalink
[grid] Abstract SessionMap creation away to the options
Browse files Browse the repository at this point in the history
This makes it possible to use the RedisSessionMap if that's what we
want to do.
  • Loading branch information
shs96c committed Jan 16, 2020
1 parent 7db852f commit d7389a2
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class DefaultHubConfig extends MapConfig {

DefaultHubConfig() {
super(ImmutableMap.of(
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443",
"bind", true)));
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443",
"bind", true),
"sessions", ImmutableMap.of(
"implementation", "org.openqa.selenium.grid.sessionmap.local.LocalSessionMap")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class DefaultStandaloneConfig extends MapConfig {

DefaultStandaloneConfig() {
super(ImmutableMap.of(
"events", ImmutableMap.of(
"implementation", "org.openqa.selenium.events.local.GuavaEventBus")));
"events", ImmutableMap.of(
"implementation", "org.openqa.selenium.events.local.GuavaEventBus"),
"sessions", ImmutableMap.of(
"implementation", "org.openqa.selenium.grid.sessionmap.local.LocalSessionMap")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Executable configure(String... args) {
NetworkOptions networkOptions = new NetworkOptions(config);
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory();

SessionMap sessions = new SessionMapOptions(config).getSessionMap(tracer, clientFactory);
SessionMap sessions = new SessionMapOptions(config).getSessionMap();

Distributor distributor = new LocalDistributor(
tracer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public Executable configure(String... args) {
HttpClient.Factory clientFactory = networkOptions.getHttpClientFactory();

SessionMapOptions sessionsOptions = new SessionMapOptions(config);
SessionMap sessions = sessionsOptions.getSessionMap(tracer, clientFactory);
SessionMap sessions = sessionsOptions.getSessionMap();

BaseServerOptions serverOptions = new BaseServerOptions(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ java_library(
"//java/client/src/org/openqa/selenium/remote/http",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/remote",
artifact("com.beust:jcommander"),
artifact("io.opentracing:opentracing-api"),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,32 @@

package org.openqa.selenium.grid.sessionmap.config;

import io.opentracing.Tracer;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.config.ConfigException;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.remote.RemoteSessionMap;
import org.openqa.selenium.remote.http.HttpClient;

import java.net.MalformedURLException;
import java.net.URL;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
import java.util.logging.Logger;

public class SessionMapOptions {

private static final Logger LOG = Logger.getLogger(SessionMapOptions.class.getName());
private static final String DEFAULT_SESSION_MAP = "org.openqa.selenium.grid.sessionmap.remote.RemoteSessionMap";
private final Config config;

public SessionMapOptions(Config config) {
this.config = config;
}

public SessionMap getSessionMap(Tracer tracer, HttpClient.Factory clientFactory) {
HttpClient client = clientFactory.createClient(getSessionMapUrl());
return new RemoteSessionMap(tracer, client);
}

private URL getSessionMapUrl() {
Optional<URL> host = config.get("sessions", "host").map(str -> {
public URI getSessionMapUri() {
Optional<URI> host = config.get("sessions", "host").map(str -> {
try {
return new URL(str);
} catch (MalformedURLException e) {
return new URI(str);
} catch (URISyntaxException e) {
throw new ConfigException("Session map server URI is not a valid URI: " + str);
}
});
Expand All @@ -62,16 +59,45 @@ private URL getSessionMapUrl() {
}

try {
return new URL(
return new URI(
"http",
null,
hostname.get(),
port.get(),
"");
} catch (MalformedURLException e) {
"",
null,
null);
} catch (URISyntaxException e) {
throw new ConfigException(
"Session map server uri configured through host (%s) and port (%d) is not a valid URI",
hostname.get(),
port.get());
}
}

public SessionMap getSessionMap() {
String clazz = config.get("sessions", "implementation").orElse(DEFAULT_SESSION_MAP);
LOG.info("Creating event bus: " + clazz);
try {
Class<?> busClazz = Class.forName(clazz);
Method create = busClazz.getMethod("create", Config.class);

if (!Modifier.isStatic(create.getModifiers())) {
throw new IllegalArgumentException(String.format(
"Session map class %s's `create(Config)` method must be static", clazz));
}

if (!SessionMap.class.isAssignableFrom(create.getReturnType())) {
throw new IllegalArgumentException(String.format(
"Session map class %s's `create(Config)` method must return a SessionMap", clazz));
}

return (SessionMap) create.invoke(null, config);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException(String.format(
"Session map class %s must have a static `create(Config)` method", clazz));
} catch (ReflectiveOperationException e) {
throw new IllegalArgumentException("Unable to find event bus class: " + clazz, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ java_library(
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/server",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",
"//java/server/src/org/openqa/selenium/grid/sessionmap/local",
"//java/server/src/org/openqa/selenium/grid/web",
"//java/server/src/org/openqa/selenium/netty/server",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ class DefaultSessionMapConfig extends MapConfig {

public DefaultSessionMapConfig() {
super(ImmutableMap.of(
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443")));
"events", ImmutableMap.of(
"publish", "tcp://*:4442",
"subscribe", "tcp://*:4443"),
"sessions", ImmutableMap.of(
"implementation", "org.openqa.selenium.grid.sessionmap.local.LocalSessionMap")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.openqa.selenium.grid.server.HelpFlags;
import org.openqa.selenium.grid.server.Server;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.grid.sessionmap.local.LocalSessionMap;
import org.openqa.selenium.netty.server.NettyServer;
import org.openqa.selenium.remote.http.Contents;
Expand Down Expand Up @@ -106,7 +107,8 @@ public Executable configure(String... args) {
EventBusOptions events = new EventBusOptions(config);
EventBus bus = events.getEventBus();

SessionMap sessions = new LocalSessionMap(tracer, bus);
SessionMapOptions sessionMapOptions = new SessionMapOptions(config);
SessionMap sessions = sessionMapOptions.getSessionMap();

BaseServerOptions serverOptions = new BaseServerOptions(config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ java_library(
deps = [
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/events",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/server",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
import io.opentracing.Tracer;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.EventBusOptions;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.remote.SessionId;

Expand Down Expand Up @@ -50,6 +53,13 @@ public LocalSessionMap(Tracer tracer, EventBus bus) {
});
}

public static SessionMap create(Config config) {
Tracer tracer = new LoggingOptions(config).getTracer();
EventBus bus = new EventBusOptions(config).getEventBus();

return new LocalSessionMap(tracer, bus);
}

@Override
public boolean add(Session session) {
Objects.requireNonNull(session, "Session has not been set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ java_library(
deps = [
"//java/client/src/org/openqa/selenium/json",
"//java/client/src/org/openqa/selenium/remote",
"//java/server/src/org/openqa/selenium/grid/config",
"//java/server/src/org/openqa/selenium/grid/data",
"//java/server/src/org/openqa/selenium/grid/log",
"//java/server/src/org/openqa/selenium/grid/sessionmap",
"//java/server/src/org/openqa/selenium/grid/sessionmap/config",
"//java/server/src/org/openqa/selenium/grid/server",
"//java/server/src/org/openqa/selenium/grid/web",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import io.opentracing.Span;
import io.opentracing.Tracer;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.grid.config.Config;
import org.openqa.selenium.grid.data.Session;
import org.openqa.selenium.grid.log.LoggingOptions;
import org.openqa.selenium.grid.server.NetworkOptions;
import org.openqa.selenium.grid.sessionmap.SessionMap;
import org.openqa.selenium.grid.sessionmap.config.SessionMapOptions;
import org.openqa.selenium.grid.web.Values;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.remote.SessionId;
Expand All @@ -30,7 +34,10 @@
import org.openqa.selenium.remote.http.HttpResponse;
import org.openqa.selenium.remote.tracing.HttpTracing;

import java.io.UncheckedIOException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Objects;

import static org.openqa.selenium.remote.http.Contents.utf8String;
Expand All @@ -49,6 +56,18 @@ public RemoteSessionMap(Tracer tracer, HttpClient client) {
this.client = Objects.requireNonNull(client);
}

public static SessionMap create(Config config) {
Tracer tracer = new LoggingOptions(config).getTracer();
URI uri = new SessionMapOptions(config).getSessionMapUri();
HttpClient.Factory clientFactory = new NetworkOptions(config).getHttpClientFactory();

try {
return new RemoteSessionMap(tracer, clientFactory.createClient(uri.toURL()));
} catch (MalformedURLException e) {
throw new UncheckedIOException(e);
}
}

@Override
public boolean add(Session session) {
Objects.requireNonNull(session, "Session must be set");
Expand Down

0 comments on commit d7389a2

Please sign in to comment.