|
|||||||||||||||||||
| 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 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;
|
|
| 11 |
import java.util.Map;
|
|
| 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 m_resources = new HashMap(); |
|
| 44 |
|
|
| 45 |
/**
|
|
| 46 |
* Create a ResourceLocator with no parent.
|
|
| 47 |
*/
|
|
| 48 | 21 |
public DefaultResourceLocator()
|
| 49 |
{
|
|
| 50 | 21 |
this( null ); |
| 51 |
} |
|
| 52 |
|
|
| 53 |
/**
|
|
| 54 |
* Create a ResourceLocator with specified parent.
|
|
| 55 |
*
|
|
| 56 |
* @param parent the parent ResourceLocator
|
|
| 57 |
*/
|
|
| 58 | 27 |
public DefaultResourceLocator( final ResourceLocator parent )
|
| 59 |
{
|
|
| 60 | 27 |
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 | 18 |
public Object lookup( final String key )
|
| 72 |
throws MissingResourceException
|
|
| 73 |
{
|
|
| 74 | 18 |
final Object resource = getResourceMap().get( key ); |
| 75 | 18 |
if( null != resource ) |
| 76 |
{
|
|
| 77 | 6 |
return resource;
|
| 78 |
} |
|
| 79 |
|
|
| 80 | 12 |
final ResourceLocator parent = getParent(); |
| 81 | 12 |
if( null != parent ) |
| 82 |
{
|
|
| 83 | 6 |
return parent.lookup( key );
|
| 84 |
} |
|
| 85 |
else
|
|
| 86 |
{
|
|
| 87 | 6 |
final String message = "Unable to locate resource " + key + "."; |
| 88 | 6 |
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 | 18 |
public boolean contains( final String key ) |
| 99 |
{
|
|
| 100 | 18 |
final Object resource = getResourceMap().get( key ); |
| 101 | 18 |
if( null != resource ) |
| 102 |
{
|
|
| 103 | 6 |
return true; |
| 104 |
} |
|
| 105 |
|
|
| 106 | 12 |
final ResourceLocator parent = getParent(); |
| 107 | 12 |
if( null != parent ) |
| 108 |
{
|
|
| 109 | 6 |
return parent.contains( key );
|
| 110 |
} |
|
| 111 |
else
|
|
| 112 |
{
|
|
| 113 | 6 |
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 | 12 |
public void put( final String key, |
| 124 |
final Object resource ) |
|
| 125 |
{
|
|
| 126 | 12 |
if( null == key ) |
| 127 |
{
|
|
| 128 | 3 |
throw new NullPointerException( "key" ); |
| 129 |
} |
|
| 130 | 9 |
if( null == resource ) |
| 131 |
{
|
|
| 132 | 3 |
throw new NullPointerException( "resource" ); |
| 133 |
} |
|
| 134 | 6 |
checkWriteable(); |
| 135 | 6 |
getResourceMap().put( key, resource ); |
| 136 |
} |
|
| 137 |
|
|
| 138 |
/**
|
|
| 139 |
* Return the parent ResourceLocator if any.
|
|
| 140 |
*
|
|
| 141 |
* @return the parent ResourceLocator if any.
|
|
| 142 |
*/
|
|
| 143 | 24 |
protected final ResourceLocator getParent()
|
| 144 |
{
|
|
| 145 | 24 |
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 | 42 |
protected final Map getResourceMap()
|
| 154 |
{
|
|
| 155 | 42 |
return m_resources;
|
| 156 |
} |
|
| 157 |
} |
|
| 158 |
|
|
||||||||||