KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > portlet > impl > PortletPreferencesImpl


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.portlet.impl;
10
11 import java.io.IOException JavaDoc;
12 import java.util.Arrays JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Enumeration JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import javax.portlet.PortletPreferences;
21 import javax.portlet.PreferencesValidator;
22 import javax.portlet.ReadOnlyException;
23 import javax.portlet.ValidatorException;
24
25 import org.apache.log4j.Logger;
26 import org.jboss.portal.common.util.Tools;
27 import org.jboss.portal.common.value.NullValue;
28 import org.jboss.portal.common.value.StringValue;
29 import org.jboss.portal.common.value.StringValues;
30 import org.jboss.portal.common.value.Value;
31 import org.jboss.portal.server.plugins.preferences.MergeStrategy;
32 import org.jboss.portal.server.plugins.preferences.Preference;
33 import org.jboss.portal.server.plugins.preferences.PreferenceSet;
34 import org.jboss.portal.server.plugins.preferences.SimpleMergeStrategy;
35
36 /**
37  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
38  * @version $Revision: 1.4 $
39  */

40 public class PortletPreferencesImpl implements PortletPreferences
41 {
42
43    /** The logger. */
44    protected static final Logger log = Logger.getLogger(PortletPreferencesImpl.class);
45
46    /** Indicates the object is used during a action phase. */
47    public static final int ACTION = 1;
48
49    /** Indicates the object is used during a render phase. */
50    public static final int RENDER = 0;
51
52    protected final PreferenceSet[] sets1;
53    protected final PreferenceSet[] sets2;
54
55    protected final PreferenceSet system;
56    protected final PreferenceSet user;
57    protected final PreferencesValidator validator;
58    protected final int mode;
59    protected final MergeStrategy strategy;
60
61    /** Keep track of updates */
62    protected final Map JavaDoc updates;
63
64    public PortletPreferencesImpl(
65          PreferenceSet system,
66          PreferenceSet user,
67          PreferencesValidator validator,
68          int mode)
69    {
70       this.sets1 = new PreferenceSet[]{system,user};
71       this.sets2 = new PreferenceSet[]{system};
72       this.validator = validator;
73       this.mode = mode;
74       this.system = system;
75       this.user = user;
76       this.strategy = SimpleMergeStrategy.getSingleton();
77
78       this.updates = new HashMap JavaDoc();
79    }
80
81    public Map JavaDoc getMap()
82    {
83       return new MyMap(strategy, updates, sets1);
84    }
85
86    public Enumeration JavaDoc getNames()
87    {
88       Set JavaDoc keySet = strategy.getKeySet(sets1);
89       return Tools.toEnumeration(keySet.iterator());
90    }
91
92    private Value getValue(String JavaDoc key)
93    {
94       Value value = null;
95       Update update = (Update)updates.get(key);
96       if (update != null)
97       {
98          if (update.value != null)
99          {
100             value = update.value;
101          }
102          else
103          {
104             Preference preference = strategy.getPreference(sets2, key);
105             if (preference != null)
106             {
107                value = preference.getValue();
108             }
109          }
110       }
111       else
112       {
113          Preference preference = strategy.getPreference(sets1, key);
114          if (preference != null)
115          {
116             value = preference.getValue();
117          }
118       }
119       return value;
120    }
121
122    public String JavaDoc getValue(String JavaDoc key, String JavaDoc def) throws IllegalArgumentException JavaDoc
123    {
124       if (key == null)
125       {
126          throw new IllegalArgumentException JavaDoc("key must not be null");
127       }
128       Value value = getValue(key);
129       if (value != null)
130       {
131          return value.asString();
132       }
133       else
134       {
135          return def;
136       }
137    }
138
139    public String JavaDoc[] getValues(String JavaDoc key, String JavaDoc[] def) throws IllegalArgumentException JavaDoc
140    {
141       if (key == null)
142       {
143          throw new IllegalArgumentException JavaDoc("key must not be null");
144       }
145       Value value = getValue(key);
146       if (value != null)
147       {
148          return value.asStringArray();
149       }
150       else
151       {
152          return def;
153       }
154    }
155
156    public boolean isReadOnly(String JavaDoc key) throws IllegalArgumentException JavaDoc, IllegalArgumentException JavaDoc
157    {
158       if (key == null)
159       {
160          throw new IllegalArgumentException JavaDoc("key must not be null");
161       }
162       return strategy.isReadOnly(sets2, key);
163    }
164
165    public void reset(String JavaDoc key) throws IllegalArgumentException JavaDoc, ReadOnlyException
166    {
167       if (key == null)
168       {
169          throw new IllegalArgumentException JavaDoc("key must not be null");
170       }
171       if (isReadOnly(key))
172       {
173          throw new ReadOnlyException("Key " + key + " cannot be written");
174       }
175       Update update = (Update)updates.get(key);
176       if (update == null)
177       {
178          updates.put(key, new Update(null));
179       }
180       else
181       {
182          update.value = null;
183       }
184    }
185
186    public void setValue(String JavaDoc key, String JavaDoc value) throws IllegalArgumentException JavaDoc, ReadOnlyException
187    {
188       if (key == null)
189       {
190          throw new IllegalArgumentException JavaDoc("key must not be null");
191       }
192       if (isReadOnly(key))
193       {
194          throw new ReadOnlyException("Key " + key + " cannot be written");
195       }
196       Value value_ = null;
197       if (value == null)
198       {
199          value_ = NullValue.INSTANCE;
200       }
201       else
202       {
203          value_ = new StringValue(value);
204       }
205       Update update = (Update)updates.get(key);
206       if (update == null)
207       {
208          updates.put(key, new Update(value_));
209       }
210       else
211       {
212          update.value = value_;
213       }
214    }
215
216    public void setValues(String JavaDoc key, String JavaDoc[] values) throws IllegalArgumentException JavaDoc, ReadOnlyException
217    {
218       if (key == null)
219       {
220          throw new IllegalArgumentException JavaDoc("key must not be null");
221       }
222       if (isReadOnly(key))
223       {
224          throw new ReadOnlyException("Key " + key + " cannot be written");
225       }
226       Value value_ = null;
227       if (values == null)
228       {
229          value_ = NullValue.INSTANCE;
230       }
231       else
232       {
233          value_ = new StringValues(values);
234       }
235       Update update = (Update)updates.get(key);
236       if (update == null)
237       {
238          updates.put(key, new Update(value_));
239       }
240       else
241       {
242          update.value = value_;
243       }
244    }
245
246    public void store() throws IOException JavaDoc, ValidatorException
247    {
248       // Check we are in the right mode
249
if (mode != ACTION)
250       {
251          throw new IllegalStateException JavaDoc("Store must be called within the scope of an action request");
252       }
253
254       // If the optional validator is present validate
255
if (validator != null)
256       {
257          validator.validate(this);
258       }
259
260       // Copy the transient set to the persistent set
261
for (Iterator JavaDoc i = updates.entrySet().iterator();i.hasNext();)
262       {
263          Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
264          String JavaDoc key = (String JavaDoc)entry.getKey();
265          Update update = (Update)entry.getValue();
266          user.setValue(key, update.value);
267       }
268
269       //
270
updates.clear();
271    }
272
273    private static class MyMap extends HashMap JavaDoc
274    {
275
276       /**
277        * Merge both sets1 into one single map
278        */

279       private MyMap(MergeStrategy strategy, Map JavaDoc updates, PreferenceSet[] sets)
280       {
281          // Arbitraty size
282
super(10);
283
284          // Merge
285
Set JavaDoc keys = strategy.getKeySet(sets);
286          for (Iterator JavaDoc i = keys.iterator();i.hasNext();)
287          {
288             String JavaDoc key = (String JavaDoc)i.next();
289             Preference preference = strategy.getPreference(sets, key);
290             String JavaDoc[] value = preference.getValue().asStringArray();
291             String JavaDoc[] clone = new String JavaDoc[value.length];
292             System.arraycopy(value, 0, clone, 0, value.length);
293             super.put(key, clone);
294          }
295
296          //
297
for (Iterator JavaDoc i = updates.entrySet().iterator(); i.hasNext();)
298          {
299             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
300             String JavaDoc name = (String JavaDoc)entry.getKey();
301             Update update = (Update)entry.getValue();
302
303             if (update.value == null)
304             {
305                super.remove(name);
306             }
307             else
308             {
309                String JavaDoc[] value = update.value.asStringArray();
310                String JavaDoc[] clone = new String JavaDoc[value.length];
311                System.arraycopy(value, 0, clone, 0, value.length);
312                super.put(name, clone);
313             }
314          }
315       }
316
317       public boolean containsValue(Object JavaDoc value)
318       {
319          if (value instanceof String JavaDoc[])
320          {
321             String JavaDoc[] strings = (String JavaDoc[])value;
322             for (Iterator JavaDoc i = super.values().iterator();i.hasNext();)
323             {
324                String JavaDoc[] other = (String JavaDoc[])i.next();
325                if (Arrays.equals(strings, other))
326                {
327                   return true;
328                }
329             }
330          }
331          return false;
332       }
333
334       public Object JavaDoc get(Object JavaDoc key)
335       {
336          String JavaDoc[] strings = (String JavaDoc[])super.get(key); // Should be cloned here to ensure immutability
337
return strings;
338       }
339
340       public Collection JavaDoc values()
341       {
342          Collection JavaDoc values = super.values(); // String[] should be cloned here to ensure immutability
343
return values;
344       }
345
346       public Set JavaDoc entrySet()
347       {
348          return super.entrySet();
349       }
350
351       /**
352        * Do not change state.
353        *
354        * @return null
355        */

356       public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
357       {
358          return null;
359       }
360
361       /**
362        * Do not change state.
363        *
364        * @return null
365        */

366       public Object JavaDoc remove(Object JavaDoc key)
367       {
368          return null;
369       }
370
371       /**
372        * Do not change state.
373        */

374       public void putAll(Map JavaDoc t)
375       {
376       }
377
378       /**
379        * Do not change state.
380        */

381       public void clear()
382       {
383       }
384    }
385
386    protected static class Update
387    {
388       private Value value;
389       public Update(Value value)
390       {
391          this.value = value;
392       }
393    }
394 }
395
Popular Tags