Author: justin
Date: Wed May 11 19:46:32 2011
New Revision: 1102051
URL: http://svn.apache.org/viewvc?rev=1102051&view=rev
Log:
SLING-2061 / SLING-2062 - preloading configured set of resource bundles and
registering each individual resource bundle as a service
Modified:
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
sling/trunk/contrib/extensions/i18n/src/main/resources/OSGI-INF/metatype/metatype.properties
Modified:
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java?rev=1102051&r1=1102050&r2=1102051&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
(original)
+++
sling/trunk/contrib/extensions/i18n/src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java
Wed May 11 19:46:32 2011
@@ -18,8 +18,11 @@
*/
package org.apache.sling.i18n.impl;
+import java.util.ArrayList;
import java.util.Dictionary;
import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
@@ -34,6 +37,7 @@ import javax.jcr.observation.Observation
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.PropertyUnbounded;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
@@ -43,6 +47,8 @@ import org.apache.sling.api.resource.Res
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.i18n.ResourceBundleProvider;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -66,6 +72,9 @@ public class JcrResourceBundleProvider i
@Property(value = "en")
private static final String PROP_DEFAULT_LOCALE = "locale.default";
+
+ @Property(value = "en", unbounded = PropertyUnbounded.ARRAY)
+ private static final String PROP_PRELOAD_BUNDLES = "preload.bundles";
/** default log */
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -106,6 +115,12 @@ public class JcrResourceBundleProvider i
* {@link #getRootResourceBundle()}.
*/
private ResourceBundle rootResourceBundle;
+
+ private BundleContext bundleContext;
+
+ private List<ServiceRegistration> bundleServiceRegistrations;
+
+ private String[] preloadBundles;
// ---------- ResourceBundleProvider
---------------------------------------
@@ -156,7 +171,8 @@ public class JcrResourceBundleProvider i
*/
public void onEvent(EventIterator events) {
log.debug("onEvent: Resource changes, removing cached
ResourceBundles");
- resourceBundleCache.clear();
+ clearCache();
+ preloadBundles();
}
// ---------- SCR Integration
----------------------------------------------
@@ -182,6 +198,17 @@ public class JcrResourceBundleProvider i
String localeString = OsgiUtil.toString(props.get(PROP_DEFAULT_LOCALE),
null);
this.defaultLocale = toLocale(localeString);
+ this.preloadBundles =
OsgiUtil.toStringArray(props.get(PROP_PRELOAD_BUNDLES));
+
+ this.bundleContext = context.getBundleContext();
+ this.bundleServiceRegistrations = new ArrayList<ServiceRegistration>();
+ if (this.resourceResolverFactory != null) {
+ preloadBundles();
+ }
+ }
+
+ protected void deactivate() {
+ clearCache();
}
/**
@@ -194,6 +221,9 @@ public class JcrResourceBundleProvider i
releaseRepository();
}
this.resourceResolverFactory = resourceResolverFactory;
+ if (this.bundleContext != null) {
+ preloadBundles();
+ }
}
/**
@@ -236,6 +266,16 @@ public class JcrResourceBundleProvider i
"getResourceBundleInternal({}, {}): duplicate creation,
using existing ResourceBundle",
new Object[] { baseName, locale
});
+ } else {
+ synchronized (this) {
+ Dictionary serviceProps = new Hashtable();
+ if (key.baseName != null) {
+ serviceProps.put("baseName", key.baseName);
+ }
+ serviceProps.put("locale", key.locale.toString());
+ ServiceRegistration serviceReg =
bundleContext.registerService(ResourceBundle.class.getName(), resourceBundle,
serviceProps);
+ bundleServiceRegistrations.add(serviceReg);
+ }
}
}
@@ -375,6 +415,33 @@ public class JcrResourceBundleProvider i
return resourceResolver;
}
+
+ private void clearCache() {
+ resourceBundleCache.clear();
+ resourceBundleCache.clear();
+ synchronized (this) {
+ for (ServiceRegistration serviceReg : bundleServiceRegistrations) {
+ serviceReg.unregister();
+ }
+ bundleServiceRegistrations.clear();
+ }
+ }
+
+ private void preloadBundles() {
+ if (preloadBundles != null) {
+ for (String bundleSpec : preloadBundles) {
+ int idx = bundleSpec.indexOf("|");
+ if (idx > -1) {
+ String baseName = bundleSpec.substring(0, idx);
+ Locale locale = toLocale(bundleSpec.substring(idx + 1));
+ getResourceBundle(baseName, locale);
+ } else {
+ Locale locale = toLocale(bundleSpec);
+ getResourceBundle(locale);
+ }
+ }
+ }
+ }
/**
* Logs out from the repository and clears the resource bundle cache.
@@ -383,7 +450,7 @@ public class JcrResourceBundleProvider i
ResourceResolver resolver = this.resourceResolver;
this.resourceResolver = null;
- this.resourceBundleCache.clear();
+ clearCache();
if (resolver != null) {
Modified:
sling/trunk/contrib/extensions/i18n/src/main/resources/OSGI-INF/metatype/metatype.properties
URL:
http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/i18n/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1102051&r1=1102050&r2=1102051&view=diff
==============================================================================
---
sling/trunk/contrib/extensions/i18n/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++
sling/trunk/contrib/extensions/i18n/src/main/resources/OSGI-INF/metatype/metatype.properties
Wed May 11 19:46:32 2011
@@ -37,3 +37,7 @@ locale.default.name = Default Locale
locale.default.description = The default locale to assume if none can be \
resolved otherwise. This value must be in the form acceptable to the \
java.util.Locale class.
+
+preload.bundles.name = Bundles to Preload
+preload.bundles.description = A list of resource bundles which should be
pre-loaded \
+ automatically on startup or when a content change is detected.