Skip to content

Commit

Permalink
Helidon Main class
Browse files Browse the repository at this point in the history
- injection implementation
- CDI implementation
- refactored MP apps to use it
- refactored Níma FT example to use it
  • Loading branch information
tomas-langer committed Jun 29, 2023
1 parent 629819a commit c71fa41
Show file tree
Hide file tree
Showing 42 changed files with 204 additions and 474 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
15 changes: 5 additions & 10 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,16 +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-api</artifactId>
<version>${helidon.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.nima.common</groupId>
<artifactId>helidon-nima-common-tls</artifactId>
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
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
4 changes: 4 additions & 0 deletions helidon/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,9 @@
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.logging</groupId>
<artifactId>helidon-logging-common</artifactId>
</dependency>
</dependencies>
</project>
49 changes: 48 additions & 1 deletion helidon/src/main/java/io/helidon/Main.java
Original file line number Diff line number Diff line change
@@ -1,15 +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 that is discovering services.
* 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 an 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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 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 @@ -15,6 +15,8 @@
*/

/**
* APIs shared across all Helidon features that are injection based.
* Startup of Helidon applications.
*
* @see io.helidon.Main
*/
package io.helidon.nima.common.api;
package io.helidon;
17 changes: 16 additions & 1 deletion helidon/src/main/java/io/helidon/spi/HelidonStartupProvider.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
/*
* 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.spi;

/**
* {@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.
* The default startup will create Helidon Injection service registry, and start all services that should be started.
*/
public interface HelidonStartupProvider {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/

/**
* APIs for Helidon features.
* Extension point to support a single main class ({@link io.helidon.Main}) to start various styles of applications,
* such as Helidon injection based applications, and Helidon MicroProfile applications.
*/
module io.helidon.nima.common.api {
requires static io.helidon.pico.api;

exports io.helidon.nima.common.api;
}
package io.helidon.spi;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 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 @@ -15,7 +15,14 @@
*/

/**
* Main entry point for Níma application.
* Bootstraps required services, which implicitly starts server(s) available.
* Types need to start a Helidon application.
*/
package io.helidon.nima;
module io.helidon {
requires io.helidon.common;
requires io.helidon.logging.common;

exports io.helidon;
exports io.helidon.spi;

uses io.helidon.spi.HelidonStartupProvider;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import io.helidon.common.config.Config;
import io.helidon.common.http.Http;
import io.helidon.config.Config;
import io.helidon.nima.webserver.http.Handler;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;
Expand Down
4 changes: 4 additions & 0 deletions microprofile/cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<description>Helidon extension of Weld to support ahead of time compilation</description>

<dependencies>
<dependency>
<groupId>io.helidon</groupId>
<artifactId>helidon</artifactId>
</dependency>
<dependency>
<!-- the actual CDI implementation - repackaged by us -->
<groupId>io.helidon.microprofile.weld</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.microprofile.cdi;

import io.helidon.common.Weight;
import io.helidon.common.Weighted;
import io.helidon.spi.HelidonStartupProvider;

/**
* {@link java.util.ServiceLoader} implementation of a Helidon startup provider.
*/
@Weight(Weighted.DEFAULT_WEIGHT + 100) // must have higher than default, to start CDI and not Helidon Injection
public class CdiStartupProvider implements HelidonStartupProvider {
/**
* Default constructor required by {@link java.util.ServiceLoader}.
*
* @deprecated please do not use directly
*/
@Deprecated
public CdiStartupProvider() {
}

@Override
public void start(String[] arguments) {
Main.main(arguments);
}
}
4 changes: 3 additions & 1 deletion microprofile/cdi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates.
* Copyright (c) 2019, 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 @@ -37,6 +37,7 @@
// weld requires jakarta.el.ELResolver on module path
requires jakarta.el;

requires io.helidon;
requires io.helidon.common;
requires io.helidon.logging.common;
requires io.helidon.common.features.api;
Expand All @@ -56,6 +57,7 @@

uses jakarta.enterprise.inject.spi.Extension;

provides io.helidon.spi.HelidonStartupProvider with io.helidon.microprofile.cdi.CdiStartupProvider;
provides jakarta.enterprise.inject.se.SeContainerInitializer with io.helidon.microprofile.cdi.HelidonContainerInitializer;
provides jakarta.enterprise.inject.spi.CDIProvider with io.helidon.microprofile.cdi.HelidonCdiProvider;

Expand Down
43 changes: 0 additions & 43 deletions nima/common/api/pom.xml

This file was deleted.

Loading

0 comments on commit c71fa41

Please sign in to comment.