001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.community.mc.test.impl.file.dynamic;
014:
015: import com.sun.portal.community.mc.CMCException;
016: import com.sun.portal.community.mc.CMCPrincipal;
017: import com.sun.portal.community.mc.CMCUser;
018: import com.sun.portal.community.mc.ConfigTable;
019: import com.sun.portal.community.mc.ConfigTable.ConfigKey;
020: import com.sun.portal.community.mc.CMCRolePrincipal;
021: import com.sun.portal.community.mc.impl.Debug;
022: import java.io.File;
023: import java.util.HashSet;
024: import java.util.Iterator;
025:
026: import java.util.Properties;
027: import java.util.Set;
028: import java.util.Collections;
029:
030: public class CMCUserImpl extends CMCImpl implements CMCUser {
031: private String userId = null;
032:
033: public static String getStaticType() {
034: return "filedynamic";
035: }
036:
037: public String getType() {
038: return getStaticType();
039: }
040:
041: public void init(Properties p, String userId) throws CMCException {
042: this .userId = userId;
043: super .init(p);
044: }
045:
046: public String getUserId() {
047: return userId;
048: }
049:
050: public Set getMembership() throws CMCException {
051: return getMembership(CMCRolePrincipal.ALL_ROLES);
052: }
053:
054: public Set getMembership(Set rolePrincipals) throws CMCException {
055: Set byName = getMembershipByName(rolePrincipals);
056: //System.out.println("byName=" + byName);
057: Set byRole = getMembershipByRole(rolePrincipals);
058: //System.out.println("byRole=" + byRole);
059:
060: Set all = byName;
061: all.addAll(byRole);
062:
063: return all;
064: }
065:
066: public Set getMembershipByName() throws CMCException {
067: return getMembershipByName(CMCRolePrincipal.ALL_ROLES);
068: }
069:
070: public Set getMembershipByName(Set rolePrincipals)
071: throws CMCException {
072: Set principals = new HashSet();
073: File[] files = new File(getBaseDir())
074: .listFiles(propertiesFilter);
075: for (int i = 0; i < files.length; i++) {
076: Properties p = getCommunityProperties(files[i]);
077: // get roles for user
078: Set roles = getRolesFromProperties(p,
079: getUserKey(getUserId()));
080: // filter out unspecified roles
081: roles.retainAll(rolePrincipals);
082:
083: for (Iterator j = roles.iterator(); j.hasNext();) {
084: CMCPrincipal cp = getCommunityPrincipalFromFile(files[i]);
085: CMCRolePrincipal rp = (CMCRolePrincipal) j.next();
086: ConfigKey ck = new ConfigKey(cp, rp);
087: principals.add(ck);
088: }
089: }
090:
091: return principals;
092: }
093:
094: public Set getMembershipByRole() throws CMCException {
095: return getMembershipByRole(CMCRolePrincipal.ALL_ROLES);
096: }
097:
098: public Set getMembershipByRole(Set rolePrincipals)
099: throws CMCException {
100: Set principals = new HashSet();
101: File[] files = new File(getBaseDir())
102: .listFiles(propertiesFilter);
103: for (int i = 0; i < files.length; i++) {
104: Properties p = getCommunityProperties(files[i]);
105: // get roles for user
106: Set roles = getRolesFromProperties(p, getRoleKey());
107: // filter out unspecified roles
108: roles.retainAll(rolePrincipals);
109:
110: for (Iterator j = roles.iterator(); j.hasNext();) {
111: CMCPrincipal cp = getCommunityPrincipalFromFile(files[i]);
112: CMCRolePrincipal rp = (CMCRolePrincipal) j.next();
113: ConfigKey ck = new ConfigKey(cp, rp);
114: principals.add(ck);
115: }
116: }
117:
118: principals = filterMembership(principals, getMembershipByName());
119: return principals;
120: }
121:
122: private Set filterMembership(Set m1, Set m2) throws CMCException {
123: Set f = new HashSet();
124: for (Iterator i = m1.iterator(); i.hasNext();) {
125: ConfigKey ck1 = (ConfigKey) i.next();
126: for (Iterator j = m2.iterator(); j.hasNext();) {
127: ConfigKey ck2 = (ConfigKey) j.next();
128: if (ck1.getCommunityPrincipal().equals(
129: ck2.getCommunityPrincipal())) {
130: f.add(ck1);
131: }
132: }
133: }
134:
135: return f;
136: }
137:
138: public Set getAvailable() throws CMCException {
139: //
140: // 1. loop through all *.properties files
141: // 2. get names, chop off .properties. those are cty names
142: // 3. for each name, add type to construct a cty principal
143:
144: Set available = new HashSet();
145:
146: File[] files = new File(getBaseDir())
147: .listFiles(propertiesFilter);
148: for (int i = 0; i < files.length; i++) {
149: System.out.println("files[" + i + "]=" + files[i]);
150: CMCPrincipal cp = getCommunityPrincipalFromFile(files[i]);
151: available.add(cp);
152: }
153:
154: return available;
155:
156: }
157:
158: public boolean hasRole(CMCPrincipal cp, CMCRolePrincipal rp)
159: throws CMCException {
160: Properties p = getCommunityProperties(cp);
161: Set roles = getRolesFromProperties(p, getUserKey(getUserId()));
162: roles.addAll(getRolesFromProperties(p, getRoleKey()));
163: if (roles.contains(rp)) {
164: return true;
165: }
166:
167: return false;
168: }
169:
170: public ConfigTable getDPDocuments(ConfigTable lastReadTimes,
171: Set membership) throws CMCException {
172: ConfigTable ct = new ConfigTable();
173: for (Iterator i = membership.iterator(); i.hasNext();) {
174: ConfigKey ck = (ConfigKey) i.next();
175: File dpFile = getDPFile(ck);
176: if (dpFile.exists()
177: && dpFileModified(lastReadTimes, ck, dpFile)) {
178: byte[] dp = readDP(dpFile);
179: ct.put(ck, dp);
180: updateLastRead(lastReadTimes, ck);
181: }
182: }
183:
184: return ct;
185: }
186:
187: public ConfigTable getDPDocuments(ConfigTable lastReadTimes)
188: throws CMCException {
189: Set membership = getMembership();
190: return getDPDocuments(lastReadTimes, membership);
191: }
192:
193: public long getRoleCreationTime(CMCPrincipal cp,
194: CMCRolePrincipal rolePrincipal)
195: throws UnsupportedOperationException {
196: throw new UnsupportedOperationException(
197: "not supported: RoleCreationTime");
198: }
199:
200: }
|