Skip to content

Commit

Permalink
Merge pull request #4868 from jansupol/sebootstrap.class
Browse files Browse the repository at this point in the history
Added implementation of RuntimeDelegate#bootstrap(Class, Configuration)
  • Loading branch information
arjantijms authored Sep 27, 2021
2 parents adfa1ce + ac638e5 commit b9479ae
Show file tree
Hide file tree
Showing 29 changed files with 601 additions and 348 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -307,9 +307,16 @@ private void rethrow(final Throwable error) {
* @param application JAX-RS / Jersey application to be deployed on Grizzly HTTP container.
*/
/* package */ GrizzlyHttpContainer(final Application application) {
this.appHandler = new ApplicationHandler(application, new GrizzlyBinder());
cacheConfigSetStatusOverSendError();
cacheConfigEnableLeadingContextPathSlashes();
this(new ApplicationHandler(application, new GrizzlyBinder()));
}

/**
* Create a new Grizzly HTTP container.
*
* @param applicationClass JAX-RS / Jersey application to be deployed on Grizzly HTTP container.
*/
/* package */ GrizzlyHttpContainer(final Class<? extends Application> applicationClass) {
this(new ApplicationHandler(applicationClass, new GrizzlyBinder()));
}

/**
Expand All @@ -319,7 +326,11 @@ private void rethrow(final Throwable error) {
* @param parentContext DI provider specific context with application's registered bindings.
*/
/* package */ GrizzlyHttpContainer(final Application application, final Object parentContext) {
this.appHandler = new ApplicationHandler(application, new GrizzlyBinder(), parentContext);
this(new ApplicationHandler(application, new GrizzlyBinder(), parentContext));
}

private GrizzlyHttpContainer(ApplicationHandler applicationHandler) {
this.appHandler = applicationHandler;
cacheConfigSetStatusOverSendError();
cacheConfigEnableLeadingContextPathSlashes();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ final class GrizzlyHttpServer implements WebServer {
private final HttpServer httpServer;

GrizzlyHttpServer(final Application application, final JerseySeBootstrapConfiguration configuration) {
this(new GrizzlyHttpContainer(application), configuration);
}

GrizzlyHttpServer(final Class<? extends Application> applicationClass,
final JerseySeBootstrapConfiguration configuration) {
this(new GrizzlyHttpContainer(applicationClass), configuration);
}

private GrizzlyHttpServer(final GrizzlyHttpContainer container, final JerseySeBootstrapConfiguration configuration) {
final SSLContext sslContext = configuration.sslContext();
final SeBootstrap.Configuration.SSLClientAuthentication sslClientAuthentication = configuration
.sslClientAuthentication();

this.container = new GrizzlyHttpContainer(application);
this.container = container;
this.httpServer = GrizzlyHttpServerFactory.createHttpServer(
configuration.uri(false),
this.container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public final <T extends WebServer> T createServer(final Class<T> type, final App
? type.cast(new GrizzlyHttpServer(application, configuration))
: null;
}

@Override
public <T extends WebServer> T createServer(Class<T> type, Class<? extends Application> applicationClass,
JerseySeBootstrapConfiguration configuration) {
return GrizzlyHttpServer.class == type || WebServer.class == type
? type.cast(new GrizzlyHttpServer(applicationClass, configuration))
: null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,44 +61,31 @@
public final class GrizzlyHttpServerProviderTest {

@Test(timeout = 15000)
public final void shouldProvideServer() throws InterruptedException, ExecutionException {
public void shouldProvideServer() throws InterruptedException, ExecutionException {
// given
final WebServerProvider webServerProvider = new GrizzlyHttpServerProvider();
final Resource resource = new Resource();
final Application application = new Application() {
@Override
public final Set<Object> getSingletons() {
return Collections.singleton(resource);
}
};
final SeBootstrap.Configuration configuration = name -> {
switch (name) {
case SeBootstrap.Configuration.PROTOCOL:
return "HTTP";
case SeBootstrap.Configuration.HOST:
return "localhost";
case SeBootstrap.Configuration.PORT:
return getPort();
case SeBootstrap.Configuration.ROOT_PATH:
return "/";
case SeBootstrap.Configuration.SSL_CLIENT_AUTHENTICATION:
return SSLClientAuthentication.NONE;
case SeBootstrap.Configuration.SSL_CONTEXT:
try {
return SSLContext.getDefault();
} catch (final NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
case ServerProperties.WEBSERVER_AUTO_START:
return FALSE;
default:
return null;
}
};
shouldProvideServer(ShouldProvideServerApplication.class, resource);
}

@Test(timeout = 15000)
public void shouldProvideServerWithClass() throws InterruptedException, ExecutionException {
// given
final Resource resource = new Resource();
final Application application = new ShouldProvideServerApplication();
shouldProvideServer(application.getClass(), resource);
}

private void shouldProvideServer(final Object application, final Resource resource)
throws InterruptedException, ExecutionException {
// given
final WebServerProvider webServerProvider = new GrizzlyHttpServerProvider();
final SeBootstrap.Configuration configuration = configuration(getPort());

// when
final JerseySeBootstrapConfiguration jerseySeConfig = JerseySeBootstrapConfiguration.from(configuration);
final WebServer webServer = webServerProvider.createServer(WebServer.class, application, jerseySeConfig);
final WebServer webServer = Application.class.isInstance(application)
? webServerProvider.createServer(WebServer.class, (Application) application, jerseySeConfig)
: webServerProvider.createServer(WebServer.class, (Class<Application>) application, jerseySeConfig);
final Object nativeHandle = webServer.unwrap(Object.class);
final CompletionStage<?> start = webServer.start();
final Object startResult = start.toCompletableFuture().get();
Expand All @@ -124,8 +111,15 @@ public final Set<Object> getSingletons() {
protected static final class Resource {
@GET
@Override
public final String toString() {
return super.toString();
public String toString() {
return Resource.class.getName();
}
}

protected static class ShouldProvideServerApplication extends Application {
@Override
public final Set<Object> getSingletons() {
return Collections.singleton(new Resource());
}
}

Expand Down Expand Up @@ -156,35 +150,11 @@ private static final int getPort() {
}

@Test(timeout = 15000)
public final void shouldScanFreePort() throws InterruptedException, ExecutionException {
public void shouldScanFreePort() {
// given
final WebServerProvider webServerProvider = new GrizzlyHttpServerProvider();
final Application application = new Application();
final SeBootstrap.Configuration configuration = name -> {
switch (name) {
case SeBootstrap.Configuration.PROTOCOL:
return "HTTP";
case SeBootstrap.Configuration.HOST:
return "localhost";
case SeBootstrap.Configuration.PORT:
return SeBootstrap.Configuration.FREE_PORT;
case SeBootstrap.Configuration.ROOT_PATH:
return "/";
case SeBootstrap.Configuration.SSL_CLIENT_AUTHENTICATION:
return SSLClientAuthentication.NONE;
case SeBootstrap.Configuration.SSL_CONTEXT:
try {
return SSLContext.getDefault();
} catch (final NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
case ServerProperties.WEBSERVER_AUTO_START:
return TRUE;
default:
return null;
}
};

final SeBootstrap.Configuration configuration = configuration(SeBootstrap.Configuration.FREE_PORT);

// when
final JerseySeBootstrapConfiguration jerseySeConfig = JerseySeBootstrapConfiguration.from(configuration);
Expand All @@ -194,4 +164,31 @@ public final void shouldScanFreePort() throws InterruptedException, ExecutionExc
assertThat(webServer.port(), is(greaterThan(0)));
}

private SeBootstrap.Configuration configuration(int port) {
return (SeBootstrap.Configuration) name -> {
switch (name) {
case SeBootstrap.Configuration.PROTOCOL:
return "HTTP";
case SeBootstrap.Configuration.HOST:
return "localhost";
case SeBootstrap.Configuration.PORT:
return port;
case SeBootstrap.Configuration.ROOT_PATH:
return "/";
case SeBootstrap.Configuration.SSL_CLIENT_AUTHENTICATION:
return SSLClientAuthentication.NONE;
case SeBootstrap.Configuration.SSL_CONTEXT:
try {
return SSLContext.getDefault();
} catch (final NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
case ServerProperties.WEBSERVER_AUTO_START:
return TRUE;
default:
return null;
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -64,7 +64,7 @@ public class JdkHttpHandlerContainer implements HttpHandler, Container {
private volatile ApplicationHandler appHandler;

/**
* Create new lightweight Java SE HTTP server container.
* Create new lightweight Java SE HTTP server container.
*
* @param application JAX-RS / Jersey application to be deployed on the container.
*/
Expand All @@ -73,7 +73,16 @@ public class JdkHttpHandlerContainer implements HttpHandler, Container {
}

/**
* Create new lightweight Java SE HTTP server container.
* Create new lightweight Java SE HTTP server container.
*
* @param applicationClass class of JAX-RS / Jersey application to be deployed on the container.
*/
JdkHttpHandlerContainer(final Class<? extends Application> applicationClass) {
this.appHandler = new ApplicationHandler(applicationClass);
}

/**
* Create new lightweight Java SE HTTP server container.
*
* @param application JAX-RS / Jersey application to be deployed on the container.
* @param parentContext DI provider specific context with application's registered bindings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ final class JdkHttpServer implements WebServer {
private final HttpServer httpServer;

JdkHttpServer(final Application application, final JerseySeBootstrapConfiguration configuration) {
this(new JdkHttpHandlerContainer(application), configuration);
}

JdkHttpServer(final Class<? extends Application> applicationClass,
final JerseySeBootstrapConfiguration configuration) {
this(new JdkHttpHandlerContainer(applicationClass), configuration);
}

JdkHttpServer(final JdkHttpHandlerContainer container, final JerseySeBootstrapConfiguration configuration) {
final SeBootstrap.Configuration.SSLClientAuthentication sslClientAuthentication = configuration
.sslClientAuthentication();

this.container = new JdkHttpHandlerContainer(application);
this.container = container;
this.httpServer = JdkHttpServerFactory.createHttpServer(
configuration.uri(false),
this.container,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,18 @@
public final class JdkHttpServerProvider implements WebServerProvider {

@Override
public final <T extends WebServer> T createServer(final Class<T> type, final Application application,
public <T extends WebServer> T createServer(final Class<T> type, final Application application,
final JerseySeBootstrapConfiguration configuration) {
return JdkHttpServer.class == type || WebServer.class == type
? type.cast(new JdkHttpServer(application, configuration))
: null;
}

@Override
public <T extends WebServer> T createServer(final Class<T> type, final Class<? extends Application> applicationClass,
final JerseySeBootstrapConfiguration configuration) {
return JdkHttpServer.class == type || WebServer.class == type
? type.cast(new JdkHttpServer(applicationClass, configuration))
: null;
}
}
Loading

0 comments on commit b9479ae

Please sign in to comment.