KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > dna > impl > DefaultResourceLocator


1 /*
2  * Copyright (C) The DNA Group. All rights reserved.
3  *
4  * This software is published under the terms of the DNA
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.codehaus.dna.impl;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.codehaus.dna.MissingResourceException;
14 import org.codehaus.dna.ResourceLocator;
15
16 /**
17  * ResourceLocator implementation backed by a Map and
18  * optionally delegating to parent ResourceLocators.
19  * The developer should create the DefaultResourceLocator,
20  * associate resources with locator and then invoke
21  * {@link #makeReadOnly()} before passing the Locator to
22  * the client component.
23  *
24  * <p>The implementation will first check for resources
25  * associated with itself and if unable to locate resource
26  * locally it will delegate to parent ResourceLocator.</p>
27  *
28  * @version $Revision: 1.2 $ $Date: 2004/05/01 09:51:48 $
29  */

30 public class DefaultResourceLocator
31     extends AbstractFreezable
32     implements ResourceLocator
33 {
34     /**
35      * parent locator to look into if unable to
36      * find resource in current locator.
37      */

38     private final ResourceLocator m_parent;
39
40     /**
41      * Resources registered with locator.
42      */

43     private final Map JavaDoc m_resources = new HashMap JavaDoc();
44
45     /**
46      * Create a ResourceLocator with no parent.
47      */

48     public DefaultResourceLocator()
49     {
50         this( null );
51     }
52
53     /**
54      * Create a ResourceLocator with specified parent.
55      *
56      * @param parent the parent ResourceLocator
57      */

58     public DefaultResourceLocator( final ResourceLocator parent )
59     {
60         m_parent = parent;
61     }
62
63     /**
64      * Return resource registered with specified key.
65      *
66      * @param key the key
67      * @return the resource
68      * @throws MissingResourceException if unable to locate
69      * resource with specified key
70      */

71     public Object JavaDoc lookup( final String JavaDoc key )
72         throws MissingResourceException
73     {
74         final Object JavaDoc resource = getResourceMap().get( key );
75         if( null != resource )
76         {
77             return resource;
78         }
79
80         final ResourceLocator parent = getParent();
81         if( null != parent )
82         {
83             return parent.lookup( key );
84         }
85         else
86         {
87             final String JavaDoc message = "Unable to locate resource " + key + ".";
88             throw new MissingResourceException( message, key );
89         }
90     }
91
92     /**
93      * Return true if a resource exists with specified key.
94      *
95      * @param key the key
96      * @return true if a resource exists with specified key.
97      */

98     public boolean contains( final String JavaDoc key )
99     {
100         final Object JavaDoc resource = getResourceMap().get( key );
101         if( null != resource )
102         {
103             return true;
104         }
105
106         final ResourceLocator parent = getParent();
107         if( null != parent )
108         {
109             return parent.contains( key );
110         }
111         else
112         {
113             return false;
114         }
115     }
116
117     /**
118      * Add a resource to resource locator.
119      *
120      * @param key the key used to store resource (Must not be null).
121      * @param resource the resource (Must not be null).
122      */

123     public void put( final String JavaDoc key,
124                      final Object JavaDoc resource )
125     {
126         if( null == key )
127         {
128             throw new NullPointerException JavaDoc( "key" );
129         }
130         if( null == resource )
131         {
132             throw new NullPointerException JavaDoc( "resource" );
133         }
134         checkWriteable();
135         getResourceMap().put( key, resource );
136     }
137
138     /**
139      * Return the parent ResourceLocator if any.
140      *
141      * @return the parent ResourceLocator if any.
142      */

143     protected final ResourceLocator getParent()
144     {
145         return m_parent;
146     }
147
148     /**
149      * Return the map used to store resources.
150      *
151      * @return the map used to store resources.
152      */

153     protected final Map JavaDoc getResourceMap()
154     {
155         return m_resources;
156     }
157 }
158
Popular Tags