You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:22:03 UTC

[sling-org-apache-sling-commons-logservice] 02/04: SLING-2235 Use size limited, access ordered cache for bundle loggers and remove entries for uninstalled bundles

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.logservice-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-logservice.git

commit 035cdda8857f738aa75a729d09ebe4d670da4a89
Author: Felix Meschberger <fm...@apache.org>
AuthorDate: Sun Oct 2 14:55:26 2011 +0000

    SLING-2235 Use size limited, access ordered cache for bundle loggers and remove entries for uninstalled bundles
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/logservice@1178194 13f79535-47bb-0310-9956-ffa450edef68
---
 .../commons/logservice/internal/LogSupport.java    | 34 +++++++++++++++++++---
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java b/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
index 8a78926..d40d6e0 100644
--- a/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
+++ b/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
@@ -18,7 +18,7 @@ package org.apache.sling.commons.logservice.internal;
 
 import java.util.Collections;
 import java.util.Enumeration;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -67,7 +67,15 @@ public class LogSupport implements BundleListener, ServiceListener,
 
     // The loggers by bundle id used for logging messages originated from
     // specific bundles
-    private Map<Long, Logger> loggers = new HashMap<Long, Logger>();
+    @SuppressWarnings("serial")
+    private Map<Long, Logger> loggers = new LinkedHashMap<Long, Logger>(16,
+        0.75f, true) {
+        private static final int MAX_SIZE = 50;
+
+        protected boolean removeEldestEntry(Map.Entry<Long, Logger> eldest) {
+            return size() > MAX_SIZE;
+        }
+    };
 
     // the worker thread actually sending LogEvents to LogListeners
     private LogEntryDispatcher logEntryDispatcher;
@@ -233,6 +241,8 @@ public class LogSupport implements BundleListener, ServiceListener,
                 message = "BundleEvent UPDATED";
                 break;
             case BundleEvent.UNINSTALLED:
+                // remove any cached logger for the uninstalled bundle
+                ungetLogger(event.getBundle());
                 message = "BundleEvent UNINSTALLED";
                 break;
             case BundleEvent.RESOLVED:
@@ -366,7 +376,10 @@ public class LogSupport implements BundleListener, ServiceListener,
      */
     private Logger getLogger(Bundle bundle) {
         Long bundleId = new Long((bundle == null) ? 0 : bundle.getBundleId());
-        Logger log = loggers.get(bundleId);
+        Logger log;
+        synchronized (loggers) {
+            log = loggers.get(bundleId);
+        }
         if (log == null) {
 
             String name;
@@ -392,12 +405,25 @@ public class LogSupport implements BundleListener, ServiceListener,
             }
 
             log = LoggerFactory.getLogger(name);
-            loggers.put(bundleId, log);
+            synchronized (loggers) {
+                loggers.put(bundleId, log);
+            }
         }
         return log;
     }
 
     /**
+     * Removes the cached logger for the given bundle, for example if the
+     * bundle is uninstalled and thus there will be no more logs from this
+     * bundle.
+     */
+    private void ungetLogger(Bundle bundle) {
+        synchronized (loggers) {
+            loggers.remove(bundle.getBundleId());
+        }
+    }
+
+    /**
      * Actually logs the given log entry to the logger for the bundle recorded
      * in the log entry.
      */

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.