1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
30
31
32
33
34 public class DefaultEnvironment implements NamedObjectEnvironment, Destroyable {
35
36
37
38
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
47
48 public DefaultEnvironment() {
49 this(new ConcurrentHashMap<String, Object>());
50 }
51
52
53
54
55
56
57 @SuppressWarnings({"unchecked"})
58 public DefaultEnvironment(Map<String, ?> seed) {
59 this.securityManagerName = DEFAULT_SECURITY_MANAGER_KEY;
60 if (seed == null) {
61 throw new IllegalArgumentException("Backing map cannot be null.");
62 }
63 this.objects = (Map<String, Object>) seed;
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public SecurityManager getSecurityManager() throws IllegalStateException {
79 SecurityManager securityManager = lookupSecurityManager();
80 if (securityManager == null) {
81 throw new IllegalStateException("No SecurityManager found in Environment. This is an invalid " +
82 "environment state.");
83 }
84 return securityManager;
85 }
86
87 public void setSecurityManager(SecurityManager securityManager) {
88 if (securityManager == null) {
89 throw new IllegalArgumentException("Null SecurityManager instances are not allowed.");
90 }
91 String name = getSecurityManagerName();
92 setObject(name, securityManager);
93 }
94
95
96
97
98
99
100 protected SecurityManager lookupSecurityManager() {
101 String name = getSecurityManagerName();
102 return getObject(name, SecurityManager.class);
103 }
104
105
106
107
108
109
110
111
112 public String getSecurityManagerName() {
113 return securityManagerName;
114 }
115
116
117
118
119
120
121
122
123 public void setSecurityManagerName(String securityManagerName) {
124 this.securityManagerName = securityManagerName;
125 }
126
127
128
129
130
131
132 public Map<String,Object> getObjects() {
133 return this.objects;
134 }
135
136 @SuppressWarnings({"unchecked"})
137 public <T> T getObject(String name, Class<T> requiredType) throws RequiredTypeException {
138 if (name == null) {
139 throw new NullPointerException("name parameter cannot be null.");
140 }
141 if (requiredType == null) {
142 throw new NullPointerException("requiredType parameter cannot be null.");
143 }
144 Object o = this.objects.get(name);
145 if (o == null) {
146 return null;
147 }
148 if (!requiredType.isInstance(o)) {
149 String msg = "Object named '" + name + "' is not of required type [" + requiredType.getName() + "].";
150 throw new RequiredTypeException(msg);
151 }
152 return (T)o;
153 }
154
155 public void setObject(String name, Object instance) {
156 if (name == null) {
157 throw new NullPointerException("name parameter cannot be null.");
158 }
159 if (instance == null) {
160 this.objects.remove(name);
161 } else {
162 this.objects.put(name, instance);
163 }
164 }
165
166
167 public void destroy() throws Exception {
168 LifecycleUtils.destroy(this.objects.values());
169 }
170 }