| <?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>RealmSecurityManager.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 :: Core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.mgt</a> > <span class="el_source">RealmSecurityManager.java</span></div><h1>RealmSecurityManager.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.mgt; |
| |
| import org.apache.shiro.cache.CacheManager; |
| import org.apache.shiro.cache.CacheManagerAware; |
| import org.apache.shiro.event.EventBus; |
| import org.apache.shiro.event.EventBusAware; |
| import org.apache.shiro.realm.Realm; |
| import org.apache.shiro.util.LifecycleUtils; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| |
| |
| /** |
| * Shiro support of a {@link SecurityManager} class hierarchy based around a collection of |
| * {@link org.apache.shiro.realm.Realm}s. All actual {@code SecurityManager} method implementations are left to |
| * subclasses. |
| * |
| * @since 0.9 |
| */ |
| public abstract class RealmSecurityManager extends CachingSecurityManager { |
| |
| /** |
| * Internal collection of <code>Realm</code>s used for all authentication and authorization operations. |
| */ |
| private Collection<Realm> realms; |
| |
| /** |
| * Default no-arg constructor. |
| */ |
| public RealmSecurityManager() { |
| <span class="fc" id="L50"> super();</span> |
| <span class="fc" id="L51"> }</span> |
| |
| /** |
| * Convenience method for applications using a single realm that merely wraps the realm in a list and then invokes |
| * the {@link #setRealms} method. |
| * |
| * @param realm the realm to set for a single-realm application. |
| * @since 0.2 |
| */ |
| public void setRealm(Realm realm) { |
| <span class="pc bpc" id="L61" title="1 of 2 branches missed."> if (realm == null) {</span> |
| <span class="nc" id="L62"> throw new IllegalArgumentException("Realm argument cannot be null");</span> |
| } |
| <span class="fc" id="L64"> Collection<Realm> realms = new ArrayList<Realm>(1);</span> |
| <span class="fc" id="L65"> realms.add(realm);</span> |
| <span class="fc" id="L66"> setRealms(realms);</span> |
| <span class="fc" id="L67"> }</span> |
| |
| /** |
| * Sets the realms managed by this <tt>SecurityManager</tt> instance. |
| * |
| * @param realms the realms managed by this <tt>SecurityManager</tt> instance. |
| * @throws IllegalArgumentException if the realms collection is null or empty. |
| */ |
| public void setRealms(Collection<Realm> realms) { |
| <span class="pc bpc" id="L76" title="1 of 2 branches missed."> if (realms == null) {</span> |
| <span class="nc" id="L77"> throw new IllegalArgumentException("Realms collection argument cannot be null.");</span> |
| } |
| <span class="pc bpc" id="L79" title="1 of 2 branches missed."> if (realms.isEmpty()) {</span> |
| <span class="nc" id="L80"> throw new IllegalArgumentException("Realms collection argument cannot be empty.");</span> |
| } |
| <span class="fc" id="L82"> this.realms = realms;</span> |
| <span class="fc" id="L83"> afterRealmsSet();</span> |
| <span class="fc" id="L84"> }</span> |
| |
| protected void afterRealmsSet() { |
| <span class="fc" id="L87"> applyCacheManagerToRealms();</span> |
| <span class="fc" id="L88"> applyEventBusToRealms();</span> |
| <span class="fc" id="L89"> }</span> |
| |
| /** |
| * Returns the {@link Realm Realm}s managed by this SecurityManager instance. |
| * |
| * @return the {@link Realm Realm}s managed by this SecurityManager instance. |
| */ |
| public Collection<Realm> getRealms() { |
| <span class="fc" id="L97"> return realms;</span> |
| } |
| |
| /** |
| * Sets the internal {@link #getCacheManager CacheManager} on any internal configured |
| * {@link #getRealms Realms} that implement the {@link org.apache.shiro.cache.CacheManagerAware CacheManagerAware} interface. |
| * <p/> |
| * This method is called after setting a cacheManager on this securityManager via the |
| * {@link #setCacheManager(org.apache.shiro.cache.CacheManager) setCacheManager} method to allow it to be propagated |
| * down to all the internal Realms that would need to use it. |
| * <p/> |
| * It is also called after setting one or more realms via the {@link #setRealm setRealm} or |
| * {@link #setRealms setRealms} methods to allow these newly available realms to be given the cache manager |
| * already in use. |
| */ |
| protected void applyCacheManagerToRealms() { |
| <span class="fc" id="L113"> CacheManager cacheManager = getCacheManager();</span> |
| <span class="fc" id="L114"> Collection<Realm> realms = getRealms();</span> |
| <span class="pc bpc" id="L115" title="1 of 6 branches missed."> if (cacheManager != null && realms != null && !realms.isEmpty()) {</span> |
| <span class="fc bfc" id="L116" title="All 2 branches covered."> for (Realm realm : realms) {</span> |
| <span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (realm instanceof CacheManagerAware) {</span> |
| <span class="fc" id="L118"> ((CacheManagerAware) realm).setCacheManager(cacheManager);</span> |
| } |
| <span class="fc" id="L120"> }</span> |
| } |
| <span class="fc" id="L122"> }</span> |
| |
| /** |
| * Sets the internal {@link #getEventBus EventBus} on any internal configured |
| * {@link #getRealms Realms} that implement the {@link EventBusAware} interface. |
| * <p/> |
| * This method is called after setting an eventBus on this securityManager via the |
| * {@link #setEventBus(org.apache.shiro.event.EventBus) setEventBus} method to allow it to be propagated |
| * down to all the internal Realms that would need to use it. |
| * <p/> |
| * It is also called after setting one or more realms via the {@link #setRealm setRealm} or |
| * {@link #setRealms setRealms} methods to allow these newly available realms to be given the EventBus |
| * already in use. |
| * |
| * @since 1.3 |
| */ |
| protected void applyEventBusToRealms() { |
| <span class="fc" id="L139"> EventBus eventBus = getEventBus();</span> |
| <span class="fc" id="L140"> Collection<Realm> realms = getRealms();</span> |
| <span class="pc bpc" id="L141" title="2 of 6 branches missed."> if (eventBus != null && realms != null && !realms.isEmpty()) {</span> |
| <span class="fc bfc" id="L142" title="All 2 branches covered."> for(Realm realm : realms) {</span> |
| <span class="pc bpc" id="L143" title="1 of 2 branches missed."> if (realm instanceof EventBusAware) {</span> |
| <span class="nc" id="L144"> ((EventBusAware)realm).setEventBus(eventBus);</span> |
| } |
| <span class="fc" id="L146"> }</span> |
| } |
| <span class="fc" id="L148"> }</span> |
| |
| /** |
| * Simply calls {@link #applyCacheManagerToRealms() applyCacheManagerToRealms()} to allow the |
| * newly set {@link org.apache.shiro.cache.CacheManager CacheManager} to be propagated to the internal collection of <code>Realm</code> |
| * that would need to use it. |
| */ |
| protected void afterCacheManagerSet() { |
| <span class="fc" id="L156"> super.afterCacheManagerSet();</span> |
| <span class="fc" id="L157"> applyCacheManagerToRealms();</span> |
| <span class="fc" id="L158"> }</span> |
| |
| @Override |
| protected void afterEventBusSet() { |
| <span class="fc" id="L162"> super.afterEventBusSet();</span> |
| <span class="fc" id="L163"> applyEventBusToRealms();</span> |
| <span class="fc" id="L164"> }</span> |
| |
| public void destroy() { |
| <span class="fc" id="L167"> LifecycleUtils.destroy(getRealms());</span> |
| <span class="fc" id="L168"> this.realms = null;</span> |
| <span class="fc" id="L169"> super.destroy();</span> |
| <span class="fc" id="L170"> }</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> |