|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ResourceManager.java | 83.3% | 87% | 87.5% | 86% |
|
1 | /* | |
2 | * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved. | |
3 | * | |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions are met: | |
6 | * | |
7 | * 1. Redistributions of source code must retain the above copyright notice, | |
8 | * this list of conditions and the following disclaimer. | |
9 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
10 | * this list of conditions and the following disclaimer in the documentation | |
11 | * and/or other materials provided with the distribution. | |
12 | * 3. The end-user documentation included with the redistribution, if any, must | |
13 | * include the following acknowledgment: | |
14 | * | |
15 | * "This product includes software developed by Gargoyle Software Inc. | |
16 | * (http://www.GargoyleSoftware.com/)." | |
17 | * | |
18 | * Alternately, this acknowledgment may appear in the software itself, if | |
19 | * and wherever such third-party acknowledgments normally appear. | |
20 | * 4. The name "Gargoyle Software" must not be used to endorse or promote | |
21 | * products derived from this software without prior written permission. | |
22 | * For written permission, please contact info@GargoyleSoftware.com. | |
23 | * 5. Products derived from this software may not be called "GSBase", nor may | |
24 | * "GSBase" appear in their name, without prior written permission of | |
25 | * Gargoyle Software Inc. | |
26 | * | |
27 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
28 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
29 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE | |
30 | * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
31 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
32 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
33 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
36 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | package com.gargoylesoftware.base.resource; | |
39 | ||
40 | import com.gargoylesoftware.base.util.DetailedIllegalArgumentException; | |
41 | import java.util.HashMap; | |
42 | import java.util.Iterator; | |
43 | import java.util.Map; | |
44 | ||
45 | /** | |
46 | * An object that manages the resources allocated by the resource factories | |
47 | * | |
48 | * @version $Revision: 1.4 $ | |
49 | * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> | |
50 | */ | |
51 | public final class ResourceManager { | |
52 | ||
53 | private Map resourceFactories_ = new HashMap( 89 ); | |
54 | private final String name_; | |
55 | ||
56 | ||
57 | /** | |
58 | * Create an instance | |
59 | */ | |
60 | 0 | public ResourceManager() { |
61 | 0 | this( "<unknown>" ); |
62 | } | |
63 | ||
64 | ||
65 | /** | |
66 | * Create an instance | |
67 | * | |
68 | * @param name The name of this object. | |
69 | */ | |
70 | 22 | public ResourceManager( final String name ) { |
71 | 22 | if( name == null ) { |
72 | 0 | throw new NullPointerException( "name" ); |
73 | } | |
74 | 22 | name_ = name; |
75 | } | |
76 | ||
77 | ||
78 | /** | |
79 | * Return a resource from the specified factory | |
80 | * | |
81 | * @param name The name of the factory | |
82 | * @return The specified resource | |
83 | */ | |
84 | 46 | public final Object getResource( final String name ) { |
85 | 46 | if( name == null ) { |
86 | 1 | throw new NullPointerException( "name" ); |
87 | } | |
88 | 45 | final ManagedResource resource = getResourceFactory( name ).getResource( this ); |
89 | 44 | resource.setResourceFactoryName( name ); |
90 | 44 | return resource; |
91 | } | |
92 | ||
93 | ||
94 | /** | |
95 | * Release the specified resource | |
96 | * | |
97 | * @param object The resource to release | |
98 | */ | |
99 | 4 | public final void releaseResource( final ManagedResource object ) { |
100 | 4 | if( object == null ) { |
101 | 1 | throw new NullPointerException( "object" ); |
102 | } | |
103 | 3 | getResourceFactory( object.getResourceFactoryName() ).releaseResource( this, object ); |
104 | } | |
105 | ||
106 | ||
107 | /** | |
108 | * Release all resources | |
109 | */ | |
110 | 5 | public void releaseAllResources() { |
111 | 5 | final Iterator iterator = resourceFactories_.values().iterator(); |
112 | 5 | while( iterator.hasNext() ) { |
113 | 4 | ( ( ResourceFactory )iterator.next() ).releaseAllResources( this ); |
114 | } | |
115 | } | |
116 | ||
117 | ||
118 | /** | |
119 | * Adds a feature to the Factory attribute of the ResourceManager object | |
120 | * | |
121 | * @param name The feature to be added to the Factory attribute | |
122 | * @param factory The feature to be added to the Factory attribute | |
123 | */ | |
124 | 6 | public void addFactory( final String name, final ResourceFactory factory ) { |
125 | 6 | if( resourceFactories_.containsKey( name ) ) { |
126 | 0 | throw new DetailedIllegalArgumentException( "name", name, "A factory already registered for this name" ); |
127 | } | |
128 | 6 | resourceFactories_.put( name, factory ); |
129 | } | |
130 | ||
131 | ||
132 | /** | |
133 | * @param name Description of Parameter | |
134 | * @return The resourceFactory value | |
135 | * @exception ResourceFactoryNotFoundException Description of Exception | |
136 | */ | |
137 | 48 | private final ResourceFactory getResourceFactory( |
138 | final String name ) | |
139 | throws | |
140 | ResourceFactoryNotFoundException { | |
141 | ||
142 | 48 | final ResourceFactory factory = ( ResourceFactory )resourceFactories_.get( name ); |
143 | 48 | if( factory == null ) { |
144 | 2 | throw new ResourceFactoryNotFoundException( name ); |
145 | } | |
146 | ||
147 | 46 | return factory; |
148 | } | |
149 | ||
150 | ||
151 | /** | |
152 | * Return a string representation of this object | |
153 | * @return A string respresentation of this object. | |
154 | */ | |
155 | 2 | public String toString() { |
156 | 2 | return "ResourceManager[" + name_ + "]"; |
157 | } | |
158 | } | |
159 |
|