001: /*
002: * Copyright (C) The DNA Group. All rights reserved.
003: *
004: * This software is published under the terms of the DNA
005: * Software License version 1.1, a copy of which has been included
006: * with this distribution in the LICENSE.txt file.
007: */
008: package org.codehaus.dna.impl;
009:
010: import java.util.HashMap;
011: import java.util.Map;
012:
013: import org.codehaus.dna.MissingResourceException;
014: import org.codehaus.dna.ResourceLocator;
015:
016: /**
017: * ResourceLocator implementation backed by a Map and
018: * optionally delegating to parent ResourceLocators.
019: * The developer should create the DefaultResourceLocator,
020: * associate resources with locator and then invoke
021: * {@link #makeReadOnly()} before passing the Locator to
022: * the client component.
023: *
024: * <p>The implementation will first check for resources
025: * associated with itself and if unable to locate resource
026: * locally it will delegate to parent ResourceLocator.</p>
027: *
028: * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
029: */
030: public class DefaultResourceLocator extends AbstractFreezable implements
031: ResourceLocator {
032: /**
033: * parent locator to look into if unable to
034: * find resource in current locator.
035: */
036: private final ResourceLocator m_parent;
037:
038: /**
039: * Resources registered with locator.
040: */
041: private final Map m_resources = new HashMap();
042:
043: /**
044: * Create a ResourceLocator with no parent.
045: */
046: public DefaultResourceLocator() {
047: this (null);
048: }
049:
050: /**
051: * Create a ResourceLocator with specified parent.
052: *
053: * @param parent the parent ResourceLocator
054: */
055: public DefaultResourceLocator(final ResourceLocator parent) {
056: m_parent = parent;
057: }
058:
059: /**
060: * Return resource registered with specified key.
061: *
062: * @param key the key
063: * @return the resource
064: * @throws MissingResourceException if unable to locate
065: * resource with specified key
066: */
067: public Object lookup(final String key)
068: throws MissingResourceException {
069: final Object resource = getResourceMap().get(key);
070: if (null != resource) {
071: return resource;
072: }
073:
074: final ResourceLocator parent = getParent();
075: if (null != parent) {
076: return parent.lookup(key);
077: } else {
078: final String message = "Unable to locate resource " + key
079: + ".";
080: throw new MissingResourceException(message, key);
081: }
082: }
083:
084: /**
085: * Return true if a resource exists with specified key.
086: *
087: * @param key the key
088: * @return true if a resource exists with specified key.
089: */
090: public boolean contains(final String key) {
091: final Object resource = getResourceMap().get(key);
092: if (null != resource) {
093: return true;
094: }
095:
096: final ResourceLocator parent = getParent();
097: if (null != parent) {
098: return parent.contains(key);
099: } else {
100: return false;
101: }
102: }
103:
104: /**
105: * Add a resource to resource locator.
106: *
107: * @param key the key used to store resource (Must not be null).
108: * @param resource the resource (Must not be null).
109: */
110: public void put(final String key, final Object resource) {
111: if (null == key) {
112: throw new NullPointerException("key");
113: }
114: if (null == resource) {
115: throw new NullPointerException("resource");
116: }
117: checkWriteable();
118: getResourceMap().put(key, resource);
119: }
120:
121: /**
122: * Return the parent ResourceLocator if any.
123: *
124: * @return the parent ResourceLocator if any.
125: */
126: protected final ResourceLocator getParent() {
127: return m_parent;
128: }
129:
130: /**
131: * Return the map used to store resources.
132: *
133: * @return the map used to store resources.
134: */
135: protected final Map getResourceMap() {
136: return m_resources;
137: }
138: }
|