KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > BundleManager


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.loader.Environment;
33 import com.caucho.loader.EnvironmentLocal;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36 import com.caucho.util.TimedCache;
37 import com.caucho.vfs.Depend;
38 import com.caucho.vfs.Path;
39 import com.caucho.vfs.ReadStream;
40
41 import javax.servlet.jsp.jstl.fmt.LocalizationContext;
42 import java.io.InputStream JavaDoc;
43 import java.util.Enumeration JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.util.PropertyResourceBundle JavaDoc;
46 import java.util.ResourceBundle JavaDoc;
47 import java.util.logging.Logger JavaDoc;
48
49 /**
50  * Manages i18n bundles
51  */

52 public class BundleManager {
53   private static final L10N L = new L10N(BundleManager.class);
54   private static final Logger JavaDoc log = Log.open(BundleManager.class);
55
56   static LocalizationContext NULL_BUNDLE =
57     new LocalizationContext();
58
59   private static EnvironmentLocal<BundleManager> _envBundle =
60     new EnvironmentLocal<BundleManager>();
61
62   private TimedCache<String JavaDoc,LocalizationContext> _bundleCache;
63
64   private BundleManager()
65   {
66     long updateInterval = Environment.getDependencyCheckInterval();
67     
68     _bundleCache = new TimedCache(256, updateInterval);
69   }
70
71   /**
72    * Returns the environment's bundle.
73    */

74   public static BundleManager create()
75   {
76     BundleManager manager;
77
78     synchronized (_envBundle) {
79       manager = _envBundle.get();
80       if (manager == null) {
81         manager = new BundleManager();
82     _envBundle.set(manager);
83       }
84     }
85
86     return manager;
87   }
88   /**
89    * Returns the named ResourceBundle.
90    */

91   public LocalizationContext getBundle(String JavaDoc name, String JavaDoc cacheKey,
92                                        Enumeration JavaDoc<Locale JavaDoc> locales)
93   {
94     LocalizationContext cachedValue = _bundleCache.get(cacheKey);
95
96     if (cachedValue != null)
97       return cachedValue == NULL_BUNDLE ? null : cachedValue;
98
99     while (locales.hasMoreElements()) {
100       Locale JavaDoc locale = locales.nextElement();
101
102       LocalizationContext bundle = getBundle(name, locale);
103
104       if (bundle != null) {
105         _bundleCache.put(cacheKey, bundle);
106         return bundle;
107       }
108     }
109
110     _bundleCache.put(cacheKey, NULL_BUNDLE);
111
112     return null;
113   }
114
115   /**
116    * Returns the named ResourceBundle.
117    */

118   public LocalizationContext getBundle(String JavaDoc name, Locale JavaDoc locale)
119   {
120     String JavaDoc cacheName = (name + '_' +
121                        locale.getLanguage() + '_' +
122                        locale.getCountry() + '_' +
123                        locale.getVariant());
124     
125     LocalizationContext bundle;
126
127     bundle = _bundleCache.get(cacheName);
128     
129     if (bundle != null)
130       return bundle != NULL_BUNDLE ? bundle : null;
131
132     String JavaDoc fullName = cacheName;
133
134     ResourceBundle JavaDoc resourceBundle;
135     resourceBundle = getBaseBundle(fullName);
136     if (resourceBundle != null) {
137       bundle = new LocalizationContext(resourceBundle, locale);
138       _bundleCache.put(cacheName, bundle);
139       return bundle;
140     }
141       
142     fullName = name + '_' + locale.getLanguage() + '_' + locale.getCountry();
143     resourceBundle = getBaseBundle(fullName);
144     if (resourceBundle != null) {
145       bundle = new LocalizationContext(resourceBundle, locale);
146       _bundleCache.put(cacheName, bundle);
147       return bundle;
148     }
149     
150     fullName = name + '_' + locale.getLanguage();
151     resourceBundle = getBaseBundle(fullName);
152     if (resourceBundle != null) {
153       bundle = new LocalizationContext(resourceBundle, locale);
154       _bundleCache.put(cacheName, bundle);
155       return bundle;
156     }
157
158     _bundleCache.put(cacheName, NULL_BUNDLE);
159
160     return null;
161   }
162   
163
164   /**
165    * Returns the named ResourceBundle.
166    */

167   public LocalizationContext getBundle(String JavaDoc name)
168   {
169     LocalizationContext bundle = _bundleCache.get(name);
170     if (bundle != null)
171       return bundle != NULL_BUNDLE ? bundle : null;
172     
173     ResourceBundle JavaDoc resourceBundle = getBaseBundle(name);
174     if (resourceBundle != null) {
175       bundle = new LocalizationContext(resourceBundle);
176       _bundleCache.put(name, bundle);
177       return bundle;
178     }
179
180     _bundleCache.put(name, NULL_BUNDLE);
181
182     return null;
183   }
184
185   /**
186    * Returns the base resource bundle.
187    */

188   private ResourceBundle JavaDoc getBaseBundle(String JavaDoc name)
189   {
190     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
191     
192     try {
193       Class JavaDoc cl = Class.forName(name, false, loader);
194
195       if (cl != null) {
196         ResourceBundle JavaDoc rb = (ResourceBundle JavaDoc) cl.newInstance();
197
198         if (rb != null)
199           return rb;
200       }
201     } catch (Throwable JavaDoc e) {
202     }
203     
204     try {
205       InputStream JavaDoc is = loader.getResourceAsStream(name.replace('.', '/') + ".properties");
206
207       if (is instanceof ReadStream) {
208         Path path = ((ReadStream) is).getPath();
209         Environment.addDependency(new Depend(path));
210       }
211
212       ResourceBundle JavaDoc bundle = new PropertyResourceBundle JavaDoc(is);
213
214       is.close();
215
216       return bundle;
217     } catch (Exception JavaDoc e) {
218     }
219
220     return null;
221   }
222 }
223
Popular Tags