incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1519454 - in /sling/trunk/contrib/extensions/healthcheck: api/src/main/java/org/apache/sling/hc/util/ healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/ webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/
Date Mon, 02 Sep 2013 14:47:09 GMT
Author: cziegeler
Date: Mon Sep  2 14:47:08 2013
New Revision: 1519454

URL: http://svn.apache.org/r1519454
Log:
Correctly free service references

Modified:
    sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/util/HealthCheckFilter.java
    sling/trunk/contrib/extensions/healthcheck/healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java
    sling/trunk/contrib/extensions/healthcheck/webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/HealthCheckWebconsolePlugin.java

Modified: sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/util/HealthCheckFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/util/HealthCheckFilter.java?rev=1519454&r1=1519453&r2=1519454&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/util/HealthCheckFilter.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/api/src/main/java/org/apache/sling/hc/util/HealthCheckFilter.java
Mon Sep  2 14:47:08 2013
@@ -20,7 +20,9 @@ package org.apache.sling.hc.util;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import org.apache.sling.hc.api.HealthCheck;
 import org.osgi.framework.BundleContext;
@@ -29,13 +31,28 @@ import org.osgi.framework.ServiceReferen
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/** Select from available {@link HealthCheck} services */
+/**
+ * Select from available {@link HealthCheck} services.
+ * Once this filter object and the returned health check services are no longer
+ * be used {@link #dispose()} should be called, to free the service
+ * references.
+ *
+ * This class is not thread safe and instances shouldn't be used concurrently
+ * from different threads.
+ */
 public class HealthCheckFilter {
 
     private final Logger log = LoggerFactory.getLogger(getClass());
     private final BundleContext bundleContext;
+
+
     public static final String OMIT_PREFIX = "-";
 
+    private final Set<ServiceReference> usedReferences = new HashSet<ServiceReference>();
+
+    /**
+     * Create a new filter object
+     */
     public HealthCheckFilter(final BundleContext bc) {
         bundleContext = bc;
     }
@@ -57,6 +74,7 @@ public class HealthCheckFilter {
                 final HealthCheck hc = (HealthCheck)bundleContext.getService(ref);
                 log.debug("Selected HealthCheck service {}", hc);
                 if ( hc != null ) {
+                    this.usedReferences.add(ref);
                     result.add(hc);
                 }
             }
@@ -104,4 +122,14 @@ public class HealthCheckFilter {
             return new ServiceReference[0];
         }
     }
+
+    /**
+     * Dispose all used service references
+     */
+    public void dispose() {
+        for(final ServiceReference ref : this.usedReferences) {
+            this.bundleContext.ungetService(ref);
+        }
+        this.usedReferences.clear();
+    }
 }

Modified: sling/trunk/contrib/extensions/healthcheck/healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java?rev=1519454&r1=1519453&r2=1519454&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/healthchecks/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java
Mon Sep  2 14:47:08 2013
@@ -71,37 +71,41 @@ public class CompositeHealthCheck implem
     @Override
     public Result execute() {
         final FormattingResultLog resultLog = new FormattingResultLog();
-        final List<HealthCheck> checks = new HealthCheckFilter(bundleContext).getTaggedHealthChecks(filterTags);
-        if(checks.size() == 0) {
-            resultLog.warn("HealthCheckFilter returns no HealthCheck for tags {}", Arrays.asList(filterTags));
-            return new Result(resultLog);
-        }
-
-        int executed = 0;
-        resultLog.debug("Executing {} HealthCheck selected by the {} tags", checks.size(),
Arrays.asList(filterTags));
-        int failures = 0;
-        for(HealthCheck hc : checks) {
-            if(hc == this) {
-                resultLog.info("Cowardly forfeiting execution of this HealthCheck in an infinite
loop, ignoring it");
-                continue;
+        final HealthCheckFilter filter = new HealthCheckFilter(bundleContext);
+        try {
+            final List<HealthCheck> checks = filter.getTaggedHealthChecks(filterTags);
+            if(checks.size() == 0) {
+                resultLog.warn("HealthCheckFilter returns no HealthCheck for tags {}", Arrays.asList(filterTags));
+                return new Result(resultLog);
             }
-            resultLog.debug("Executing {}", hc);
-            executed++;
-            final Result sub = hc.execute();
-            if(!sub.isOk()) {
-                failures++;
-            }
-            for(ResultLog.Entry e : sub) {
-                resultLog.add(e);
+
+            int executed = 0;
+            resultLog.debug("Executing {} HealthCheck selected by the {} tags", checks.size(),
Arrays.asList(filterTags));
+            int failures = 0;
+            for(HealthCheck hc : checks) {
+                if(hc == this) {
+                    resultLog.info("Cowardly forfeiting execution of this HealthCheck in
an infinite loop, ignoring it");
+                    continue;
+                }
+                resultLog.debug("Executing {}", hc);
+                executed++;
+                final Result sub = hc.execute();
+                if(!sub.isOk()) {
+                    failures++;
+                }
+                for(ResultLog.Entry e : sub) {
+                    resultLog.add(e);
+                }
             }
-        }
 
-        if(failures == 0) {
-            resultLog.debug("{} HealthCheck executed, all ok", executed);
-        } else {
-            resultLog.warn("{} HealthCheck executed, {} failures", executed, failures);
+            if(failures == 0) {
+                resultLog.debug("{} HealthCheck executed, all ok", executed);
+            } else {
+                resultLog.warn("{} HealthCheck executed, {} failures", executed, failures);
+            }
+        } finally {
+            filter.dispose();
         }
-
         return new Result(resultLog);
     }
 }
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/healthcheck/webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/HealthCheckWebconsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/HealthCheckWebconsolePlugin.java?rev=1519454&r1=1519453&r2=1519454&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/HealthCheckWebconsolePlugin.java
(original)
+++ sling/trunk/contrib/extensions/healthcheck/webconsole/src/main/java/org/apache/sling/hc/webconsole/impl/HealthCheckWebconsolePlugin.java
Mon Sep  2 14:47:08 2013
@@ -113,13 +113,17 @@ public class HealthCheckWebconsolePlugin
             for(final ServiceReference ref : references) {
                 final HealthCheck hc = (HealthCheck) this.bundleContext.getService(ref);
                 if ( hc != null ) {
-                    final Result r = hc.execute();
-                    total++;
-                    if (!r.isOk()) {
-                        failed++;
-                    }
-                    if (!quiet || !r.isOk()) {
-                        renderResult(resp, ref, hc, r, debug);
+                    try {
+                        final Result r = hc.execute();
+                        total++;
+                        if (!r.isOk()) {
+                            failed++;
+                        }
+                        if (!quiet || !r.isOk()) {
+                            renderResult(resp, ref, hc, r, debug);
+                        }
+                    } finally {
+                        this.bundleContext.ungetService(ref);
                     }
                 }
             }



Mime
View raw message