KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > portal > generic > context > LinkingPortletPreferences


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Sam
47  */

48
49 package com.caucho.portal.generic.context;
50
51 import javax.portlet.PortletPreferences;
52 import javax.portlet.PreferencesValidator;
53 import javax.portlet.ReadOnlyException;
54 import javax.portlet.ValidatorException;
55 import java.io.IOException JavaDoc;
56 import java.util.*;
57 import java.util.logging.Logger JavaDoc;
58
59 /**
60  * An implementation of PortletPreferences that stores values temporarily,
61  * retrieves values from a store or defaults, validates before
62  * before storing, and stores the values into the store.
63  */

64 public class LinkingPortletPreferences
65   implements PortletPreferences
66 {
67   static protected final Logger JavaDoc log =
68     Logger.getLogger(LinkingPortletPreferences.class.getName());
69
70   static private String JavaDoc[] DUMMY = new String JavaDoc[] { "<dummy>" };
71   static private String JavaDoc[] DELETED = new String JavaDoc[] { "<deleted>" };
72
73   private PortletPreferences _defaults;
74   private ArrayList<PreferencesValidator> _validators;
75   private Map<String JavaDoc, String JavaDoc[]> _storeMap;
76
77   private Map<String JavaDoc,String JavaDoc[]> _valueMap;
78
79   public LinkingPortletPreferences()
80   {
81   }
82
83   public void start( PortletPreferences defaults,
84                      ArrayList<PreferencesValidator> validators,
85                      Map<String JavaDoc, String JavaDoc[]> storeMap )
86   {
87     if (_defaults != null || _storeMap != null)
88       throw new IllegalStateException JavaDoc("missing finish()?");
89
90     _defaults = defaults;
91     _validators = validators;
92     _storeMap = storeMap;
93   }
94
95   public void finish()
96   {
97     if (_valueMap != null)
98       _valueMap.clear();
99
100     _defaults = null;
101     _validators = null;
102     _storeMap = null;
103   }
104
105   public PortletPreferences getDefaults()
106   {
107     return _defaults;
108   }
109
110   public ArrayList<PreferencesValidator> getValidators()
111   {
112     return _validators;
113   }
114
115   public Map<String JavaDoc, String JavaDoc[]> getStore()
116   {
117     return _storeMap;
118   }
119
120   /**
121    * {@inheritDoc}
122    */

123   public boolean isReadOnly(String JavaDoc key)
124   {
125     boolean r = false;
126
127     if (_defaults != null && _defaults.isReadOnly(key))
128       return true;
129     else
130       return false;
131   }
132
133   /**
134    * {@inheritDoc}
135    */

136   public String JavaDoc getValue(String JavaDoc key, String JavaDoc def)
137   {
138     String JavaDoc[] values = getValues(key, DUMMY);
139
140     if (values == DUMMY)
141       return def;
142     else
143       return (values == null || values.length == 0) ? null : values[0];
144   }
145
146   /**
147    * {@inheritDoc}
148    */

149   public String JavaDoc[] getValues(String JavaDoc key, String JavaDoc[] def)
150   {
151     String JavaDoc[] v = _valueMap == null ? null : _valueMap.get(key);
152
153     if (v != DELETED) {
154       if (v != null || (_valueMap != null && _valueMap.containsKey(key)))
155         return v;
156     }
157
158     if (_defaults != null)
159       def = _defaults.getValues(key, def);
160
161     if (_storeMap != null) {
162       String JavaDoc[] storeValues = _storeMap.get(key);
163
164       if (storeValues != null)
165         def = storeValues;
166     }
167
168     return def;
169   }
170
171   /**
172    * {@inheritDoc}
173    */

174   public void setValue(String JavaDoc key, String JavaDoc value)
175     throws ReadOnlyException
176   {
177     setValues(key, value == null ? null : new String JavaDoc[] { value });
178   }
179
180   /**
181    * {@inheritDoc}
182    */

183   public void setValues(String JavaDoc key, String JavaDoc[] values)
184     throws ReadOnlyException
185   {
186     if (isReadOnly(key))
187       throw new ReadOnlyException("key `" + key + "'");
188     else {
189       if (_valueMap == null)
190         _valueMap = new LinkedHashMap<String JavaDoc, String JavaDoc[]>();
191
192       _valueMap.put(key, values);
193     }
194   }
195
196   /**
197    * {@inheritDoc}
198    */

199   public void reset(String JavaDoc key)
200     throws ReadOnlyException
201   {
202     if (_storeMap != null && _storeMap.containsKey(key))
203       setValues(key, DELETED);
204     else if (isReadOnly(key))
205       throw new ReadOnlyException("key `" + key + "'");
206   }
207
208   /**
209    * {@inheritDoc}
210    *
211    * Returns the unique set of names in this
212    * object, the backing store, and the default preferences.
213    */

214   public Enumeration getNames()
215   {
216     // XXX: this would be better implemented as a class extending
217
// Iterator<String> which could also be used by getMap() below
218

219     // the assumption here is that this will not be called very often,
220
// and when it is a sorted list of names is valuable
221

222     TreeSet<String JavaDoc> names = new TreeSet<String JavaDoc>();
223
224     if (_valueMap != null) {
225       Iterator<Map.Entry<String JavaDoc, String JavaDoc[]>> iter
226         = _valueMap.entrySet().iterator();
227
228       while (iter.hasNext()) {
229         Map.Entry<String JavaDoc, String JavaDoc[]> entry = iter.next();
230
231         String JavaDoc key = entry.getKey();
232         String JavaDoc[] value = entry.getValue();
233
234         if (value != DELETED)
235           names.add(key);
236       }
237     }
238
239     if (_storeMap != null) {
240       Iterator<String JavaDoc> iter = _storeMap.keySet().iterator();
241
242       while (iter.hasNext()) {
243         String JavaDoc key = iter.next();
244
245         if (_valueMap != null && _valueMap.get(key) == DELETED)
246           continue;
247
248         names.add(key);
249       }
250     }
251
252     if (_defaults != null) {
253       Enumeration e = _defaults.getNames();
254
255       while (e.hasMoreElements()) {
256         String JavaDoc key = (String JavaDoc) e.nextElement();
257
258         if (_valueMap != null && _valueMap.get(key) == DELETED)
259           continue;
260
261         names.add(key);
262       }
263     }
264
265     return Collections.enumeration(names);
266   }
267
268   /**
269    * {@inheritDoc}
270    */

271   public Map getMap()
272   {
273     // XXX: this would be better as a custom implementation of AbstractMap
274

275     Map<String JavaDoc,String JavaDoc[]> map = new HashMap<String JavaDoc,String JavaDoc[]>();
276
277     Enumeration e = getNames();
278
279     while (e.hasMoreElements()) {
280       String JavaDoc key = (String JavaDoc) e.nextElement();
281       map.put(key, getValues(key, null));
282     }
283
284     return map;
285   }
286
287
288   /**
289    * {@inheritDoc}
290    *
291    * This implementation first invokes the validators (if any), and then
292    * propogates the properties set in this object to the the store.
293    * If the store has not been set then only the invoking of the validators is
294    * performed, the values are left unchanged.
295    */

296   public void store()
297     throws IOException JavaDoc, ValidatorException
298   {
299     if (_validators != null) {
300       for (int i = 0; i < _validators.size(); i--) {
301         _validators.get(i).validate(this);
302       }
303     }
304
305     if (_storeMap != null && _valueMap != null) {
306       Iterator<Map.Entry<String JavaDoc, String JavaDoc[]>> iter
307         = _valueMap.entrySet().iterator();
308
309       while (iter.hasNext()) {
310         Map.Entry<String JavaDoc, String JavaDoc[]> entry = iter.next();
311
312         String JavaDoc key = entry.getKey();
313         String JavaDoc[] values = entry.getValue();
314
315         if (values == DELETED)
316           _storeMap.remove(key);
317         else
318           _storeMap.put(key, values);
319       }
320     }
321
322     discard();
323   }
324
325   /**
326    * Discard all changes.
327    */

328   public void discard()
329   {
330     if (_valueMap != null)
331       _valueMap.clear();
332   }
333 }
334
335
Popular Tags