01: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.services.entityproperties;
07:
08: import java.util.Hashtable;
09: import java.util.Iterator;
10: import java.util.List;
11: import java.util.Map;
12:
13: import org.apache.commons.logging.Log;
14: import org.apache.commons.logging.LogFactory;
15: import org.jasig.portal.EntityIdentifier;
16: import org.jasig.portal.services.PersonDirectory;
17: import org.jasig.portal.services.persondir.IPersonAttributeDao;
18: import org.jasig.portal.utils.SoftHashMap;
19:
20: /**
21: * A finder implementation to provide IPerson properties derived from the
22: * PersonDirectory
23: *
24: * @author Alex Vigdor av317@columbia.edu
25: * @version $Revision: 35513 $
26: */
27: public class PersonDirPropertyFinder implements IEntityPropertyFinder {
28:
29: private static final Log log = LogFactory
30: .getLog(PersonDirPropertyFinder.class);
31:
32: private Class person = org.jasig.portal.security.IPerson.class;
33: private IPersonAttributeDao pa;
34: private SoftHashMap cache;
35:
36: public PersonDirPropertyFinder() {
37: pa = PersonDirectory.getPersonAttributeDao();
38: cache = new SoftHashMap(120);
39: }
40:
41: public String[] getPropertyNames(EntityIdentifier entityID) {
42: String[] r = new String[0];
43: if (entityID.getType().equals(person)) {
44: r = (String[]) getPropertiesHash(entityID).keySet()
45: .toArray(r);
46: }
47: return r;
48: }
49:
50: public String getProperty(EntityIdentifier entityID, String name) {
51: String r = null;
52: if (entityID.getType().equals(person)) {
53: Object o = getPropertiesHash(entityID).get(name);
54: if (o instanceof String) {
55: r = (String) o;
56: } else if (o instanceof List) {
57: StringBuffer sb = new StringBuffer();
58: List values = (List) o;
59: for (Iterator iter = values.iterator(); iter.hasNext();) {
60: Object value = (Object) iter.next();
61: sb.append(value.toString());
62: if (iter.hasNext()) {
63: sb.append(", ");
64: }
65: }
66: r = sb.toString();
67: }
68: }
69: return r;
70: }
71:
72: protected Hashtable getPropertiesHash(EntityIdentifier entityID) {
73: Map ht;
74: if ((ht = (Map) cache.get(entityID.getKey())) == null) {
75: ht = new Hashtable(0);
76: try {
77: ht = pa.getUserAttributes(entityID.getKey());
78: } catch (Exception e) {
79: log.error(
80: "Error getting properties hash for entityID ["
81: + entityID + "]", e);
82: }
83: cache.put(entityID.getKey(), ht);
84: }
85: return new Hashtable(ht);
86: }
87:
88: }
|