KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n;
9
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * Manager for resources.
14  *
15  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
16  */

17 public class ResourceManager
18 {
19     private final static HashMap JavaDoc c_resources = new HashMap JavaDoc();
20
21     /**
22      * Retrieve resource with specified basename.
23      *
24      * @param baseName the basename
25      * @return the Resources
26      */

27     public final static Resources getBaseResources( final String JavaDoc baseName )
28     {
29         return getBaseResources( baseName, null );
30     }
31
32     /**
33      * Retrieve resource with specified basename.
34      *
35      * @param baseName the basename
36      * @param classLoader the classLoader to load resources from
37      * @return the Resources
38      */

39     public final static Resources getBaseResources( final String JavaDoc baseName,
40                                                     final ClassLoader JavaDoc classLoader )
41     {
42         //TODO: Make these weak references????
43
Resources packet = (Resources)c_resources.get( baseName );
44
45         if( null == packet )
46         {
47             packet = new Resources( baseName, classLoader );
48             c_resources.put( baseName, packet );
49         }
50
51         return packet;
52     }
53
54     /**
55      * Retrieve resource for specified name.
56      * The basename is determined by name postfixed with ".Resources".
57      *
58      * @param clazz the Class
59      * @return the Resources
60      */

61     public final static Resources getResources( final String JavaDoc resource )
62     {
63         return getBaseResources( resource + ".Resources" );
64     }
65
66     /**
67      * Retrieve resource for specified Classes package.
68      * The basename is determined by name of classes package
69      * postfixed with ".Resources".
70      *
71      * @param clazz the Class
72      * @return the Resources
73      */

74     public final static Resources getPackageResources( final Class JavaDoc clazz )
75     {
76         return getBaseResources( getPackageResourcesBaseName( clazz ), clazz.getClassLoader() );
77     }
78
79     /**
80      * Retrieve resource for specified Class.
81      * The basename is determined by name of Class
82      * postfixed with "Resources".
83      *
84      * @param clazz the Class
85      * @return the Resources
86      */

87     public final static Resources getClassResources( final Class JavaDoc clazz )
88     {
89         return getBaseResources( getClassResourcesBaseName( clazz ), clazz.getClassLoader() );
90     }
91
92     /**
93      * Retrieve resource basename for specified Classes package.
94      * The basename is determined by name of classes package
95      * postfixed with ".Resources".
96      *
97      * @param clazz the Class
98      * @return the resource basename
99      */

100     public final static String JavaDoc getPackageResourcesBaseName( final Class JavaDoc clazz )
101     {
102         final Package JavaDoc pkg = clazz.getPackage();
103
104         String JavaDoc baseName;
105         if ( null == pkg )
106         {
107             final String JavaDoc name = clazz.getName();
108             if ( -1 == name.lastIndexOf( "." ) )
109             {
110                 baseName = "Resources";
111             }
112             else
113             {
114                 baseName = name.substring( 0, name.lastIndexOf( "." ) ) + ".Resources";
115             }
116         }
117         else
118         {
119             baseName = pkg.getName() + ".Resources";
120         }
121
122         return baseName;
123     }
124
125     /**
126      * Retrieve resource basename for specified Class.
127      * The basename is determined by name of Class
128      * postfixed with "Resources".
129      *
130      * @param clazz the Class
131      * @return the resource basename
132      */

133     public final static String JavaDoc getClassResourcesBaseName( final Class JavaDoc clazz )
134     {
135         return clazz.getName() + "Resources";
136     }
137
138     /**
139      * Private Constructor to block instantiation.
140      */

141     private ResourceManager()
142     {
143     }
144 }
145
Popular Tags