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 * @since 0.2
044 */
045 public class LifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor {
046
047 /**
048 * Private internal class log instance.
049 */
050 private static final Logger log = LoggerFactory.getLogger(LifecycleBeanPostProcessor.class);
051
052 /**
053 * Calls the <tt>init()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Initializable}
054 *
055 * @param object the object being initialized.
056 * @param name the name of the bean being initialized.
057 * @return the initialized bean.
058 * @throws BeansException if any exception is thrown during initialization.
059 */
060 public Object postProcessBeforeInitialization(Object object, String name) throws BeansException {
061 if (object instanceof Initializable) {
062 try {
063 if (log.isDebugEnabled()) {
064 log.debug("Initializing bean [" + name + "]...");
065 }
066
067 ((Initializable) object).init();
068 } catch (Exception e) {
069 throw new FatalBeanException("Error initializing bean [" + name + "]", e);
070 }
071 }
072 return object;
073 }
074
075
076 /**
077 * Does nothing - merely returns the object argument immediately.
078 */
079 public Object postProcessAfterInitialization(Object object, String name) throws BeansException {
080 // Does nothing after initialization
081 return object;
082 }
083
084
085 /**
086 * Calls the <tt>destroy()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Destroyable}
087 *
088 * @param object the object being initialized.
089 * @param name the name of the bean being initialized.
090 * @throws BeansException if any exception is thrown during initialization.
091 */
092 public void postProcessBeforeDestruction(Object object, String name) throws BeansException {
093 if (object instanceof Destroyable) {
094 try {
095 if (log.isDebugEnabled()) {
096 log.debug("Destroying bean [" + name + "]...");
097 }
098
099 ((Destroyable) object).destroy();
100 } catch (Exception e) {
101 throw new FatalBeanException("Error destroying bean [" + name + "]", e);
102 }
103 }
104 }
105 }