Search in sources :

Example 1 with EventBus

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);
            }
        }
    }
}
Also used : EventBusAware(org.apache.shiro.event.EventBusAware) EventBus(org.apache.shiro.event.EventBus) Realm(org.apache.shiro.realm.Realm)

Example 2 with 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());
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) Injector(com.google.inject.Injector) DefaultEventBus(org.apache.shiro.event.support.DefaultEventBus) EventBus(org.apache.shiro.event.EventBus) Test(org.junit.Test)

Example 3 with EventBus

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);
}
Also used : AnnotatedBindingBuilder(com.google.inject.binder.AnnotatedBindingBuilder) DefaultEventBus(org.apache.shiro.event.support.DefaultEventBus) EventBus(org.apache.shiro.event.EventBus) Test(org.junit.Test)

Example 4 with 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);
}
Also used : DefaultEventBus(org.apache.shiro.event.support.DefaultEventBus) EventBus(org.apache.shiro.event.EventBus)

Aggregations

EventBus (org.apache.shiro.event.EventBus)4 DefaultEventBus (org.apache.shiro.event.support.DefaultEventBus)3 Test (org.junit.Test)2 Injector (com.google.inject.Injector)1 AnnotatedBindingBuilder (com.google.inject.binder.AnnotatedBindingBuilder)1 EventBusAware (org.apache.shiro.event.EventBusAware)1 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)1 SecurityManager (org.apache.shiro.mgt.SecurityManager)1 Realm (org.apache.shiro.realm.Realm)1