@Test(expected = OutOfScopeException.class) public void testOutOfScope() throws Exception { ShiroSessionScope underTest = new ShiroSessionScope(); Provider<SomeClass> mockProvider = createMock(Provider.class); replay(mockProvider); underTest.scope(Key.get(SomeClass.class), mockProvider).get(); }
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();  }