001: /* Copyright 2001, 2004 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;
007:
008: import java.util.ArrayList;
009: import java.util.HashSet;
010: import java.util.Hashtable;
011: import java.util.Iterator;
012: import java.util.Map;
013: import java.util.Set;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.security.IPerson;
018: import org.jasig.portal.security.PersonFactory;
019: import org.jasig.portal.security.provider.RestrictedPerson;
020: import org.jasig.portal.services.persondir.IPersonAttributeDao;
021: import org.jasig.portal.spring.PortalApplicationContextFacade;
022: import org.springframework.beans.factory.BeanFactory;
023:
024: /**
025: * PersonDirectory is a static lookup mechanism for a singleton instance of
026: * IPersonAttributeDao. It is configurable via a
027: * Spring beans.dtd compliant configuration file in the properties directory
028: * called personDirectory.xml (as referenced by applicationContext.xml -
029: * that is, you could choose to declare the underlying IPersonAttributesDao
030: * backing your PersonDirectory directly in applicationContext.xml,
031: * or elsewhere. PersonDirectory looks for an IPersonAttributeDao instance
032: * named 'personAttributeDao'.
033: *
034: * This class serves as the lookup mechanism for clients to obtain a reference
035: * to the singleton IPersonAttributeDao instance. Via legacy methods,
036: * PersonDirectory also serves as the interface by which client
037: * code accesses user attributes. These deprecated legacy methods are a facade
038: * to the PersonAttributeDao.
039: *
040: * The default configuration of that file implements the legacy behavior of using
041: * the PersonDirs.xml file for configuration. It is expected that PersonDirs.xml
042: * offers the flexibility necessary to support most uPortal installations.
043: *
044: * @author Howard Gilbert
045: * @author andrew.petro@yale.edu
046: * @author Eric Dalquist <a href="mailto:edalquist@unicon.net">edalquist@unicon.net</a>
047: * @version $Revision: 35513 $ $Date: 2005-04-06 08:19:32 -0700 (Wed, 06 Apr 2005) $
048: */
049: public class PersonDirectory {
050:
051: private static final String PADAO_BEAN_NAME = "personAttributeDao";
052: private static final Log log = LogFactory
053: .getLog(PersonDirectory.class);
054:
055: /**
056: * This instance variable used to contain the set of attributes mapped in
057: * PersonDir.xml. It now is merely an empty Set. It is no longer used by
058: * PersonDirectory and should be removed in a future release.
059: *
060: * @deprecated you cannot get the list of attributes in the abstract, only
061: * for a particular user.
062: */
063: public static HashSet propertynames = new HashSet();
064:
065: /** Singleton reference to PersonDirectory */
066: private static PersonDirectory instance;
067:
068: /** Wrapped class which provides the functionality */
069: private IPersonAttributeDao impl;
070:
071: /**
072: * Private constructor to allow for singleton behavior.
073: *
074: * @param impl The {@link IPersonAttributeDao} instance to wrap.
075: */
076: private PersonDirectory(IPersonAttributeDao impl) {
077: this .impl = impl;
078: }
079:
080: /**
081: * Static lookup for a the configured {@link IPersonAttributeDao}
082: * implementation available from PortalApplicationContextFacade.
083: *
084: * @return The PortalApplicationContextFacade configured {@link IPersonAttributeDao} implementation.
085: * @throws IllegalStateException - if PortalApplicationContextFacade does not
086: * supply the IPersonAttributeDao instance.
087: */
088: public static IPersonAttributeDao getPersonAttributeDao() {
089: final BeanFactory factory = PortalApplicationContextFacade
090: .getPortalApplicationContext();
091:
092: final IPersonAttributeDao delegate = (IPersonAttributeDao) factory
093: .getBean(PADAO_BEAN_NAME, IPersonAttributeDao.class);
094:
095: if (delegate == null)
096: throw new IllegalStateException(
097: "PortalAppicationContextFacade "
098: + "config did not declare a bean named '"
099: + PADAO_BEAN_NAME + "'.");
100:
101: return delegate;
102: }
103:
104: /**
105: * Obtain the singleton instance of PersonDirectory.
106: *
107: * @return the singleton instance of PersonDirectory.
108: * @deprecated Use {@link #getPersonAttributeDao()}
109: */
110: public static synchronized PersonDirectory instance() {
111: if (instance == null) {
112: try {
113: instance = new PersonDirectory(getPersonAttributeDao());
114: } catch (Throwable t) {
115: log.error("Error instantiating PersonDirectory", t);
116: }
117: }
118:
119: return instance;
120: }
121:
122: /**
123: * This method returns an iterator over the names of attributes. The
124: * method behavior is not well defined because attribute sources may choose
125: * to return different attributes depending upon about whom they are asked.
126: * Therefore, you can only know the attributes for particular users, not the
127: * namespace of all possible attributes.
128: *
129: * @return an iterator over the attribute names declared by the underlying
130: * IPersonAttributeDao instance, if any.
131: * @deprecated Use {@link IPersonAttributeDao#getPossibleUserAttributeNames()}
132: */
133: public static Iterator getPropertyNamesIterator() {
134: final Set attrNames = getPersonAttributeDao()
135: .getPossibleUserAttributeNames();
136:
137: if (attrNames != null)
138: return attrNames.iterator();
139: else
140: return (new ArrayList()).iterator();
141: }
142:
143: /**
144: * Returns a reference to a restricted IPerson represented by the supplied
145: * user ID. The restricted IPerson allows access to person attributes, but
146: * not the security context.
147: *
148: * @param uid the user ID
149: * @return the corresponding person, restricted so that its security context is inaccessible
150: * @deprecated Use {@link PersonFactory#createRestrictedPerson()} and
151: * {@link IPersonAttributeDao#getUserAttributes(String)} and
152: * {@link RestrictedPerson#setAttributes(Map)}
153: */
154: public static RestrictedPerson getRestrictedPerson(final String uid) {
155: final RestrictedPerson rp = PersonFactory
156: .createRestrictedPerson();
157: final Map attributes = instance().impl.getUserAttributes(uid);
158:
159: rp.setAttributes(attributes);
160:
161: return rp;
162: }
163:
164: /**
165: * Obtain a HashTable of attributes for the given user.
166: *
167: * @param username the name of the user
168: * @return a Hashtable from user names to attributes.
169: * @deprecated Use {@link IPersonAttributeDao#getUserAttributes(String)}
170: */
171: public Hashtable getUserDirectoryInformation(String username) {
172: final Map attrs = this .impl.getUserAttributes(username);
173: return new Hashtable(attrs);
174: }
175:
176: /**
177: * Populate an IPerson with the attributes from the user directory for the
178: * given uid.
179: *
180: * @param uid person for whom we are obtaining attributes
181: * @param person person object into which to store the attributes
182: * @deprecated Use {@link IPersonAttributeDao#getUserAttributes(String)} and
183: * {@link IPerson#setAttributes(Map)}
184: */
185: public void getUserDirectoryInformation(String uid, IPerson person) {
186: final Map attrs = this.impl.getUserAttributes(uid);
187: person.setAttributes(attrs);
188: }
189: }
|