Skip to content

Commit d1617ad

Browse files
committed
Hikari module + initial implementation of service registry
1 parent 2ba4497 commit d1617ad

File tree

12 files changed

+880
-10
lines changed

12 files changed

+880
-10
lines changed

examples/pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<artifactId>jooby-netty</artifactId>
3030
<version>${jooby.version}</version>
3131
</dependency>
32+
<dependency>
33+
<groupId>io.jooby</groupId>
34+
<artifactId>jooby-hikari</artifactId>
35+
<version>${jooby.version}</version>
36+
</dependency>
3237
<dependency>
3338
<groupId>org.ow2.asm</groupId>
3439
<artifactId>asm</artifactId>
@@ -45,6 +50,14 @@
4550
<groupId>io.projectreactor</groupId>
4651
<artifactId>reactor-core</artifactId>
4752
</dependency>
53+
<dependency>
54+
<groupId>com.h2database</groupId>
55+
<artifactId>h2</artifactId>
56+
</dependency>
57+
<dependency>
58+
<groupId>mysql</groupId>
59+
<artifactId>mysql-connector-java</artifactId>
60+
</dependency>
4861
</dependencies>
4962

5063
<dependencyManagement>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 examples;
17+
18+
import io.jooby.Jooby;
19+
import io.jooby.hikari.Hikari;
20+
21+
import javax.sql.DataSource;
22+
import java.sql.Connection;
23+
import java.sql.PreparedStatement;
24+
import java.sql.ResultSet;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
28+
public class HikariApp extends Jooby {
29+
30+
{
31+
install(new Hikari("jdbc:mysql://localhost/hello?user=root&password="));
32+
33+
get("/", ctx -> {
34+
try (Connection connection = require(DataSource.class).getConnection()) {
35+
try (PreparedStatement stt = connection.prepareStatement("select * from users")) {
36+
try (ResultSet rs = stt.executeQuery()) {
37+
List<String> names = new ArrayList<>();
38+
while (rs.next()) {
39+
names.add(rs.getString(1));
40+
}
41+
return names;
42+
}
43+
}
44+
}
45+
});
46+
}
47+
48+
public static void main(String[] args) {
49+
run(HikariApp::new, args);
50+
}
51+
}

