Clover Coverage Report - Atlassian Core
Coverage timestamp: Sun Nov 30 2008 18:33:35 CST
146   445   71   3.95
46   323   0.49   37
37     1.92  
1    
 
 
  CachingPropertySet       Line # 31 146 71 0% 0.0
 
No Tests
 
1    /*
2    * Created by IntelliJ IDEA.
3    * User: Mike
4    * Date: Jun 1, 2004
5    * Time: 3:08:51 PM
6    */
7    package com.atlassian.core.propertyset;
8   
9    import com.opensymphony.module.propertyset.PropertySet;
10    import com.opensymphony.module.propertyset.PropertyException;
11    import com.opensymphony.module.propertyset.PropertySetSchema;
12    import com.opensymphony.module.propertyset.PropertySetManager;
13    import com.opensymphony.module.propertyset.memory.SerializablePropertySet;
14    import com.opensymphony.util.DataUtil;
15   
16    import java.io.Serializable;
17    import java.util.*;
18   
19    import org.w3c.dom.Document;
20   
21    /**
22    * A PropertySet which decorates another PropertySet and caches the results.
23    * <p/>
24    * <p/>
25    * This class performs similar objective as the
26    * com.opensymphony.module.propertyset.cached.CachingPropertySet class but does more
27    * caching.
28    * <p/>
29    * (originally from JiraCachingPropertySet in JIRA)
30    */
 
