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