Skip to content

Commit

Permalink
allow static initialization of vxms in AbstractVerticles
Browse files Browse the repository at this point in the history
  • Loading branch information
JacpFX committed Aug 21, 2017
1 parent c2313ec commit e681a3c
Show file tree
Hide file tree
Showing 6 changed files with 1,640 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Arrays;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import org.jacpfx.common.exceptions.EndpointExecutionException;
Expand Down Expand Up @@ -79,7 +80,7 @@ public static Optional<Method> findMethodBySignature(String methodName, Object[]
if (first.isPresent()) {
return first;
} else {
log.info(StringFormatter
log.log(Level.FINE,StringFormatter
.format("method %s was not found in class %s with parameters %s", methodName,
invokeTo.getClass(),
Arrays.toString(Stream.of(parameters).map(p -> p.getClass()).toArray()))
Expand Down
67 changes: 43 additions & 24 deletions vxms-core/src/main/java/org/jacpfx/vertx/services/VxmsEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,26 @@ public final void start(final Future<Void> startFuture) {
// register info (keepAlive) handler
vertx.eventBus()
.consumer(ConfigurationUtil.getServiceName(getConfig(), this.getClass()) + "-info",
this::info);
VxmsEndpoint::info);
initEndpoint(startFuture, this, new VxmsShared(vertx, new LocalData(vertx)));

}

/**
* Initialize an existing Vert.x instance as a Vxms endpoint so you can use an Verticle (Extending
* AbstractVerticle) as a fully functional Vxms Endpoint). Caution: this start methods completes
* the start future when it's ready, so it must be the last initialisation (order & callback
* the start future when it's ready, so it must be the last initialisation (order and callback
* wise)
* @param startFuture the Start Future from Vert.x
* @param registrationObject the AbstractVerticle to register (mostly this)
*/
public void start(final Future<Void> startFuture, AbstractVerticle registrationObject) {
// initEndpoint(startFuture,registrationObject);
public static void start(final Future<Void> startFuture, AbstractVerticle registrationObject) {
final Vertx vertx = registrationObject.getVertx();
final JsonObject config = vertx.getOrCreateContext().config();
vertx.eventBus()
.consumer(ConfigurationUtil.getServiceName(config, registrationObject.getClass()) + "-info",
VxmsEndpoint::info);
initEndpoint(startFuture, registrationObject, new VxmsShared(vertx, new LocalData(vertx)));
}


Expand All @@ -77,15 +84,17 @@ public void start(final Future<Void> startFuture, AbstractVerticle registrationO
*
* @param startFuture, the Vertx start feature
*/
private void initEndpoint(final Future<Void> startFuture, AbstractVerticle registrationObject,
private static void initEndpoint(final Future<Void> startFuture, AbstractVerticle registrationObject,
VxmsShared vxmsShared) {
final Vertx vertx = vxmsShared.getVertx();
final JsonObject config = vertx.getOrCreateContext().config();
final Class<? extends AbstractVerticle> serviceClass = registrationObject.getClass();
final int port = ConfigurationUtil.getEndpointPort(getConfig(), serviceClass);
final String host = ConfigurationUtil.getEndpointHost(getConfig(), serviceClass);
final String contexRoot = ConfigurationUtil.getContextRoot(getConfig(), serviceClass);
final int port = ConfigurationUtil.getEndpointPort(config, serviceClass);
final String host = ConfigurationUtil.getEndpointHost(config, serviceClass);
final String contexRoot = ConfigurationUtil.getContextRoot(config, serviceClass);
final CustomServerOptions endpointConfig = ConfigurationUtil.getEndpointOptions(serviceClass);
final HttpServerOptions options = endpointConfig.getServerOptions(registrationObject.config());
final Vertx vertx = vxmsShared.getVertx();

final HttpServer server = vertx.createHttpServer(options.setHost(host).setPort(port));

final boolean secure = options.isSsl();
Expand All @@ -97,7 +106,7 @@ private void initEndpoint(final Future<Void> startFuture, AbstractVerticle regis
final EndpointConfiguration endpointConfiguration = getEndpointConfiguration(
registrationObject);

getConfig().put("secure", secure);
config.put("secure", secure);

initEndoitConfiguration(endpointConfiguration, vertx, router, secure, host, port);

Expand All @@ -120,15 +129,15 @@ private void initEndpoint(final Future<Void> startFuture, AbstractVerticle regis

}

private void initNoHTTPEndpoint(AbstractVerticle registrationObject, Future<Void> startFuture,
private static void initNoHTTPEndpoint(AbstractVerticle registrationObject, Future<Void> startFuture,
Router topRouter) {
startFuture.setHandler(result -> logStartfuture(startFuture));
executePostConstruct(registrationObject, topRouter, startFuture);
}

private void initHandlerSPIs(HttpServer server, Router router,
private static void initHandlerSPIs(HttpServer server, Router router,
AbstractVerticle registrationObject, VxmsShared vxmsShared) {
initWebSocketExtensions(server, registrationObject);
initWebSocketExtensions(server, registrationObject,vxmsShared);
initRESTExtensions(router, registrationObject, vxmsShared);
initEventBusExtensions(registrationObject, vxmsShared);
}
Expand All @@ -142,7 +151,8 @@ private void initHandlerSPIs(HttpServer server, Router router,
* @param server the vertx server
* @param topRouter the router object
*/
private void initHTTPEndpoint(AbstractVerticle registrationObject, Future<Void> startFuture,
private static void initHTTPEndpoint(AbstractVerticle registrationObject,
Future<Void> startFuture,
int port, String host, HttpServer server,
Router topRouter) {
startFuture.setHandler(result -> logStartfuture(startFuture));
Expand All @@ -156,14 +166,14 @@ private void initHTTPEndpoint(AbstractVerticle registrationObject, Future<Void>
});
}

private void logStartfuture(Future<Void> startFuture) {
private static void logStartfuture(Future<Void> startFuture) {
final Throwable cause = startFuture.cause();
String causeMessage = cause != null ? cause.getMessage() : "";
log("startFuture.isComplete(): " + startFuture.isComplete() + " startFuture.failed(): "
+ startFuture.failed() + " message:" + causeMessage);
}

private void initRESTExtensions(Router router, AbstractVerticle registrationObject,
private static void initRESTExtensions(Router router, AbstractVerticle registrationObject,
VxmsShared vxmsShared) {
// check for REST extension
Optional.
Expand All @@ -172,20 +182,24 @@ private void initRESTExtensions(Router router, AbstractVerticle registrationObje
.initRESTHandler(vxmsShared, router, registrationObject));
}

private void initEventBusExtensions(AbstractVerticle registrationObject, VxmsShared vxmsShared) {
private static void initEventBusExtensions(AbstractVerticle registrationObject,
VxmsShared vxmsShared) {
// check for REST extension
Optional.
ofNullable(getEventBusSPI()).
ifPresent(eventbusHandlerSPI -> eventbusHandlerSPI
.initEventHandler(vxmsShared, registrationObject));
}

private void initWebSocketExtensions(HttpServer server, AbstractVerticle registrationObject) {
private static void initWebSocketExtensions(HttpServer server,
AbstractVerticle registrationObject, VxmsShared vxmsShared) {
// check for websocket extension
final Vertx vertx = vxmsShared.getVertx();
final JsonObject config = vertx.getOrCreateContext().config();
Optional.
ofNullable(getWebSocketSPI()).
ifPresent(webSockethandlerSPI -> webSockethandlerSPI
.registerWebSocketHandler(server, vertx, getConfig(), registrationObject));
.registerWebSocketHandler(server, vertx, config, registrationObject));
}


Expand Down Expand Up @@ -224,7 +238,10 @@ private static void executePostConstruct(AbstractVerticle registrationObject, Ro
registrationObject, new Object[]{startFuture}, startFuture));
final Optional<ReflectionExecutionWrapper> methodWrapperToInvoke = reflectionExecutionWrapperStream
.filter(ReflectionExecutionWrapper::isPresent).findFirst();
methodWrapperToInvoke.ifPresent(wrapper -> wrapper.invoke());
methodWrapperToInvoke.ifPresent(ReflectionExecutionWrapper::invoke);
if(!methodWrapperToInvoke.isPresent() && !startFuture.isComplete()) {
startFuture.complete();
}

}

Expand All @@ -248,7 +265,8 @@ public void postConstruct(final Future<Void> startFuture) {
}


private void initEndoitConfiguration(EndpointConfiguration endpointConfiguration, Vertx vertx,
private static void initEndoitConfiguration(EndpointConfiguration endpointConfiguration,
Vertx vertx,
Router router, boolean secure, String host, int port) {
Optional.of(endpointConfiguration).ifPresent(endpointConfig -> {

Expand All @@ -264,18 +282,19 @@ private void initEndoitConfiguration(EndpointConfiguration endpointConfiguration
});
}

private void postEndoitConfiguration(EndpointConfiguration endpointConfiguration, Router router) {
private static void postEndoitConfiguration(EndpointConfiguration endpointConfiguration,
Router router) {
Optional.of(endpointConfiguration)
.ifPresent(endpointConfig -> endpointConfig.staticHandler(router));
}


private void log(final String value) {
private static void log(final String value) {
log.info(value);
}


private void info(Message m) {
private static void info(Message m) {
// TODO create info message about service
}

Expand Down
Loading

0 comments on commit e681a3c

Please sign in to comment.