incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1178194 - /sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
Date Sun, 02 Oct 2011 14:55:26 GMT
Author: fmeschbe
Date: Sun Oct  2 14:55:26 2011
New Revision: 1178194

URL: http://svn.apache.org/viewvc?rev=1178194&view=rev
Log:
SLING-2235 Use size limited, access ordered cache for bundle loggers and remove entries for
uninstalled bundles

Modified:
    sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java

Modified: sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java?rev=1178194&r1=1178193&r2=1178194&view=diff
==============================================================================
--- sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
(original)
+++ sling/trunk/bundles/commons/logservice/src/main/java/org/apache/sling/commons/logservice/internal/LogSupport.java
Sun Oct  2 14:55:26 2011
@@ -18,7 +18,7 @@ package org.apache.sling.commons.logserv
 
 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 Bundl
 
     // 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 Bundl
                 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 Bundl
      */
     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 Bundl
             }
 
             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.
      */



Mime
View raw message