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 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