| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>LifecycleBeanPostProcessor.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">Apache Shiro :: Support :: Spring</a> > <a href="index.source.html" class="el_package">org.apache.shiro.spring</a> > <span class="el_source">LifecycleBeanPostProcessor.java</span></div><h1>LifecycleBeanPostProcessor.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.spring; |
| |
| import org.springframework.beans.BeansException; |
| import org.springframework.beans.FatalBeanException; |
| import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; |
| import org.springframework.core.PriorityOrdered; |
| |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import org.apache.shiro.util.Destroyable; |
| import org.apache.shiro.util.Initializable; |
| |
| |
| /** |
| * <p>Bean post processor for Spring that automatically calls the <tt>init()</tt> and/or |
| * <tt>destroy()</tt> methods on Shiro objects that implement the {@link org.apache.shiro.util.Initializable} |
| * or {@link org.apache.shiro.util.Destroyable} interfaces, respectfully. This post processor makes it easier |
| * to configure Shiro beans in Spring, since the user never has to worry about whether or not if they |
| * have to specify init-method and destroy-method bean attributes.</p> |
| * |
| * <p><b>Warning: This post processor has no way to determine if <tt>init()</tt> or <tt>destroy()</tt> have |
| * already been called, so if you define this post processor in your applicationContext, do not also call these |
| * methods manually or via Spring's <tt>init-method</tt> or <tt>destroy-method</tt> bean attributes.</b></p> |
| * |
| * @since 0.2 |
| */ |
| public class LifecycleBeanPostProcessor implements DestructionAwareBeanPostProcessor, PriorityOrdered { |
| |
| /** |
| * Private internal class log instance. |
| */ |
| <span class="fc" id="L51"> private static final Logger log = LoggerFactory.getLogger(LifecycleBeanPostProcessor.class);</span> |
| |
| /** |
| * Order value of this BeanPostProcessor. |
| */ |
| private int order; |
| |
| /** |
| * Default Constructor. |
| */ |
| public LifecycleBeanPostProcessor() { |
| <span class="fc" id="L62"> this(LOWEST_PRECEDENCE);</span> |
| <span class="fc" id="L63"> }</span> |
| |
| /** |
| * Constructor with definable {@link #getOrder() order value}. |
| * |
| * @param order order value of this BeanPostProcessor. |
| */ |
| <span class="fc" id="L70"> public LifecycleBeanPostProcessor(int order) {</span> |
| <span class="fc" id="L71"> this.order = order;</span> |
| <span class="fc" id="L72"> }</span> |
| |
| /** |
| * Calls the <tt>init()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Initializable} |
| * |
| * @param object the object being initialized. |
| * @param name the name of the bean being initialized. |
| * @return the initialized bean. |
| * @throws BeansException if any exception is thrown during initialization. |
| */ |
| public Object postProcessBeforeInitialization(Object object, String name) throws BeansException { |
| <span class="fc bfc" id="L83" title="All 2 branches covered."> if (object instanceof Initializable) {</span> |
| try { |
| <span class="pc bpc" id="L85" title="1 of 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="nc" id="L86"> log.debug("Initializing bean [" + name + "]...");</span> |
| } |
| |
| <span class="fc" id="L89"> ((Initializable) object).init();</span> |
| <span class="nc" id="L90"> } catch (Exception e) {</span> |
| <span class="nc" id="L91"> throw new FatalBeanException("Error initializing bean [" + name + "]", e);</span> |
| <span class="fc" id="L92"> }</span> |
| } |
| <span class="fc" id="L94"> return object;</span> |
| } |
| |
| |
| /** |
| * Does nothing - merely returns the object argument immediately. |
| */ |
| public Object postProcessAfterInitialization(Object object, String name) throws BeansException { |
| // Does nothing after initialization |
| <span class="fc" id="L103"> return object;</span> |
| } |
| |
| |
| /** |
| * Calls the <tt>destroy()</tt> methods on the bean if it implements {@link org.apache.shiro.util.Destroyable} |
| * |
| * @param object the object being initialized. |
| * @param name the name of the bean being initialized. |
| * @throws BeansException if any exception is thrown during initialization. |
| */ |
| public void postProcessBeforeDestruction(Object object, String name) throws BeansException { |
| <span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (object instanceof Destroyable) {</span> |
| try { |
| <span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="nc" id="L118"> log.debug("Destroying bean [" + name + "]...");</span> |
| } |
| |
| <span class="fc" id="L121"> ((Destroyable) object).destroy();</span> |
| <span class="nc" id="L122"> } catch (Exception e) {</span> |
| <span class="nc" id="L123"> throw new FatalBeanException("Error destroying bean [" + name + "]", e);</span> |
| <span class="fc" id="L124"> }</span> |
| } |
| <span class="fc" id="L126"> }</span> |
| |
| /** |
| * Order value of this BeanPostProcessor. |
| * |
| * @return order value. |
| */ |
| public int getOrder() { |
| // LifecycleBeanPostProcessor needs Order. See https://issues.apache.org/jira/browse/SHIRO-222 |
| <span class="fc" id="L135"> return order;</span> |
| } |
| |
| /** |
| * Return true only if <code>bean</code> implements Destroyable. |
| * @param bean bean to check if requires destruction. |
| * @return true only if <code>bean</code> implements Destroyable. |
| * @since 1.4 |
| */ |
| @SuppressWarnings("unused") |
| public boolean requiresDestruction(Object bean) { |
| <span class="fc" id="L146"> return (bean instanceof Destroyable);</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |