Skip to content

Commit 14dc716

Browse files
committed
App import: override service registry
1 parent da49ece commit 14dc716

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ public Jooby use(@Nonnull Router router) {
258258
return this;
259259
}
260260

261+
/**
262+
* The underlying router.
263+
*
264+
* @return The underlying router.
265+
*/
266+
public @Nonnull Router getRouter() {
267+
return router;
268+
}
269+
261270
@Nonnull @Override
262271
public Jooby use(@Nonnull Predicate<Context> predicate, @Nonnull Router router) {
263272
this.router.use(predicate, router);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public RouterImpl(ClassLoader loader) {
223223

224224
@Nonnull @Override
225225
public Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router) {
226+
syncState(router);
226227
Chi tree = new Chi();
227228
if (predicateMap == null) {
228229
predicateMap = new LinkedHashMap<>();
@@ -236,7 +237,20 @@ public Router use(@Nonnull Predicate<Context> predicate, @Nonnull Router router)
236237
return this;
237238
}
238239

240+
private void syncState(Router router) {
241+
if (router instanceof Jooby) {
242+
Jooby app = (Jooby) router;
243+
syncState(app.getRouter());
244+
} else if (router instanceof RouterImpl) {
245+
RouterImpl that = (RouterImpl) router;
246+
// Inherited the services from router owner
247+
// TODO: what to do with existing services? Is there anything we can do?
248+
that.services = this.services;
249+
}
250+
}
251+
239252
@Nonnull @Override public Router use(@Nonnull String path, @Nonnull Router router) {
253+
syncState(router);
240254
String prefix = Router.leadingSlash(path);
241255
for (Route route : router.getRoutes()) {
242256
String routePattern = new PathBuilder(prefix, route.getPattern()).toString();

starters/quartz-starter/src/main/java/starter/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class App extends Jooby {
2020

2121
install(new QuartzModule(SampleJob.class, BeanJob.class));
2222

23-
use("/quartz", new QuartzApp(require(Scheduler.class)));
23+
use("/quartz", new QuartzApp());
2424
}
2525

2626
public static void main(String[] args) {

0 commit comments

Comments
 (0)