001: /**
002: * $Id: ConfigTable.java,v 1.9 2005/07/27 19:56:41 jtb 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;
014:
015: import java.util.Map;
016: import java.util.HashMap;
017: import java.util.Set;
018: import java.util.Iterator;
019: import java.util.StringTokenizer;
020: import java.util.Collection;
021: import java.util.HashSet;
022:
023: import com.sun.portal.community.mc.CMCRolePrincipal;
024:
025: /**
026: * Provides a map-like interface to get and set per-community, per-role
027: * configuration.
028: */
029: public class ConfigTable {
030: /**
031: * Key into a <code>ConfigTable</code> object.
032: *
033: * A <code>ConfigKey</code> consists of a community principal
034: * and a role principal.
035: */
036: public static class ConfigKey implements Comparable {
037: // three underscores
038: private static final String SEPARATOR = "___";
039: // four underscores
040: private static final String COLLECTION_SEPARATOR = "____";
041:
042: private CMCPrincipal communityPrincipal = null;
043: private CMCRolePrincipal rolePrincipal = null;
044:
045: public ConfigKey(CMCPrincipal communityPrincipal,
046: CMCRolePrincipal rolePrincipal) {
047: if (communityPrincipal == null) {
048: throw new NullPointerException(
049: "null community principal is not allowed");
050: }
051: if (rolePrincipal == null) {
052: throw new NullPointerException(
053: "null role principal is not allowed");
054: }
055:
056: this .communityPrincipal = communityPrincipal;
057: this .rolePrincipal = rolePrincipal;
058: }
059:
060: public ConfigKey(String configKeyString) {
061: if (configKeyString == null) {
062: throw new NullPointerException(
063: "null config key string is not allowed");
064: }
065:
066: String[] tokens = configKeyString.split(SEPARATOR);
067: if (tokens.length != 2) {
068: throw new IllegalArgumentException(
069: "invalid config key string: " + configKeyString);
070: }
071:
072: String principalString = tokens[0];
073: this .communityPrincipal = new CMCPrincipal(principalString);
074:
075: String roleString = tokens[1];
076: this .rolePrincipal = CMCRolePrincipal.valueOf(roleString);
077: }
078:
079: public int hashCode() {
080: return toString().hashCode();
081: }
082:
083: public boolean equals(Object o) {
084: if (!(o instanceof ConfigKey)) {
085: return false;
086: }
087:
088: ConfigKey other = (ConfigKey) o;
089: if (communityPrincipal.equals(other.communityPrincipal)
090: && rolePrincipal.equals(other.rolePrincipal)) {
091: return true;
092: }
093:
094: return false;
095: }
096:
097: public CMCPrincipal getCommunityPrincipal() {
098: return communityPrincipal;
099: }
100:
101: public CMCRolePrincipal getRolePrincipal() {
102: return rolePrincipal;
103: }
104:
105: /**
106: * Get a string representation of this object.
107: */
108: public String toString() {
109: return getCommunityPrincipal() + SEPARATOR
110: + getRolePrincipal();
111: }
112:
113: public int compareTo(Object o) {
114: if (!(o instanceof ConfigKey)) {
115: throw new ClassCastException(
116: "object was not of this type");
117: }
118:
119: ConfigKey other = (ConfigKey) o;
120: return toString().compareTo(other.toString());
121: }
122:
123: public static String asString(Collection configKeys) {
124: if (configKeys == null) {
125: throw new NullPointerException(
126: "null config keys collection is not allowed");
127: }
128:
129: StringBuffer b = new StringBuffer();
130: for (Iterator i = configKeys.iterator(); i.hasNext();) {
131: ConfigKey ck = (ConfigKey) i.next();
132: b.append(ck.toString());
133: if (i.hasNext()) {
134: b.append(COLLECTION_SEPARATOR);
135: }
136: }
137:
138: return b.toString();
139: }
140:
141: public static Collection asCollection(String configKeysString) {
142: if (configKeysString == null) {
143: throw new NullPointerException(
144: "null config keys string is not allowed");
145: }
146: String[] configKeyStrings = configKeysString
147: .split(COLLECTION_SEPARATOR);
148: Collection configKeys = new HashSet();
149: for (int i = 0; i < configKeyStrings.length; i++) {
150: String configKeyString = configKeyStrings[i];
151: ConfigKey ck = new ConfigKey(configKeyString);
152: configKeys.add(ck);
153: }
154:
155: return configKeys;
156: }
157: }
158:
159: private Map table;
160: private Integer hash;
161:
162: public ConfigTable() {
163: this (new HashMap());
164: }
165:
166: public ConfigTable(Map table) {
167: if (table == null) {
168: throw new NullPointerException("null table not allowed");
169: }
170: this .table = table;
171: }
172:
173: public Object get(ConfigKey ck) {
174: if (ck == null) {
175: throw new NullPointerException("null key not allowed");
176: }
177: return table.get(ck);
178: }
179:
180: public void put(ConfigKey ck, Object o) {
181: if (ck == null) {
182: throw new NullPointerException("null key not allowed");
183: }
184: if (o == null) {
185: throw new NullPointerException("null value not allowed");
186: }
187: table.put(ck, o);
188: }
189:
190: public void remove(ConfigKey ck) {
191: table.remove(ck);
192: }
193:
194: public Set getConfigKeys() {
195: return table.keySet();
196: }
197:
198: public void putAll(ConfigTable other) {
199: for (Iterator i = other.getConfigKeys().iterator(); i.hasNext();) {
200: ConfigKey ck = (ConfigKey) i.next();
201: put(ck, other.get(ck));
202: }
203: }
204:
205: /**
206: * Get a string representation of this object.
207: *
208: * For debugging purposes only.
209: */
210: public String toString() {
211: return table.toString();
212: }
213:
214: public int size() {
215: return table.size();
216: }
217:
218: /**
219: * Is this object equal to another?
220: * @return a <CODE>boolean</CODE>, <CODE>true</CODE> if:
221: * <ul>
222: * <li>The objects are the same type
223: * <li>The tables have the same keys
224: * <li>The value for each common key is equal
225: * </ul>
226: * Otherwise, <CODE>false</CODE>.
227: */
228: public boolean equals(Object o) {
229: if (!(o instanceof ConfigTable)) {
230: return false;
231: }
232:
233: ConfigTable other = (ConfigTable) o;
234: return this .table.equals(other.table);
235: }
236:
237: public int hashCode() {
238: return table.hashCode();
239: }
240: }
|