KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > user > PropertyMapImpl


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.impl.user;
10
11 import java.lang.reflect.Field JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.jboss.portal.core.model.PropertyMap;
22 import org.jboss.portal.portlet.PortletConstants;
23
24 /**
25  * A mutable map that expose user properties.
26  *
27  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
28  * @version $Revision: 1.2 $
29  */

30 public class PropertyMapImpl implements PropertyMap
31 {
32
33    private static final Property[] props = initialize();
34
35    private static Property[] initialize()
36    {
37       return new Property[]
38       {
39          new Property(PortletConstants.INFO_USER_NAME_NICKNAME, "userName", false)
40       };
41    }
42
43    private UserImpl user;
44
45    public PropertyMapImpl(UserImpl user)
46    {
47       this.user = user;
48    }
49
50    private Map JavaDoc getDynamic()
51    {
52       return user.getDynamic();
53    }
54
55    public boolean isReadOnly(Object JavaDoc key)
56    {
57       if (!(key instanceof String JavaDoc))
58       {
59          return false;
60       }
61       Property prop = getProperty((String JavaDoc)key);
62       return prop != null && prop.writable;
63    }
64
65    public int size()
66    {
67       return props.length + getDynamic().size();
68    }
69
70    public boolean isEmpty()
71    {
72       return false;
73    }
74
75    public boolean containsKey(Object JavaDoc key)
76    {
77       if (!(key instanceof String JavaDoc))
78       {
79          return false;
80       }
81       Property prop = getProperty((String JavaDoc)key);
82       if (prop != null)
83       {
84          return true;
85       }
86       return getDynamic().containsKey(key);
87    }
88
89    public boolean containsValue(Object JavaDoc value1)
90    {
91       for (int i = 0;i < props.length;i++)
92       {
93          Property prop = props[i];
94          Object JavaDoc value2 = prop.get(user);
95          if (value1 == value2 || (value1 != null && value1.equals(value2)))
96          {
97             return true;
98          }
99       }
100       return getDynamic().containsValue(value1);
101    }
102
103    public Object JavaDoc get(Object JavaDoc key)
104    {
105       if (!(key instanceof String JavaDoc))
106       {
107          return null;
108       }
109       Property prop = getProperty((String JavaDoc)key);
110       if (prop != null && prop.field.getName().equals(key))
111       {
112          return prop.get(user);
113       }
114       return getDynamic().get(key);
115    }
116
117    /**
118     *
119     * @throws IllegalArgumentException if the key is not modifiable
120     */

121    public Object JavaDoc put(Object JavaDoc key, Object JavaDoc newValue) throws IllegalArgumentException JavaDoc
122    {
123       if (!(key instanceof String JavaDoc))
124       {
125          throw new IllegalArgumentException JavaDoc("Key is not a String");
126       }
127       Property prop = getProperty((String JavaDoc)key);
128       if (prop != null && prop.field.getName().equals(key))
129       {
130          if (!prop.writable)
131          {
132             throw new IllegalArgumentException JavaDoc("Key " + key + " is not modifiable");
133          }
134          else
135          {
136             Object JavaDoc oldValue = prop.get(user);
137             prop.set(user, newValue);
138             return oldValue;
139          }
140       }
141       return getDynamic().put(key, newValue);
142    }
143
144    /**
145     * Only affect dynamic properties, otherwise it throws an IllegalArgumentException.
146     *
147     * @throws IllegalArgumentException if the key is a not removable user property
148     */

149    public Object JavaDoc remove(Object JavaDoc key) throws IllegalArgumentException JavaDoc
150    {
151       if (!(key instanceof String JavaDoc))
152       {
153          return null;
154       }
155       Property prop = getProperty((String JavaDoc)key);
156       if (prop != null)
157       {
158          throw new IllegalArgumentException JavaDoc("Key " + key + " is not modifiable");
159       }
160       return getDynamic().remove(key);
161    }
162
163    /**
164     * Clear only dynamic properties.
165     */

166    public void clear()
167    {
168       getDynamic().clear();
169    }
170
171    public Set JavaDoc keySet()
172    {
173       // Get the data
174
Set JavaDoc set = new HashSet JavaDoc(getDynamic().keySet());
175
176       // Overwrite with the user ones
177
for (int i = 0;i < props.length;i++)
178       {
179          set.add(props[i].name);
180       }
181       return set;
182    }
183
184    public Collection JavaDoc values()
185    {
186       ArrayList JavaDoc collection = new ArrayList JavaDoc(size());
187       for (int i = 0;i < props.length;i++)
188       {
189          collection.add(props[i].get(user));
190       }
191       collection.addAll(getDynamic().values());
192       return collection;
193    }
194
195    /**
196     * Returns an immutable collection of entries.
197     */

198    public Set JavaDoc entrySet()
199    {
200       Map JavaDoc copy = new HashMap JavaDoc(getDynamic());
201       for (int i = 0;i < props.length;i++)
202       {
203          Property prop = props[i];
204          copy.put(prop.name, prop.get(user));
205       }
206       return Collections.unmodifiableMap(copy).entrySet();
207    }
208
209    public void putAll(Map JavaDoc map)
210    {
211       for (Iterator JavaDoc i = map.entrySet().iterator();i.hasNext();)
212       {
213          Map.Entry JavaDoc entry = (Entry)i.next();
214          Object JavaDoc key = entry.getKey();
215          if (key instanceof String JavaDoc)
216          {
217             Object JavaDoc value = entry.getValue();
218             Property prop = getProperty((String JavaDoc)key);
219             if (prop != null)
220             {
221                if (prop.writable)
222                {
223                   prop.set(user, value);
224                }
225                else
226                {
227                   // julien : Do something better ?
228
}
229             }
230             else
231             {
232                getDynamic().put(key, value);
233             }
234          }
235       }
236    }
237
238    /**
239     * A property of the user object.
240     */

241    private static class Property
242    {
243
244       final String JavaDoc name;
245       final boolean writable;
246       final Field JavaDoc field;
247
248       public Property(String JavaDoc propertyName, String JavaDoc fieldName, boolean writable)
249       {
250          try
251          {
252             this.name = propertyName;
253             this.writable = writable;
254             this.field = UserImpl.class.getField(fieldName);
255          }
256          catch (NoSuchFieldException JavaDoc e)
257          {
258             throw new Error JavaDoc(e);
259          }
260       }
261       public void set(Object JavaDoc instance, Object JavaDoc value)
262       {
263          try
264          {
265             field.set(instance, value);
266          }
267          catch (IllegalAccessException JavaDoc e)
268          {
269             throw new Error JavaDoc(e);
270          }
271       }
272       public Object JavaDoc get(Object JavaDoc instance)
273       {
274          try
275          {
276             return field.get(instance);
277          }
278          catch (IllegalAccessException JavaDoc e)
279          {
280             throw new Error JavaDoc(e);
281          }
282       }
283    }
284
285    private Property getProperty(String JavaDoc key)
286    {
287       for (int i = 0;i < props.length;i++)
288       {
289          Property prop = props[i];
290          if (prop.name.equals(key))
291          {
292             return prop;
293          }
294       }
295       return null;
296    }
297 }
298
Popular Tags