31    public class CachingPropertySet implements PropertySet, Serializable
32    {
33    private PropertySet decoratedPS;
34    private SerializablePropertySet cachePS;
35    private Map existantKeyCache;
36   
37    /**
38    * Args required are:
39    * <ul>
40    * <li>PropertySet - the PropertySet being decorated
41    * <li>serializableName - the name of the serializable PropertySet to use for the 'property cache' (optional - defaults to 'serializable')
42    * <li>bulkload - a Boolean determining whether or not to bulk load all of the properties into the cache upon startup
43    * </ul>
44    * @param config
45    * @param args
46    */
 
47  0 toggle public void init(Map config, Map args)
48    {
49  0 decoratedPS = (PropertySet) args.get("PropertySet");
50   
51  0 String serializableName = (String) config.get("serializableName");
52   
53  0 if (serializableName == null)
54    {
55  0 serializableName = "serializable";
56    }
57   
58  0 cachePS = (SerializablePropertySet) PropertySetManager.getInstance(serializableName, null);
59  0 existantKeyCache = new HashMap();
60   
61  0 Boolean bulkload = (Boolean) args.get("bulkload");
62   
63  0 if ((bulkload != null) && bulkload.booleanValue())
64    {
65  0 PropertySetManager.clone(decoratedPS, cachePS);
66    }
67    }
68   
 
69  0 toggle public void setAsActualType(String key, Object value) throws PropertyException
70    {
71  0 if (value instanceof Boolean)
72    {
73  0 setBoolean(key, DataUtil.getBoolean((Boolean) value));
74    }
75  0 else if (value instanceof Integer)
76    {
77  0 setInt(key, DataUtil.getInt((Integer) value));
78    }
79  0 else if (value instanceof Long)
80    {
81  0 setLong(key, DataUtil.getLong((Long) value));
82    }
83  0 else if (value instanceof Double)
84    {
85  0 setDouble(key, DataUtil.getDouble((Double) value));
86    }
87  0 else if (value instanceof String)
88    {
89  0 setString(key, (String) value);
90    }
91  0 else if (value instanceof Date)
92    {
93  0 setDate(key, (Date) value);
94    }
95  0 else if (value instanceof Document)
96    {
97  0 setXML(key, (Document) value);
98    }
99  0 else if (value instanceof byte[])
100    {
101  0 setData(key, (byte[]) value);
102    }
103  0 else if (value instanceof Properties)
104    {
105  0 setProperties(key, (Properties) value);
106    }
107    else
108    {
109  0 setObject(key, value);
110    }
111    }
112   
 
113  0 toggle public Object getAsActualType(String key) throws PropertyException
114    {
115  0 int type = getType(key);
116  0 Object value = null;
117   
118  0 switch (type)
119    {
120  0 case BOOLEAN:
121  0 value = new Boolean(getBoolean(key));
122  0 break;
123   
124  0 case INT:
125  0 value = new Integer(getInt(key));
126  0 break;
127   
128  0 case LONG:
129  0 value = new Long(getLong(key));
130  0 break;
131   
132  0 case DOUBLE:
133  0 value = new Double(getDouble(key));
134  0 break;
135   
136  0 case STRING:
137  0 value = getString(key);
138  0 break;
139   
140  0 case DATE:
141  0 value = getDate(key);
142  0 break;
143   
144  0 case XML:
145  0 value = getXML(key);
146  0 break;
147   
148  0 case DATA:
149  0 value = getData(key);
150  0 break;
151   
152  0 case PROPERTIES:
153  0 value = getProperties(key);
154  0 break;
155   
156  0 case OBJECT:
157  0 value = getObject(key);
158  0 break;
159    }
160   
161  0 return value;
162    }
163   
 
164  0 toggle public void setBoolean(String key, boolean value) throws PropertyException
165    {
166  0 decoratedPS.setBoolean(key, value);
167  0 cachePS.setBoolean(key, value);
168   
169    // Clear cache
170  0 existantKeyCache.remove(key);
171    }
172   
 
173  0 toggle public boolean getBoolean(String key) throws PropertyException
174    {
175  0 if (!cachePS.exists(key))
176    {
177  0 cachePS.setBoolean(key, decoratedPS.getBoolean(key));
178    }
179   
180  0 return cachePS.getBoolean(key);
181    }
182   
 
183  0 toggle public void setData(String key, byte[] value) throws PropertyException
184    {
185  0 decoratedPS.setData(key, value);
186  0 cachePS.setData(key, value);
187   
188    // Clear cache
189  0 existantKeyCache.remove(key);
190    }
191   
 
192  0 toggle public byte[] getData(String key) throws PropertyException
193    {
194  0 if (!cachePS.exists(key))
195    {
196  0 cachePS.setData(key, decoratedPS.getData(key));
197    }
198   
199  0 return cachePS.getData(key);
200    }
201   
 
202  0 toggle public void setDate(String key, Date value) throws PropertyException
203    {
204  0 decoratedPS.setDate(key, value);
205  0 cachePS.setDate(key, value);
206   
207    // Clear cache
208  0 existantKeyCache.remove(key);
209    }
210   
 
211  0 toggle public Date getDate(String key) throws PropertyException
212    {
213  0 if (!cachePS.exists(key))
214    {
215  0 cachePS.setDate(key, decoratedPS.getDate(key));
216    }
217   
218  0 return cachePS.getDate(key);
219    }
220   
 
221  0 toggle public void setDouble(String key, double value) throws PropertyException
222    {
223  0 decoratedPS.setDouble(key, value);
224  0 cachePS.setDouble(key, value);
225   
226    // Clear cache
227  0 existantKeyCache.remove(key);
228    }
229   
 
230  0 toggle public double getDouble(String key) throws PropertyException
231    {
232  0 if (!cachePS.exists(key))
233    {
234  0 cachePS.setDouble(key, decoratedPS.getDouble(key));
235    }
236   
237  0 return cachePS.getDouble(key);
238    }
239   
 
240  0 toggle public void setInt(String key, int value) throws PropertyException
241    {
242  0 decoratedPS.setInt(key, value);
243  0 cachePS.setInt(key, value);
244   
245    // Clear cache
246  0 existantKeyCache.remove(key);
247    }
248   
 
249  0 toggle public int getInt(String key) throws PropertyException
250    {
251  0 if (!cachePS.exists(key))
252    {
253  0 cachePS.setInt(key, decoratedPS.getInt(key));
254    }
255   
256  0 return cachePS.getInt(key);
257    }
258   
 
259  0 toggle public Collection getKeys() throws PropertyException
260    {
261  0 return decoratedPS.getKeys();
262    }
263   
 
264  0 toggle public Collection getKeys(int type) throws PropertyException
265    {
266  0 return decoratedPS.getKeys(type);
267    }
268   
 
269  0 toggle public Collection getKeys(String prefix) throws PropertyException
270    {
271  0 return decoratedPS.getKeys(prefix);
272    }
273   
 
274  0 toggle public Collection getKeys(String prefix, int type) throws PropertyException
275    {
276  0 return decoratedPS.getKeys(prefix, type);
277    }
278   
 
279  0 toggle public void setLong(String key, long value) throws PropertyException
280    {
281  0 decoratedPS.setLong(key, value);
282  0 cachePS.setLong(key, value);
283   
284    // Clear cache
285  0 existantKeyCache.remove(key);
286    }
287   
 
288  0 toggle public long getLong(String key) throws PropertyException
289    {
290  0 if (!cachePS.exists(key))
291    {
292  0 cachePS.setLong(key, decoratedPS.getLong(key));
293    }
294   
295  0 return cachePS.getLong(key);
296    }
297   
 
298  0 toggle public void setObject(String key, Object value) throws PropertyException
299    {
300  0 decoratedPS.setObject(key, value);
301  0 cachePS.setObject(key, value);
302   
303    // Clear cache
304  0 existantKeyCache.remove(key);
305    }
306   
 
307  0 toggle public Object getObject(String key) throws PropertyException
308    {
309  0 if (!cachePS.exists(key))
310    {
311  0 cachePS.setObject(key, decoratedPS.getObject(key));
312    }
313   
314  0 return cachePS.getObject(key);
315    }
316   
 
317  0 toggle public void setProperties(String key, Properties value) throws PropertyException
318    {
319  0 decoratedPS.setProperties(key, value);
320  0 cachePS.setProperties(key, value);
321   
322    // Clear cache
323  0 existantKeyCache.remove(key);
324    }
325   
 
326  0 toggle public Properties getProperties(String key) throws PropertyException
327    {
328  0 if (!cachePS.exists(key))
329    {
330  0 cachePS.setProperties(key, decoratedPS.getProperties(key));
331    }
332   
333  0 return cachePS.getProperties(key);
334    }
335   
 
336  0 toggle public void setSchema(PropertySetSchema schema) throws PropertyException
337    {
338  0 decoratedPS.setSchema(schema);
339    }
340   
 
341  0 toggle public PropertySetSchema getSchema() throws PropertyException
342    {
343  0 return decoratedPS.getSchema();
344    }
345   
 
346  0 toggle public boolean isSettable(String property)
347    {
348  0 return decoratedPS.isSettable(property);
349    }
350   
 
351  0 toggle public void setString(String key, String value) throws PropertyException
352    {
353  0 decoratedPS.setString(key, value);
354  0 cachePS.setString(key, value);
355   
356    // Clear cache
357  0 existantKeyCache.remove(key);
358    }
359   
 
360  0 toggle public String getString(String key) throws PropertyException
361    {
362  0 if (!cachePS.exists(key))
363    {
364  0 cachePS.setString(key, decoratedPS.getString(key));
365    }
366   
367  0 return cachePS.getString(key);
368    }
369   
 
370  0 toggle public void setText(String key, String value) throws PropertyException
371    {
372  0 decoratedPS.setText(key, value);
373  0 cachePS.setText(key, value);
374   
375    // Clear cache
376  0 existantKeyCache.remove(key);
377    }
378   
 
379  0 toggle public String getText(String key) throws PropertyException
380    {
381  0 if (!cachePS.exists(key))
382    {
383  0 cachePS.setText(key, decoratedPS.getText(key));
384    }
385   
386  0 return cachePS.getText(key);
387    }
388   
 
389  0 toggle public int getType(String key) throws PropertyException
390    {
391  0 return decoratedPS.getType(key);
392    }
393   
 
394  0 toggle public void setXML(String key, Document value) throws PropertyException
395    {
396  0 decoratedPS.setXML(key, value);
397  0 cachePS.setXML(key, value);
398   
399    // Clear cache
400  0 existantKeyCache.remove(key);
401    }
402   
 
403  0 toggle public Document getXML(String key) throws PropertyException
404    {
405  0 if (!cachePS.exists(key))
406    {
407  0 cachePS.setXML(key, decoratedPS.getXML(key));
408    }
409   
410  0 return cachePS.getXML(key);
411    }
412   
 
413  0 toggle public boolean exists(String key) throws PropertyException
414    {
415    // Check if we have the result for the exists() call cached. If not check the
416    // decoratedPS
417  0 if (existantKeyCache.containsKey(key))
418    {
419  0 return Boolean.TRUE.equals(existantKeyCache.get(key));
420    }
421   
422  0 boolean keyExists = decoratedPS.exists(key);
423   
424    // Cache the result
425  0 existantKeyCache.put(key, new Boolean(keyExists));
426  0 return keyExists;
427    }
428   
 
429  0 toggle public void remove(String key) throws PropertyException
430    {
431  0 existantKeyCache.remove(key);
432  0 cachePS.remove(key);
433  0 decoratedPS.remove(key);
434    }
435   
 
436  0 toggle public boolean supportsType(int type)
437    {
438  0 return decoratedPS.supportsType(type);
439    }
440   
 
441  0 toggle public boolean supportsTypes()
442    {
443  0 return decoratedPS.supportsTypes();
444    }
445    }