Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RealmSecurityManager |
|
| 2.375;2.375 |
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.mgt; | |
20 | ||
21 | import org.apache.shiro.cache.CacheManager; | |
22 | import org.apache.shiro.cache.CacheManagerAware; | |
23 | import org.apache.shiro.realm.Realm; | |
24 | import org.apache.shiro.util.LifecycleUtils; | |
25 | ||
26 | import java.util.ArrayList; | |
27 | import java.util.Collection; | |
28 | ||
29 | ||
30 | /** | |
31 | * Shiro support of a {@link SecurityManager} class hierarchy based around a collection of | |
32 | * {@link org.apache.shiro.realm.Realm}s. All actual {@code SecurityManager} method implementations are left to | |
33 | * subclasses. | |
34 | * | |
35 | * @since 0.9 | |
36 | */ | |
37 | public abstract class RealmSecurityManager extends CachingSecurityManager { | |
38 | ||
39 | /** | |
40 | * Internal collection of <code>Realm</code>s used for all authentication and authorization operations. | |
41 | */ | |
42 | private Collection<Realm> realms; | |
43 | ||
44 | /** | |
45 | * Default no-arg constructor. | |
46 | */ | |
47 | public RealmSecurityManager() { | |
48 | 36 | super(); |
49 | 36 | } |
50 | ||
51 | /** | |
52 | * Convenience method for applications using a single realm that merely wraps the realm in a list and then invokes | |
53 | * the {@link #setRealms} method. | |
54 | * | |
55 | * @param realm the realm to set for a single-realm application. | |
56 | * @since 0.2 | |
57 | */ | |
58 | public void setRealm(Realm realm) { | |
59 | 12 | if (realm == null) { |
60 | 0 | throw new IllegalArgumentException("Realm argument cannot be null"); |
61 | } | |
62 | 12 | Collection<Realm> realms = new ArrayList<Realm>(1); |
63 | 12 | realms.add(realm); |
64 | 12 | setRealms(realms); |
65 | 12 | } |
66 | ||
67 | /** | |
68 | * Sets the realms managed by this <tt>SecurityManager</tt> instance. | |
69 | * | |
70 | * @param realms the realms managed by this <tt>SecurityManager</tt> instance. | |
71 | * @throws IllegalArgumentException if the realms collection is null or empty. | |
72 | */ | |
73 | public void setRealms(Collection<Realm> realms) { | |
74 | 32 | if (realms == null) { |
75 | 0 | throw new IllegalArgumentException("Realms collection argument cannot be null."); |
76 | } | |
77 | 32 | if (realms.isEmpty()) { |
78 | 0 | throw new IllegalArgumentException("Realms collection argument cannot be empty."); |
79 | } | |
80 | 32 | this.realms = realms; |
81 | 32 | afterRealmsSet(); |
82 | 32 | } |
83 | ||
84 | protected void afterRealmsSet() { | |
85 | 32 | applyCacheManagerToRealms(); |
86 | 32 | } |
87 | ||
88 | /** | |
89 | * Returns the {@link Realm Realm}s managed by this SecurityManager instance. | |
90 | * | |
91 | * @return the {@link Realm Realm}s managed by this SecurityManager instance. | |
92 | */ | |
93 | public Collection<Realm> getRealms() { | |
94 | 153 | return realms; |
95 | } | |
96 | ||
97 | /** | |
98 | * Sets the internal {@link #getCacheManager CacheManager} on any internal configured | |
99 | * {@link #getRealms Realms} that implement the {@link org.apache.shiro.cache.CacheManagerAware CacheManagerAware} interface. | |
100 | * <p/> | |
101 | * This method is called after setting a cacheManager on this securityManager via the | |
102 | * {@link #setCacheManager(org.apache.shiro.cache.CacheManager) setCacheManager} method to allow it to be propagated | |
103 | * down to all the internal Realms that would need to use it. | |
104 | * <p/> | |
105 | * It is also called after setting one or more realms via the {@link #setRealm setRealm} or | |
106 | * {@link #setRealms setRealms} methods to allow these newly available realms to be given the cache manager | |
107 | * already in use. | |
108 | */ | |
109 | protected void applyCacheManagerToRealms() { | |
110 | 34 | CacheManager cacheManager = getCacheManager(); |
111 | 34 | Collection<Realm> realms = getRealms(); |
112 | 34 | if (cacheManager != null && realms != null && !realms.isEmpty()) { |
113 | 2 | for (Realm realm : realms) { |
114 | 2 | if (realm instanceof CacheManagerAware) { |
115 | 2 | ((CacheManagerAware) realm).setCacheManager(cacheManager); |
116 | } | |
117 | 2 | } |
118 | } | |
119 | 34 | } |
120 | ||
121 | /** | |
122 | * Simply calls {@link #applyCacheManagerToRealms() applyCacheManagerToRealms()} to allow the | |
123 | * newly set {@link org.apache.shiro.cache.CacheManager CacheManager} to be propagated to the internal collection of <code>Realm</code> | |
124 | * that would need to use it. | |
125 | */ | |
126 | protected void afterCacheManagerSet() { | |
127 | 2 | applyCacheManagerToRealms(); |
128 | 2 | } |
129 | ||
130 | public void destroy() { | |
131 | 19 | LifecycleUtils.destroy(getRealms()); |
132 | 19 | this.realms = null; |
133 | 19 | super.destroy(); |
134 | 19 | } |
135 | ||
136 | } |