blob: e83e4496ce616657df56a3194a6d7661f15d580b [file] [log] [blame]
<?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>SimplePrincipalMap.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 :: Jar Bundle</a> &gt; <a href="../index.html" class="el_bundle">shiro-core</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.subject</a> &gt; <span class="el_source">SimplePrincipalMap.java</span></div><h1>SimplePrincipalMap.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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.subject;
import org.apache.shiro.util.CollectionUtils;
import java.util.*;
/**
* Default implementation of the {@link PrincipalMap} interface.
*
* *EXPERIMENTAL for Shiro 1.2 - DO NOT USE YET*
*
* @author Les Hazlewood
* @since 1.2
*/
public class SimplePrincipalMap implements PrincipalMap {
//Key: realm name, Value: map of principals specific to that realm
// internal map - key: principal name, value: principal
private Map&lt;String, Map&lt;String, Object&gt;&gt; realmPrincipals;
//maintains the principals from all realms plus any that are modified via the Map modification methods
//this ensures a fast lookup of any named principal instead of needing to iterate over
//the realmPrincipals for each lookup.
private Map&lt;String, Object&gt; combinedPrincipals;
public SimplePrincipalMap() {
<span class="nc" id="L45"> this(null);</span>
<span class="nc" id="L46"> }</span>
<span class="nc" id="L48"> public SimplePrincipalMap(Map&lt;String, Map&lt;String, Object&gt;&gt; backingMap) {</span>
<span class="nc bnc" id="L49" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(backingMap)) {</span>
<span class="nc" id="L50"> this.realmPrincipals = backingMap;</span>
<span class="nc bnc" id="L51" title="All 2 branches missed."> for (Map&lt;String, Object&gt; principals : this.realmPrincipals.values()) {</span>
<span class="nc bnc" id="L52" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(principals) ) {</span>
<span class="nc" id="L53"> ensureCombinedPrincipals().putAll(principals);</span>
}
<span class="nc" id="L55"> }</span>
}
<span class="nc" id="L57"> }</span>
public int size() {
<span class="nc" id="L60"> return CollectionUtils.size(this.combinedPrincipals);</span>
}
protected Map&lt;String, Object&gt; ensureCombinedPrincipals() {
<span class="nc bnc" id="L64" title="All 2 branches missed."> if (this.combinedPrincipals == null) {</span>
<span class="nc" id="L65"> this.combinedPrincipals = new HashMap&lt;String, Object&gt;();</span>
}
<span class="nc" id="L67"> return this.combinedPrincipals;</span>
}
public boolean containsKey(Object o) {
<span class="nc bnc" id="L71" title="All 4 branches missed."> return this.combinedPrincipals != null &amp;&amp; this.combinedPrincipals.containsKey(o);</span>
}
public boolean containsValue(Object o) {
<span class="nc bnc" id="L75" title="All 4 branches missed."> return this.combinedPrincipals != null &amp;&amp; this.combinedPrincipals.containsKey(o);</span>
}
public Object get(Object o) {
<span class="nc bnc" id="L79" title="All 4 branches missed."> return this.combinedPrincipals != null &amp;&amp; this.combinedPrincipals.containsKey(o);</span>
}
public Object put(String s, Object o) {
<span class="nc" id="L83"> return ensureCombinedPrincipals().put(s, o);</span>
}
public Object remove(Object o) {
<span class="nc bnc" id="L87" title="All 2 branches missed."> return this.combinedPrincipals != null ? this.combinedPrincipals.remove(o) : null;</span>
}
public void putAll(Map&lt;? extends String, ?&gt; map) {
<span class="nc bnc" id="L91" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(map)) {</span>
<span class="nc" id="L92"> ensureCombinedPrincipals().putAll(map);</span>
}
<span class="nc" id="L94"> }</span>
public Set&lt;String&gt; keySet() {
<span class="nc bnc" id="L97" title="All 2 branches missed."> return CollectionUtils.isEmpty(this.combinedPrincipals) ?</span>
<span class="nc" id="L98"> Collections.&lt;String&gt;emptySet() :</span>
<span class="nc" id="L99"> Collections.unmodifiableSet(this.combinedPrincipals.keySet());</span>
}
public Collection&lt;Object&gt; values() {
<span class="nc bnc" id="L103" title="All 2 branches missed."> return CollectionUtils.isEmpty(this.combinedPrincipals) ?</span>
<span class="nc" id="L104"> Collections.emptySet() :</span>
<span class="nc" id="L105"> Collections.unmodifiableCollection(this.combinedPrincipals.values());</span>
}
public Set&lt;Entry&lt;String, Object&gt;&gt; entrySet() {
<span class="nc bnc" id="L109" title="All 2 branches missed."> return CollectionUtils.isEmpty(this.combinedPrincipals) ?</span>
<span class="nc" id="L110"> Collections.&lt;Entry&lt;String,Object&gt;&gt;emptySet() :</span>
<span class="nc" id="L111"> Collections.unmodifiableSet(this.combinedPrincipals.entrySet());</span>
}
public void clear() {
<span class="nc" id="L115"> this.realmPrincipals = null;</span>
<span class="nc" id="L116"> this.combinedPrincipals = null;</span>
<span class="nc" id="L117"> }</span>
public Object getPrimaryPrincipal() {
//heuristic - just use the first one we come across:
<span class="nc bnc" id="L121" title="All 2 branches missed."> return !CollectionUtils.isEmpty(this.combinedPrincipals) ?</span>
<span class="nc" id="L122"> this.combinedPrincipals.values().iterator().next() :</span>
null;
}
public &lt;T&gt; T oneByType(Class&lt;T&gt; type) {
<span class="nc bnc" id="L127" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.combinedPrincipals)) {</span>
<span class="nc" id="L128"> return null;</span>
}
<span class="nc bnc" id="L130" title="All 2 branches missed."> for( Object value : this.combinedPrincipals.values()) {</span>
<span class="nc bnc" id="L131" title="All 2 branches missed."> if (type.isInstance(value) ) {</span>
<span class="nc" id="L132"> return type.cast(value);</span>
}
<span class="nc" id="L134"> }</span>
<span class="nc" id="L135"> return null;</span>
}
public &lt;T&gt; Collection&lt;T&gt; byType(Class&lt;T&gt; type) {
<span class="nc bnc" id="L139" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.combinedPrincipals)) {</span>
<span class="nc" id="L140"> return Collections.emptySet();</span>
}
<span class="nc" id="L142"> Collection&lt;T&gt; instances = null;</span>
<span class="nc bnc" id="L143" title="All 2 branches missed."> for( Object value : this.combinedPrincipals.values()) {</span>
<span class="nc bnc" id="L144" title="All 2 branches missed."> if (type.isInstance(value) ) {</span>
<span class="nc bnc" id="L145" title="All 2 branches missed."> if (instances == null) {</span>
<span class="nc" id="L146"> instances = new ArrayList&lt;T&gt;();</span>
}
<span class="nc" id="L148"> instances.add(type.cast(value));</span>
}
<span class="nc" id="L150"> }</span>
<span class="nc bnc" id="L151" title="All 2 branches missed."> return instances != null ? instances : Collections.&lt;T&gt;emptyList();</span>
}
public List asList() {
<span class="nc bnc" id="L155" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.combinedPrincipals)) {</span>
<span class="nc" id="L156"> return Collections.emptyList();</span>
}
<span class="nc" id="L158"> List&lt;Object&gt; list = new ArrayList&lt;Object&gt;(this.combinedPrincipals.size());</span>
<span class="nc" id="L159"> list.addAll(this.combinedPrincipals.values());</span>
<span class="nc" id="L160"> return list;</span>
}
public Set asSet() {
<span class="nc bnc" id="L164" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.combinedPrincipals)) {</span>
<span class="nc" id="L165"> return Collections.emptySet();</span>
}
<span class="nc" id="L167"> Set&lt;Object&gt; set = new HashSet&lt;Object&gt;(this.combinedPrincipals.size());</span>
<span class="nc" id="L168"> set.addAll(this.combinedPrincipals.values());</span>
<span class="nc" id="L169"> return set;</span>
}
public Collection fromRealm(String realmName) {
<span class="nc bnc" id="L173" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.realmPrincipals)) {</span>
<span class="nc" id="L174"> return Collections.emptySet();</span>
}
<span class="nc" id="L176"> Map&lt;String,Object&gt; principals = this.realmPrincipals.get(realmName);</span>
<span class="nc bnc" id="L177" title="All 2 branches missed."> if (CollectionUtils.isEmpty(principals)) {</span>
<span class="nc" id="L178"> return Collections.emptySet();</span>
}
<span class="nc" id="L180"> return Collections.unmodifiableCollection(principals.values());</span>
}
public Set&lt;String&gt; getRealmNames() {
<span class="nc bnc" id="L184" title="All 2 branches missed."> if (CollectionUtils.isEmpty(this.realmPrincipals)) {</span>
<span class="nc" id="L185"> return Collections.emptySet();</span>
}
<span class="nc" id="L187"> return Collections.unmodifiableSet(this.realmPrincipals.keySet());</span>
}
public boolean isEmpty() {
<span class="nc" id="L191"> return CollectionUtils.isEmpty(this.combinedPrincipals);</span>
}
public Iterator iterator() {
<span class="nc" id="L195"> return asList().iterator();</span>
}
public Map&lt;String, Object&gt; getRealmPrincipals(String name) {
<span class="nc bnc" id="L199" title="All 2 branches missed."> if (this.realmPrincipals == null) {</span>
<span class="nc" id="L200"> return null;</span>
}
<span class="nc" id="L202"> Map&lt;String,Object&gt; principals = this.realmPrincipals.get(name);</span>
<span class="nc bnc" id="L203" title="All 2 branches missed."> if (principals == null) {</span>
<span class="nc" id="L204"> return null;</span>
}
<span class="nc" id="L206"> return Collections.unmodifiableMap(principals);</span>
}
public Map&lt;String,Object&gt; setRealmPrincipals(String realmName, Map&lt;String, Object&gt; principals) {
<span class="nc bnc" id="L210" title="All 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L211"> throw new NullPointerException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L213" title="All 2 branches missed."> if (this.realmPrincipals == null) {</span>
<span class="nc bnc" id="L214" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(principals)) {</span>
<span class="nc" id="L215"> this.realmPrincipals = new HashMap&lt;String,Map&lt;String,Object&gt;&gt;();</span>
<span class="nc" id="L216"> return this.realmPrincipals.put(realmName, new HashMap&lt;String,Object&gt;(principals));</span>
} else {
<span class="nc" id="L218"> return null;</span>
}
} else {
<span class="nc" id="L221"> Map&lt;String,Object&gt; existingPrincipals = this.realmPrincipals.remove(realmName);</span>
<span class="nc bnc" id="L222" title="All 2 branches missed."> if (!CollectionUtils.isEmpty(principals)) {</span>
<span class="nc" id="L223"> this.realmPrincipals.put(realmName, new HashMap&lt;String,Object&gt;(principals));</span>
}
<span class="nc" id="L225"> return existingPrincipals;</span>
}
}
public Object setRealmPrincipal(String realmName, String principalName, Object principal) {
<span class="nc bnc" id="L230" title="All 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L231"> throw new NullPointerException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L233" title="All 2 branches missed."> if (principalName == null) {</span>
<span class="nc" id="L234"> throw new NullPointerException((&quot;principalName argument cannot be null.&quot;));</span>
}
<span class="nc bnc" id="L236" title="All 2 branches missed."> if (principal == null) {</span>
<span class="nc" id="L237"> return removeRealmPrincipal(realmName, principalName);</span>
}
<span class="nc bnc" id="L239" title="All 2 branches missed."> if (this.realmPrincipals == null) {</span>
<span class="nc" id="L240"> this.realmPrincipals = new HashMap&lt;String,Map&lt;String,Object&gt;&gt;();</span>
}
<span class="nc" id="L242"> Map&lt;String,Object&gt; principals = this.realmPrincipals.get(realmName);</span>
<span class="nc bnc" id="L243" title="All 2 branches missed."> if (principals == null) {</span>
<span class="nc" id="L244"> principals = new HashMap&lt;String,Object&gt;();</span>
<span class="nc" id="L245"> this.realmPrincipals.put(realmName, principals);</span>
}
<span class="nc" id="L247"> return principals.put(principalName, principal);</span>
}
public Object getRealmPrincipal(String realmName, String principalName) {
<span class="nc bnc" id="L251" title="All 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L252"> throw new NullPointerException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L254" title="All 2 branches missed."> if (principalName == null) {</span>
<span class="nc" id="L255"> throw new NullPointerException((&quot;principalName argument cannot be null.&quot;));</span>
}
<span class="nc bnc" id="L257" title="All 2 branches missed."> if (this.realmPrincipals == null) {</span>
<span class="nc" id="L258"> return null;</span>
}
<span class="nc" id="L260"> Map&lt;String,Object&gt; principals = this.realmPrincipals.get(realmName);</span>
<span class="nc bnc" id="L261" title="All 2 branches missed."> if (principals != null) {</span>
<span class="nc" id="L262"> return principals.get(principalName);</span>
}
<span class="nc" id="L264"> return null;</span>
}
public Object removeRealmPrincipal(String realmName, String principalName) {
<span class="nc bnc" id="L268" title="All 2 branches missed."> if (realmName == null) {</span>
<span class="nc" id="L269"> throw new NullPointerException(&quot;realmName argument cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L271" title="All 2 branches missed."> if (principalName == null) {</span>
<span class="nc" id="L272"> throw new NullPointerException((&quot;principalName argument cannot be null.&quot;));</span>
}
<span class="nc bnc" id="L274" title="All 2 branches missed."> if (this.realmPrincipals == null) {</span>
<span class="nc" id="L275"> return null;</span>
}
<span class="nc" id="L277"> Map&lt;String,Object&gt; principals = this.realmPrincipals.get(realmName);</span>
<span class="nc bnc" id="L278" title="All 2 branches missed."> if (principals != null) {</span>
<span class="nc" id="L279"> return principals.remove(principalName);</span>
}
<span class="nc" id="L281"> return null;</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>