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.springframework.beans.BeansException;
022import org.springframework.beans.FatalBeanException;
023import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
024import org.springframework.core.PriorityOrdered;
025
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029import org.apache.shiro.util.Destroyable;
030import org.apache.shiro.util.Initializable;
031
032
033/**
034 * <p>Bean post processor for Spring that automatically calls the <tt>init()</tt> and/or
035 * <tt>destroy()</tt> methods on Shiro objects that implement the {@link org.apache.shiro.util.Initializable}
036 * or {@link org.apache.shiro.util.Destroyable} interfaces, respectfully.  This post processor makes it easier
037 * to configure Shiro beans in Spring, since the user never has to worry about whether or not if they
038 * have to specify init-method and destroy-method bean attributes.</p>
039 *
040 * <p><b>Warning: This post processor has no way to determine if <tt>init()</tt> or <tt>destroy()</tt> have
041 * already been called, so if you define this post processor in your applicationContext, do not also call these
042 * methods manually or via Spring's <tt>init-method</tt> or <tt>destroy-method</tt> bean attributes.</b></p>
043 *
044 * @since 0.2
045 */
046public class LifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor, PriorityOrdered {
047
048    /**
049     * Private internal class log instance.
050     */
051    private static final Logger log = LoggerFactory.getLogger(LifecycleBeanPostProcessor.class);
052
053    /**
054     * Order value of this BeanPostProcessor.
055     */
056    private int order;
057
058    /**
059     * Default Constructor.
060     */
061    public LifecycleBeanPostProcessor() {
062        this(LOWEST_PRECEDENCE);
063    }
064
065    /**
066     * Constructor with definable {@link #getOrder() order value}.
067     *
068     * @param order order value of this BeanPostProcessor.
069     */
070    public LifecycleBeanPostProcessor(int order) {
071        this.order = order;
072    }
073
074    /**
075     * Calls the <tt>init()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Initializable}
076     *
077     * @param object the object being initialized.
078     * @param name   the name of the bean being initialized.
079     * @return the initialized bean.
080     * @throws BeansException if any exception is thrown during initialization.
081     */
082    public Object postProcessBeforeInitialization(Object object, String name) throws BeansException {
083        if (object instanceof Initializable) {
084            try {
085                if (log.isDebugEnabled()) {
086                    log.debug("Initializing bean [" + name + "]...");
087                }
088
089                ((Initializable) object).init();
090            } catch (Exception e) {
091                throw new FatalBeanException("Error initializing bean [" + name + "]", e);
092            }
093        }
094        return object;
095    }
096
097
098    /**
099     * Does nothing - merely returns the object argument immediately.
100     */
101    public Object postProcessAfterInitialization(Object object, String name) throws BeansException {
102        // Does nothing after initialization
103        return object;
104    }
105
106
107    /**
108     * Calls the <tt>destroy()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Destroyable}
109     *
110     * @param object the object being initialized.
111     * @param name   the name of the bean being initialized.
112     * @throws BeansException if any exception is thrown during initialization.
113     */
114    public void postProcessBeforeDestruction(Object object, String name) throws BeansException {
115        if (object instanceof Destroyable) {
116            try {
117                if (log.isDebugEnabled()) {
118                    log.debug("Destroying bean [" + name + "]...");
119                }
120
121                ((Destroyable) object).destroy();
122            } catch (Exception e) {
123                throw new FatalBeanException("Error destroying bean [" + name + "]", e);
124            }
125        }
126    }
127
128    /**
129     * Order value of this BeanPostProcessor.
130     *
131     * @return order value.
132     */
133    public int getOrder() {
134        // LifecycleBeanPostProcessor needs Order. See https://issues.apache.org/jira/browse/SHIRO-222
135        return order;
136    }
137}