1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.i18n;
24
25 import java.lang.ref.WeakReference;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 /***
30 * Manager for internatialized resources.
31 *
32 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
33 */
34 public final class ResourceManager {
35
36 /*** Permission needed to clear complete cache. */
37 private static final RuntimePermission CLEAR_CACHE_PERMISSION = new RuntimePermission("i18n.clearCompleteCache");
38
39 /*** Static cache for resources. */
40 private static final Map RESOURCES = new HashMap();
41
42 /***
43 * Private Constructor to block instantiation.
44 */
45 private ResourceManager() {
46 }
47
48 /***
49 * Retrieve resource with specified base name.
50 *
51 * @param baseName the base name
52 * @return the Resources
53 */
54 public static Resources getBaseResources(final String baseName) {
55 return getBaseResources(baseName, null);
56 }
57
58 /***
59 * Retrieve resource with specified base name.
60 *
61 * @param baseName the base name
62 * @param classLoader the classLoader to load resources from
63 * @return the Resources
64 */
65 public static synchronized Resources getBaseResources(final String baseName, final ClassLoader classLoader) {
66 Resources theResources = getCachedResource(baseName);
67 if (null == theResources) {
68 theResources = new Resources(baseName, classLoader);
69 putCachedResource(baseName, theResources);
70 }
71 return theResources;
72 }
73
74 /***
75 * Clear the cache of all resources currently loaded into the
76 * system. This method is useful if you need to dump the complete
77 * cache and because part of the application is reloading and
78 * thus the resources may need to be reloaded.
79 *
80 * <p>Note that the caller must have been granted the
81 * "i18n.clearCompleteCache" {@link RuntimePermission} or
82 * else a security exception will be thrown.</p>
83 *
84 * @throws SecurityException if the caller does not have
85 * permission to clear cache
86 */
87 public static synchronized void clearResourceCache() throws SecurityException {
88 final SecurityManager sm = System.getSecurityManager();
89 if (null != sm) {
90 sm.checkPermission(CLEAR_CACHE_PERMISSION);
91 }
92 RESOURCES.clear();
93 }
94
95 /***
96 * Cache specified resource in weak reference.
97 *
98 * @param baseName the resource key
99 * @param theResources the resources object
100 */
101 private static synchronized void putCachedResource(final String baseName, final Resources theResources) {
102 RESOURCES.put(baseName, new WeakReference(theResources));
103 }
104
105 /***
106 * Retrieve cached resource.
107 *
108 * @param baseName the resource key
109 * @return resources the resources object
110 */
111 private static synchronized Resources getCachedResource(final String baseName) {
112 final WeakReference weakReference = (WeakReference) RESOURCES.get(baseName);
113 if (null != weakReference) {
114 return (Resources) weakReference.get();
115 }
116 return null;
117 }
118
119 /***
120 * Retrieve resource for specified name.
121 * The base name is determined by name postfixed with ".Resources".
122 *
123 * @param name the name to use when looking up resources
124 * @return the Resources
125 */
126 public static Resources getResources(final String name) {
127 return getBaseResources(name + ".Resources");
128 }
129
130 /***
131 * Retrieve resource for specified Classes package.
132 * The base name is determined by name of classes package
133 * postfixed with ".Resources".
134 *
135 * @param clazz the Class
136 * @return the Resources
137 */
138 public static Resources getPackageResources(final Class clazz) {
139 return getBaseResources(getPackageResourcesBaseName(clazz), clazz.getClassLoader());
140 }
141
142 /***
143 * Retrieve resource for specified Class.
144 * The base name is determined by name of Class
145 * postfixed with "Resources".
146 *
147 * @param clazz the Class
148 * @return the Resources
149 */
150 public static Resources getClassResources(final Class clazz) {
151 return getBaseResources(getClassResourcesBaseName(clazz), clazz.getClassLoader());
152 }
153
154 /***
155 * Retrieve resource base name for specified Classes package.
156 * The base name is determined by name of classes package
157 * postfixed with ".Resources".
158 *
159 * @param clazz the Class
160 * @return the resource base name
161 */
162 public static String getPackageResourcesBaseName(final Class clazz) {
163 final Package pkg = clazz.getPackage();
164 String baseName;
165 if (null == pkg) {
166 final String name = clazz.getName();
167 if (-1 == name.lastIndexOf(".")) {
168 baseName = "Resources";
169 } else {
170 baseName = name.substring(0, name.lastIndexOf(".")) + ".Resources";
171 }
172 } else {
173 baseName = pkg.getName() + ".Resources";
174 }
175 return baseName;
176 }
177
178 /***
179 * Retrieve resource base name for specified Class.
180 * The base name is determined by name of Class
181 * postfixed with "Resources".
182 *
183 * @param clazz the Class
184 * @return the resource base name
185 */
186 public static String getClassResourcesBaseName(final Class clazz) {
187 return clazz.getName() + "Resources";
188 }
189 }