Skip to content

Commit bcaaac6

Browse files
committed
Refactor rename generator modules
1 parent bfeb263 commit bcaaac6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+712
-18
lines changed

example-helidon/pom.xml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<parent>
7+
<artifactId>web-reactor</artifactId>
8+
<groupId>io.dinject</groupId>
9+
<version>1.0</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
14+
<artifactId>example-helidon</artifactId>
15+
16+
<properties>
17+
<mainClass>org.example.Main</mainClass>
18+
<helidon-version>2.0.1</helidon-version>
19+
<dinject-version>1.18</dinject-version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>io.dinject</groupId>
26+
<artifactId>dinject</artifactId>
27+
<version>${dinject-version}</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>io.dinject</groupId>
32+
<artifactId>dinject-controller</artifactId>
33+
<version>${dinject-version}</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>io.dinject</groupId>
38+
<artifactId>dinject-generator</artifactId>
39+
<version>${dinject-version}</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>io.dinject</groupId>
45+
<artifactId>helidon-generator</artifactId>
46+
<version>1.19-SNAPSHOT</version>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>io.helidon.webserver</groupId>
52+
<artifactId>helidon-webserver</artifactId>
53+
<version>${helidon-version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>io.helidon.health</groupId>
57+
<artifactId>helidon-health</artifactId>
58+
<version>${helidon-version}</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>io.helidon.health</groupId>
62+
<artifactId>helidon-health-checks</artifactId>
63+
<version>${helidon-version}</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>io.helidon.metrics</groupId>
67+
<artifactId>helidon-metrics</artifactId>
68+
<version>${helidon-version}</version>
69+
</dependency>
70+
<!-- <dependency>-->
71+
<!-- <groupId>io.helidon.media</groupId>-->
72+
<!-- <artifactId>helidon-media-jsonp</artifactId>-->
73+
<!-- <version>${helidon-version}</version>-->
74+
<!-- </dependency>-->
75+
<dependency>
76+
<groupId>io.helidon.media</groupId>
77+
<artifactId>helidon-media-jackson</artifactId>
78+
<version>${helidon-version}</version>
79+
</dependency>
80+
<dependency>
81+
<groupId>io.helidon.config</groupId>
82+
<artifactId>helidon-config-yaml</artifactId>
83+
<version>${helidon-version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.junit.jupiter</groupId>
87+
<artifactId>junit-jupiter-api</artifactId>
88+
<version>5.5.2</version>
89+
<scope>test</scope>
90+
</dependency>
91+
<dependency>
92+
<groupId>io.helidon.webclient</groupId>
93+
<artifactId>helidon-webclient</artifactId>
94+
<version>${helidon-version}</version>
95+
<scope>test</scope>
96+
</dependency>
97+
</dependencies>
98+
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<version>3.8.1</version>
105+
<configuration>
106+
<release>11</release>
107+
</configuration>
108+
</plugin>
109+
</plugins>
110+
</build>
111+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.example;
2+
3+
import io.dinject.controller.Controller;
4+
import io.dinject.controller.Get;
5+
import io.dinject.controller.Path;
6+
import io.dinject.controller.Produces;
7+
8+
@Controller
9+
@Path("/foo")
10+
public class FooController {
11+
12+
@Produces("text/plain")
13+
@Get
14+
public String hello() {
15+
return "Hello from Foo";
16+
}
17+
18+
@Get("{name}")
19+
public Foo getOne(String name) {
20+
Foo foo = new Foo();
21+
foo.name = name;
22+
foo.age = 42;
23+
return foo;
24+
}
25+
26+
public static class Foo {
27+
public String name;
28+
public int age;
29+
}
30+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
2+
package org.example;
3+
4+
import io.helidon.common.http.Http;
5+
import io.helidon.webserver.Routing;
6+
import io.helidon.webserver.ServerRequest;
7+
import io.helidon.webserver.ServerResponse;
8+
import io.helidon.webserver.Service;
9+
10+
import javax.json.Json;
11+
import javax.json.JsonBuilderFactory;
12+
import javax.json.JsonException;
13+
import javax.json.JsonObject;
14+
import java.util.Collections;
15+
import java.util.concurrent.atomic.AtomicReference;
16+
import java.util.logging.Level;
17+
import java.util.logging.Logger;
18+
19+
/**
20+
* A simple service to greet you. Examples:
21+
* <p>
22+
* Get default greeting message:
23+
* curl -X GET http://localhost:8080/greet
24+
* <p>
25+
* Get greeting message for Joe:
26+
* curl -X GET http://localhost:8080/greet/Joe
27+
* <p>
28+
* Change greeting
29+
* curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Howdy"}' http://localhost:8080/greet/greeting
30+
* <p>
31+
* The message is returned as a JSON object
32+
*/
33+
34+
public class GreetService implements Service {
35+
36+
/**
37+
* The config value for the key {@code greeting}.
38+
*/
39+
private final AtomicReference<String> greeting = new AtomicReference<>();
40+
41+
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap());
42+
43+
private static final Logger LOGGER = Logger.getLogger(GreetService.class.getName());
44+
45+
GreetService() {
46+
greeting.set("hello");
47+
}
48+
49+
/**
50+
* A service registers itself by updating the routing rules.
51+
*
52+
* @param rules the routing rules.
53+
*/
54+
@Override
55+
public void update(Routing.Rules rules) {
56+
rules
57+
.get("/", this::getDefaultMessageHandler)
58+
.get("/{name}", this::getMessageHandler)
59+
.put("/greeting", this::updateGreetingHandler);
60+
}
61+
62+
/**
63+
* Return a worldly greeting message.
64+
*
65+
* @param request the server request
66+
* @param response the server response
67+
*/
68+
private void getDefaultMessageHandler(ServerRequest request, ServerResponse response) {
69+
sendResponse(response, "World");
70+
}
71+
72+
/**
73+
* Return a greeting message using the name that was provided.
74+
*
75+
* @param request the server request
76+
* @param response the server response
77+
*/
78+
private void getMessageHandler(ServerRequest request, ServerResponse response) {
79+
String name = request.path().param("name");
80+
sendResponse(response, name);
81+
}
82+
83+
private void sendResponse(ServerResponse response, String name) {
84+
String msg = String.format("%s %s!", greeting.get(), name);
85+
86+
JsonObject returnObject = JSON.createObjectBuilder()
87+
.add("message", msg)
88+
.build();
89+
response.send(returnObject);
90+
}
91+
92+
private static <T> T processErrors(Throwable ex, ServerRequest request, ServerResponse response) {
93+
94+
if (ex.getCause() instanceof JsonException) {
95+
96+
LOGGER.log(Level.FINE, "Invalid JSON", ex);
97+
JsonObject jsonErrorObject = JSON.createObjectBuilder()
98+
.add("error", "Invalid JSON")
99+
.build();
100+
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject);
101+
} else {
102+
103+
LOGGER.log(Level.FINE, "Internal error", ex);
104+
JsonObject jsonErrorObject = JSON.createObjectBuilder()
105+
.add("error", "Internal error")
106+
.build();
107+
response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(jsonErrorObject);
108+
}
109+
110+
return null;
111+
}
112+
113+
private void updateGreetingFromJson(JsonObject jo, ServerResponse response) {
114+
if (!jo.containsKey("greeting")) {
115+
JsonObject jsonErrorObject = JSON.createObjectBuilder()
116+
.add("error", "No greeting provided")
117+
.build();
118+
response.status(Http.Status.BAD_REQUEST_400)
119+
.send(jsonErrorObject);
120+
return;
121+
}
122+
123+
greeting.set(jo.getString("greeting"));
124+
response.status(Http.Status.NO_CONTENT_204).send();
125+
}
126+
127+
/**
128+
* Set the greeting to use in future messages.
129+
*
130+
* @param request the server request
131+
* @param response the server response
132+
*/
133+
private void updateGreetingHandler(ServerRequest request,
134+
ServerResponse response) {
135+
request.content().as(JsonObject.class)
136+
.thenAccept(jo -> updateGreetingFromJson(jo, response))
137+
.exceptionally(ex -> processErrors(ex, request, response));
138+
}
139+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.example;
2+
3+
import io.dinject.SystemContext;
4+
import io.helidon.health.HealthSupport;
5+
import io.helidon.health.checks.HealthChecks;
6+
import io.helidon.media.jackson.JacksonSupport;
7+
import io.helidon.metrics.MetricsSupport;
8+
import io.helidon.webserver.Routing;
9+
import io.helidon.webserver.Service;
10+
import io.helidon.webserver.WebServer;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
public class Main {
16+
17+
public static void main(String[] args) throws IOException {
18+
startServer();
19+
}
20+
21+
static WebServer startServer() throws IOException {
22+
23+
//setupLogging();
24+
25+
// By default this will pick up application.yaml from the classpath
26+
//Config config = Config.create();
27+
28+
JacksonSupport jacksonSupport = JacksonSupport.create();
29+
30+
31+
// Build server with JSONP support
32+
WebServer server = WebServer.builder(createRouting())
33+
//.config(config.get("server"))
34+
// .addMediaSupport(JsonpSupport.create())
35+
.addMediaSupport(jacksonSupport)
36+
.port(8083)
37+
.build();
38+
39+
// Try to start the server. If successful, print some info and arrange to
40+
// print a message at shutdown. If unsuccessful, print the exception.
41+
server.start()
42+
.thenAccept(ws -> {
43+
System.out.println(
44+
"WEB server is up! http://localhost:" + ws.port() + "/greet");
45+
ws.whenShutdown().thenRun(()
46+
-> System.out.println("WEB server is DOWN. Good bye!"));
47+
})
48+
.exceptionally(t -> {
49+
System.err.println("Startup failed: " + t.getMessage());
50+
t.printStackTrace(System.err);
51+
return null;
52+
});
53+
54+
// Server threads are not daemon. No need to block. Just react.
55+
56+
return server;
57+
}
58+
59+
private static Routing createRouting() {
60+
61+
MetricsSupport metrics = MetricsSupport.create();
62+
HealthSupport health = HealthSupport.builder()
63+
.addLiveness(HealthChecks.healthChecks()) // Adds a convenient set of checks
64+
.build();
65+
66+
67+
final Routing.Builder builder = Routing.builder()
68+
.register(health) // Health at "/health"
69+
.register(metrics) // Metrics at "/metrics"
70+
.register("/greet", new GreetService());
71+
72+
final List<Service> services = SystemContext.getBeans(Service.class);
73+
services.forEach(builder::register);
74+
75+
return builder.build();
76+
}
77+
78+
// /**
79+
// * Configure logging from logging.properties file.
80+
// */
81+
// private static void setupLogging() throws IOException {
82+
// try (InputStream is = Main.class.getResourceAsStream("/logging.properties")) {
83+
// LogManager.getLogManager().readConfiguration(is);
84+
// }
85+
// }
86+
}
File renamed without changes.

common/src/main/java/io/dinject/webroutegen/BaseControllerWriter.java renamed to generator-core/src/main/java/io/dinject/webroutegen/BaseControllerWriter.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ protected void writePackage() {
5151
}
5252

5353
protected void writeImports() {
54-
5554
writer.append(Constants.IMPORT_PATH_TYPE_CONVERT).eol();
5655
for (String type : reader.getStaticImportTypes()) {
5756
writer.append("import static %s;", type).eol();

common/src/main/java/io/dinject/webroutegen/ControllerReader.java renamed to generator-core/src/main/java/io/dinject/webroutegen/ControllerReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ TypeElement getBeanType() {
149149
return beanType;
150150
}
151151

152-
boolean isDocHidden() {
152+
public boolean isDocHidden() {
153153
return docHidden;
154154
}
155155

0 commit comments

Comments
 (0)