1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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 public ResourceManager() {
61 this( "<unknown>" );
62 }
63
64
65 /***
66 * Create an instance
67 *
68 * @param name The name of this object.
69 */
70 public ResourceManager( final String name ) {
71 if( name == null ) {
72 throw new NullPointerException( "name" );
73 }
74 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 public final Object getResource( final String name ) {
85 if( name == null ) {
86 throw new NullPointerException( "name" );
87 }
88 final ManagedResource resource = getResourceFactory( name ).getResource( this );
89 resource.setResourceFactoryName( name );
90 return resource;
91 }
92
93
94 /***
95 * Release the specified resource
96 *
97 * @param object The resource to release
98 */
99 public final void releaseResource( final ManagedResource object ) {
100 if( object == null ) {
101 throw new NullPointerException( "object" );
102 }
103 getResourceFactory( object.getResourceFactoryName() ).releaseResource( this, object );
104 }
105
106
107 /***
108 * Release all resources
109 */
110 public void releaseAllResources() {
111 final Iterator iterator = resourceFactories_.values().iterator();
112 while( iterator.hasNext() ) {
113 ( ( 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 public void addFactory( final String name, final ResourceFactory factory ) {
125 if( resourceFactories_.containsKey( name ) ) {
126 throw new DetailedIllegalArgumentException( "name", name, "A factory already registered for this name" );
127 }
128 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 private final ResourceFactory getResourceFactory(
138 final String name )
139 throws
140 ResourceFactoryNotFoundException {
141
142 final ResourceFactory factory = ( ResourceFactory )resourceFactories_.get( name );
143 if( factory == null ) {
144 throw new ResourceFactoryNotFoundException( name );
145 }
146
147 return factory;
148 }
149
150
151 /***
152 * Return a string representation of this object
153 * @return A string respresentation of this object.
154 */
155 public String toString() {
156 return "ResourceManager[" + name_ + "]";
157 }
158 }
159