@Test public void testScope() throws Exception { Subject subject = createMock(Subject.class); try { ThreadContext.bind(subject); final Key<SomeClass> key = Key.get(SomeClass.class); Provider<SomeClass> mockProvider = createMock(Provider.class); Session session = createMock(Session.class); SomeClass retuned = new SomeClass(); expect(subject.getSession()).andReturn(session); expect(session.getAttribute(key)).andReturn(null); expect(mockProvider.get()).andReturn(retuned); expect(subject.getSession()).andReturn(session); expect(session.getAttribute(key)).andReturn(retuned); replay(subject, mockProvider, session); ShiroSessionScope underTest = new ShiroSessionScope(); assertSame(retuned, underTest.scope(key, mockProvider).get()); assertSame(retuned, underTest.scope(key, mockProvider).get()); verify(subject, mockProvider, session); } finally { ThreadContext.unbindSubject(); } }
public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) { return new Provider<T>() { public T get() { Subject subject = ThreadContext.getSubject(); if (subject == null) { throw new OutOfScopeException("There is no Shiro Session currently in scope."); } Session session = subject.getSession(); T scoped = castSessionAttribute(session); if (scoped == null) { scoped = unscoped.get(); } return scoped; } @SuppressWarnings({"unchecked"}) private T castSessionAttribute(Session session) { return (T) session.getAttribute(key); } @Override public String toString() { return unscoped.toString(); } }; }
ShiroSessionScope implements Scope { public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) { return new Provider<T>() { public T get() { Subject subject = ThreadContext.getSubject(); if (subject == null) { throw new OutOfScopeException("There is no Shiro Session currently in scope."); } Session session = subject.getSession(); T scoped = castSessionAttribute(session); if (scoped == null) { scoped = unscoped.get(); } return scoped; } @SuppressWarnings({"unchecked"}) private T castSessionAttribute(Session session) { return (T) session.getAttribute(key); } @Override public String toString() { return unscoped.toString(); } }; } }
ShiroSessionScope implements Scope { public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) { return new Provider<T>() { public T get() { Subject subject = ThreadContext.getSubject(); if (subject == null) { throw new OutOfScopeException("There is no Shiro Session currently in scope."); } Session session = subject.getSession(); T scoped = castSessionAttribute(session); if (scoped == null) { scoped = unscoped.get(); } return scoped; } @SuppressWarnings({"unchecked"}) private T castSessionAttribute(Session session) { return (T) session.getAttribute(key); } @Override public String toString() { return unscoped.toString(); } }; }  }
ShiroSessionScope implements Scope { public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) { return new Provider<T>() { public T get() { Subject subject = ThreadContext.getSubject(); if (subject == null) { throw new OutOfScopeException("There is no Shiro Session currently in scope."); } Session session = subject.getSession(); T scoped = castSessionAttribute(session); if (scoped == null) { scoped = unscoped.get(); } return scoped; } @SuppressWarnings({"unchecked"}) private T castSessionAttribute(Session session) { return (T) session.getAttribute(key); } @Override public String toString() { return unscoped.toString(); } }; }  Provider<T> scope(final Key<T> key, final Provider<T> unscoped); @Override String toString(); }
ShiroSessionScope implements Scope { public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) { return new Provider<T>() { public T get() { Subject subject = ThreadContext.getSubject(); if (subject == null) { throw new OutOfScopeException("There is no Shiro Session currently in scope."); } Session session = subject.getSession(); T scoped = castSessionAttribute(session); if (scoped == null) { scoped = unscoped.get(); } return scoped; } @SuppressWarnings({"unchecked"}) private T castSessionAttribute(Session session) { return (T) session.getAttribute(key); } @Override public String toString() { return unscoped.toString(); } }; }  Provider<T> scope(final Key<T> key, final Provider<T> unscoped); @Override String toString();  }