Skip to content

Commit

Permalink
4.x: Helidon main class (#7136)
Browse files Browse the repository at this point in the history
* Helidon main class.
- injection implementation
- CDI implementation
- refactored MP apps to use it
- refactored Níma FT example to use it
  • Loading branch information
tomas-langer authored Jun 30, 2023
1 parent 39bd2aa commit a20a7d0
Show file tree
Hide file tree
Showing 60 changed files with 719 additions and 264 deletions.
2 changes: 1 addition & 1 deletion applications/mp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<version.plugin.jaxb2>0.14.0</version.plugin.jaxb2>
<version.plugin.eclipselink>2.7.5.1</version.plugin.eclipselink>
<version.plugin.hibernate.enhance>6.1.7.Final</version.plugin.hibernate.enhance>
<mainClass>io.helidon.microprofile.cdi.Main</mainClass>
<mainClass>io.helidon.Main</mainClass>
</properties>

<build>
Expand Down
10 changes: 5 additions & 5 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.helidon</groupId>
<artifactId>helidon</artifactId>
<version>${helidon.version}</version>
</dependency>
<!-- webserver -->
<dependency>
<groupId>io.helidon.reactive.webserver</groupId>
Expand Down Expand Up @@ -1175,11 +1180,6 @@
</dependency>

<!-- Níma -->
<dependency>
<groupId>io.helidon.nima</groupId>
<artifactId>helidon-nima</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.nima.common</groupId>
<artifactId>helidon-nima-common-tls</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
* Copyright (c) 2017, 2023 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 @@ -81,6 +81,7 @@ private ConfigMappers() {
private static Map<Class<?>, Function<Config, ?>> initEssentialMappers() {

return Map.of(Config.class, (node) -> node,
io.helidon.common.config.Config.class, (node) -> node,
String.class, wrap(value -> value),
OptionalInt.class, ConfigMappers::optionalIntEssential,
OptionalLong.class, ConfigMappers::optionalLongEssential,
Expand Down
6 changes: 3 additions & 3 deletions examples/nima/fault-tolerance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
<name>Helidon Examples Níma Fault Tolerance</name>

<properties>
<mainClass>io.helidon.examples.nima.faulttolerance.FtMain</mainClass>
<mainClass>io.helidon.Main</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.nima</groupId>
<artifactId>helidon-nima</artifactId>
<groupId>io.helidon</groupId>
<artifactId>helidon</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webserver</groupId>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ String greet() {
String greetNamed(@Endpoint.PathParam("name") String name,
@Endpoint.QueryParam(value = "throw", defaultValue = "false") String shouldThrow,
@Endpoint.HeaderParam(Http.Header.HOST_STRING) String hostHeader) {
System.out.println(Thread.currentThread().getName());
if ("true".equalsIgnoreCase(shouldThrow)) {
throw new PicoException("Failed on purpose");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ fault-tolerance:
myname:
executor-name: "platform-executor"
other-name:
executor-name: "abcd"
executor-name: "abcd"
pico:
permits-dynamic: true
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.examples.nima.faulttolerance;

import io.helidon.Main;
import io.helidon.common.http.Http;
import io.helidon.nima.webclient.WebClient;
import io.helidon.nima.webclient.http1.Http1Client;
Expand All @@ -39,7 +40,7 @@ class FaultToleranceTest {

@BeforeAll
static void init() {
FtMain.main(new String[0]);
Main.main(new String[0]);
WebServer webServer = PicoServices.realizedServices()
.lookup(WebServer.class)
.get();
Expand All @@ -59,8 +60,8 @@ static void init() {

@AfterAll
static void shutDown() {
// PicoServices.picoServices()
// .map(PicoServices::shutdown);
PicoServices.picoServices()
.map(PicoServices::shutdown);
}

@Test
Expand Down Expand Up @@ -95,7 +96,6 @@ void testGreetNamed() {
}

@Test
@Disabled
void testGreetNamedFallback() {
String response = webClient.get("/greet/helidon")
.queryParam("throw", "true")
Expand Down
16 changes: 0 additions & 16 deletions examples/nima/imperative/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,10 @@
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.nima</groupId>
<artifactId>helidon-nima</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.nima.webserver</groupId>
<artifactId>helidon-nima-webserver</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.pico</groupId>
<artifactId>helidon-pico-api</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.pico</groupId>
<artifactId>helidon-pico-runtime</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.pico.configdriven</groupId>
<artifactId>helidon-pico-configdriven-runtime</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-yaml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package io.helidon.examples.nima.imperative;

import io.helidon.common.config.Config;
import io.helidon.common.config.GlobalConfig;
import io.helidon.common.http.Http;
import io.helidon.nima.Nima;
import io.helidon.nima.webserver.WebServer;
import io.helidon.nima.webserver.http.HttpRouting;

Expand All @@ -35,7 +35,7 @@ private ImperativeMain() {
* @param args ignored
*/
public static void main(String[] args) {
Config config = Nima.config();
Config config = GlobalConfig.config();

WebServer server = WebServer.create(ws -> ws.config(config.get("server"))
.routing(ImperativeMain::routing))
Expand Down
26 changes: 12 additions & 14 deletions nima/nima/pom.xml → helidon/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022, 2023 Oracle and/or its affiliates.
Copyright (c) 2023 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 @@ -14,32 +13,31 @@
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.
-->

-->

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.nima</groupId>
<artifactId>helidon-nima-project</artifactId>
<groupId>io.helidon</groupId>
<artifactId>helidon-project</artifactId>
<version>4.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>helidon-nima</artifactId>
<name>Helidon Níma</name>
<artifactId>helidon</artifactId>
<name>Helidon</name>

<description>Helidon main entry point</description>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-common</artifactId>
</dependency>
</dependencies>
</project>
62 changes: 62 additions & 0 deletions helidon/src/main/java/io/helidon/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2023 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.
* 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 io.helidon;

import java.util.ServiceLoader;

import io.helidon.common.HelidonServiceLoader;
import io.helidon.logging.common.LogConfig;
import io.helidon.spi.HelidonStartupProvider;

/**
* Main entry point for any Helidon application.
* {@link java.util.ServiceLoader} is used to discover the correct {@link io.helidon.spi.HelidonStartupProvider}
* to start the application (probably either Helidon Injection based application, or a CDI based application).
* <p>
* The default option is to start Helidon injection based application.
*/
public class Main {
static {
LogConfig.initClass();
}

private Main() {
}

/**
* Start Helidon.
* This method is required to start directly from a command line.
*
* @param args arguments of the application
*/
public static void main(String[] args) {
// we always initialize logging
LogConfig.configureRuntime();

// this should only be called once in a lifetime of the server, so no need to optimize
var services = HelidonServiceLoader.create(ServiceLoader.load(HelidonStartupProvider.class))
.asList();

if (services.isEmpty()) {
throw new IllegalStateException("Helidon Main class can only be called if a startup provider is available. "
+ "Please use either Helidon Injection, or Helidon MicroProfile "
+ "(or a custom extension). If neither is available, you should use "
+ "your own Main class.");
}
services.get(0).start(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
module io.helidon.nima {
requires io.helidon.common;
requires io.helidon.config;
requires io.helidon.logging.common;

exports io.helidon.nima;
}
/**
* Startup of Helidon applications.
*
* @see io.helidon.Main
*/
package io.helidon;
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,17 @@
* limitations under the License.
*/

package io.helidon.examples.nima.faulttolerance;

import io.helidon.config.Config;

import jakarta.inject.Provider;
import jakarta.inject.Singleton;
package io.helidon.spi;

/**
* This service will be part of Níma on Pico module.
* It may use pico to get config sources exposed through pico.
* {@link java.util.ServiceLoader} provider interface to discover the correct startup type.
* Only the first provider (with the highest {@link io.helidon.common.Weight}) will be used.
*/
@Singleton
class ConfigService implements Provider<Config> {
private static volatile Config config;
static void config(Config config) {
ConfigService.config = config;
}

@Override
public Config get() {
return config;
}
public interface HelidonStartupProvider {
/**
* Start the runtime.
*
* @param arguments command line arguments
*/
void start(String[] arguments);
}
Loading

0 comments on commit a20a7d0

Please sign in to comment.