Skip to content

Commit 39b5141

Browse files
committed
Integrate registry with Guice + bug fixes from env refactor
1 parent 0d19b94 commit 39b5141

File tree

5 files changed

+115
-19
lines changed

5 files changed

+115
-19
lines changed

jooby/pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
<dependency>
173173
<groupId>com.google.code.findbugs</groupId>
174174
<artifactId>jsr305</artifactId>
175-
<scope>provided</scope>
176175
</dependency>
177176

178177
<!-- Logging System -->
@@ -210,7 +209,6 @@
210209
<dependency>
211210
<groupId>javax.inject</groupId>
212211
<artifactId>javax.inject</artifactId>
213-
<version>1</version>
214212
</dependency>
215213

216214
<!-- netty-buffer -->
@@ -252,6 +250,12 @@
252250
<optional>true</optional>
253251
</dependency>
254252

253+
<!-- Guice -->
254+
<dependency>
255+
<groupId>com.google.inject</groupId>
256+
<artifactId>guice</artifactId>
257+
<optional>true</optional>
258+
</dependency>
255259

256260
<!-- Test dependencies -->
257261
<dependency>

jooby/src/main/java/io/jooby/Jooby.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.jooby;
1717

18+
import io.jooby.internal.RegistryImpl;
1819
import io.jooby.internal.RouteAnalyzer;
1920
import io.jooby.internal.RouterImpl;
2021
import org.slf4j.Logger;
@@ -26,7 +27,6 @@
2627
import java.nio.file.Path;
2728
import java.nio.file.Paths;
2829
import java.util.ArrayList;
29-
import java.util.Arrays;
3030
import java.util.Iterator;
3131
import java.util.LinkedList;
3232
import java.util.List;
@@ -62,7 +62,7 @@ public class Jooby implements Router, Registry {
6262

6363
private LinkedList<Throwing.Runnable> stopCallbacks;
6464

65-
private Env environment;
65+
private Env env;
6666

6767
private Registry registry;
6868

@@ -71,14 +71,11 @@ public Jooby() {
7171
}
7272

7373
public @Nonnull Env environment() {
74-
return environment;
74+
return env;
7575
}
7676

7777
public @Nonnull Jooby environment(@Nonnull Env environment) {
78-
this.environment = environment;
79-
if (tmpdir == null) {
80-
tmpdir = Paths.get(environment.get("application.tmpdir").value()).toAbsolutePath();
81-
}
78+
this.env = environment;
8279
return this;
8380
}
8481

@@ -285,8 +282,8 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
285282
return require(new AttributeKey<>(type));
286283
}
287284

288-
public @Nonnull Jooby registry(@Nonnull Registry registry) {
289-
this.registry = registry;
285+
public @Nonnull Jooby registry(@Nonnull Object registry) {
286+
this.registry = RegistryImpl.wrap(getClass().getClassLoader(), registry);
290287
return this;
291288
}
292289

@@ -326,20 +323,21 @@ private <T> T require(AttributeKey<T> key) {
326323
if (serverConfigurer != null) {
327324
serverConfigurer.accept(server);
328325
}
329-
if (environment == null) {
330-
environment = Env.defaultEnvironment(getClass().getClassLoader());
331-
}
332-
if (tmpdir == null) {
333-
tmpdir = Paths.get(environment.get("application.tmpdir").value()).toAbsolutePath();
334-
}
335326
return server.start(this);
336327
}
337328

338329
public @Nonnull Jooby start(@Nonnull Server server) {
330+
if (env == null) {
331+
env = Env.defaultEnvironment(getClass().getClassLoader());
332+
}
333+
if (tmpdir == null) {
334+
tmpdir = Paths.get(env.get("application.tmpdir").value()).toAbsolutePath();
335+
}
336+
339337
/** Start router: */
340338
ensureTmpdir(tmpdir);
341339

342-
log().debug("environment:\n{}", environment);
340+
log().debug("environment:\n{}", env);
343341

344342
if (mode == null) {
345343
mode = ExecutionMode.DEFAULT;
@@ -361,7 +359,7 @@ private <T> T require(AttributeKey<T> key) {
361359
log.info(" PID: {}", System.getProperty("PID"));
362360
log.info(" port: {}", server.port());
363361
log.info(" server: {}", server.getClass().getSimpleName().toLowerCase());
364-
log.info(" env: {}", environment.name());
362+
log.info(" env: {}", env.name());
365363
log.info(" thread mode: {}", mode.name().toLowerCase());
366364
log.info(" user: {}", System.getProperty("user.name"));
367365
log.info(" app dir: {}", System.getProperty("user.dir"));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
16+
package io.jooby.internal;
17+
18+
import io.jooby.Registry;
19+
import io.jooby.internal.registry.GuiceAdapter;
20+
21+
public class RegistryImpl {
22+
public static Registry wrap(ClassLoader loader, Object candidate) {
23+
if (candidate instanceof Registry) {
24+
return (Registry) candidate;
25+
}
26+
if (isInstanceOf(loader, "com.google.inject.Injector", candidate)) {
27+
return new GuiceAdapter(candidate);
28+
}
29+
throw new IllegalArgumentException("No registry adapter for: " + candidate);
30+
}
31+
32+
private static boolean isInstanceOf(ClassLoader loader, String klass, Object candidate) {
33+
try {
34+
return loader.loadClass(klass).isInstance(candidate);
35+
} catch (ClassNotFoundException x) {
36+
return false;
37+
}
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*
14+
* Copyright 2014 Edgar Espina
15+
*/
16+
package io.jooby.internal.registry;
17+
18+
import com.google.inject.Injector;
19+
import com.google.inject.Key;
20+
import com.google.inject.name.Names;
21+
import io.jooby.Registry;
22+
23+
import javax.annotation.Nonnull;
24+
25+
public class GuiceAdapter implements Registry {
26+
private final Injector injector;
27+
28+
public GuiceAdapter(Object candidate) {
29+
this.injector = (Injector) candidate;
30+
}
31+
32+
@Nonnull @Override public <T> T require(@Nonnull Class<T> type) {
33+
return injector.getInstance(type);
34+
}
35+
36+
@Nonnull @Override public <T> T require(@Nonnull Class<T> type, @Nonnull String name) {
37+
return injector.getInstance(Key.get(type, Names.named(name)));
38+
}
39+
}

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
<rxjava.version>2.2.5</rxjava.version>
4848
<reactor.version>3.2.5.RELEASE</reactor.version>
4949

50+
<!-- Guice -->
51+
<guice.version>4.2.2</guice.version>
52+
5053
<!-- Misc -->
5154
<okhttp.version>3.11.0</okhttp.version>
5255

@@ -344,6 +347,12 @@
344347
<version>${h2.version}</version>
345348
</dependency>
346349

350+
<!-- javax.inject -->
351+
<dependency>
352+
<groupId>javax.inject</groupId>
353+
<artifactId>javax.inject</artifactId>
354+
<version>${javax.inject.version}</version>
355+
</dependency>
347356

348357
<!-- OKHttp -->
349358
<dependency>
@@ -436,6 +445,13 @@
436445
<version>${reactor.version}</version>
437446
</dependency>
438447

448+
<!-- Guice -->
449+
<dependency>
450+
<groupId>com.google.inject</groupId>
451+
<artifactId>guice</artifactId>
452+
<version>${guice.version}</version>
453+
</dependency>
454+
439455
<!-- Kotlin -->
440456
<dependency>
441457
<groupId>org.jetbrains.kotlin</groupId>

0 commit comments

Comments
 (0)