Skip to content

Commit a87c6e3

Browse files
committed
Add detection of request scope for Helidon Nima and Helidon Reactive
1 parent 4facfc8 commit a87c6e3

File tree

1 file changed

+61
-30
lines changed

1 file changed

+61
-30
lines changed

inject-generator/src/main/java/io/avaje/inject/generator/RequestScope.java

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,25 @@ class RequestScope {
1313
private static final String JAVALIN_CONTEXT = "io.javalin.http.Context";
1414
private static final String HELIDON_REQ = "io.helidon.webserver.ServerRequest";
1515
private static final String HELIDON_RES = "io.helidon.webserver.ServerResponse";
16-
private static final Map<String, Handler> TYPES = new HashMap<>();
1716

17+
private static final String NIMA_REQ = "io.helidon.nima.webserver.http.ServerRequest";
18+
private static final String NIMA_RES = "io.helidon.nima.webserver.http.ServerResponse";
19+
private static final String HELIDON_REACTIVE_REQ = "io.helidon.reactive.webserver.ServerRequest";
20+
private static final String HELIDON_REACTIVE_RES = "io.helidon.reactive.webserver.ServerResponse";
21+
22+
private static final Map<String, Handler> TYPES = new HashMap<>();
1823
static {
19-
TYPES.put(JEX_CONTEXT, new Jex());
20-
TYPES.put(JAVALIN_CONTEXT, new Javalin());
21-
TYPES.put(HELIDON_REQ, new Helidon());
22-
TYPES.put(HELIDON_RES, new Helidon());
24+
TYPES.put(JEX_CONTEXT, new JexHandler());
25+
TYPES.put(JAVALIN_CONTEXT, new JavalinHandler());
26+
final var helidon = new Helidon();
27+
TYPES.put(HELIDON_REQ, helidon);
28+
TYPES.put(HELIDON_RES, helidon);
29+
final var helidonReactive = new HelidonReactive();
30+
TYPES.put(HELIDON_REACTIVE_REQ, helidonReactive);
31+
TYPES.put(HELIDON_REACTIVE_RES, helidonReactive);
32+
final var helidonNima = new HelidonNima();
33+
TYPES.put(NIMA_REQ, helidonNima);
34+
TYPES.put(NIMA_RES, helidonNima);
2335
}
2436

2537
/**
@@ -62,37 +74,48 @@ interface Handler {
6274
String argumentName(String paramType);
6375
}
6476

65-
/**
66-
* Jex support for request scoping/BeanFactory.
67-
*/
68-
private static class Jex implements Handler {
6977

70-
@Override
71-
public void factoryInterface(Append writer, String parentType) {
72-
writer.append("BeanFactory<%s, %s>", parentType, "Context");
78+
private static final class JexHandler extends ContextHandler {
79+
private JexHandler() {
80+
super(JEX_CONTEXT);
7381
}
82+
}
7483

75-
@Override
76-
public void addImports(Set<String> importTypes) {
77-
importTypes.add(Constants.BEAN_FACTORY);
78-
importTypes.add(JEX_CONTEXT);
84+
private static final class JavalinHandler extends ContextHandler {
85+
private JavalinHandler() {
86+
super(JAVALIN_CONTEXT);
7987
}
88+
}
8089

81-
@Override
82-
public void writeCreateMethod(Append writer, String parentType) {
83-
writer.append(" public %s create(Context context) {", parentType).eol();
90+
private static final class Helidon extends RequestResponseHandler {
91+
Helidon() {
92+
super(HELIDON_REQ, HELIDON_RES);
8493
}
94+
}
8595

86-
@Override
87-
public String argumentName(String paramType) {
88-
return "context";
96+
private static final class HelidonReactive extends RequestResponseHandler {
97+
HelidonReactive() {
98+
super(HELIDON_REACTIVE_REQ, HELIDON_REACTIVE_RES);
99+
}
100+
}
101+
102+
private static final class HelidonNima extends RequestResponseHandler {
103+
HelidonNima() {
104+
super(NIMA_REQ, NIMA_RES);
89105
}
90106
}
91107

108+
92109
/**
93-
* Javalin support for request scoping/BeanFactory.
110+
* Single Context based handlers.
94111
*/
95-
private static class Javalin implements Handler {
112+
private static abstract class ContextHandler implements Handler {
113+
114+
final String contextType;
115+
116+
private ContextHandler(String contextType) {
117+
this.contextType = contextType;
118+
}
96119

97120
@Override
98121
public void factoryInterface(Append writer, String parentType) {
@@ -102,7 +125,7 @@ public void factoryInterface(Append writer, String parentType) {
102125
@Override
103126
public void addImports(Set<String> importTypes) {
104127
importTypes.add(Constants.BEAN_FACTORY);
105-
importTypes.add(JAVALIN_CONTEXT);
128+
importTypes.add(contextType);
106129
}
107130

108131
@Override
@@ -117,9 +140,17 @@ public String argumentName(String paramType) {
117140
}
118141

119142
/**
120-
* Helidon support for request scoping/BeanFactory.
143+
* ServerRequest ServerResponse based Handlers.
121144
*/
122-
private static class Helidon implements Handler {
145+
private static abstract class RequestResponseHandler implements Handler {
146+
147+
final String reqType;
148+
final String resType;
149+
150+
RequestResponseHandler(String reqType, String resType) {
151+
this.reqType = reqType;
152+
this.resType = resType;
153+
}
123154

124155
@Override
125156
public void factoryInterface(Append writer, String parentType) {
@@ -129,8 +160,8 @@ public void factoryInterface(Append writer, String parentType) {
129160
@Override
130161
public void addImports(Set<String> importTypes) {
131162
importTypes.add(Constants.BEAN_FACTORY2);
132-
importTypes.add(HELIDON_REQ);
133-
importTypes.add(HELIDON_RES);
163+
importTypes.add(reqType);
164+
importTypes.add(resType);
134165
}
135166

136167
@Override
@@ -140,7 +171,7 @@ public void writeCreateMethod(Append writer, String parentType) {
140171

141172
@Override
142173
public String argumentName(String paramType) {
143-
if (paramType.equals(HELIDON_RES)) {
174+
if (paramType.equals(resType)) {
144175
return "response";
145176
} else {
146177
return "request";

0 commit comments

Comments
 (0)