01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.migration.model20.impl.preferences;
23:
24: import org.apache.log4j.Logger;
25: import org.jboss.portal.migration.model20.model.PreferenceSet;
26: import org.jboss.portal.migration.model20.model.PreferenceStore;
27: import org.jboss.portal.migration.model20.other.FQN;
28:
29: import java.util.HashMap;
30: import java.util.Map;
31:
32: /**
33: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
34: * @version $Revision: 8784 $
35: */
36: public abstract class MappedPreferenceStore implements PreferenceStore {
37:
38: /** Our logger. */
39: private static final Logger log = Logger
40: .getLogger(MappedPreferenceStore.class);
41:
42: /** The cached contexts. */
43: protected Map descendants;
44:
45: public MappedPreferenceStore() {
46: descendants = new HashMap();
47: }
48:
49: public PreferenceSet get(FQN fqn) {
50: return getDescendant(fqn);
51: }
52:
53: /** Return the root. */
54: protected abstract MappedPreferenceSet getRoot();
55:
56: private MappedPreferenceSet getDescendant(FQN fqn) {
57: //
58: boolean debug = log.isDebugEnabled();
59:
60: // Try to get from the cache
61: MappedPreferenceSet set = (MappedPreferenceSet) descendants
62: .get(fqn);
63:
64: // Log if necessary
65: if (debug) {
66: if (set == null) {
67: log.debug("Preference set not found for " + fqn);
68: } else {
69: log.debug("Got cached preference set for " + fqn);
70: }
71: }
72:
73: // Cache miss
74: if (set == null) {
75: // Get it from the root
76: MappedPreferenceSet root = getRoot();
77: set = root.getDescendant(fqn);
78:
79: // Update the cache with all of these
80: for (MappedPreferenceSet current = set; current != null; current = current
81: .getParent()) {
82: if (descendants.put(current.getFQN(), current) != null) {
83: // Stop if one is already in the cache
84: break;
85: }
86: }
87: }
88:
89: // Return it
90: return set;
91: }
92: }
|