Skip to content

Commit

Permalink
Server now properly fails when CDI is started externally.
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-langer committed Feb 7, 2020
1 parent 4fecc31 commit 22f9d6a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,6 @@

package io.helidon.microprofile.example.helloworld.explicit;

import java.util.logging.Logger;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.se.SeContainerInitializer;

import io.helidon.microprofile.server.Server;

import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
Expand All @@ -29,8 +24,6 @@
* Explicit example.
*/
public class Main {
private static final Logger LOGGER = Logger.getLogger("io.helidon.microprofile.startup");

private Main() {
}

Expand All @@ -40,12 +33,6 @@ private Main() {
* @param args command line arguments (ignored)
*/
public static void main(String[] args) {
LOGGER.finest("Main method");
SeContainerInitializer initializer = SeContainerInitializer.newInstance();
LOGGER.finest("Weld instance");
SeContainer cdiContainer = initializer.initialize();
LOGGER.finest("Weld initialized");

Server server = Server.builder()
.addApplication(HelloWorldApplication.class)
// using a customized helidon config instance (in this case the default...)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2018, 2020 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,10 @@
#

handlers=io.helidon.common.HelidonConsoleHandler
java.util.logging.SimpleFormatter.format=[%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS.%1$tL] %4$s %3$s : %5$s%6$s%n

io.helidon.common.HelidonConsoleHandler.level=ALL
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$s %3$s !thread!: %5$s%6$s%n

#All log level details
.level=INFO
# Microprofile config
# io.helidon.microprofile.config.level = FINEST
# Microprofile server startup
io.helidon.microprofile.startup.level=FINEST
org.glassfish.jersey.internal.Errors.level=SEVERE
.level=WARNING
io.helidon.level=INFO
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,7 @@ public HelidonContainerInitializer() {
LOGGER.warning("You are using SeContainerInitializer. This application will not work with GraalVM native-image."
+ " You can disable this warning by configuring " + CONFIG_INITIALIZER_NO_WARN + "=true.");
}
ContainerInstanceHolder.set(container);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,5 +42,6 @@ void testSeInitializerFails() {
ConfigProviderResolver.instance().registerConfig((org.eclipse.microprofile.config.Config) config, Thread.currentThread().getContextClassLoader());
SeContainerInitializer seContainerInitializer = SeContainerInitializer.newInstance();
assertThat(seContainerInitializer, instanceOf(HelidonContainerInitializer.class));
seContainerInitializer.initialize().close();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
import java.util.function.Supplier;
import java.util.logging.Logger;

import javax.enterprise.inject.se.SeContainer;
import javax.enterprise.inject.spi.CDI;
import javax.ws.rs.core.Application;

Expand Down Expand Up @@ -159,6 +160,28 @@ private Builder() {
* @throws MpException in case the server fails to be created
*/
public Server build() {
// make sure server is not already running
try {
ServerCdiExtension server = CDI.current()
.getBeanManager()
.getExtension(ServerCdiExtension.class);

if (server.started()) {
SeContainer container = (SeContainer) CDI.current();
container.close();
throw new MpException("Server is already started. Maybe you have initialized CDI yourself? "
+ "If you do so, then you cannot use Server.builder() to set up your server. "
+ "Config is initialized with defaults or using "
+ "meta-config.yaml; applications are discovered using CDI. "
+ "To use custom configuration, you can use "
+ "ConfigProviderResolver.instance().registerConfig(config, "
+ "classLoader);");
}
} catch (IllegalStateException ignored) {
// container is not running - server cannot be started in such a case
// ignore this
}

// we may have shutdown the original instance, this is to ensure we use the current CDI.
HelidonContainer instance = HelidonContainer.instance();
// now run the build within context already
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -85,8 +85,11 @@ public class ServerCdiExtension implements Extension {

// runtime
private WebServer webserver;
private int port;
private String listenHost = "0.0.0.0";

// these fields may be accessed from different threads than created on
private volatile int port;
private volatile String listenHost = "0.0.0.0";
private volatile boolean started;

private void prepareRuntime(@Observes @RuntimeStart Config config) {
serverConfigBuilder.config(config.get("server"));
Expand All @@ -95,6 +98,7 @@ private void prepareRuntime(@Observes @RuntimeStart Config config) {

private void startServer(@Observes @Priority(PLATFORM_AFTER + 100) @Initialized(ApplicationScoped.class) Object event,
BeanManager beanManager) {

// make sure all configuration is in place
if (null == jaxRsExecutorService) {
jaxRsExecutorService = ServerThreadPoolSupplier.builder()
Expand Down Expand Up @@ -126,6 +130,7 @@ private void startServer(@Observes @Priority(PLATFORM_AFTER + 100) @Initialized(

try {
webserver.start().toCompletableFuture().get();
started = true;
} catch (Exception e) {
throw new DeploymentException("Failed to start webserver", e);
}
Expand Down Expand Up @@ -418,6 +423,14 @@ public void defaultExecutorService(Supplier<? extends ExecutorService> defaultEx
this.jaxRsExecutorService = defaultExecutorService;
}

/**
* Current host the server is running on.
* @return host of this server
*/
public String host() {
return listenHost;
}

/**
* Current port the server is running on. This information is only available after the
* server is actually started.
Expand All @@ -428,6 +441,15 @@ public int port() {
return port;
}

/**
* State of the server.
*
* @return {@code true} if the server is already started, {@code false} otherwise
*/
public boolean started() {
return started;
}

/**
* Base path of this server. This is used to redirect when a request is made for root ("/").
*
Expand Down

0 comments on commit 22f9d6a

Please sign in to comment.