KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > impl > preferences > MappedPreferenceStore


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.preferences;
10
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.jboss.portal.common.FQN;
16 import org.jboss.portal.server.plugins.preferences.PreferenceSet;
17 import org.jboss.portal.server.plugins.preferences.PreferenceStore;
18
19 /**
20  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
21  * @version $Revision: 1.2 $
22  */

23 public abstract class MappedPreferenceStore implements PreferenceStore
24 {
25
26    /** Our logger. */
27    private static final Logger log = Logger.getLogger(MappedPreferenceStore.class);
28
29    /** The cached contexts. */
30    protected Map JavaDoc descendants;
31
32    public MappedPreferenceStore()
33    {
34       descendants = new HashMap JavaDoc();
35    }
36
37    public PreferenceSet get(FQN fqn)
38    {
39       return getDescendant(fqn);
40    }
41
42    /**
43     * Return the root.
44     */

45    protected abstract MappedPreferenceSet getRoot();
46
47    private MappedPreferenceSet getDescendant(FQN fqn)
48    {
49       //
50
boolean debug = log.isDebugEnabled();
51
52       // Try to get from the cache
53
MappedPreferenceSet set = (MappedPreferenceSet)descendants.get(fqn);
54
55       // Log if necessary
56
if (debug)
57       {
58          if (set == null)
59          {
60             log.debug("Preference set not found for " + fqn);
61          }
62          else
63          {
64             log.debug("Got cached preference set for " + fqn);
65          }
66       }
67
68       // Cache miss
69
if (set == null)
70       {
71          // Get it from the root
72
MappedPreferenceSet root = getRoot();
73          set = root.getDescendant(fqn);
74
75          // Update the cache with all of these
76
for (MappedPreferenceSet current = set;current != null;current = current.getParent())
77          {
78             if (descendants.put(current.getFQN(), current) != null)
79             {
80                // Stop if one is already in the cache
81
break;
82             }
83          }
84       }
85
86       // Return it
87
return set;
88    }
89 }
90
Popular Tags