001: /*
002: * JOSSO: Java Open Single Sign-On
003: *
004: * Copyright 2004-2008, Atricore, Inc.
005: *
006: * This is free software; you can redistribute it and/or modify it
007: * under the terms of the GNU Lesser General Public License as
008: * published by the Free Software Foundation; either version 2.1 of
009: * the License, or (at your option) any later version.
010: *
011: * This software is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this software; if not, write to the Free
018: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
020: */
021:
022: package org.josso.jb42.agent;
023:
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.jboss.util.CachePolicy;
027:
028: import java.security.Principal;
029: import java.util.HashMap;
030:
031: /**
032: * Security Manager JBoss Cache Policy proxy.
033: * <p>
034: * Its used inside JBoss's JaasSecurityManager class to cache authenticated user entries.
035: * This class replaces, in the JaasSecurityManager, the default CachePolicy to allow
036: * handling cache entry lookups using SSO Session Identifier Principals as keys.
037: *
038: * @deprecated No longer needed for JBoss 3.2.6 .
039: * @author <a href="mailto:gbrigand@josso.org">Gianluca Brigandi</a>
040: * @version CVS $Id: SessionMappingCachePolicy.java 338 2006-02-09 16:53:07Z sgonzalez $
041: */
042:
043: public class SessionMappingCachePolicy implements CachePolicy {
044: private static final Log logger = LogFactory
045: .getLog(SessionMappingCachePolicy.class);
046:
047: /** HashMap<SSOUserPrincipal, JossoSessionIdPrincipal> */
048: private HashMap _userSessionMap = new HashMap();
049:
050: /** Proxyed Cache Policy */
051: private CachePolicy _cachePolicy;
052:
053: /**
054: * Constructs a SessionMappingCachePolicy instance which acts as a proxy
055: * of the supplied CachePolicy instance.
056: *
057: * @param cachePolicy the cache policy to be proxyed
058: */
059: public SessionMappingCachePolicy(CachePolicy cachePolicy) {
060: _cachePolicy = cachePolicy;
061: }
062:
063: /**
064: * Used to associate a Session Principal with a SSOUser Principal.
065: * This method is invoked by the JBossCatalinaRealm on successful
066: * authentication against the SecurityManager.
067: * Everytime an entry is requested given a user Principal key, before
068: * calling the proxyed CachePolicy, it will map such key to the
069: * session key used for storing cache entries.
070: *
071: * @param session
072: * @param user
073: */
074: public void attachSessionToUser(Principal session, Principal user) {
075: _userSessionMap.put(user, session);
076: }
077:
078: /**
079: * Returns the object paired with the specified key if it's
080: * present in the cache, otherwise must return null. <br>
081: * If the supplied key is a user session principal, it will be
082: * mapped to the associated user.
083: *
084: * @param key the key paired with the object
085: * @see #peek
086: */
087: public Object get(Object key) {
088: Object targetKey = key;
089:
090: logger.trace("Get, Entries = " + _cachePolicy.size());
091:
092: synchronized (this ) {
093: if (_userSessionMap.containsKey(key)) {
094: targetKey = _userSessionMap.get(key);
095:
096: logger.trace("Get, mapped user principal '" + key + "'"
097: + " to session principal '" + targetKey + "'");
098: }
099: }
100:
101: return _cachePolicy.get(targetKey);
102: }
103:
104: /**
105: * Returns the object paired with the specified key if it's
106: * present in the cache, otherwise must return null. <br>
107: * If the supplied key is a user session principal, it will be
108: * mapped to the associated user.
109: *
110: * @param key the key paired with the object
111: * @see #get
112: */
113: public Object peek(Object key) {
114: Object targetKey = key;
115:
116: logger.trace("Peek, Entries = " + _cachePolicy.size());
117:
118: synchronized (this ) {
119: if (_userSessionMap.containsKey(key)) {
120: targetKey = _userSessionMap.get(key);
121:
122: logger.trace("Peek, mapped user principal '" + key
123: + "'" + " to session principal '" + targetKey
124: + "'");
125: }
126: }
127:
128: return _cachePolicy.peek(targetKey);
129: }
130:
131: /**
132: * Inserts the specified object into the cache following the
133: * implemented policy. <br>
134: *
135: * @param key the key paired with the object
136: * @param object the object to cache
137: * @see #remove
138: */
139: public void insert(Object key, Object object) {
140: logger.trace("Insert, key = " + key + ", object = " + object
141: + " Entries = " + _cachePolicy.size());
142:
143: _cachePolicy.insert(key, object);
144: }
145:
146: /**
147: * Remove the cached object paired with the specified key. <br>
148: * In case the supplied key is a user principal mapped to a session principal, it will
149: * be removed.
150: *
151: * @param key the key paired with the object
152: * @see #insert
153: */
154: public void remove(Object key) {
155: logger.trace("Remove, key = " + key + ", Entries = "
156: + _cachePolicy.size());
157:
158: synchronized (this ) {
159: if (_userSessionMap.containsKey(key)) {
160: _userSessionMap.remove(key);
161: logger
162: .trace("Remove, removed session mapping for user '"
163: + key + "'");
164: }
165: }
166:
167: _cachePolicy.remove(key);
168: }
169:
170: /**
171: * Flushes the cached objects from the cache.<br>
172: * All user-to-session mapping are removed as well.
173: */
174: public void flush() {
175: logger.trace("Flush Entries = " + _cachePolicy.size());
176:
177: _userSessionMap.clear();
178: _cachePolicy.flush();
179: }
180:
181: /**
182: * Get the size of the cache.
183: */
184: public int size() {
185: return _cachePolicy.size();
186: }
187:
188: /**
189: * create the service, do expensive operations etc
190: */
191: public void create() throws Exception {
192: _cachePolicy.create();
193: }
194:
195: /**
196: * start the service, create is already called
197: */
198: public void start() throws Exception {
199: _cachePolicy.start();
200: }
201:
202: /**
203: * stop the service
204: */
205: public void stop() {
206: _cachePolicy.stop();
207: }
208:
209: /**
210: * destroy the service, tear down
211: */
212: public void destroy() {
213: _cachePolicy.destroy();
214: }
215: }
|