use of org.apache.shiro.event.EventBus in project shiro by apache.
the class RealmSecurityManager method applyEventBusToRealms.
/**
* Sets the internal {@link #getEventBus EventBus} on any internal configured
* {@link #getRealms Realms} that implement the {@link EventBusAware} interface.
* <p/>
* This method is called after setting an eventBus on this securityManager via the
* {@link #setEventBus(org.apache.shiro.event.EventBus) setEventBus} method to allow it to be propagated
* down to all the internal Realms that would need to use it.
* <p/>
* It is also called after setting one or more realms via the {@link #setRealm setRealm} or
* {@link #setRealms setRealms} methods to allow these newly available realms to be given the EventBus
* already in use.
*
* @since 1.3
*/
protected void applyEventBusToRealms() {
EventBus eventBus = getEventBus();
Collection<Realm> realms = getRealms();
if (eventBus != null && realms != null && !realms.isEmpty()) {
for (Realm realm : realms) {
if (realm instanceof EventBusAware) {
((EventBusAware) realm).setEventBus(eventBus);
}
}
}
}
use of org.apache.shiro.event.EventBus in project shiro by apache.
the class ShiroModuleTest method testEventBusAware.
/**
* @since 1.4
* @throws Exception
*/
@Test
public void testEventBusAware() throws Exception {
final MockRealm mockRealm = createMock(MockRealm.class);
final ShiroModule shiroModule = new ShiroModule() {
@Override
protected void configureShiro() {
bindRealm().to(MockRealm.class);
binder().bind(MockEventBusAware.class).asEagerSingleton();
expose(MockEventBusAware.class);
}
@Provides
public MockRealm createRealm() {
return mockRealm;
}
};
Injector injector = Guice.createInjector(shiroModule);
EventBus eventBus = injector.getInstance(EventBus.class);
SecurityManager securityManager = injector.getInstance(SecurityManager.class);
MockEventBusAware eventBusAware = injector.getInstance(MockEventBusAware.class);
assertSame(eventBus, eventBusAware.eventBus);
assertSame(eventBus, ((DefaultSecurityManager) securityManager).getEventBus());
}
use of org.apache.shiro.event.EventBus in project shiro by apache.
the class ShiroModuleTest method testEventListener.
/**
* @since 1.4
* @throws Exception
*/
@Test
public void testEventListener() throws Exception {
final MockRealm mockRealm = createMock(MockRealm.class);
final EventBus eventBus = createMock(EventBus.class);
// expect both objects to be registered
eventBus.register(anyObject(MockEventListener1.class));
eventBus.register(anyObject(MockEventListener2.class));
replay(eventBus);
final ShiroModule shiroModule = new ShiroModule() {
@Override
protected void configureShiro() {
bindRealm().to(MockRealm.class);
// bind our event listeners
binder().bind(MockEventListener1.class).asEagerSingleton();
binder().bind(MockEventListener2.class).asEagerSingleton();
}
@Override
protected void bindEventBus(AnnotatedBindingBuilder<EventBus> bind) {
bind.toInstance(eventBus);
}
@Provides
public MockRealm createRealm() {
return mockRealm;
}
};
Guice.createInjector(shiroModule);
verify(eventBus);
}
use of org.apache.shiro.event.EventBus in project shiro by apache.
the class ReflectionBuilder method apply.
private void apply(Map<String, ?> objects) {
if (!isEmpty(objects)) {
this.objects.putAll(objects);
}
EventBus found = findEventBus(this.objects);
Assert.notNull(found, "An " + EventBus.class.getName() + " instance must be present in the object defaults");
enableEvents(found);
}
Aggregations