You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2011/05/11 21:46:33 UTC

svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

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.



Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Justin Edelson <ju...@justinedelson.com>.
Committed a change so that all bundles are preloaded. I'd like to
optimize the implementation a bit more, but I thought this was good
enough to get some feedback.

Thanks,
Justin

On Thu, May 12, 2011 at 9:29 AM, Justin Edelson
<ju...@justinedelson.com> wrote:
> Well, I was trying to keep in the spirit of lazy loading and I see I
> actually messed up by making preload.bundles default to "en" (had
> intended to keep it blank by default).
>
> Proxies seem like overkill to me and you lose the automatic
> notification provided by services registration/unregistration - I'd
> rather just register all available ResourceBundles on startup. WDYT?
>
> Justin
>
> On Thu, May 12, 2011 at 12:39 AM, Carsten Ziegeler <cz...@apache.org> wrote:
>> Isn't there maybe a better way which doesn't require a configuration?
>> It seems a little bit strange to me that resource bundles are only
>> registered if they are either configured or loaded by some other means.
>> I would rather expect to get the resource bundle as a service in all cases.
>>
>> So maybe we should separate registering from the actual loading and
>> maybe just register proxies or something?
>>
>> Regards
>> Carsten
>>
>>> 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
>>
>>
>> --
>> Carsten Ziegeler
>> cziegeler@apache.org
>>
>

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Carsten Ziegeler <cz...@apache.org>.
Justin Edelson  wrote:
> On Fri, May 13, 2011 at 5:24 PM, Carsten Ziegeler <cz...@apache.org> wrote:
>> Justin Edelson  wrote
>>>
>>> It looks like simply running the query will add a few seconds to
>>> startup, so in r1102729, I made preloading optional. Proxying won't
>>> help because we don't know the set of bundles to proxy without doing
>>> the query. We could proxy the default locale bundle, but that's
>>> inching back to preloading by configuration.
>>>
>> Yes, that's right - usually if a query is involved we do async setup -
>> so starting the query in a new thread started by activate. What do you
>> think about this? Sorry for being a pita on this issue :)
> 
> Done in r1103946.
> 
> Am I good to close SLING-2061?
> 
Yes and many thanks!

Carsten

-- 
Carsten Ziegeler
cziegeler@apache.org

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Justin Edelson <ju...@justinedelson.com>.
On Fri, May 13, 2011 at 5:24 PM, Carsten Ziegeler <cz...@apache.org> wrote:
> Justin Edelson  wrote
>>
>> It looks like simply running the query will add a few seconds to
>> startup, so in r1102729, I made preloading optional. Proxying won't
>> help because we don't know the set of bundles to proxy without doing
>> the query. We could proxy the default locale bundle, but that's
>> inching back to preloading by configuration.
>>
> Yes, that's right - usually if a query is involved we do async setup -
> so starting the query in a new thread started by activate. What do you
> think about this? Sorry for being a pita on this issue :)

Done in r1103946.

Am I good to close SLING-2061?

Thanks,
Justin

>
> Regards
> Carsten
> --
> Carsten Ziegeler
> cziegeler@apache.org
>

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Carsten Ziegeler <cz...@apache.org>.
Justin Edelson  wrote
> 
> It looks like simply running the query will add a few seconds to
> startup, so in r1102729, I made preloading optional. Proxying won't
> help because we don't know the set of bundles to proxy without doing
> the query. We could proxy the default locale bundle, but that's
> inching back to preloading by configuration.
> 
Yes, that's right - usually if a query is involved we do async setup -
so starting the query in a new thread started by activate. What do you
think about this? Sorry for being a pita on this issue :)

Regards
Carsten
-- 
Carsten Ziegeler
cziegeler@apache.org

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Justin Edelson <ju...@justinedelson.com>.
On Thu, May 12, 2011 at 10:11 AM, Carsten Ziegeler <cz...@apache.org> wrote:
> Am 12.05.11 06:29, schrieb Justin Edelson:
>> Well, I was trying to keep in the spirit of lazy loading and I see I
>> actually messed up by making preload.bundles default to "en" (had
>> intended to keep it blank by default).
>>
>> Proxies seem like overkill to me and you lose the automatic
>> notification provided by services registration/unregistration - I'd
>> rather just register all available ResourceBundles on startup. WDYT?
>>
> Yeah, sounds good to me - I just thought about proxie as this would keep
> the lazy loading approach and maybe reduce startup time etc.
> I haven't looked at your new implementation yet - as long as it is not
> increasing startup time I'm fine :)

It looks like simply running the query will add a few seconds to
startup, so in r1102729, I made preloading optional. Proxying won't
help because we don't know the set of bundles to proxy without doing
the query. We could proxy the default locale bundle, but that's
inching back to preloading by configuration.

Justin

>
> Regards
> Carsten
>
>
> --
> Carsten Ziegeler
> cziegeler@apache.org
>

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Carsten Ziegeler <cz...@apache.org>.
Am 12.05.11 06:29, schrieb Justin Edelson:
> Well, I was trying to keep in the spirit of lazy loading and I see I
> actually messed up by making preload.bundles default to "en" (had
> intended to keep it blank by default).
> 
> Proxies seem like overkill to me and you lose the automatic
> notification provided by services registration/unregistration - I'd
> rather just register all available ResourceBundles on startup. WDYT?
> 
Yeah, sounds good to me - I just thought about proxie as this would keep
the lazy loading approach and maybe reduce startup time etc.
I haven't looked at your new implementation yet - as long as it is not
increasing startup time I'm fine :)

Regards
Carsten


-- 
Carsten Ziegeler
cziegeler@apache.org

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Justin Edelson <ju...@justinedelson.com>.
Well, I was trying to keep in the spirit of lazy loading and I see I
actually messed up by making preload.bundles default to "en" (had
intended to keep it blank by default).

Proxies seem like overkill to me and you lose the automatic
notification provided by services registration/unregistration - I'd
rather just register all available ResourceBundles on startup. WDYT?

Justin

On Thu, May 12, 2011 at 12:39 AM, Carsten Ziegeler <cz...@apache.org> wrote:
> Isn't there maybe a better way which doesn't require a configuration?
> It seems a little bit strange to me that resource bundles are only
> registered if they are either configured or loaded by some other means.
> I would rather expect to get the resource bundle as a service in all cases.
>
> So maybe we should separate registering from the actual loading and
> maybe just register proxies or something?
>
> Regards
> Carsten
>
>> 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
>
>
> --
> Carsten Ziegeler
> cziegeler@apache.org
>

Re: svn commit: r1102051 - in /sling/trunk/contrib/extensions/i18n/src/main: java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java resources/OSGI-INF/metatype/metatype.properties

Posted by Carsten Ziegeler <cz...@apache.org>.
Isn't there maybe a better way which doesn't require a configuration?
It seems a little bit strange to me that resource bundles are only
registered if they are either configured or loaded by some other means.
I would rather expect to get the resource bundle as a service in all cases.

So maybe we should separate registering from the actual loading and
maybe just register proxies or something?

Regards
Carsten

> 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


-- 
Carsten Ziegeler
cziegeler@apache.org