001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.services.persondir.support;
007:
008: import java.util.Collections;
009: import java.util.HashSet;
010: import java.util.Iterator;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import org.jasig.portal.services.persondir.IPersonAttributeDao;
015:
016: /**
017: * Implements IPersonAttributeDao by looking up the specified user in a configured
018: * Map. The configured Map is a Map from String userids to Maps. The Map
019: * values are Maps from String user attribute names to user attribute values.
020: * @version $Revision: 35796 $ $Date: 2005-05-16 10:19:41 -0700 (Mon, 16 May 2005) $
021: */
022: public class ComplexStubPersonAttributeDao implements
023: IPersonAttributeDao {
024:
025: /**
026: * Map from userids to Maps. The Map values are maps from
027: * attribute names to attribute values.
028: */
029: private final Map backingMap;
030:
031: /**
032: * Lazily initialized Set of possible all attribute names that map to an attribute
033: * value for some user in our backing map.
034: */
035: private Set possibleAttributeNames = null;
036:
037: public ComplexStubPersonAttributeDao(Map backingMap) {
038: this .backingMap = Collections.unmodifiableMap(backingMap);
039: }
040:
041: public Set getPossibleUserAttributeNames() {
042: if (this .possibleAttributeNames == null) {
043: initializePossibleAttributeNames();
044: }
045: return this .possibleAttributeNames;
046: }
047:
048: /**
049: * Compute the set of attribute names that map to a value for at least one
050: * user in our backing map and store it as the instance variable
051: * possibleAttributeNames.
052: */
053: private synchronized void initializePossibleAttributeNames() {
054: // check again whether we need to run because the value may have
055: // been initialized after the logic check that decided to call this method
056: // but before the lock to enter this method was actually acquired.
057: if (this .possibleAttributeNames == null) {
058: /*
059: * Compute the union of the keysets of all the Sets that are values in our
060: * backing map.
061: */
062:
063: Set possibleAttribNames = new HashSet();
064:
065: for (Iterator iter = this .backingMap.values().iterator(); iter
066: .hasNext();) {
067: Map attributeMapForSomeUser = (Map) iter.next();
068: possibleAttribNames.addAll(attributeMapForSomeUser
069: .keySet());
070: }
071:
072: this .possibleAttributeNames = possibleAttribNames;
073: }
074: }
075:
076: public Map getUserAttributes(final Map seed) {
077: if (seed == null) {
078: throw new IllegalArgumentException(
079: "Illegal to invoke getUserAttributes(Map) with a null argument.");
080: }
081: return (Map) this .backingMap.get(seed.get("uid"));
082:
083: }
084:
085: public Map getUserAttributes(final String uid) {
086: if (uid == null) {
087: throw new IllegalArgumentException(
088: "Illegal to invoke getUserAttributes(String) with a null argument.");
089: }
090:
091: return (Map) this .backingMap.get(uid);
092: }
093:
094: /**
095: * Get the Map backing this ComplexStubPersonAttributeDao.
096: *
097: * @return Returns the backingMap.
098: */
099: public Map getBackingMap() {
100: return this.backingMap;
101: }
102:
103: }
|