Skip to content

Commit d240098

Browse files
committed
Rename ResourceKey/resources to ServiceKey/services
- Add ServiceRegistry - Normalize provisioning exception to RegistryException - Update guice, spring and weld
1 parent f39acfe commit d240098

File tree

16 files changed

+339
-97
lines changed

16 files changed

+339
-97
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
package io.jooby;
1717

1818
import com.typesafe.config.Config;
19-
import com.typesafe.config.ConfigFactory;
20-
import com.typesafe.config.ConfigValueFactory;
2119
import io.jooby.internal.RouterImpl;
2220
import org.slf4j.Logger;
2321
import org.slf4j.LoggerFactory;
@@ -449,11 +447,24 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
449447
}
450448

451449
@Nonnull @Override public <T> T require(@Nonnull Class<T> type, @Nonnull String name) {
452-
return checkRegistry().require(type, name);
450+
return require(ServiceKey.key(type, name));
453451
}
454452

455453
@Nonnull @Override public <T> T require(@Nonnull Class<T> type) {
456-
return checkRegistry().require(type);
454+
return require(ServiceKey.key(type));
455+
}
456+
457+
private @Nonnull <T> T require(@Nonnull ServiceKey<T> key) {
458+
ServiceRegistry services = getServices();
459+
T service = services.getOrNull(key);
460+
if (service == null) {
461+
if (registry == null) {
462+
throw new RegistryException("Service not found: " + key);
463+
}
464+
String name = key.getName();
465+
return name == null ? registry.require(key.getType()) : registry.require(key.getType(), name);
466+
}
467+
return service;
457468
}
458469

