/** * Binds a key to use for injecting setters in shiro classes. * @param typeLiteral the bean property type * @param key the key to use to satisfy the bean property dependency * @param <T> */ protected final <T> void bindBeanType(TypeLiteral<T> typeLiteral, Key<? extends T> key) { BeanTypeListener.bindBeanType(binder(), typeLiteral, key); }
public void add(Destroyable destroyable) { ShiroModule.this.add(destroyable); }
public void afterInjection(Destroyable injectee) { registry.add(injectee); }
public void configure() { // setup security manager bindSecurityManager(bind(SecurityManager.class)); bindSessionManager(bind(SessionManager.class)); bindEnvironment(bind(Environment.class)); bindListener(BeanTypeListener.MATCHER, new BeanTypeListener()); bindEventBus(bind(EventBus.class)); bindListener(Matchers.any(), new SubscribedEventTypeListener()); bindListener(Matchers.any(), new EventBusAwareTypeListener()); final DestroyableInjectionListener.DestroyableRegistry registry = new DestroyableInjectionListener.DestroyableRegistry() { public void add(Destroyable destroyable) { ShiroModule.this.add(destroyable); } @PreDestroy public void destroy() { ShiroModule.this.destroy(); } }; bindListener(LifecycleTypeListener.MATCHER, new LifecycleTypeListener(registry)); expose(SecurityManager.class); expose(EventBus.class); configureShiro(); bind(realmCollectionKey()) .to(realmSetKey()); bind(DestroyableInjectionListener.DestroyableRegistry.class).toInstance(registry); BeanTypeListener.ensureBeanTypeMapExists(binder()); }
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) { if (InitializableInjectionListener.MATCHER.matches(type)) { encounter.register(this.<I>castListener(new InitializableInjectionListener<Initializable>())); } if (DestroyableInjectionListener.MATCHER.matches(type)) { encounter.register(this.<I>castListener(new DestroyableInjectionListener<Destroyable>(registry))); } }
@Test public void testAfterInjection() throws Exception { DestroyableInjectionListener.DestroyableRegistry registry = createMock(DestroyableInjectionListener.DestroyableRegistry.class); Destroyable destroyable = createMock(Destroyable.class); registry.add(destroyable); replay(registry, destroyable); DestroyableInjectionListener underTest = new DestroyableInjectionListener(registry); underTest.afterInjection(destroyable); verify(registry, destroyable); } }
@Test public void testAfterInjection() throws Exception { Initializable initializable = createMock(Initializable.class); initializable.init(); replay(initializable); InitializableInjectionListener underTest = new InitializableInjectionListener(); underTest.afterInjection(initializable); verify(initializable); } }
static <T> void bindBeanType(Binder binder, TypeLiteral<T> typeLiteral, Key<? extends T> key) { beanTypeMapBinding(binder).addBinding(typeLiteral).toInstance(new BeanTypeKey(key)); }
@PreDestroy public void destroy() { ShiroModule.this.destroy(); } };
/** * This is the preferred manner to bind a realm. The {@link org.apache.shiro.mgt.SecurityManager} will be injected with any Realm bound * with this method. * * @return a binding builder for a realm */ protected final LinkedBindingBuilder<Realm> bindRealm() { Multibinder<Realm> multibinder = Multibinder.newSetBinder(binder(), Realm.class); return multibinder.addBinding(); }
private static Key<?> createDependencyKey(PropertyDescriptor propertyDescriptor, Type propertyType) { if(requiresName(propertyType)) { return Key.get(propertyType, Names.named("shiro." + propertyDescriptor.getName())); } else { return Key.get(propertyType); } }
@Override protected void configureShiro() { bindRealm().to(MockRealm.class); }
@Override protected void configureShiro() { bindRealm().to(MockRealm.class); }
@Override protected void configureShiro() { bindRealm().to(MockRealm.class); }
@Override protected void configureShiro() { bindRealm().to(MockRealm.class); }
private static Key<?> getMappedKey(Injector injector, Key<?> key) { Map<TypeLiteral, BeanTypeKey> beanTypeMap = getBeanTypeMap(injector); if(key.getAnnotation() == null && beanTypeMap.containsKey(key.getTypeLiteral())) { return beanTypeMap.get(key.getTypeLiteral()).key; } else { return key; } }
static void ensureBeanTypeMapExists(Binder binder) { beanTypeMapBinding(binder).addBinding(TypeLiteral.get(BeanTypeKey.class)).toInstance(new BeanTypeKey(null)); }