001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model20.impl.user;
023:
024: import org.jboss.portal.migration.model20.model.PropertyMap;
025: import org.jboss.portal.migration.model20.other.PortletConstants;
026:
027: import java.lang.reflect.Field;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Collections;
031: import java.util.HashMap;
032: import java.util.HashSet;
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.Set;
036:
037: /**
038: * A mutable map that expose user properties.
039: *
040: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
041: * @version $Revision: 8784 $
042: */
043: public class PropertyMapImpl implements PropertyMap {
044:
045: private static final Property[] props = initialize();
046:
047: private static Property[] initialize() {
048: return new Property[] { new Property(
049: PortletConstants.INFO_USER_NAME_NICKNAME, "userName",
050: false) };
051: }
052:
053: private User20Impl user;
054:
055: public PropertyMapImpl(User20Impl user) {
056: this .user = user;
057: }
058:
059: private Map getDynamic() {
060: return user.getDynamic();
061: }
062:
063: public boolean isReadOnly(Object key) {
064: if (!(key instanceof String)) {
065: return false;
066: }
067: Property prop = getProperty((String) key);
068: return prop != null && !prop.writable;
069: }
070:
071: public int size() {
072: return props.length + getDynamic().size();
073: }
074:
075: public boolean isEmpty() {
076: return false;
077: }
078:
079: public boolean containsKey(Object key) {
080: if (!(key instanceof String)) {
081: return false;
082: }
083: Property prop = getProperty((String) key);
084: if (prop != null) {
085: return true;
086: }
087: return getDynamic().containsKey(key);
088: }
089:
090: public boolean containsValue(Object value1) {
091: for (int i = 0; i < props.length; i++) {
092: Property prop = props[i];
093: Object value2 = prop.get(user);
094: if (value1 == value2
095: || (value1 != null && value1.equals(value2))) {
096: return true;
097: }
098: }
099: return getDynamic().containsValue(value1);
100: }
101:
102: public Object get(Object key) {
103: if (!(key instanceof String)) {
104: return null;
105: }
106: Property prop = getProperty((String) key);
107: if (prop != null && prop.field.getName().equals(key)) {
108: return prop.get(user);
109: }
110: return getDynamic().get(key);
111: }
112:
113: /** @throws IllegalArgumentException if the key is not modifiable */
114: public Object put(Object key, Object newValue)
115: throws IllegalArgumentException {
116: if (!(key instanceof String)) {
117: throw new IllegalArgumentException("Key is not a String");
118: }
119: Property prop = getProperty((String) key);
120: if (prop != null && prop.field.getName().equals(key)) {
121: if (!prop.writable) {
122: throw new IllegalArgumentException("Key " + key
123: + " is not modifiable");
124: } else {
125: Object oldValue = prop.get(user);
126: prop.set(user, newValue);
127: return oldValue;
128: }
129: }
130: return getDynamic().put(key, newValue);
131: }
132:
133: /**
134: * Only affect dynamic properties, otherwise it throws an IllegalArgumentException.
135: *
136: * @throws IllegalArgumentException if the key is a not removable user property
137: */
138: public Object remove(Object key) throws IllegalArgumentException {
139: if (!(key instanceof String)) {
140: return null;
141: }
142: Property prop = getProperty((String) key);
143: if (prop != null) {
144: throw new IllegalArgumentException("Key " + key
145: + " is not modifiable");
146: }
147: return getDynamic().remove(key);
148: }
149:
150: /** Clear only dynamic properties. */
151: public void clear() {
152: getDynamic().clear();
153: }
154:
155: public Set keySet() {
156: // Get the data
157: Set set = new HashSet(getDynamic().keySet());
158:
159: // Overwrite with the user ones
160: for (int i = 0; i < props.length; i++) {
161: set.add(props[i].name);
162: }
163: return set;
164: }
165:
166: public Collection values() {
167: ArrayList collection = new ArrayList(size());
168: for (int i = 0; i < props.length; i++) {
169: collection.add(props[i].get(user));
170: }
171: collection.addAll(getDynamic().values());
172: return collection;
173: }
174:
175: /** Returns an immutable collection of entries. */
176: public Set entrySet() {
177: Map copy = new HashMap(getDynamic());
178: for (int i = 0; i < props.length; i++) {
179: Property prop = props[i];
180: copy.put(prop.name, prop.get(user));
181: }
182: return Collections.unmodifiableMap(copy).entrySet();
183: }
184:
185: public void putAll(Map map) {
186: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
187: Map.Entry entry = (Entry) i.next();
188: Object key = entry.getKey();
189: if (key instanceof String) {
190: Object value = entry.getValue();
191: Property prop = getProperty((String) key);
192: if (prop != null) {
193: if (prop.writable) {
194: prop.set(user, value);
195: } else {
196: // julien : Do something better ?
197: }
198: } else {
199: getDynamic().put(key, value);
200: }
201: }
202: }
203: }
204:
205: /** A property of the user object. */
206: private static class Property {
207:
208: final String name;
209: final boolean writable;
210: final Field field;
211:
212: public Property(String propertyName, String fieldName,
213: boolean writable) {
214: try {
215: this .name = propertyName;
216: this .writable = writable;
217: this .field = User20Impl.class.getField(fieldName);
218: } catch (NoSuchFieldException e) {
219: throw new Error(e);
220: }
221: }
222:
223: public void set(Object instance, Object value) {
224: try {
225: field.set(instance, value);
226: } catch (IllegalAccessException e) {
227: throw new Error(e);
228: }
229: }
230:
231: public Object get(Object instance) {
232: try {
233: return field.get(instance);
234: } catch (IllegalAccessException e) {
235: throw new Error(e);
236: }
237: }
238: }
239:
240: private Property getProperty(String key) {
241: for (int i = 0; i < props.length; i++) {
242: Property prop = props[i];
243: if (prop.name.equals(key)) {
244: return prop;
245: }
246: }
247: return null;
248: }
249: }
|