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.impl.CMCProperties;
018: import com.sun.portal.community.mc.CMCUser;
019: import com.sun.portal.community.mc.ConfigTable;
020: import com.sun.portal.community.mc.CMCRolePrincipal;
021: import com.sun.portal.community.mc.impl.Debug;
022:
023: import java.io.BufferedReader;
024: import java.io.BufferedWriter;
025: import java.io.File;
026: import java.io.FileFilter;
027: import java.io.FileInputStream;
028: import java.io.FileNotFoundException;
029: import java.io.FileOutputStream;
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.io.OutputStream;
034: import java.io.OutputStreamWriter;
035: import java.util.*;
036: import java.util.regex.*;
037:
038: //
039: // all files are under base directory
040: // two files per community, format is:
041: //
042: // <principal name>.properties - community config + members
043: // <principal>-<role>.xml - DP XML config, one file per role
044: //
045: // .properties file format is:
046: //
047: // <uid>=role1|role2|...|roleN
048: //
049: // .xml files contain valid DP documents
050:
051: public class CMCImpl {
052: private static final String BASEDIR_KEY = "baseDir";
053:
054: private static final String USER_KEY_PREFIX = "user.";
055: private static final String ROLE_KEY = ".roles";
056:
057: private static final class PropertiesFileFilter implements
058: FileFilter {
059: public boolean accept(File f) {
060: if (f.toString().endsWith(".properties")) {
061: return true;
062: }
063:
064: return false;
065: }
066: }
067:
068: private static final class DPFileFilter implements FileFilter {
069: public boolean accept(File f) {
070: if (f.toString().endsWith(".xml")) {
071: return true;
072: }
073:
074: return false;
075: }
076: }
077:
078: protected static final FileFilter propertiesFilter = new PropertiesFileFilter();
079: protected static final FileFilter dpFilter = new DPFileFilter();
080:
081: private String baseDir = null;
082:
083: protected void init(Properties p) throws CMCException {
084: this .baseDir = CMCProperties.getTypeProperty(getType(),
085: BASEDIR_KEY);
086: if (baseDir == null || baseDir.length() == 0) {
087: throw new CMCException("base directory was not defined");
088: }
089: }
090:
091: protected String getBaseDir() {
092: return baseDir;
093: }
094:
095: protected String getType() {
096: return "filedynamic";
097: }
098:
099: protected static String stripKeyPrefix(String key) {
100: int i = key.indexOf(".");
101: return key.substring(i + 1);
102: }
103:
104: protected static boolean isUserKey(String key) {
105: return key.startsWith(USER_KEY_PREFIX);
106: }
107:
108: protected static boolean isRoleKey(String key) {
109: return key.equals(ROLE_KEY);
110: }
111:
112: protected static String getUserKey(String uid) {
113: return USER_KEY_PREFIX + uid;
114: }
115:
116: protected static String getRoleKey() {
117: return ROLE_KEY;
118: }
119:
120: protected Properties[] getCommunitiesProperties()
121: throws CMCException {
122: File[] files = new File(baseDir).listFiles(propertiesFilter);
123: Properties[] props = new Properties[files.length];
124: for (int i = 0; i < files.length; i++) {
125: props[i] = loadProperties(files[i]);
126: }
127:
128: return props;
129: }
130:
131: protected Properties getCommunityProperties(CMCPrincipal cp)
132: throws CMCException {
133: File f = new File(baseDir + "/" + cp.getName() + ".properties");
134: return getCommunityProperties(f);
135: }
136:
137: protected Properties getCommunityProperties(File f)
138: throws CMCException {
139: return loadProperties(f);
140: }
141:
142: protected CMCPrincipal getCommunityPrincipalFromFile(File f)
143: throws CMCException {
144: String filename = f.getName();
145: int i = filename.lastIndexOf(".");
146: if (i == -1) {
147: throw new CMCException("was not a properties file: " + f);
148: }
149: String name = filename.substring(0, i);
150: CMCPrincipal cp = new CMCPrincipal(getType(), name);
151:
152: return cp;
153: }
154:
155: protected File getDPFile(CMCPrincipal cp, CMCRolePrincipal rp) {
156: File dpFile = new File(baseDir + "/" + cp.getName() + "-"
157: + rp.getName() + ".xml");
158: return dpFile;
159: }
160:
161: protected File getDPFile(ConfigTable.ConfigKey ck) {
162: return getDPFile(ck.getCommunityPrincipal(), ck
163: .getRolePrincipal());
164: }
165:
166: protected void updateLastRead(ConfigTable ct,
167: ConfigTable.ConfigKey ck) {
168: Long now = new Long(System.currentTimeMillis());
169: ct.put(ck, now);
170: }
171:
172: protected boolean dpFileModified(ConfigTable lastReadTimes,
173: ConfigTable.ConfigKey ck, File dpFile) {
174: if (lastReadTimes == null) {
175: return true;
176: }
177:
178: Long lastRead = (Long) lastReadTimes.get(ck);
179: if (lastRead == null || lastRead.longValue() == -1) {
180: return true;
181: }
182:
183: if (dpFile.lastModified() > lastRead.longValue()) {
184: return true;
185: }
186:
187: return false;
188: }
189:
190: protected Properties loadProperties(File f) throws CMCException {
191: InputStream is = null;
192: Properties p;
193:
194: try {
195: is = new FileInputStream(f);
196:
197: p = new Properties();
198: p.load(is);
199: } catch (FileNotFoundException fnfe) {
200: throw new CMCException(fnfe);
201: } catch (IOException ioe) {
202: throw new CMCException(ioe);
203: } finally {
204: try {
205: if (is != null) {
206: is.close();
207: }
208: } catch (IOException ioe) {
209: throw new CMCException(ioe);
210: }
211: }
212:
213: return p;
214: }
215:
216: protected File getFileFromCommunityPrincipal(CMCPrincipal cp) {
217: return new File(getBaseDir() + "/" + cp.getName()
218: + ".properties");
219: }
220:
221: protected void storeProperties(CMCPrincipal cp, Properties p)
222: throws CMCException {
223: File f = getFileFromCommunityPrincipal(cp);
224: storeProperties(f, p);
225: }
226:
227: protected void storeProperties(File f, Properties p)
228: throws CMCException {
229: OutputStream os = null;
230:
231: try {
232: os = new FileOutputStream(f);
233: p.store(os, "");
234: } catch (FileNotFoundException fnfe) {
235: throw new CMCException(fnfe);
236: } catch (IOException ioe) {
237: throw new CMCException(ioe);
238: } finally {
239: try {
240: if (os != null) {
241: os.close();
242: }
243: } catch (IOException ioe) {
244: throw new CMCException(ioe);
245: }
246: }
247: }
248:
249: protected byte[] readDP(File f) throws CMCException {
250: byte[] dp;
251: InputStream is = null;
252:
253: try {
254: is = new FileInputStream(f);
255: int n = is.available();
256: dp = new byte[n];
257: is.read(dp);
258: } catch (FileNotFoundException fnfe) {
259: throw new CMCException(fnfe);
260: } catch (IOException ioe) {
261: throw new CMCException(ioe);
262: } finally {
263: try {
264: if (is != null) {
265: is.close();
266: }
267: } catch (IOException ioe) {
268: throw new CMCException(ioe);
269: }
270: }
271:
272: return dp;
273: }
274:
275: protected void writeDP(File f, String dp) throws CMCException {
276: BufferedWriter bw = null;
277:
278: try {
279: OutputStream os = new FileOutputStream(f);
280: OutputStreamWriter osr = new OutputStreamWriter(os);
281: bw = new BufferedWriter(osr);
282:
283: bw.write(dp, 0, dp.length());
284: bw.close();
285: } catch (FileNotFoundException fnfe) {
286: throw new CMCException(fnfe);
287: } catch (IOException ioe) {
288: throw new CMCException(ioe);
289: } finally {
290: try {
291: if (bw != null) {
292: bw.close();
293: }
294: } catch (IOException ioe) {
295: throw new CMCException(ioe);
296: }
297: }
298: }
299:
300: protected Set getRolesFromProperties(Properties p, String key)
301: throws CMCException {
302: Set roles = new HashSet();
303:
304: String rs = p.getProperty(key);
305: if (rs == null) {
306: return roles;
307: }
308: StringTokenizer tokens = new StringTokenizer(rs, "|");
309: while (tokens.hasMoreTokens()) {
310: String r = tokens.nextToken();
311: CMCRolePrincipal rp = CMCRolePrincipal.valueOf(r);
312: roles.add(rp);
313: }
314:
315: return roles;
316: }
317:
318: protected void setRolesToProperties(Properties p, String uid,
319: Set roles) throws CMCException {
320: String rolesString = rolesToString(roles);
321: if (rolesString.length() == 0) {
322: p.remove(uid);
323: } else {
324: p.setProperty(uid, rolesString);
325: }
326: }
327:
328: private String rolesToString(Set roles) {
329: StringBuffer b = new StringBuffer();
330: for (Iterator i = roles.iterator(); i.hasNext();) {
331: String roleString = ((CMCRolePrincipal) i.next()).getName();
332: b.append(roleString);
333: if (i.hasNext()) {
334: b.append("|");
335: }
336: }
337:
338: return b.toString();
339: }
340:
341: protected Set filterPrincipals(Set principals) {
342: Set filtered = new HashSet();
343: for (Iterator i = principals.iterator(); i.hasNext();) {
344: CMCPrincipal cmcp = (CMCPrincipal) i.next();
345: if (!cmcp.getType().equals(getType())) {
346: continue;
347: }
348: filtered.add(cmcp);
349: }
350:
351: return filtered;
352: }
353: }
|