3131import java .util .LinkedList ;
3232import java .util .List ;
3333import java .util .Map ;
34+ import java .util .NoSuchElementException ;
3435import java .util .ServiceLoader ;
3536import java .util .Spliterator ;
3637import java .util .concurrent .Executor ;
4445import static java .util .Spliterators .spliteratorUnknownSize ;
4546import static java .util .stream .StreamSupport .stream ;
4647
47- public class Jooby implements Router {
48+ public class Jooby implements Router , Registry {
4849
4950 private RouterImpl router ;
5051
@@ -60,8 +61,6 @@ public class Jooby implements Router {
6061
6162 private LinkedList <Throwing .Runnable > stopCallbacks ;
6263
63- private Map <Object , Object > services = new HashMap <>();
64-
6564 /**
6665 * Not ideal but useful. We want to have access to environment properties from instance
6766 * initializer. So external method before creating a new Jooby instance does a call to
@@ -72,6 +71,8 @@ public class Jooby implements Router {
7271
7372 protected Env environment ;
7473
74+ private Registry registry ;
75+
7576 public Jooby () {
7677 router = new RouterImpl (new RouteAnalyzer (getClass ().getClassLoader (), false ));
7778 environment = ENV .get ();
@@ -286,50 +287,36 @@ public Jooby errorCode(@Nonnull Class<? extends Throwable> type,
286287 return this ;
287288 }
288289
289- public @ Nonnull < T > T require ( @ Nonnull Class < T > type ) {
290- return findService ( type , type . getName () );
290+ @ Nonnull @ Override public AttributeMap attributes ( ) {
291+ return router . attributes ( );
291292 }
292293
293294 public @ Nonnull <T > T require (@ Nonnull Class <T > type , @ Nonnull String name ) {
294- return findService (type , type .getName () + "." + name );
295- }
296-
297- private @ Nonnull <T > T findService (@ Nonnull Class <T > type , @ Nonnull String key ) {
298- Object service = services .get (key );
299- if (service == null ) {
300- throw new IllegalStateException ("Service not found: " + type );
301- }
302- return type .cast (service );
303- }
304-
305- public @ Nonnull <T > Jooby addService (@ Nonnull Class <T > type , @ Nonnull T service ) {
306- putService (type , null , service );
307- return this ;
308- }
309-
310- public @ Nonnull <T > Jooby addService (@ Nonnull T service ) {
311- putService (service .getClass (), null , service );
312- return this ;
295+ return require (new AttributeKey <>(type , name ));
313296 }
314297
315- public @ Nonnull <T > Jooby addService (@ Nonnull String name , @ Nonnull T service ) {
316- putService (service .getClass (), name , service );
317- return this ;
298+ public @ Nonnull <T > T require (@ Nonnull Class <T > type ) {
299+ return require (new AttributeKey <>(type ));
318300 }
319301
320- public @ Nonnull < T > Jooby addService (@ Nonnull Class < T > type , @ Nonnull String name , @ Nonnull T service ) {
321- putService ( type , name , service ) ;
302+ public @ Nonnull Jooby registry (@ Nonnull Registry registry ) {
303+ this . registry = registry ;
322304 return this ;
323305 }
324306
325- private void putService (@ Nonnull Class type , String name , @ Nonnull Object service ) {
326- String defkey = type .getName ();
327- String key = type .getName ();
328- if (name != null ) {
329- key += "." + name ;
307+ private <T > T require (AttributeKey <T > key ) {
308+ AttributeMap attributes = attributes ();
309+ if (attributes .contains (key )) {
310+ return attributes .get (key );
311+ }
312+ if (registry != null ) {
313+ String name = key .getName ();
314+ if (name == null ) {
315+ return registry .require (key .getType ());
316+ }
317+ return registry .require (key .getType (), name );
330318 }
331- services .put (key , service );
332- services .putIfAbsent (defkey , service );
319+ throw new NoSuchElementException (key .toString ());
333320 }
334321
335322 /** Boot: */
@@ -420,7 +407,8 @@ public static void run(@Nonnull Supplier<Jooby> provider, String... args) {
420407 run (provider , ExecutionMode .DEFAULT , args );
421408 }
422409
423- public static void run (@ Nonnull Supplier <Jooby > provider , @ Nonnull ExecutionMode mode , String ... args ) {
410+ public static void run (@ Nonnull Supplier <Jooby > provider , @ Nonnull ExecutionMode mode ,
411+ String ... args ) {
424412 Server server ;
425413 try {
426414 Env environment = Env .defaultEnvironment (args );
0 commit comments