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 java.util.ArrayList;
41 import java.util.List;
42 import junit.framework.AssertionFailedError;
43
44 /***
45 * A resource factory that logs tracing information to a list
46 *
47 * @version $Revision: 1.6 $
48 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
49 */
50 public class ProxyResourceFactory
51 extends ResourceFactory {
52
53 private boolean allowReinitialization_ = true;
54 private String resourceFactoryName_;
55 private List methodCollectingList_;
56
57 private int counter_ = 1;
58 private final List allocatedResourcesList_ = new ArrayList();
59
60
61 /***
62 * Create an instance
63 */
64 public ProxyResourceFactory() {
65 }
66
67
68 /***
69 * Create an instance
70 *
71 * @param collectingList The list that will collect the tracing information
72 */
73 public ProxyResourceFactory( final List collectingList ) {
74 if( collectingList == null ) {
75 throw new NullPointerException( "collectingList" );
76 }
77 methodCollectingList_ = collectingList;
78 }
79
80
81 /***
82 * Set the name
83 *
84 * @param name The name of the factory
85 */
86 public void setResourceFactoryName( final String name ) {
87 resourceFactoryName_ = name;
88 trace( "setResourceFactoryName" );
89 }
90
91
92 /***
93 * Set whether or not to allow reinitialization of resources
94 * @param allow true if reinitialization is to be allowed
95 */
96 public void setAllowReinitialization( final boolean allow ) {
97 allowReinitialization_ = allow;
98 }
99
100
101 /***
102 * Allocate and return a resource
103 *
104 * @param resourceManager The ResourceManager that owns this factory
105 * @return The resource
106 */
107 public ManagedResource getResourceImpl( final ResourceManager resourceManager ) {
108 trace( "getResourceImpl" );
109 final ManagedResource resource
110 = new FakeResource( String.valueOf( counter_++ ) );
111 allocatedResourcesList_.add( resource );
112 return resource;
113 }
114
115
116 /***
117 * Return the name
118 *
119 * @return The name
120 */
121 public String getResourceFactoryName() {
122 trace( "setResourceFactoryName()" );
123 return resourceFactoryName_;
124 }
125
126
127 /***
128 * Return a count of the allocated resources
129 * @return The number of allocated resources
130 */
131 public int getAllocatedResourceCount() {
132 return allocatedResourcesList_.size();
133 }
134
135
136 /***
137 * Release a resource which had been previously allocated from this factory
138 *
139 * @param resourceManager The resource manager that owns this factory
140 * @param resource The resource to release
141 */
142 public void releaseResourceImpl( final ResourceManager resourceManager, final ManagedResource resource ) {
143 trace( "releaseResourceImpl" );
144
145 if( allocatedResourcesList_.contains( resource ) == false ) {
146 throw new AssertionFailedError( "resource wasn't allocated from this store" );
147 }
148 allocatedResourcesList_.remove( resource );
149 }
150
151
152 /***
153 * Release all resources that had been allocated from this factory
154 *
155 * @param resourceManager The resource manager that owns this factory
156 */
157 public void releaseAllResources( final ResourceManager resourceManager ) {
158 trace( "releaseAllResources" );
159 super.releaseAllResources( resourceManager );
160 }
161
162
163 /***
164 * Try to reinitialize the specified resource to a known state
165 *
166 * @param resource The resource to reinitialize
167 * @return true if reinitialization was successful
168 */
169 public boolean reinitializeResourceIfPossible( final ManagedResource resource ) {
170 trace( "reinitializeResourceIfPossible" );
171 return allowReinitialization_;
172 }
173
174
175 private void trace( final String methodName ) {
176 if( methodCollectingList_ != null ) {
177 methodCollectingList_.add( methodName );
178 }
179 }
180
181 /*** Fake class for testing */
182 static class FakeResource implements ManagedResource {
183 private final String name_;
184 private String resourceFactoryName_;
185
186
187 /***
188 * Create an instance
189 *
190 * @param name The name of the resource
191 */
192 public FakeResource( final String name ) {
193 name_ = name;
194 }
195
196 /***
197 * Set the name of the resource factory
198 * @param name The new name.
199 */
200 public void setResourceFactoryName( final String name ) {
201 resourceFactoryName_ = name;
202 }
203
204 /***
205 * Get the name of the resource factory
206 * @return The name
207 */
208 public String getResourceFactoryName() {
209 return resourceFactoryName_;
210 }
211
212
213 /*** @return A string representation of this object */
214 public String toString() {
215 return name_;
216 }
217 }
218 }
219