You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/04/05 00:38:32 UTC
svn commit: r644970 - in
/incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n:
ResourceBundleProvider.java impl/I18NFilter.java
impl/JcrResourceBundleProvider.java
Author: fmeschbe
Date: Fri Apr 4 15:38:27 2008
New Revision: 644970
URL: http://svn.apache.org/viewvc?rev=644970&view=rev
Log:
SLING-363 Add support for the new SlingHttpServletRequest.getResourceBunlde(String, Locale) method
in the filter and the resource bundle provider
Modified:
incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java
incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
Modified: incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java?rev=644970&r1=644969&r2=644970&view=diff
==============================================================================
--- incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java (original)
+++ incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/ResourceBundleProvider.java Fri Apr 4 15:38:27 2008
@@ -45,4 +45,20 @@
*/
ResourceBundle getResourceBundle(Locale locale);
+ /**
+ * Returns a <code>ResourceBundle</code> for the given locale.
+ *
+ * @param baseName The base name for the resource bundle. If this is
+ * <code>null</code>, the same resource bundle will be
+ * returned as when calling the
+ * {@link #getResourceBundle(Locale)} method.
+ * @param locale The <code>Locale</code> for which to return the resource
+ * bundle. If this is <code>null</code> the default locale as
+ * returned by {@link #getDefaultLocale()} is assumed.
+ * @return The <code>ResourceBundle</code> for the given locale
+ * @throws MissingResourceException If the service is not capable of
+ * returning a <code>ResourceBundle</code>
+ */
+ ResourceBundle getResourceBundle(String baseName, Locale locale);
+
}
Modified: incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java?rev=644970&r1=644969&r2=644970&view=diff
==============================================================================
--- incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java (original)
+++ incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/I18NFilter.java Fri Apr 4 15:38:27 2008
@@ -138,13 +138,18 @@
@Override
public ResourceBundle getResourceBundle(Locale locale) {
+ return getResourceBundle(null, locale);
+ }
+
+ @Override
+ public ResourceBundle getResourceBundle(String baseName, Locale locale) {
if (bundleProvider != null) {
if (locale == null) {
locale = getLocale();
}
try {
- return bundleProvider.getResourceBundle(locale);
+ return bundleProvider.getResourceBundle(baseName, locale);
} catch (MissingResourceException mre) {
log.warn(
"getResourceBundle: Cannot get ResourceBundle from provider",
@@ -154,9 +159,9 @@
log.info("getResourceBundle: ResourceBundleProvider not available, calling default implementation");
}
- return super.getResourceBundle(locale);
+ return super.getResourceBundle(baseName, locale);
}
-
+
@Override
public Locale getLocale() {
if (locale == null) {
Modified: incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java?rev=644970&r1=644969&r2=644970&view=diff
==============================================================================
--- incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java (original)
+++ incubator/sling/trunk/sling/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java Fri Apr 4 15:38:27 2008
@@ -54,19 +54,17 @@
public class JcrResourceBundleProvider implements ResourceBundleProvider,
EventListener {
- /** default log */
- private final Logger log = LoggerFactory.getLogger(getClass());
-
/** @scr.property value="" */
private static final String PROP_USER = "user";
/** @scr.property value="" */
private static final String PROP_PASS = "password";
- /**
- * @scr.property value="en"
- */
- public static final String PAR_DEFAULT_LOCALE = "locale.default";
+ /** @scr.property value="en" */
+ private static final String PROP_DEFAULT_LOCALE = "locale.default";
+
+ /** default log */
+ private final Logger log = LoggerFactory.getLogger(getClass());
/** @scr.reference cardinality="0..1" policy="dynamic" */
private SlingRepository repository;
@@ -74,13 +72,32 @@
/** @scr.reference cardinality="0..1" policy="dynamic" */
private JcrResourceResolverFactory resourceResolverFactory;
+ /**
+ * The default Locale as configured with the <i>locale.default</i>
+ * configuration property. This defaults to <code>Locale.ENGLISH</code> if
+ * the configuration property is not set.
+ */
private Locale defaultLocale = Locale.ENGLISH;
+ /**
+ * The credentials to access the repository or <code>null</code> to use
+ * access the repository as the anonymous user, which is the case if the
+ * <i>user</i> property is not set in the configuration.
+ */
private Credentials repoCredentials;
+ /**
+ * The resource resolver used to access the resource bundles. This object is
+ * retrieved from the {@link #resourceResolverFactory} using the anonymous
+ * session or the session acquired using the {@link #repoCredentials}.
+ */
private ResourceResolver resourceResolver;
- private Map<Locale, ResourceBundle> resourceBundleCache = new HashMap<Locale, ResourceBundle>();
+ /**
+ * Matrix of cached resource bundles. The first key is the resource bundle
+ * base name, the second key is the Locale.
+ */
+ private final Map<String, Map<Locale, ResourceBundle>> resourceBundleCache = new HashMap<String, Map<Locale, ResourceBundle>>();
// ---------- ResourceBundleProvider ---------------------------------------
@@ -105,11 +122,15 @@
* is not available to access the resources.
*/
public ResourceBundle getResourceBundle(Locale locale) {
+ return getResourceBundle(null, locale);
+ }
+
+ public ResourceBundle getResourceBundle(String baseName, Locale locale) {
if (locale == null) {
locale = defaultLocale;
}
- return getResourceBundleInternal(locale);
+ return getResourceBundleInternal(baseName, locale);
}
// ---------- EventListener ------------------------------------------------
@@ -150,7 +171,7 @@
repoCredentials = new SimpleCredentials(user, pwd);
}
- String localeString = (String) props.get(PAR_DEFAULT_LOCALE);
+ String localeString = (String) props.get(PROP_DEFAULT_LOCALE);
this.defaultLocale = toLocale(localeString);
}
@@ -213,26 +234,35 @@
* created and the <code>ResourceResolver</code> is not
* available to access the resources.
*/
- private ResourceBundle getResourceBundleInternal(Locale locale) {
- ResourceBundle resourceBundle;
-
+ private ResourceBundle getResourceBundleInternal(String baseName,
+ Locale locale) {
+ ResourceBundle resourceBundle = null;
synchronized (resourceBundleCache) {
- resourceBundle = resourceBundleCache.get(locale);
+ Map<Locale, ResourceBundle> appBundles = resourceBundleCache.get(baseName);
+ if (appBundles != null) {
+ resourceBundle = appBundles.get(locale);
+ }
}
if (resourceBundle == null) {
- resourceBundle = createResourceBundle(locale);
+ resourceBundle = createResourceBundle(baseName, locale);
synchronized (resourceBundleCache) {
+ Map<Locale, ResourceBundle> appBundles = resourceBundleCache.get(baseName);
+ if (appBundles == null) {
+ appBundles = new HashMap<Locale, ResourceBundle>();
+ resourceBundleCache.put(baseName, appBundles);
+ }
+
// while creating the resource bundle, another thread may
// have created the same and already stored it in the cache.
// in this case we don't use the one we just created but use
// the bundle from the cache. Otherwise, we store our bundle
// in the cache and keep using it.
- if (resourceBundleCache.containsKey(locale)) {
- resourceBundle = resourceBundleCache.get(locale);
+ if (appBundles.containsKey(locale)) {
+ resourceBundle = appBundles.get(locale);
} else {
- resourceBundleCache.put(locale, resourceBundle);
+ appBundles.put(locale, resourceBundle);
}
}
@@ -247,7 +277,7 @@
* @throws MissingResourceException If the <code>ResourceResolver</code>
* is not available to access the resources.
*/
- private ResourceBundle createResourceBundle(Locale locale) {
+ private ResourceBundle createResourceBundle(String baseName, Locale locale) {
ResourceResolver resolver = getResourceResolver();
if (resolver == null) {
@@ -256,12 +286,13 @@
"ResourceResolver not available", getClass().getName(), "");
}
- JcrResourceBundle bundle = new JcrResourceBundle(locale, resolver);
+ JcrResourceBundle bundle = new JcrResourceBundle(locale, baseName,
+ resolver);
// set parent resource bundle
Locale parentLocale = getParentLocale(locale);
if (parentLocale != null) {
- bundle.setParent(getResourceBundleInternal(parentLocale));
+ bundle.setParent(getResourceBundleInternal(baseName, parentLocale));
}
return bundle;