001: /**
002: * $Id: CMCNodeImpl.java,v 1.3 2007/01/26 03:47:56 portalbld Exp $
003: * Copyright 2005 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
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.CMCExistsException;
017: import com.sun.portal.community.mc.CMCNode;
018: import com.sun.portal.community.mc.CMCPrincipal;
019: import com.sun.portal.community.mc.ConfigTable;
020: import com.sun.portal.community.mc.CMCRolePrincipal;
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.*;
024: import java.util.regex.*;
025:
026: public class CMCNodeImpl extends CMCImpl implements CMCNode {
027: private CMCPrincipal communityPrincipal = null;
028:
029: public static String getStaticType() {
030: return "filedynamic";
031: }
032:
033: public String getType() {
034: return getStaticType();
035: }
036:
037: public void init(Properties p, CMCPrincipal cp) throws CMCException {
038: this .communityPrincipal = cp;
039: super .init(p);
040: }
041:
042: public CMCPrincipal getCMCPrincipal() {
043: return communityPrincipal;
044: }
045:
046: public Set getUsers() throws CMCException {
047: return getUsers(CMCRolePrincipal.ALL_ROLES);
048: }
049:
050: public Set getUsers(Set rolePrincipals) throws CMCException {
051: Set uids = new HashSet();
052: Properties p = getCommunityProperties(getCMCPrincipal());
053: for (Iterator i = p.keySet().iterator(); i.hasNext();) {
054: String key = (String) i.next();
055: if (!isUserKey(key)) {
056: continue;
057: }
058: String uid = stripKeyPrefix(key);
059: Set roles = getRolesFromProperties(p, key);
060: roles.retainAll(rolePrincipals);
061: if (roles.size() > 0) {
062: uids.add(uid);
063: }
064: }
065:
066: return uids;
067: }
068:
069: public Set getRoles() throws CMCException {
070: Properties p = getCommunityProperties(getCMCPrincipal());
071: Set roles = getRolesFromProperties(p, getRoleKey());
072:
073: return roles;
074: }
075:
076: public boolean supportsGetRoles() {
077: return true;
078: }
079:
080: public void addUsers(Set uids, CMCRolePrincipal rolePrincipal)
081: throws CMCException {
082: Properties p = getCommunityProperties(getCMCPrincipal());
083: for (Iterator i = uids.iterator(); i.hasNext();) {
084: String uid = (String) i.next();
085: String key = getUserKey(uid);
086: Set roles = getRolesFromProperties(p, key);
087: roles.add(rolePrincipal);
088: setRolesToProperties(p, key, roles);
089: }
090:
091: storeProperties(getCMCPrincipal(), p);
092: }
093:
094: public boolean supportsAddUsers() {
095: return true;
096: }
097:
098: public void addRole(CMCRolePrincipal rolePrincipal)
099: throws CMCException {
100: Properties p = getCommunityProperties(getCMCPrincipal());
101: Set roles = getRolesFromProperties(p, getRoleKey());
102: roles.add(rolePrincipal);
103: setRolesToProperties(p, getRoleKey(), roles);
104:
105: storeProperties(getCMCPrincipal(), p);
106: }
107:
108: public boolean supportsAddRole() {
109: return true;
110: }
111:
112: public void removeUsers(Set uids, Set rolePrincipals)
113: throws CMCException {
114: Properties p = getCommunityProperties(getCMCPrincipal());
115: for (Iterator i = uids.iterator(); i.hasNext();) {
116: String uid = (String) i.next();
117: String key = getUserKey(uid);
118:
119: Set roles = getRolesFromProperties(p, key);
120: roles.removeAll(rolePrincipals);
121:
122: setRolesToProperties(p, key, roles);
123: }
124:
125: storeProperties(getCMCPrincipal(), p);
126: }
127:
128: public boolean supportsRemoveUsers() {
129: return true;
130: }
131:
132: public void removeRole(CMCRolePrincipal rolePrincipal)
133: throws CMCException {
134: Properties p = getCommunityProperties(getCMCPrincipal());
135: Set roles = getRolesFromProperties(p, getRoleKey());
136: roles.remove(rolePrincipal);
137: setRolesToProperties(p, getRoleKey(), roles);
138:
139: storeProperties(getCMCPrincipal(), p);
140: }
141:
142: public boolean supportsRemoveRole() {
143: return true;
144: }
145:
146: public boolean supportsRole(CMCRolePrincipal rolePrincipal) {
147: if (rolePrincipal.equals(CMCRolePrincipal.VISITOR_ROLE)) {
148: return true;
149: }
150:
151: if (rolePrincipal.equals(CMCRolePrincipal.MEMBER_ROLE)) {
152: return true;
153: }
154:
155: if (rolePrincipal.equals(CMCRolePrincipal.OWNER_ROLE)) {
156: return true;
157: }
158:
159: return false;
160: }
161:
162: private File getFileFromCommunityPrincipal() {
163: return getFileFromCommunityPrincipal(getCMCPrincipal());
164: }
165:
166: public void create() throws CMCExistsException, CMCException {
167: File f = getFileFromCommunityPrincipal();
168: if (f.exists()) {
169: throw new CMCExistsException("exists: " + getCMCPrincipal());
170: }
171:
172: try {
173: if (!f.createNewFile()) {
174: throw new CMCException("could not create file: " + f);
175: }
176: } catch (IOException ioe) {
177: throw new CMCException(ioe);
178: }
179: }
180:
181: public boolean supportsCreate() {
182: return true;
183: }
184:
185: public void remove() throws CMCException {
186: File f = getFileFromCommunityPrincipal();
187: f.delete();
188: }
189:
190: public boolean supportsRemove() {
191: return true;
192: }
193:
194: public boolean exists() {
195: File f = getFileFromCommunityPrincipal();
196: return f.exists();
197: }
198:
199: private File getDPFile(CMCRolePrincipal rp) {
200: return getDPFile(getCMCPrincipal(), rp);
201: }
202:
203: public ConfigTable getDPDocuments() throws CMCException {
204: return getDPDocuments(CMCRolePrincipal.ALL_ROLES);
205: }
206:
207: public ConfigTable getDPDocumentsLastModified() throws CMCException {
208: return getDPDocumentsLastModified(CMCRolePrincipal.ALL_ROLES);
209: }
210:
211: public ConfigTable getDPDocuments(Set rolePrincipals)
212: throws CMCException {
213: ConfigTable dpDocs = new ConfigTable();
214:
215: for (Iterator i = rolePrincipals.iterator(); i.hasNext();) {
216: CMCRolePrincipal rp = (CMCRolePrincipal) i.next();
217: File dpFile = getDPFile(rp);
218: if (dpFile.exists()) {
219: byte[] dp = readDP(dpFile);
220: ConfigTable.ConfigKey ck = new ConfigTable.ConfigKey(
221: getCMCPrincipal(), rp);
222: dpDocs.put(ck, dp);
223: }
224: }
225:
226: return dpDocs;
227: }
228:
229: public ConfigTable getDPDocumentsLastModified(Set rolePrincipals)
230: throws CMCException {
231: ConfigTable dpDocs = new ConfigTable();
232:
233: for (Iterator i = rolePrincipals.iterator(); i.hasNext();) {
234: CMCRolePrincipal rp = (CMCRolePrincipal) i.next();
235: File dpFile = getDPFile(rp);
236: if (dpFile.exists()) {
237: ConfigTable.ConfigKey ck = new ConfigTable.ConfigKey(
238: getCMCPrincipal(), rp);
239: dpDocs.put(ck, new Long(dpFile.lastModified()));
240: }
241: }
242:
243: return dpDocs;
244: }
245:
246: public void setDPDocuments(ConfigTable dpDocuments)
247: throws CMCException {
248: for (Iterator i = dpDocuments.getConfigKeys().iterator(); i
249: .hasNext();) {
250: ConfigTable.ConfigKey ck = (ConfigTable.ConfigKey) i.next();
251: if (!ck.getCommunityPrincipal().equals(getCMCPrincipal())) {
252: //
253: // don't set if it doesn't match the passed principal
254: //
255: continue;
256: }
257: String dp = (String) dpDocuments.get(ck);
258: if (dp == null) {
259: //
260: // write an empty DP in place of a null
261: // this could be an attempt to clear the DP
262: // so passing null is valid
263: //
264: dp = "";
265: }
266:
267: File dpFile = getDPFile(ck);
268: writeDP(dpFile, dp);
269: }
270: }
271:
272: public boolean supportsSetDPDocuments() {
273: return true;
274: }
275:
276: public String getDescription() throws CMCException {
277: throw new UnsupportedOperationException(
278: "not supported: getDescription");
279: }
280:
281: public void setDescription(String description) throws CMCException {
282: throw new UnsupportedOperationException(
283: "not supported: setDescription");
284: }
285:
286: public boolean supportsDescription() throws CMCException {
287: return false;
288: }
289:
290: public String getCategory() throws CMCException {
291: throw new UnsupportedOperationException(
292: "not supported: getCategory");
293: }
294:
295: public void setCategory(String category) throws CMCException {
296: throw new UnsupportedOperationException(
297: "not supported: setCategory");
298: }
299:
300: public boolean supportsCategory() throws CMCException {
301: return false;
302: }
303:
304: public long getCommunityCreationTime() throws CMCException {
305: throw new UnsupportedOperationException(
306: "not supported: getCommunityCreationTime");
307: }
308:
309: public boolean supportsCommunityCreationTime() throws CMCException {
310: return false;
311: }
312:
313: public boolean isMembershipRestricted() throws CMCException {
314: throw new UnsupportedOperationException(
315: "not supported: isMembershipRestricted");
316: }
317:
318: public void setMembershipRestricted(boolean isMembershipRestricted)
319: throws CMCException {
320: throw new UnsupportedOperationException(
321: "not supported: setMembershipRestricted");
322: }
323:
324: public boolean supportsMembershipRestriction() throws CMCException {
325: return false;
326: }
327:
328: public boolean isSecure() throws CMCException {
329: throw new UnsupportedOperationException(
330: "not supported: isSecure");
331: }
332:
333: public void setSecure(boolean isSecure) throws CMCException {
334: throw new UnsupportedOperationException(
335: "not supported: setSecure");
336: }
337:
338: public boolean supportsSecuring() throws CMCException {
339: return false;
340: }
341:
342: public boolean isListed() throws CMCException {
343: throw new UnsupportedOperationException(
344: "not supported: isListed");
345: }
346:
347: public void setListed(boolean isListed) throws CMCException {
348: throw new UnsupportedOperationException(
349: "not supported: setListed");
350: }
351:
352: public boolean supportsListing() throws CMCException {
353: return false;
354: }
355:
356: public long getRoleCreationTime(String uid,
357: CMCRolePrincipal rolePrincipal) throws CMCException {
358: throw new UnsupportedOperationException(
359: "not supported: getRoleCreationTime");
360: }
361:
362: public boolean supportsRoleCreationTime() throws CMCException {
363: return false;
364: }
365:
366: }
|