|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultResourceLocator.java | 100% | 100% | 100% | 100% |
|
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 | 14 |
public DefaultResourceLocator()
|
48 |
{ |
|
49 | 14 |
this( null ); |
50 |
} |
|
51 |
|
|
52 |
/**
|
|
53 |
* Create a ResourceLocator with specified parent.
|
|
54 |
*
|
|
55 |
* @param parent the parent ResourceLocator
|
|
56 |
*/
|
|
57 | 19 |
public DefaultResourceLocator( final ResourceLocator parent )
|
58 |
{ |
|
59 | 19 |
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 | 12 |
public Object lookup( final String key )
|
71 |
throws MissingResourceException
|
|
72 |
{ |
|
73 | 12 |
final Object resource = getResourceMap().get( key ); |
74 | 12 |
if( null != resource ) |
75 |
{ |
|
76 | 4 |
return resource;
|
77 |
} |
|
78 |
|
|
79 | 8 |
final ResourceLocator parent = getParent(); |
80 | 8 |
if( null != parent ) |
81 |
{ |
|
82 | 4 |
return parent.lookup( key );
|
83 |
} |
|
84 |
else
|
|
85 |
{ |
|
86 | 4 |
final String message = "Unable to locate resource " + key + "."; |
87 | 4 |
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 | 12 |
public boolean contains( final String key ) |
98 |
{ |
|
99 | 12 |
final Object resource = getResourceMap().get( key ); |
100 | 12 |
if( null != resource ) |
101 |
{ |
|
102 | 4 |
return true; |
103 |
} |
|
104 |
|
|
105 | 8 |
final ResourceLocator parent = getParent(); |
106 | 8 |
if( null != parent ) |
107 |
{ |
|
108 | 4 |
return parent.contains( key );
|
109 |
} |
|
110 |
else
|
|
111 |
{ |
|
112 | 4 |
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 | 8 |
public void put( final String key, |
123 |
final Object resource ) |
|
124 |
{ |
|
125 | 8 |
if( null == key ) |
126 |
{ |
|
127 | 2 |
throw new NullPointerException( "key" ); |
128 |
} |
|
129 | 6 |
if( null == resource ) |
130 |
{ |
|
131 | 2 |
throw new NullPointerException( "resource" ); |
132 |
} |
|
133 | 4 |
checkWriteable(); |
134 | 4 |
getResourceMap().put( key, resource ); |
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Return the parent ResourceLocator if any.
|
|
139 |
*
|
|
140 |
* @return the parent ResourceLocator if any.
|
|
141 |
*/
|
|
142 | 16 |
protected final ResourceLocator getParent()
|
143 |
{ |
|
144 | 16 |
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 | 28 |
protected final Map getResourceMap()
|
153 |
{ |
|
154 | 28 |
return m_resources;
|
155 |
} |
|
156 |
} |
|
157 |
|
|