001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.spring;
020
021import org.apache.shiro.event.EventBus;
022import org.apache.shiro.event.EventBusAware;
023import org.apache.shiro.event.Subscribe;
024import org.apache.shiro.util.ClassUtils;
025import org.apache.shiro.util.CollectionUtils;
026import org.springframework.beans.BeansException;
027import org.springframework.beans.factory.config.BeanPostProcessor;
028
029import java.util.List;
030
031/**
032 * Spring {@link BeanPostProcessor} that detects, {@link EventBusAware} and classes containing {@link Subscribe @Subscribe} methods.
033 * Any classes implementing EventBusAware will have the setEventBus() method called with the <code>eventBus</code>. Any
034 * classes discovered with methods that are annotated with @Subscribe will be automaticly registered with the EventBus.
035 *
036 * <p><strong>NOTE:</strong> in a Spring environment implementing EventBusAware is not necessary, as you can just inject the EventBus with
037 * {@link org.springframework.beans.factory.annotation.Autowire @Autowire}.</p>
038 *
039 * @see EventBusAware
040 * @see Subscribe
041 * @since 1.4
042 */
043public class ShiroEventBusBeanPostProcessor implements BeanPostProcessor {
044
045    final private EventBus eventBus;
046
047    public ShiroEventBusBeanPostProcessor(EventBus eventBus) {
048        this.eventBus = eventBus;
049    }
050
051    @Override
052    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
053        return bean;
054    }
055
056    @Override
057    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
058        if (bean instanceof EventBusAware) {
059            ((EventBusAware) bean).setEventBus(eventBus);
060        }
061        else if (isEventSubscriber(bean)) {
062            eventBus.register(bean);
063        }
064
065        return bean;
066    }
067
068    private boolean isEventSubscriber(Object bean) {
069        List annotatedMethods = ClassUtils.getAnnotatedMethods(bean.getClass(), Subscribe.class);
070        return !CollectionUtils.isEmpty(annotatedMethods);
071    }
072
073}