KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > util > i18n > ResourceManager


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.avalon.util.i18n;
18
19 import java.lang.ref.WeakReference JavaDoc;
20 import java.util.HashMap JavaDoc;
21
22 /**
23  * Manager for resources.
24  *
25  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
26  */

27 public class ResourceManager
28 {
29     /**
30      * Permission needed to clear complete cache.
31      */

32     private static final RuntimePermission JavaDoc CLEAR_CACHE_PERMISSION =
33         new RuntimePermission JavaDoc( "i18n.clearCompleteCache" );
34     private static final HashMap JavaDoc c_resources = new HashMap JavaDoc();
35
36     /**
37      * Retrieve resource with specified basename.
38      *
39      * @param baseName the basename
40      * @return the Resources
41      */

42     public static final Resources getBaseResources( final String JavaDoc baseName )
43     {
44         return getBaseResources( baseName, null );
45     }
46
47     /**
48      * Retrieve resource with specified basename.
49      *
50      * @param baseName the basename
51      * @param classLoader the classLoader to load resources from
52      * @return the Resources
53      */

54     public synchronized static final Resources getBaseResources( final String JavaDoc baseName,
55                                                                  final ClassLoader JavaDoc classLoader )
56     {
57         Resources resources = getCachedResource( baseName );
58         if( null == resources )
59         {
60             resources = new Resources( baseName, classLoader );
61             putCachedResource( baseName, resources );
62         }
63
64         return resources;
65     }
66
67     /**
68      * Clear the cache of all resources currently loaded into the
69      * system. This method is useful if you need to dump the complete
70      * cache and because part of the application is reloading and
71      * thus the resources may need to be reloaded.
72      *
73      * <p>Note that the caller must have been granted the
74      * "i18n.clearCompleteCache" {@link RuntimePermission} or
75      * else a security exception will be thrown.</p>
76      *
77      * @throws SecurityException if the caller does not have
78      * permission to clear cache
79      */

80     public synchronized static final void clearResourceCache()
81         throws SecurityException JavaDoc
82     {
83         final SecurityManager JavaDoc sm = System.getSecurityManager();
84         if( null != sm )
85         {
86             sm.checkPermission( CLEAR_CACHE_PERMISSION );
87         }
88
89         c_resources.clear();
90     }
91
92     /**
93      * Cache specified resource in weak reference.
94      *
95      * @param baseName the resource key
96      * @param resources the resources object
97      */

98     private synchronized static final void putCachedResource( final String JavaDoc baseName,
99                                                               final Resources resources )
100     {
101         c_resources.put( baseName,
102                          new WeakReference JavaDoc( resources ) );
103     }
104
105     /**
106      * Retrieve cached resource.
107      *
108      * @param baseName the resource key
109      * @return resources the resources object
110      */

111     private synchronized static final Resources getCachedResource( final String JavaDoc baseName )
112     {
113         final WeakReference JavaDoc weakReference =
114             (WeakReference JavaDoc)c_resources.get( baseName );
115         if( null == weakReference )
116         {
117             return null;
118         }
119         else
120         {
121             return (Resources)weakReference.get();
122         }
123     }
124
125     /**
126      * Retrieve resource for specified name.
127      * The basename is determined by name postfixed with ".Resources".
128      *
129      * @param name the name to use when looking up resources
130      * @return the Resources
131      */

132     public static final Resources getResources( final String JavaDoc name )
133     {
134         return getBaseResources( name + ".Resources" );
135     }
136
137     /**
138      * Retrieve resource for specified Classes package.
139      * The basename is determined by name of classes package
140      * postfixed with ".Resources".
141      *
142      * @param clazz the Class
143      * @return the Resources
144      */

145     public static final Resources getPackageResources( final Class JavaDoc clazz )
146     {
147         return getBaseResources( getPackageResourcesBaseName( clazz ), clazz.getClassLoader() );
148     }
149
150     /**
151      * Retrieve resource for specified Class.
152      * The basename is determined by name of Class
153      * postfixed with "Resources".
154      *
155      * @param clazz the Class
156      * @return the Resources
157      */

158     public static final Resources getClassResources( final Class JavaDoc clazz )
159     {
160         return getBaseResources( getClassResourcesBaseName( clazz ), clazz.getClassLoader() );
161     }
162
163     /**
164      * Retrieve resource basename for specified Classes package.
165      * The basename is determined by name of classes package
166      * postfixed with ".Resources".
167      *
168      * @param clazz the Class
169      * @return the resource basename
170      */

171     public static final String JavaDoc getPackageResourcesBaseName( final Class JavaDoc clazz )
172     {
173         final Package JavaDoc pkg = clazz.getPackage();
174
175         String JavaDoc baseName;
176         if( null == pkg )
177         {
178             final String JavaDoc name = clazz.getName();
179             if( -1 == name.lastIndexOf( "." ) )
180             {
181                 baseName = "Resources";
182             }
183             else
184             {
185                 baseName = name.substring( 0, name.lastIndexOf( "." ) ) + ".Resources";
186             }
187         }
188         else
189         {
190             baseName = pkg.getName() + ".Resources";
191         }
192
193         return baseName;
194     }
195
196     /**
197      * Retrieve resource basename for specified Class.
198      * The basename is determined by name of Class
199      * postfixed with "Resources".
200      *
201      * @param clazz the Class
202      * @return the resource basename
203      */

204     public static final String JavaDoc getClassResourcesBaseName( final Class JavaDoc clazz )
205     {
206         return clazz.getName() + "Resources";
207     }
208
209     /**
210      * Private Constructor to block instantiation.
211      */

212     private ResourceManager()
213     {
214     }
215 }
216
Popular Tags