ecolombo commented on code in PR #15:
URL: 
https://github.com/apache/sling-org-apache-sling-i18n/pull/15#discussion_r1716440867


##########
src/main/java/org/apache/sling/i18n/impl/JcrResourceBundleProvider.java:
##########
@@ -791,4 +733,102 @@ public String toString() {
             return "Key(" + baseName + ", " + locale + ")";
         }
     }
+
+    /**
+     * Registry of the loaded <code>resource bundles</code> and the associated 
<code>service registrations</code>
+     * The <code>ResourceBundleRegistry</code> takes care of the 
registration/deregistration of the resource bundles as OSGi services.
+     * It stores the references to the registered resource bundles and to the 
associated service registrations.
+     */
+    private static class ResourceBundleRegistry {
+        private final Logger log = LoggerFactory.getLogger(getClass());
+        private final BundleContext bundleContext;
+        final AtomicBoolean isClosed = new AtomicBoolean(false);
+        private final Map<Key, Entry> bundleServiceRegistrations = new 
ConcurrentHashMap<>();
+
+        private static class Entry {
+            final JcrResourceBundle resourceBundle;
+            final ServiceRegistration<ResourceBundle> serviceRegistration;
+            Entry(JcrResourceBundle resourceBundle, 
ServiceRegistration<ResourceBundle> serviceRegistration) {
+                this.resourceBundle = resourceBundle;
+                this.serviceRegistration = serviceRegistration;
+            }
+        }
+
+        ResourceBundleRegistry(BundleContext bundleContext) {
+            this.bundleContext = bundleContext;
+        }
+
+        JcrResourceBundle getRB(Key key) {
+            Entry entry = bundleServiceRegistrations.get(key);
+            return entry != null ? entry.resourceBundle : null;
+        }
+
+        Collection<JcrResourceBundle> getRBs() {
+            return bundleServiceRegistrations.values().stream().map(e -> 
e.resourceBundle).collect(Collectors.toList());
+        }
+
+        void updateRB(Key key, JcrResourceBundle resourceBundle) {
+            if (isClosed.get()) {
+                return;
+            }
+            ServiceRegistration<ResourceBundle> serviceReg = 
bundleContext.registerService(ResourceBundle.class, resourceBundle, 
serviceProps(key));
+            Entry oldEntry = bundleServiceRegistrations.put(key, new 
Entry(resourceBundle, serviceReg));
+            if (oldEntry != null) {
+                oldEntry.serviceRegistration.unregister();
+            }
+            log.debug("[ResourceBundleRegistry.updateRB] Registry updated - Nr 
of entries: {} - Keys: {}", bundleServiceRegistrations.size(), 
bundleServiceRegistrations.keySet());
+        }
+
+        private static Dictionary<String, Object> serviceProps(Key key) {
+            Dictionary<String, Object> serviceProps = new Hashtable<>();
+            if (key.baseName != null) {
+                serviceProps.put("baseName", key.baseName);
+            }
+            serviceProps.put("locale", key.locale.toString());
+            return serviceProps;
+        }
+
+        void unregisterRB(Key key) {
+            if (isClosed.get()) {
+                return;
+            }
+            Entry oldEntry = bundleServiceRegistrations.remove(key);
+            if (oldEntry!= null) {
+                oldEntry.serviceRegistration.unregister();
+            } else {
+                log.warn("[ResourceBundleRegistry.unregisterRB] Could not find 
resource bundle service for {}", key);
+            }
+        }
+
+        void clear() {
+            if (isClosed.get()) {
+                return;
+            }
+            clearInternal();
+        }
+
+        private void clearInternal() {
+            log.debug("[ResourceBundleRegistry.clearInternal] Before - Nr of 
Keys: {} - Keys: {}", bundleServiceRegistrations.size(), 
bundleServiceRegistrations.keySet());
+            List<ServiceRegistration<ResourceBundle>> regs = new ArrayList<>();
+            Iterator<java.util.Map.Entry<Key, Entry>> iterator = 
bundleServiceRegistrations.entrySet().iterator();
+            while(iterator.hasNext()) {
+                regs.add(iterator.next().getValue().serviceRegistration);
+                iterator.remove();
+            }

Review Comment:
   Implemented, seems even more robust and code still quite clean.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to