459470
/**
@@ -467,18 +478,8 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
467478
return this;
468479
}
469480

470-
@Nonnull @Override public Map<ResourceKey, Object> getResources() {
471-
return this.router.getResources();
472-
}
473-
474-
@Nonnull @Override public <R> Jooby resource(@Nonnull ResourceKey<R> key, @Nonnull R resource) {
475-
this.router.resource(key, resource);
476-
return this;
477-
}
478-
479-
@Nonnull @Override public <T> T resource(@Nonnull ResourceKey<T> key)
480-
throws IllegalStateException {
481-
return this.router.resource(key);
481+
@Nonnull @Override public ServiceRegistry getServices() {
482+
return this.router.getServices();
482483
}
483484

484485
private Registry checkRegistry() {

jooby/src/main/java/io/jooby/Registry.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*
2323
* @since 2.0.0
2424
* @author edgar
25+
* @see ServiceRegistry
2526
*/
2627
public interface Registry {
2728
/**
@@ -30,8 +31,9 @@ public interface Registry {
3031
* @param type Object type.
3132
* @param <T> Object type.
3233
* @return Instance of this type.
34+
* @throws RegistryException If there was a runtime failure while providing an instance.
3335
*/
34-
@Nonnull <T> T require(@Nonnull Class<T> type);
36+
@Nonnull <T> T require(@Nonnull Class<T> type) throws RegistryException;
3537

3638
/**
3739
* Provides an instance of the given type where name matches it.
@@ -40,6 +42,7 @@ public interface Registry {
4042
* @param name Object name.
4143
* @param <T> Object type.
4244
* @return Instance of this type.
45+
* @throws RegistryException If there was a runtime failure while providing an instance.
4346
*/
44-
@Nonnull <T> T require(@Nonnull Class<T> type, @Nonnull String name);
47+
@Nonnull <T> T require(@Nonnull Class<T> type, @Nonnull String name) throws RegistryException;
4548
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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;
17+
18+
import javax.annotation.Nonnull;
19+
20+
public class RegistryException extends Err {
21+
public RegistryException(@Nonnull String message, Throwable cause) {
22+
super(StatusCode.SERVER_ERROR, message, cause);
23+
}
24+
25+
public RegistryException(@Nonnull String message) {
26+
super(StatusCode.SERVER_ERROR, message);
27+
}
28+
}

jooby/src/main/java/io/jooby/Router.java

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,41 +124,14 @@ interface Match {
124124
}
125125

126126
/**
127-
* Application resource registry. A resource must be initialized at application startup time
128-
* and release it using {@link Jooby#onStop(AutoCloseable)} (if need it).
127+
* Application service registry. Services are accessible via this registry or
128+
* {@link Jooby#require(Class)} calls.
129129
*
130-
* @return Resource registry.
131-
*/
132-
@Nonnull Map<ResourceKey, Object> getResources();
133-
134-
/**
135-
* Get a resource under this key or throws a {@link IllegalStateException} exception.
130+
* This method returns a mutable registry. You are free to modify/alter the registry.
136131
*
137-
* @param key Attribute key.
138-
* @param <T> Attribute type.
139-
* @return Attribute value.
140-
* @throws IllegalStateException If there is no resource under this key.
132+
* @return Service registry.
141133
*/
142-
default @Nonnull <T> T resource(@Nonnull ResourceKey<T> key) throws IllegalStateException {
143-
Object resource = getResources().get(key);
144-
if (resource == null) {
145-
throw new IllegalStateException("Resource not found: " + key);
146-
}
147-
return (T) resource;
148-
}
149-
150-
/**
151-
* Put an application resource.
152-
*
153-
* @param key Resource key.
154-
* @param resource Resource value.
155-
* @param <R> Resource type.
156-
* @return This router.
157-
*/
158-
default @Nonnull <R> Router resource(@Nonnull ResourceKey<R> key, @Nonnull R resource) {
159-
getResources().put(key, resource);
160-
return this;
161-
}
134+
@Nonnull ServiceRegistry getServices();
162135

163136
/**
164137
* Set application context path. Context path is the base path for all routes. Default is:

jooby/src/main/java/io/jooby/ResourceKey.java renamed to jooby/src/main/java/io/jooby/ServiceKey.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
import java.util.Objects;
2121

2222
/**
23-
* Utility class which creates a String key from type and optionally a name.
23+
* Utility class to access application services.
2424
*
25-
* @param <T> Resource type.
25+
* @param <T> Service type.
2626
*/
27-
public final class ResourceKey<T> {
27+
public final class ServiceKey<T> {
2828
private final Class<T> type;
2929

3030
private final int hashCode;
3131

3232
private final String name;
3333

34-
private ResourceKey(Class<T> type, String name) {
34+
private ServiceKey(Class<T> type, String name) {
3535
this.type = type;
3636
this.name = name;
3737
this.hashCode = Objects.hash(type, name);
@@ -56,8 +56,8 @@ private ResourceKey(Class<T> type, String name) {
5656
}
5757

5858
@Override public boolean equals(Object obj) {
59-
if (obj instanceof ResourceKey) {
60-
ResourceKey that = (ResourceKey) obj;
59+
if (obj instanceof ServiceKey) {
60+
ServiceKey that = (ServiceKey) obj;
6161
return this.type == that.type && Objects.equals(this.name, that.name);
6262
}
6363
return false;
@@ -81,8 +81,8 @@ private ResourceKey(Class<T> type, String name) {
8181
* @param <T> Type.
8282
* @return A new resource key.
8383
*/
84-
public static @Nonnull <T> ResourceKey<T> key(@Nonnull Class<T> type) {
85-
return new ResourceKey<>(type, null);
84+
public static @Nonnull <T> ServiceKey<T> key(@Nonnull Class<T> type) {
85+
return new ServiceKey<>(type, null);
8686
}
8787

8888
/**
@@ -93,7 +93,7 @@ private ResourceKey(Class<T> type, String name) {
9393
* @param <T> Type.
9494
* @return A new resource key.
9595
*/
96-
public static @Nonnull <T> ResourceKey<T> key(@Nonnull Class<T> type, @Nonnull String name) {
97-
return new ResourceKey<>(type, name);
96+
public static @Nonnull <T> ServiceKey<T> key(@Nonnull Class<T> type, @Nonnull String name) {
97+
return new ServiceKey<>(type, name);
9898
}
9999
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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;
17+
18+
import javax.annotation.Nonnull;
19+
import javax.annotation.Nullable;
20+
import java.util.Set;
21+
22+
/**
23+
* Default registry which use a simply key/value mechanism for storing and retrieving services.
24+
*
25+
* @author edgar
26+
* @since 2.0.0
27+
*/
28+
public interface ServiceRegistry extends Registry {
29+
/**
30+
* Registered service keys.
31+
*
32+
* @return Service keys.
33+
*/
34+
@Nonnull Set<ServiceKey<?>> keySet();
35+
36+
/**
37+
* Retrieve a service/resource by key.
38+
*
39+
* @param key Service/resource key.
40+
* @param <T> Service/resource type.
41+
* @return Service.
42+
* @throws RegistryException If there was a runtime failure while providing an instance.
43+
*/
44+
default @Nonnull <T> T get(@Nonnull ServiceKey<T> key) {
45+
T service = getOrNull(key);
46+
if (service == null) {
47+
throw new RegistryException("Service not found: " + key);
48+
}
49+
return service;
50+
}
51+
52+
/**
53+
* Retrieve a service/resource by key.
54+
*
55+
* @param type Service/resource key.
56+
* @param <T> Service/resource type.
57+
* @return Service.
58+
* @throws RegistryException If there was a runtime failure while providing an instance.
59+
*/
60+
default @Nonnull <T> T get(@Nonnull Class<T> type) {
61+
return get(ServiceKey.key(type));
62+
}
63+
64+
/**
65+
* Retrieve an existing service or <code>null</code> if not exists.
66+
*
67+
* @param key Service/resource key.
68+
* @param <T> Service/resource type.
69+
* @return Service or <code>null</code>.
70+
*/
71+
@Nullable <T> T getOrNull(@Nonnull ServiceKey<T> key);
72+
73+
/**
74+
* Retrieve an existing service or <code>null</code> if not exists.
75+
*
76+
* @param type Service/resource key.
77+
* @param <T> Service/resource type.
78+
* @return Service or <code>null</code>.
79+
*/
80+
default @Nullable <T> T getOrNull(@Nonnull Class<T> type) {
81+
return getOrNull(ServiceKey.key(type));
82+
}
83+
84+
/**
85+
* Put a service in this registry. This method overrides any previous registered service.
86+
*
87+
* @param key Service/resource key.
88+
* @param service Service instance.
89+
* @param <T> Service type.
90+
* @return Previously registered service or <code>null</code>.
91+
*/
92+
@Nullable <T> T put(@Nonnull ServiceKey<T> key, T service);
93+
94+
/**
95+
* Put a service in this registry. This method overrides any previous registered service.
96+
*
97+
* @param type Service/resource key.
98+
* @param service Service instance.
99+
* @param <T> Service type.
100+
* @return Previously registered service or <code>null</code>.
101+
*/
102+
default @Nullable <T> T put(@Nonnull Class<T> type, T service) {
103+
return put(ServiceKey.key(type), service);
104+
}
105+
106+
/**
107+
* Put/register a service in this registry if there isn't the same service already registered.
108+
*
109+
*
110+
* @param key Service/resource key.
111+
* @param service Service instance.
112+
* @param <T> Service type.
113+
* @return Previously registered service or <code>null</code>.
114+
*/
115+
@Nullable <T> T putIfAbsent(@Nonnull ServiceKey<T> key, T service);
116+
117+
/**
118+
* Put/register a service in this registry if there isn't the same service already registered.
119+
*
120+
*
121+
* @param type Service/resource key.
122+
* @param service Service instance.
123+
* @param <T> Service type.
124+
* @return Previously registered service or <code>null</code>.
125+
*/
126+
default @Nullable <T> T putIfAbsent(@Nonnull Class<T> type, T service) {
127+
return putIfAbsent(ServiceKey.key(type), service);
128+
}
129+
130+
default @Nonnull @Override <T> T require(@Nonnull Class<T> type) {
131+
return get(ServiceKey.key(type));
132+
}
133+
134+
default @Nonnull @Override <T> T require(@Nonnull Class<T> type, @Nonnull String name) {
135+
return get(ServiceKey.key(type, name));
136+
}
137+
}

jooby/src/main/java/io/jooby/internal/RouterImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
import io.jooby.MediaType;
2424
import io.jooby.Parser;
2525
import io.jooby.Renderer;
26-
import io.jooby.ResourceKey;
2726
import io.jooby.ResponseHandler;
2827
import io.jooby.Route;
2928
import io.jooby.Router;
3029
import io.jooby.RouterOptions;
30+
import io.jooby.ServiceRegistry;
3131
import io.jooby.SessionOptions;
3232
import io.jooby.StatusCode;
3333
import io.jooby.Throwing;
@@ -151,7 +151,7 @@ public Stack executor(Executor executor) {
151151

152152
private List<ResponseHandler> handlers = new ArrayList<>();
153153

154-
private Map<ResourceKey, Object> resources = new HashMap<>();
154+
private ServiceRegistry services = new ServiceRegistryImpl();
155155

156156
private MvcAnnotationParser annotationParser;
157157

@@ -510,8 +510,8 @@ public void destroy() {
510510
return this;
511511
}
512512

513-
@Nonnull @Override public Map<ResourceKey, Object> getResources() {
514-
return resources;
513+
@Nonnull @Override public ServiceRegistry getServices() {
514+
return services;
515515
}
516516

517517
@Override public String toString() {

0 commit comments

Comments
 (0)