| <?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 :: All (aggregate jar)</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.env</a> > <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 |
| * "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.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 = "securityManager"; |
| |
| protected final Map<String, Object> objects; |
| private String securityManagerName; |
| |
| /** |
| * Creates a new instance with a thread-safe {@link ConcurrentHashMap} backing map. |
| */ |
| public DefaultEnvironment() { |
| <span class="fc" id="L49"> this(new ConcurrentHashMap<String, Object>());</span> |
| <span class="fc" id="L50"> }</span> |
| |
| /** |
| * Creates a new instance with the specified backing map. |
| * |
| * @param seed backing map to use to maintain Shiro objects. |
| */ |
| @SuppressWarnings({"unchecked"}) |
| <span class="fc" id="L58"> public DefaultEnvironment(Map<String, ?> seed) {</span> |
| <span class="fc" id="L59"> this.securityManagerName = DEFAULT_SECURITY_MANAGER_KEY;</span> |
| <span class="pc bpc" id="L60" title="1 of 2 branches missed."> if (seed == null) {</span> |
| <span class="nc" id="L61"> throw new IllegalArgumentException("Backing map cannot be null.");</span> |
| } |
| <span class="fc" id="L63"> this.objects = (Map<String, Object>) seed;</span> |
| <span class="fc" 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. |
| * <p/> |
| * 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("No SecurityManager found in Environment. This is an invalid " +</span> |
| "environment state."); |
| } |
| <span class="nc" id="L84"> return securityManager;</span> |
| } |
| |
| public void setSecurityManager(SecurityManager securityManager) { |
| <span class="pc bpc" id="L88" title="1 of 2 branches missed."> if (securityManager == null) {</span> |
| <span class="nc" id="L89"> throw new IllegalArgumentException("Null SecurityManager instances are not allowed.");</span> |
| } |
| <span class="fc" id="L91"> String name = getSecurityManagerName();</span> |
| <span class="fc" id="L92"> setObject(name, securityManager);</span> |
| <span class="fc" 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="fc" 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<String,Object> getObjects() { |
| <span class="fc" id="L133"> return this.objects;</span> |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| public <T> T getObject(String name, Class<T> 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("name parameter cannot be null.");</span> |
| } |
| <span class="nc bnc" id="L141" title="All 2 branches missed."> if (requiredType == null) {</span> |
| <span class="nc" id="L142"> throw new NullPointerException("requiredType parameter cannot be null.");</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 = "Object named '" + name + "' is not of required type [" + requiredType.getName() + "].";</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="pc bpc" id="L156" title="1 of 2 branches missed."> if (name == null) {</span> |
| <span class="nc" id="L157"> throw new NullPointerException("name parameter cannot be null.");</span> |
| } |
| <span class="pc bpc" id="L159" title="1 of 2 branches missed."> if (instance == null) {</span> |
| <span class="nc" id="L160"> this.objects.remove(name);</span> |
| } else { |
| <span class="fc" id="L162"> this.objects.put(name, instance);</span> |
| } |
| <span class="fc" 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.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |