blob: 5e7013dfe98c1ffe45facd57bc3b8a61f5a26c0f [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>DefaultEnvironment.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> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.env</a> &gt; <span class="el_source">DefaultEnvironment.java</span></div><h1>DefaultEnvironment.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.env;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.LifecycleUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Simple/default {@code Environment} implementation that stores Shiro objects as key-value pairs in a
* {@link java.util.Map Map} instance. The key is the object name, the value is the object itself.
*
* @since 1.2
*/
public class DefaultEnvironment implements NamedObjectEnvironment, Destroyable {
/**
* The default name under which the application's {@code SecurityManager} instance may be acquired, equal to
* {@code securityManager}.
*/
public static final String DEFAULT_SECURITY_MANAGER_KEY = &quot;securityManager&quot;;
protected final Map&lt;String, Object&gt; objects;
private String securityManagerName;
/**
* Creates a new instance with a thread-safe {@link ConcurrentHashMap} backing map.
*/
public DefaultEnvironment() {
<span class="nc" id="L49"> this(new ConcurrentHashMap&lt;String, Object&gt;());</span>
<span class="nc" id="L50"> }</span>
/**
* Creates a new instance with the specified backing map.
*
* @param seed backing map to use to maintain Shiro objects.
*/
@SuppressWarnings({&quot;unchecked&quot;})
<span class="nc" id="L58"> public DefaultEnvironment(Map&lt;String, ?&gt; seed) {</span>
<span class="nc" id="L59"> this.securityManagerName = DEFAULT_SECURITY_MANAGER_KEY;</span>
<span class="nc bnc" id="L60" title="All 2 branches missed."> if (seed == null) {</span>
<span class="nc" id="L61"> throw new IllegalArgumentException(&quot;Backing map cannot be null.&quot;);</span>
}
<span class="nc" id="L63"> this.objects = (Map&lt;String, Object&gt;) seed;</span>
<span class="nc" id="L64"> }</span>
/**
* Returns the application's {@code SecurityManager} instance accessible in the backing map using the
* {@link #getSecurityManagerName() securityManagerName} property as the lookup key.
* &lt;p/&gt;
* This implementation guarantees that a non-null instance is always returned, as this is expected for
* Environment API end-users. If subclasses have the need to perform the map lookup without this guarantee
* (for example, during initialization when the instance may not have been added to the map yet), the
* {@link #lookupSecurityManager()} method is provided as an alternative.
*
* @return the application's {@code SecurityManager} instance accessible in the backing map using the
* {@link #getSecurityManagerName() securityManagerName} property as the lookup key.
*/
public SecurityManager getSecurityManager() throws IllegalStateException {
<span class="nc" id="L79"> SecurityManager securityManager = lookupSecurityManager();</span>
<span class="nc bnc" id="L80" title="All 2 branches missed."> if (securityManager == null) {</span>
<span class="nc" id="L81"> throw new IllegalStateException(&quot;No SecurityManager found in Environment. This is an invalid &quot; +</span>
&quot;environment state.&quot;);
}
<span class="nc" id="L84"> return securityManager;</span>
}
public void setSecurityManager(SecurityManager securityManager) {
<span class="nc bnc" id="L88" title="All 2 branches missed."> if (securityManager == null) {</span>
<span class="nc" id="L89"> throw new IllegalArgumentException(&quot;Null SecurityManager instances are not allowed.&quot;);</span>
}
<span class="nc" id="L91"> String name = getSecurityManagerName();</span>
<span class="nc" id="L92"> setObject(name, securityManager);</span>
<span class="nc" id="L93"> }</span>
/**
* Looks up the {@code SecurityManager} instance in the backing map without performing any non-null guarantees.
*
* @return the {@code SecurityManager} in the backing map, or {@code null} if it has not yet been populated.
*/
protected SecurityManager lookupSecurityManager() {
<span class="nc" id="L101"> String name = getSecurityManagerName();</span>
<span class="nc" id="L102"> return getObject(name, SecurityManager.class);</span>
}
/**
* Returns the name of the {@link SecurityManager} instance in the backing map. Used as a key to lookup the
* instance. Unless set otherwise, the default is {@code securityManager}.
*
* @return the name of the {@link SecurityManager} instance in the backing map. Used as a key to lookup the
* instance.
*/
public String getSecurityManagerName() {
<span class="nc" id="L113"> return securityManagerName;</span>
}
/**
* Sets the name of the {@link SecurityManager} instance in the backing map. Used as a key to lookup the
* instance. Unless set otherwise, the default is {@code securityManager}.
*
* @param securityManagerName the name of the {@link SecurityManager} instance in the backing map. Used as a key
* to lookup the instance. 
*/
public void setSecurityManagerName(String securityManagerName) {
<span class="nc" id="L124"> this.securityManagerName = securityManagerName;</span>
<span class="nc" id="L125"> }</span>
/**
* Returns the live (modifiable) internal objects collection.
*
* @return the live (modifiable) internal objects collection.
*/
public Map&lt;String,Object&gt; getObjects() {
<span class="nc" id="L133"> return this.objects;</span>
}
@SuppressWarnings({&quot;unchecked&quot;})
public &lt;T&gt; T getObject(String name, Class&lt;T&gt; requiredType) throws RequiredTypeException {
<span class="nc bnc" id="L138" title="All 2 branches missed."> if (name == null) {</span>
<span class="nc" id="L139"> throw new NullPointerException(&quot;name parameter cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L141" title="All 2 branches missed."> if (requiredType == null) {</span>
<span class="nc" id="L142"> throw new NullPointerException(&quot;requiredType parameter cannot be null.&quot;);</span>
}
<span class="nc" id="L144"> Object o = this.objects.get(name);</span>
<span class="nc bnc" id="L145" title="All 2 branches missed."> if (o == null) {</span>
<span class="nc" id="L146"> return null;</span>
}
<span class="nc bnc" id="L148" title="All 2 branches missed."> if (!requiredType.isInstance(o)) {</span>
<span class="nc" id="L149"> String msg = &quot;Object named '&quot; + name + &quot;' is not of required type [&quot; + requiredType.getName() + &quot;].&quot;;</span>
<span class="nc" id="L150"> throw new RequiredTypeException(msg);</span>
}
<span class="nc" id="L152"> return (T)o;</span>
}
public void setObject(String name, Object instance) {
<span class="nc bnc" id="L156" title="All 2 branches missed."> if (name == null) {</span>
<span class="nc" id="L157"> throw new NullPointerException(&quot;name parameter cannot be null.&quot;);</span>
}
<span class="nc bnc" id="L159" title="All 2 branches missed."> if (instance == null) {</span>
<span class="nc" id="L160"> this.objects.remove(name);</span>
} else {
<span class="nc" id="L162"> this.objects.put(name, instance);</span>
}
<span class="nc" id="L164"> }</span>
public void destroy() throws Exception {
<span class="nc" id="L168"> LifecycleUtils.destroy(this.objects.values());</span>
<span class="nc" id="L169"> }</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>