View Javadoc

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