Coverage Report - org.apache.shiro.env.DefaultEnvironment
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultEnvironment
0%
0/42
0%
0/18
2.545
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.shiro.env;
 20  
 
 21  
 import org.apache.shiro.mgt.SecurityManager;
 22  
 import org.apache.shiro.util.Destroyable;
 23  
 import org.apache.shiro.util.LifecycleUtils;
 24  
 
 25  
 import java.util.Map;
 26  
 import java.util.concurrent.ConcurrentHashMap;
 27  
 
 28  
 /**
 29  
  * Simple/default {@code Environment} implementation that stores Shiro objects as key-value pairs in a
 30  
  * {@link java.util.Map Map} instance.  The key is the object name, the value is the object itself.
 31  
  *
 32  
  * @since 1.2
 33  
  */
 34  
 public class DefaultEnvironment implements NamedObjectEnvironment, Destroyable {
 35  
 
 36  
     /**
 37  
      * The default name under which the application's {@code SecurityManager} instance may be acquired, equal to
 38  
      * {@code securityManager}.
 39  
      */
 40  
     public static final String DEFAULT_SECURITY_MANAGER_KEY = "securityManager";
 41  
 
 42  
     protected final Map<String, Object> objects;
 43  
     private String securityManagerName;
 44  
 
 45  
     /**
 46  
      * Creates a new instance with a thread-safe {@link ConcurrentHashMap} backing map.
 47  
      */
 48  
     public DefaultEnvironment() {
 49  0
         this(new ConcurrentHashMap<String, Object>());
 50  0
     }
 51  
 
 52  
     /**
 53  
      * Creates a new instance with the specified backing map.
 54  
      *
 55  
      * @param seed backing map to use to maintain Shiro objects.
 56  
      */
 57  
     @SuppressWarnings({"unchecked"})
 58  0
     public DefaultEnvironment(Map<String, ?> seed) {
 59  0
         this.securityManagerName = DEFAULT_SECURITY_MANAGER_KEY;
 60  0
         if (seed == null) {
 61  0
             throw new IllegalArgumentException("Backing map cannot be null.");
 62  
         }
 63  0
         this.objects = (Map<String, Object>) seed;
 64  0
     }
 65  
 
 66  
     /**
 67  
      * Returns the application's {@code SecurityManager} instance accessible in the backing map using the
 68  
      * {@link #getSecurityManagerName() securityManagerName} property as the lookup key.
 69  
      * <p/>
 70  
      * This implementation guarantees that a non-null instance is always returned, as this is expected for
 71  
      * Environment API end-users.  If subclasses have the need to perform the map lookup without this guarantee
 72  
      * (for example, during initialization when the instance may not have been added to the map yet), the
 73  
      * {@link #lookupSecurityManager()} method is provided as an alternative.
 74  
      *
 75  
      * @return the application's {@code SecurityManager} instance accessible in the backing map using the
 76  
      *         {@link #getSecurityManagerName() securityManagerName} property as the lookup key.
 77  
      */
 78  
     public SecurityManager getSecurityManager() throws IllegalStateException {
 79  0
         SecurityManager securityManager = lookupSecurityManager();
 80  0
         if (securityManager == null) {
 81  0
             throw new IllegalStateException("No SecurityManager found in Environment.  This is an invalid " +
 82  
                     "environment state.");
 83  
         }
 84  0
         return securityManager;
 85  
     }
 86  
 
 87  
     public void setSecurityManager(SecurityManager securityManager) {
 88  0
         if (securityManager == null) {
 89  0
             throw new IllegalArgumentException("Null SecurityManager instances are not allowed.");
 90  
         }
 91  0
         String name = getSecurityManagerName();
 92  0
         setObject(name, securityManager);
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Looks up the {@code SecurityManager} instance in the backing map without performing any non-null guarantees.
 97  
      *
 98  
      * @return the {@code SecurityManager} in the backing map, or {@code null} if it has not yet been populated.
 99  
      */
 100  
     protected SecurityManager lookupSecurityManager() {
 101  0
         String name = getSecurityManagerName();
 102  0
         return getObject(name, SecurityManager.class);
 103  
     }
 104  
 
 105  
     /**
 106  
      * Returns the name of the {@link SecurityManager} instance in the backing map.  Used as a key to lookup the
 107  
      * instance.  Unless set otherwise, the default is {@code securityManager}.
 108  
      *
 109  
      * @return the name of the {@link SecurityManager} instance in the backing map.  Used as a key to lookup the
 110  
      *         instance.
 111  
      */
 112  
     public String getSecurityManagerName() {
 113  0
         return securityManagerName;
 114  
     }
 115  
 
 116  
     /**
 117  
      * Sets the name of the {@link SecurityManager} instance in the backing map.  Used as a key to lookup the
 118  
      * instance.  Unless set otherwise, the default is {@code securityManager}.
 119  
      *
 120  
      * @param securityManagerName the name of the {@link SecurityManager} instance in the backing map.  Used as a key
 121  
      *                            to lookup the instance. 
 122  
      */
 123  
     public void setSecurityManagerName(String securityManagerName) {
 124  0
         this.securityManagerName = securityManagerName;
 125  0
     }
 126  
 
 127  
     /**
 128  
      * Returns the live (modifiable) internal objects collection.
 129  
      *
 130  
      * @return the live (modifiable) internal objects collection.
 131  
      */
 132  
     public Map<String,Object> getObjects() {
 133  0
         return this.objects;
 134  
     }
 135  
 
 136  
     @SuppressWarnings({"unchecked"})
 137  
     public <T> T getObject(String name, Class<T> requiredType) throws RequiredTypeException {
 138  0
         if (name == null) {
 139  0
             throw new NullPointerException("name parameter cannot be null.");
 140  
         }
 141  0
         if (requiredType == null) {
 142  0
             throw new NullPointerException("requiredType parameter cannot be null.");
 143  
         }
 144  0
         Object o = this.objects.get(name);
 145  0
         if (o == null) {
 146  0
             return null;
 147  
         }
 148  0
         if (!requiredType.isInstance(o)) {
 149  0
             String msg = "Object named '" + name + "' is not of required type [" + requiredType.getName() + "].";
 150  0
             throw new RequiredTypeException(msg);
 151  
         }
 152  0
         return (T)o;
 153  
     }
 154  
 
 155  
     public void setObject(String name, Object instance) {
 156  0
         if (name == null) {
 157  0
             throw new NullPointerException("name parameter cannot be null.");
 158  
         }
 159  0
         if (instance == null) {
 160  0
             this.objects.remove(name);
 161  
         } else {
 162  0
             this.objects.put(name, instance);
 163  
         }
 164  0
     }
 165  
 
 166  
 
 167  
     public void destroy() throws Exception {
 168  0
         LifecycleUtils.destroy(this.objects.values());
 169  0
     }
 170  
 }