1 /*
2 * Copyright (C) The JContainer Group. All rights reserved.
3 *
4 * This software is published under the terms of the JContainer
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.jcontainer.dna.impl;
9
10 import java.util.HashMap;
11 import java.util.Map;
12 import org.jcontainer.dna.MissingResourceException;
13 import org.jcontainer.dna.ResourceLocator;
14
15 /***
16 * ResourceLocator implementation backed by a Map and
17 * optionally delegating to parent ResourceLocators.
18 * The developer should create the DefaultResourceLocator,
19 * associate resources with locator and then invoke
20 * {@link #makeReadOnly()} before passing the Locator to
21 * the client component.
22 *
23 * <p>The implementation will first check for resources
24 * associated with itself and if unable to locate resource
25 * locally it will delegate to parent ResourceLocator.</p>
26 *
27 * @version $Revision: 1.11 $ $Date: 2003/09/23 10:15:26 $
28 */
29 public class DefaultResourceLocator
30 extends AbstractFreezable
31 implements ResourceLocator
32 {
33 /***
34 * parent locator to look into if unable to
35 * find resource in current locator.
36 */
37 private final ResourceLocator m_parent;
38
39 /***
40 * Resources registered with locator.
41 */
42 private final Map m_resources = new HashMap();
43
44 /***
45 * Create a ResourceLocator with no parent.
46 */
47 public DefaultResourceLocator()
48 {
49 this( null );
50 }
51
52 /***
53 * Create a ResourceLocator with specified parent.
54 *
55 * @param parent the parent ResourceLocator
56 */
57 public DefaultResourceLocator( final ResourceLocator parent )
58 {
59 m_parent = parent;
60 }
61
62 /***
63 * Return resource registered with specified key.
64 *
65 * @param key the key
66 * @return the resource
67 * @throws MissingResourceException if unable to locate
68 * resource with specified key
69 */
70 public Object lookup( final String key )
71 throws MissingResourceException
72 {
73 final Object resource = getResourceMap().get( key );
74 if( null != resource )
75 {
76 return resource;
77 }
78
79 final ResourceLocator parent = getParent();
80 if( null != parent )
81 {
82 return parent.lookup( key );
83 }
84 else
85 {
86 final String message = "Unable to locate resource " + key + ".";
87 throw new MissingResourceException( message, key );
88 }
89 }
90
91 /***
92 * Return true if a resource exists with specified key.
93 *
94 * @param key the key
95 * @return true if a resource exists with specified key.
96 */
97 public boolean contains( final String key )
98 {
99 final Object resource = getResourceMap().get( key );
100 if( null != resource )
101 {
102 return true;
103 }
104
105 final ResourceLocator parent = getParent();
106 if( null != parent )
107 {
108 return parent.contains( key );
109 }
110 else
111 {
112 return false;
113 }
114 }
115
116 /***
117 * Add a resource to resource locator.
118 *
119 * @param key the key used to store resource (Must not be null).
120 * @param resource the resource (Must not be null).
121 */
122 public void put( final String key,
123 final Object resource )
124 {
125 if( null == key )
126 {
127 throw new NullPointerException( "key" );
128 }
129 if( null == resource )
130 {
131 throw new NullPointerException( "resource" );
132 }
133 checkWriteable();
134 getResourceMap().put( key, resource );
135 }
136
137 /***
138 * Return the parent ResourceLocator if any.
139 *
140 * @return the parent ResourceLocator if any.
141 */
142 protected final ResourceLocator getParent()
143 {
144 return m_parent;
145 }
146
147 /***
148 * Return the map used to store resources.
149 *
150 * @return the map used to store resources.
151 */
152 protected final Map getResourceMap()
153 {
154 return m_resources;
155 }
156 }
This page was automatically generated by Maven