diff --git a/examples/microprofile/hello-world-explicit/src/main/java/io/helidon/microprofile/example/helloworld/explicit/Main.java b/examples/microprofile/hello-world-explicit/src/main/java/io/helidon/microprofile/example/helloworld/explicit/Main.java index 7db9c623aec..77e12c9054b 100644 --- a/examples/microprofile/hello-world-explicit/src/main/java/io/helidon/microprofile/example/helloworld/explicit/Main.java +++ b/examples/microprofile/hello-world-explicit/src/main/java/io/helidon/microprofile/example/helloworld/explicit/Main.java @@ -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. @@ -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; @@ -29,8 +24,6 @@ * Explicit example. */ public class Main { - private static final Logger LOGGER = Logger.getLogger("io.helidon.microprofile.startup"); - private Main() { } @@ -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...) diff --git a/examples/microprofile/hello-world-explicit/src/main/resources/logging.properties b/examples/microprofile/hello-world-explicit/src/main/resources/logging.properties index b272eb2eb13..0615cfef72c 100644 --- a/examples/microprofile/hello-world-explicit/src/main/resources/logging.properties +++ b/examples/microprofile/hello-world-explicit/src/main/resources/logging.properties @@ -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. @@ -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 diff --git a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java index c0bcceb6ffc..0441edbaa7d 100644 --- a/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java +++ b/microprofile/cdi/src/main/java/io/helidon/microprofile/cdi/HelidonContainerInitializer.java @@ -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. @@ -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 diff --git a/microprofile/cdi/src/test/java/io/helidon/microprofile/cdi/HelidonContainerInitializerTest.java b/microprofile/cdi/src/test/java/io/helidon/microprofile/cdi/HelidonContainerInitializerTest.java index ec7618722ce..3c111d903ba 100644 --- a/microprofile/cdi/src/test/java/io/helidon/microprofile/cdi/HelidonContainerInitializerTest.java +++ b/microprofile/cdi/src/test/java/io/helidon/microprofile/cdi/HelidonContainerInitializerTest.java @@ -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. @@ -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(); } } diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java index 11b4d97003f..d17f689cda1 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/Server.java @@ -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. @@ -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; @@ -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 diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java index 6e9ca8f86c7..8d3e8a6cfa7 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/ServerCdiExtension.java @@ -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. @@ -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")); @@ -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() @@ -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); } @@ -418,6 +423,14 @@ public void defaultExecutorService(Supplier 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. @@ -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 ("/"). *