Skip to content

Commit 3f77543

Browse files
committed
API Change: Jooby.use(Session.Store) -> Jooby.session(Session.Store) fix #57
1 parent 6d1f011 commit 3f77543

7 files changed

Lines changed: 53 additions & 134 deletions

File tree

coverage-report/src/test/java/org/jooby/session/SessionWithNoTimeOutFeature.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

jooby-hbs/src/main/java/org/jooby/internal/hbs/LocalsValueResolver.java renamed to jooby-hbs/src/main/java/org/jooby/internal/hbs/RequestValueResolver.java

File renamed without changes.

jooby-hbs/src/test/java/org/jooby/internal/hbs/LocalsValueResolverTest.java renamed to jooby-hbs/src/test/java/org/jooby/internal/hbs/RequestValueResolverTest.java

File renamed without changes.

jooby/src/main/java/org/jooby/Cookie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ public static boolean valid(final String value, final String secret) {
520520
Optional<String> domain();
521521

522522
/**
523-
* Gets the maximum age in seconds of this Cookie.
523+
* Gets the maximum age of this cookie (in seconds).
524524
*
525525
* <p>
526526
* By default, <code>-1</code> is returned, which indicates that the cookie will persist until

jooby/src/main/java/org/jooby/Jooby.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
import javax.inject.Singleton;
7777

78+
import org.jooby.Session.Store;
7879
import org.jooby.internal.AppPrinter;
7980
import org.jooby.internal.AssetFormatter;
8081
import org.jooby.internal.AssetHandler;
@@ -485,7 +486,7 @@ public Jooby() {
485486
private List<BodyParser> parsers = new LinkedList<>();
486487

487488
/** Session store. */
488-
private Session.Definition session = new Session.Definition(new Session.MemoryStore());
489+
private Session.Definition session = new Session.Definition(Session.Mem.class);
489490

490491
/** Flag to control the addition of the asset formatter. */
491492
private boolean assetFormatter = false;
@@ -532,6 +533,18 @@ public <T> T require(final Class<T> type) {
532533
return injector.getInstance(type);
533534
}
534535

536+
/**
537+
* Setup a session store to use. Useful if you want/need to persist sessions between shutdowns.
538+
* Sessions are not persisted by defaults.
539+
*
540+
* @param store A session store.
541+
* @return A session store definition.
542+
*/
543+
public Session.Definition session(final Class<? extends Session.Store> store) {
544+
this.session = new Session.Definition(requireNonNull(store, "A session store is required."));
545+
return this.session;
546+
}
547+
535548
/**
536549
* Setup a session store to use. Useful if you want/need to persist sessions between shutdowns.
537550
* Sessions are not persisted by defaults.
@@ -2684,6 +2697,7 @@ private Injector bootstrap() throws Exception {
26842697
Stage stage = "dev".equals(env.name()) ? Stage.DEVELOPMENT : Stage.PRODUCTION;
26852698

26862699
// dependency injection
2700+
@SuppressWarnings("unchecked")
26872701
Injector injector = Guice.createInjector(stage, binder -> {
26882702

26892703
// type converters
@@ -2721,9 +2735,6 @@ private Injector bootstrap() throws Exception {
27212735
Multibinder<BodyFormatter> formatterBinder = Multibinder
27222736
.newSetBinder(binder, BodyFormatter.class);
27232737

2724-
// session definition
2725-
binder.bind(Session.Definition.class).toInstance(session);
2726-
27272738
// Routes
27282739
Multibinder<Route.Definition> definitions = Multibinder
27292740
.newSetBinder(binder, Route.Definition.class);
@@ -2794,7 +2805,15 @@ private Injector bootstrap() throws Exception {
27942805
binder.bindScope(RequestScoped.class, requestScope);
27952806

27962807
// session manager
2797-
binder.bind(SessionManager.class).toInstance(new SessionManager(config, session));
2808+
binder.bind(SessionManager.class).asEagerSingleton();
2809+
binder.bind(Session.Definition.class).toInstance(session);
2810+
Object sstore = session.store();
2811+
if (sstore instanceof Class) {
2812+
binder.bind(Session.Store.class).to((Class<? extends Store>) sstore)
2813+
.asEagerSingleton();
2814+
} else {
2815+
binder.bind(Session.Store.class).toInstance((Store) sstore);;
2816+
}
27982817

27992818
binder.bind(Request.class).toProvider(() -> {
28002819
throw new OutOfScopeException(Request.class.getName());

jooby/src/main/java/org/jooby/Locals.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

jooby/src/test/java/org/jooby/JoobyTest.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -345,15 +345,22 @@ public Object m1() {
345345
private MockUnit.Block session = unit -> {
346346
Binder binder = unit.get(Binder.class);
347347

348-
AnnotatedBindingBuilder<SessionManager> smbinding = unit.mock(AnnotatedBindingBuilder.class);
349-
smbinding.toInstance(isA(SessionManager.class));
348+
AnnotatedBindingBuilder<SessionManager> smABB = unit.mock(AnnotatedBindingBuilder.class);
349+
smABB.asEagerSingleton();
350350

351-
expect(binder.bind(SessionManager.class)).andReturn(smbinding);
351+
ScopedBindingBuilder ssSBB = unit.mock(ScopedBindingBuilder.class);
352+
ssSBB.asEagerSingleton();
352353

353-
AnnotatedBindingBuilder<Session.Definition> binding = unit.mock(AnnotatedBindingBuilder.class);
354-
binding.toInstance(isA(Session.Definition.class));
354+
AnnotatedBindingBuilder<Store> ssABB= unit.mock(AnnotatedBindingBuilder.class);
355+
expect(ssABB.to(Session.Mem.class)).andReturn(ssSBB);
355356

356-
expect(binder.bind(Session.Definition.class)).andReturn(binding);
357+
expect(binder.bind(SessionManager.class)).andReturn(smABB);
358+
expect(binder.bind(Session.Store.class)).andReturn(ssABB);
359+
360+
AnnotatedBindingBuilder<Session.Definition> sdABB = unit.mock(AnnotatedBindingBuilder.class);
361+
sdABB.toInstance(isA(Session.Definition.class));
362+
363+
expect(binder.bind(Session.Definition.class)).andReturn(sdABB);
357364
};
358365

359366
private MockUnit.Block boot = unit -> {
@@ -2105,17 +2112,22 @@ public void useStore() throws Exception {
21052112
.expect(unit -> {
21062113
Binder binder = unit.get(Binder.class);
21072114

2108-
AnnotatedBindingBuilder<SessionManager> smbinding = unit
2109-
.mock(AnnotatedBindingBuilder.class);
2110-
smbinding.toInstance(isA(SessionManager.class));
2115+
AnnotatedBindingBuilder<SessionManager> smABB = unit.mock(AnnotatedBindingBuilder.class);
2116+
smABB.asEagerSingleton();
2117+
2118+
ScopedBindingBuilder ssSBB = unit.mock(ScopedBindingBuilder.class);
2119+
ssSBB.asEagerSingleton();
2120+
2121+
AnnotatedBindingBuilder<Store> ssABB= unit.mock(AnnotatedBindingBuilder.class);
2122+
expect(ssABB.to(unit.get(Session.Store.class).getClass())).andReturn(ssSBB);
21112123

2112-
expect(binder.bind(SessionManager.class)).andReturn(smbinding);
2124+
expect(binder.bind(SessionManager.class)).andReturn(smABB);
2125+
expect(binder.bind(Session.Store.class)).andReturn(ssABB);
21132126

2114-
AnnotatedBindingBuilder<Session.Definition> binding = unit
2115-
.mock(AnnotatedBindingBuilder.class);
2116-
binding.toInstance(unit.capture(Session.Definition.class));
2127+
AnnotatedBindingBuilder<Session.Definition> sdABB = unit.mock(AnnotatedBindingBuilder.class);
2128+
sdABB.toInstance(unit.capture(Session.Definition.class));
21172129

2118-
expect(binder.bind(Session.Definition.class)).andReturn(binding);
2130+
expect(binder.bind(Session.Definition.class)).andReturn(sdABB);
21192131
})
21202132
.expect(routes)
21212133
.expect(routeHandler)
@@ -2127,14 +2139,14 @@ public void useStore() throws Exception {
21272139
.run(unit -> {
21282140

21292141
Jooby jooby = new Jooby();
2130-
jooby.session(unit.get(Store.class));
2142+
jooby.session(unit.get(Store.class).getClass());
21312143

21322144
jooby.start();
21332145

21342146
}, boot,
21352147
unit -> {
21362148
Definition def = unit.captured(Session.Definition.class).iterator().next();
2137-
assertEquals(unit.get(Store.class), def.store());
2149+
assertEquals(unit.get(Store.class).getClass(), def.store());
21382150
});
21392151
}
21402152

0 commit comments

Comments
 (0)