jooby/src/main/java/io/jooby/Env.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ public Env(final String name) {
7272
@Override public Value get(@Nonnull String name) {
7373
Value result = super.get(name);
7474
if (result.isMissing()) {
75-
// fallback to full path access
76-
String value = fromSource(sources, name, null);
77-
if (value != null) {
78-
return Value.value(name, value);
75+
// Subpath lookup?
76+
String[] path = name.split("\\.");
77+
result = super.get(path[0]);
78+
for (int i = 1; i < path.length; i++) {
79+
result = result.get(path[i]);
7980
}
8081
}
8182
return result;

jooby/src/main/java/io/jooby/Extension.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
package io.jooby;
1717

18+
import javax.annotation.Nonnull;
19+
1820
public interface Extension {
1921

20-
void install(Env env, Router router);
22+
void install(@Nonnull Jooby application);
2123
}

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
import java.nio.file.Path;
2727
import java.nio.file.Paths;
2828
import java.util.ArrayList;
29+
import java.util.HashMap;
2930
import java.util.Iterator;
3031
import java.util.LinkedList;
3132
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Optional;
3235
import java.util.ServiceLoader;
3336
import java.util.concurrent.Executor;
3437
import java.util.function.Consumer;
@@ -52,6 +55,8 @@ public class Jooby implements Router {
5255

5356
private LinkedList<Throwing.Runnable> stopCallbacks;
5457

58+
private Map<Object, Object> services = new HashMap<>();
59+
5560
/**
5661
* Not ideal but useful. We want to have access to environment properties from instance
5762
* initializer. So external method before creating a new Jooby instance does a call to
@@ -169,7 +174,7 @@ public Jooby renderer(@Nonnull String contentType, @Nonnull Renderer renderer) {
169174
}
170175

171176
@Nonnull public Jooby install(@Nonnull Extension extension) {
172-
extension.install(environment, this);
177+
extension.install(this);
173178
return this;
174179
}
175180

@@ -252,6 +257,52 @@ public Jooby mode(ExecutionMode mode) {
252257
return this;
253258
}
254259

260+
public <T> T require(Class<T> type) {
261+
return findService(type, type.getName());
262+
}
263+
264+
public <T> T require(Class<T> type, String name) {
265+
return findService(type, type.getName() + "." + name);
266+
}
267+
268+
private <T> T findService(Class<T> type, String key) {
269+
Object service = services.get(key);
270+
if (service == null) {
271+
throw new IllegalStateException("Service not found: " + type);
272+
}
273+
return type.cast(service);
274+
}
275+
276+
public <T> Jooby addService(@Nonnull Class<T> type, @Nonnull T service) {
277+
putService(type, null, service);
278+
return this;
279+
}
280+
281+
public <T> Jooby addService(@Nonnull T service) {
282+
putService(service.getClass(), null, service);
283+
return this;
284+
}
285+
286+
public <T> Jooby addService(@Nonnull String name, @Nonnull T service) {
287+
putService(service.getClass(), name, service);
288+
return this;
289+
}
290+
291+
public <T> Jooby addService(@Nonnull Class<T> type, @Nonnull String name, @Nonnull T service) {
292+
putService(type, name, service);
293+
return this;
294+
}
295+
296+
private void putService(@Nonnull Class type, String name, @Nonnull Object service) {
297+
String defkey = type.getName();
298+
String key = type.getName();
299+
if (name != null) {
300+
key += "." + name;
301+
}
302+
services.put(key, service);
303+
services.putIfAbsent(defkey, service);
304+
}
305+
255306
/** Boot: */
256307
public Server start() {
257308
List<Server> servers = ServiceLoader.load(Server.class).stream()

jooby/src/test/java/io/jooby/EnvTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public void defaultEnv() {
3636
});
3737
}
3838

39+
@Test
40+
public void objectLookup() {
41+
42+
Env env = Env.build(new Env.PropertySource("test", Map.of("h.pool", "1", "h.db.pool", "2")));
43+
44+
assertEquals("1", env.get("h.pool").value());
45+
assertEquals("2", env.get("h.db.pool").value());
46+
assertEquals(Map.of("db.pool", "2"), env.get("h.db").toMap());
47+
}
48+
3949
@Test
4050
public void args() {
4151
Env.PropertySource args = Env.parse("foo", " bar = ");

modules/jooby-bom/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<handlebars.version>4.1.2</handlebars.version>
1919
<jackson.version>2.9.6</jackson.version>
2020

21+
<!-- hikari -->
22+
<hikari.version>3.2.0</hikari.version>
23+
<mysql-connector-java.version>8.0.13</mysql-connector-java.version>
24+
<log4jdbc.version>1.2</log4jdbc.version>
25+
<h2.version>1.4.197</h2.version>
26+
2127
<!-- logging -->
2228
<logback-classic.version>1.2.3</logback-classic.version>
2329
<slf4j-api.version>1.7.25</slf4j-api.version>
@@ -96,6 +102,19 @@
96102
<version>${jooby.version}</version>
97103
</dependency>
98104

105+
<dependency>
106+
<groupId>io.jooby</groupId>
107+
<artifactId>jooby-hikari</artifactId>
108+
<version>${jooby.version}</version>
109+
</dependency>
110+
111+
<!-- Connection Pool -->
112+
<dependency>
113+
<groupId>com.zaxxer</groupId>
114+
<artifactId>HikariCP</artifactId>
115+
<version>${hikari.version}</version>
116+
</dependency>
117+
99118
<!-- ASM -->
100119
<dependency>
101120
<groupId>org.ow2.asm</groupId>
@@ -178,6 +197,13 @@
178197
<version>${handlebars.version}</version>
179198
</dependency>
180199

200+
<!-- log4jdbc -->
201+
<dependency>
202+
<groupId>com.googlecode.log4jdbc</groupId>
203+
<artifactId>log4jdbc</artifactId>
204+
<version>${log4jdbc.version}</version>
205+
</dependency>
206+
181207
<!-- Logging System -->
182208
<dependency>
183209
<groupId>org.slf4j</groupId>
@@ -216,6 +242,21 @@
216242
<version>${guava.version}</version>
217243
</dependency>
218244

245+
<!-- mySQL -->
246+
<dependency>
247+
<groupId>mysql</groupId>
248+
<artifactId>mysql-connector-java</artifactId>
249+
<version>${mysql-connector-java.version}</version>
250+
</dependency>
251+
252+
<!-- h2 -->
253+
<dependency>
254+
<groupId>com.h2database</groupId>
255+
<artifactId>h2</artifactId>
256+
<version>${h2.version}</version>
257+
</dependency>
258+
259+
219260
<!-- OKHttp -->
220261
<dependency>
221262
<groupId>com.squareup.okhttp3</groupId>

modules/jooby-hikari/pom.xml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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/maven-v4_0_0.xsd">
5+
6+
<parent>
7+
<groupId>io.jooby</groupId>
8+
<artifactId>modules</artifactId>
9+
<version>2.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>jooby-hikari</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>com.google.code.findbugs</groupId>
18+
<artifactId>jsr305</artifactId>
19+
<scope>provided</scope>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>io.jooby</groupId>
24+
<artifactId>jooby</artifactId>
25+
<version>${jooby.version}</version>
26+
</dependency>
27+
28+
<!-- Connection Pool -->
29+
<dependency>
30+
<groupId>com.zaxxer</groupId>
31+
<artifactId>HikariCP</artifactId>
32+
</dependency>
33+
34+
<!-- Test dependencies -->
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-api</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-engine</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.junit.platform</groupId>
49+
<artifactId>junit-platform-launcher</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>org.jacoco</groupId>
55+
<artifactId>org.jacoco.agent</artifactId>
56+
<classifier>runtime</classifier>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>com.squareup.okhttp3</groupId>
62+
<artifactId>okhttp</artifactId>
63+
<scope>test</scope>
64+
</dependency>
65+
66+
<!-- mySQL -->
67+
<dependency>
68+
<groupId>mysql</groupId>
69+
<artifactId>mysql-connector-java</artifactId>
70+
<scope>test</scope>
71+
</dependency>
72+
73+
<!-- log4jdbc -->
74+
<dependency>
75+
<groupId>com.googlecode.log4jdbc</groupId>
76+
<artifactId>log4jdbc</artifactId>
77+
<scope>test</scope>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>io.jooby</groupId>
82+
<artifactId>jooby</artifactId>
83+
<version>${jooby.version}</version>
84+
<classifier>tests</classifier>
85+
<scope>test</scope>
86+
</dependency>
87+
</dependencies>
88+
</project>

0 commit comments

Comments
 (0)