Skip to content

Commit

Permalink
Provide actual working information for the Java Module System
Browse files Browse the repository at this point in the history
Because we need to make this stuff work with the Java
Platform Module System, and currently, we don't. The changes
here break into a few types:

* We weren't declaring that we were building multi-release
  jars properly in our manifests. This is now fixed.
* Automated tools for figuring out packages were screwing
  up with our shaded Jetty, so we built a custom module-info
  just for that.
* Our server classes didn't really have module definitions,
  and we'd set things up so that the same class could appear
  in two different JARs. This was suboptimal as it cause a
  split package, which causes modularised java to barf.
* Some of our internal classes were being used in modules
  other than the one that they were declared in. This is a
  big no-no, and is also considered suboptimal.

I'm pretty sure this isn't the end of the matter, but I
think that this will get us a lot further. The ideal goal will
be to use jlink to build Grid TNG and have that work from an
image. We'll see if we can get there. Wish us luck.
  • Loading branch information
shs96c committed May 31, 2019
1 parent 7797d1e commit 96fec3c
Show file tree
Hide file tree
Showing 32 changed files with 316 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ JAVA_RELEASE_TARGETS = [
'//java/client/src/org/openqa/selenium:client-combined',
'//java/server/src/com/thoughtworks/selenium:leg-rc',
'//java/server/src/org/openqa/grid/selenium:classes',
'//java/server/src/org/openqa/selenium/grid:module',
'//java/server/src/org/openqa/selenium/grid:grid',
'//third_party/java/jetty:jetty'
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module org.openqa.selenium.firefox.xpi {
requires transitive org.openqa.selenium.core;
requires transitive org.openqa.selenium.remote;

exports org.openqa.selenium.firefox;
exports org.openqa.selenium.firefox.xpi;

provides org.openqa.selenium.remote.service.DriverService$Builder with
org.openqa.selenium.firefox.xpi.XpiDriverService$Builder;
Expand Down
2 changes: 2 additions & 0 deletions java/client/src/org/openqa/selenium/remote/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ java_library(
"//java/client/src/org/openqa/selenium:selenium",
],
deps = [
"//java/client/src/org/openqa/selenium/remote/tracing:tracing",
"//third_party/java/bytebuddy:byte-buddy",
"//third_party/java/guava:guava",
],
Expand All @@ -86,6 +87,7 @@ java_library(
"HandshakeResponse.java",
"HttpCommandExecutor.java",
"InitialHandshakeResponse.java",
"JsonToWebElementConverter.java",
"JsonWireProtocolResponse.java",
"LocalFileDetector.java",
"NewSessionPayload.java",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.remote;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.openqa.selenium.WebElement;

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
* Reconstitutes {@link WebElement}s from their JSON representation. Will recursively convert Lists
* and Maps to catch nested references. All other values pass through the converter unchanged.
*/
public class JsonToWebElementConverter implements Function<Object, Object> {

private final RemoteWebDriver driver;

public JsonToWebElementConverter(RemoteWebDriver driver) {
this.driver = driver;
}

@Override
public Object apply(Object result) {
if (result instanceof Collection<?>) {
Collection<?> results = (Collection<?>) result;
return Lists.newArrayList(Iterables.transform(results, this));
}

if (result instanceof Map<?, ?>) {
Map<?, ?> resultAsMap = (Map<?, ?>) result;
String elementKey = getElementKey(resultAsMap);
if (null != elementKey) {
RemoteWebElement element = newRemoteWebElement();
element.setId(String.valueOf(resultAsMap.get(elementKey)));
return element;
} else {
return Maps.transformValues(resultAsMap, this);
}
}

if (result instanceof RemoteWebElement) {
return setOwner((RemoteWebElement) result);
}

if (result instanceof Number) {
if (result instanceof Float || result instanceof Double) {
return ((Number) result).doubleValue();
}
return ((Number) result).longValue();
}

return result;
}

protected RemoteWebElement newRemoteWebElement() {
return setOwner(new RemoteWebElement());
}

private RemoteWebElement setOwner(RemoteWebElement element) {
if (driver != null) {
element.setParent(driver);
element.setFileDetector(driver.getFileDetector());
}
return element;
}
private String getElementKey(Map<?, ?> resultAsMap) {
for (Dialect d : Dialect.values()) {
String elementKeyForDialect = d.getEncodedElementKey();
if (resultAsMap.containsKey(elementKeyForDialect)) {
return elementKeyForDialect;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.logging.Logs;
import org.openqa.selenium.logging.NeedsLocalLogs;
import org.openqa.selenium.remote.internal.JsonToWebElementConverter;
import org.openqa.selenium.remote.internal.WebElementToJsonConverter;

import java.net.URL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,70 +29,19 @@

import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
* Reconstitutes {@link WebElement}s from their JSON representation. Will recursively convert Lists
* and Maps to catch nested references. All other values pass through the converter unchanged.
*
* @deprecated Use {@link org.openqa.selenium.remote.JsonToWebElementConverter} instead.
*/
public class JsonToWebElementConverter implements Function<Object, Object> {

private final RemoteWebDriver driver;
@Deprecated
public class JsonToWebElementConverter extends org.openqa.selenium.remote.JsonToWebElementConverter {

public JsonToWebElementConverter(RemoteWebDriver driver) {
this.driver = driver;
}

@Override
public Object apply(Object result) {
if (result instanceof Collection<?>) {
Collection<?> results = (Collection<?>) result;
return Lists.newArrayList(Iterables.transform(results, this));
}

if (result instanceof Map<?, ?>) {
Map<?, ?> resultAsMap = (Map<?, ?>) result;
String elementKey = getElementKey(resultAsMap);
if (null != elementKey) {
RemoteWebElement element = newRemoteWebElement();
element.setId(String.valueOf(resultAsMap.get(elementKey)));
return element;
} else {
return Maps.transformValues(resultAsMap, this);
}
}

if (result instanceof RemoteWebElement) {
return setOwner((RemoteWebElement) result);
}

if (result instanceof Number) {
if (result instanceof Float || result instanceof Double) {
return ((Number) result).doubleValue();
}
return ((Number) result).longValue();
}

return result;
super(driver);
}

protected RemoteWebElement newRemoteWebElement() {
return setOwner(new RemoteWebElement());
}

private RemoteWebElement setOwner(RemoteWebElement element) {
if (driver != null) {
element.setParent(driver);
element.setFileDetector(driver.getFileDetector());
}
return element;
}
private String getElementKey(Map<?, ?> resultAsMap) {
for (Dialect d : Dialect.values()) {
String elementKeyForDialect = d.getEncodedElementKey();
if (resultAsMap.containsKey(elementKeyForDialect)) {
return elementKeyForDialect;
}
}
return null;
}
}
10 changes: 8 additions & 2 deletions java/client/src/org/openqa/selenium/remote/module-info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module org.openqa.selenium.remote {
requires net.bytebuddy;

requires transitive com.google.common;
requires transitive httpclient;
requires transitive httpcore;
requires transitive io.opentracing.api;
requires transitive io.opentracing.noop;
requires transitive io.opentracing.util;
requires transitive java.logging;
requires transitive okhttp3;
requires transitive org.openqa.selenium.core;
Expand All @@ -15,12 +16,17 @@ module org.openqa.selenium.remote {
exports org.openqa.selenium.net;
exports org.openqa.selenium.os;
exports org.openqa.selenium.remote;
exports org.openqa.selenium.remote.codec;
exports org.openqa.selenium.remote.codec.jwp;
exports org.openqa.selenium.remote.codec.w3c;
exports org.openqa.selenium.remote.html5;
exports org.openqa.selenium.remote.http;
exports org.openqa.selenium.remote.mobile;
exports org.openqa.selenium.remote.service;
exports org.openqa.selenium.remote.session;
exports org.openqa.selenium.remote.tracing;

uses io.opentracing.Tracer;
uses org.openqa.selenium.remote.service.CapabilitiesFilter;
uses org.openqa.selenium.remote.service.CapabilityTransform;
uses org.openqa.selenium.remote.service.DriverService$Builder;
Expand Down
9 changes: 6 additions & 3 deletions java/rules.bzl
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
def _gen_build_info(name, maven_coords):
def _gen_build_info(name, maven_coords, multi_release_jar):
if not (maven_coords):
return []

rev = native.read_config("selenium", "rev", "unknown")
time = native.read_config("selenium", "timestamp", "unknown")
multi_release = "false"
if multi_release_jar:
multi_release = "true"

native.genrule(
name = "%s-gen-manifest" % name,
out = "manifest",
cmd = 'python -c "print(\'\\n\\nName: Build-Info\\nBuild-Revision: {}\\nBuild-Time: {}\\n\\n\')" >> $OUT'.format(rev, time),
cmd = 'python -c "print(\'Multi-Release: {}\\n\\nName: Build-Info\\nBuild-Revision: {}\\nBuild-Time: {}\\n\\n\')" >> $OUT'.format(multi_release_jar, rev, time),
)

native.java_library(
Expand All @@ -35,7 +38,7 @@ def java_library(name, maven_coords = None, module_info = None, deps = [], **kwa

all_deps += [":%s-module-info" % name]

all_deps += _gen_build_info(name, maven_coords)
all_deps += _gen_build_info(name, maven_coords, module_info != None)

native.java_library(
name = name,
Expand Down
2 changes: 1 addition & 1 deletion java/server/src/com/thoughtworks/selenium/webdriven/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ java_library(
"//java/client/src/org/openqa/selenium/opera:opera",
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/client/src/org/openqa/selenium/safari:safari",
"//java/server/src/org/openqa/selenium/grid/web:web",
"//java/server/src/org/openqa/selenium/grid:grid",
"//java/server/src/org/openqa/selenium/remote/server:server",
"//java/server/src/org/openqa/selenium/remote/server:sessions",
"//java/server/src/org/openqa/selenium/remote/server:webdriver-servlet",
Expand Down
4 changes: 1 addition & 3 deletions java/server/src/org/openqa/grid/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ java_library(
"//java/client/src/org/openqa/selenium/remote:remote",
"//java/client/src/org/openqa/selenium/firefox:firefox",
"//java/client/src/org/openqa/selenium/safari:safari",
"//java/server/src/org/openqa/selenium/grid/config:config",
"//java/server/src/org/openqa/selenium/grid/server:server",
"//java/server/src/org/openqa/selenium/grid:grid",
"//java/server/src/org/openqa/selenium/remote/server:server",
"//java/server/src/org/openqa/selenium/remote/server:webdriver-servlet",
"//java/server/src/org/openqa/selenium/remote/server/log:log",
"//java/server/src/org/openqa/selenium/remote/server/jmx:jmx",
"//third_party/java/beust:jcommander",
"//third_party/java/guava:guava",
"//third_party/java/jcip:jcip-annotations",
Expand Down
10 changes: 4 additions & 6 deletions java/server/src/org/openqa/grid/selenium/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//java:version.bzl", "SE_VERSION")
load("//java:rules.bzl", "java_library")

# Force a rebuild every time we're parsed.

Expand All @@ -18,6 +19,7 @@ java_library(
name = "classes",
maven_coords = "org.seleniumhq.selenium:selenium-server:" + SE_VERSION,
maven_pom_template = "//java/client/src/org/openqa/selenium:template-pom",
module_info = "module-info.txt",
srcs = glob(
["**/*.java"],
exclude = ["node/**"],
Expand All @@ -26,19 +28,15 @@ java_library(
"//java/client/src/org/openqa/selenium:selenium",
"//java/client/src/org/openqa/selenium:client-combined",
"//java/server/src/org/openqa/grid:grid",
"//java/server/src/org/openqa/selenium/grid/server:server",
"//java/server/src/org/openqa/selenium/grid:grid",
"//java/server/src/org/openqa/selenium/remote/server/log:log",
"//java/server/src/org/openqa/selenium/remote/server:standalone-server-lib",
"//java/server/src/org/openqa/selenium/remote/server:server",
"//java/server/src/org/openqa/selenium/remote/server/jmx:jmx",
"//third_party/java/guava:guava",
"//third_party/java/servlet:javax.servlet-api",
"//third_party/java/beust:jcommander",
],
visibility = [
"//java/server/test/org/openqa/grid/e2e:tests",
"//java/server/test/org/openqa/grid/selenium/proxy:proxy",
],
visibility = [ "PUBLIC" ],
)

# This isn't very elegant, but we can build a dist zip like this:
Expand Down
Loading

0 comments on commit 96fec3c

Please sign in to comment.