You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2016/07/13 04:48:56 UTC

svn commit: r1752363 - in /sling/trunk/bundles/extensions/healthcheck/core/src: main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java

Author: cziegeler
Date: Wed Jul 13 04:48:55 2016
New Revision: 1752363

URL: http://svn.apache.org/viewvc?rev=1752363&view=rev
Log:
SLING-5839 : Implementation must not rely on SERVICE_PID

Modified:
    sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java
    sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java?rev=1752363&r1=1752362&r2=1752363&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/main/java/org/apache/sling/hc/core/impl/CompositeHealthCheck.java Wed Jul 13 04:48:55 2016
@@ -42,7 +42,6 @@ import org.apache.sling.hc.util.HealthCh
 import org.apache.sling.hc.util.HealthCheckMetadata;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
-import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentConstants;
 import org.osgi.service.component.ComponentContext;
@@ -87,14 +86,15 @@ public class CompositeHealthCheck implem
     private HealthCheckExecutor healthCheckExecutor;
 
     private BundleContext bundleContext;
-    private ServiceReference referenceToThis;
     private HealthCheckFilter healthCheckFilter;
-    
+
+    private volatile ComponentContext componentContext;
+
     @Activate
     protected void activate(final ComponentContext ctx) {
         bundleContext = ctx.getBundleContext();
+        componentContext = ctx;
         healthCheckFilter = new HealthCheckFilter(bundleContext);
-        referenceToThis = getReferenceByPid(PropertiesUtil.toString(ctx.getProperties().get(Constants.SERVICE_PID), "-1"));
 
         filterTags = PropertiesUtil.toStringArray(ctx.getProperties().get(PROP_FILTER_TAGS), new String[] {});
         log.debug("Activated, will select HealthCheck having tags {}", Arrays.asList(filterTags));
@@ -104,14 +104,15 @@ public class CompositeHealthCheck implem
     protected void deactivate() {
         bundleContext = null;
         healthCheckFilter = null;
-        referenceToThis = null;
+        componentContext = null;
     }
 
     @Override
     public Result execute() {
-
-        Result result = checkForRecursion(referenceToThis, new HashSet<String>());
-        if(result != null) {
+        final ComponentContext localCtx = this.componentContext;
+        final ServiceReference referenceToThis = localCtx == null ? null : localCtx.getServiceReference();
+        Result result = referenceToThis == null ? null : checkForRecursion(referenceToThis, new HashSet<String>());
+        if (result != null) {
             // return recursion error
             return result;
         }
@@ -135,8 +136,8 @@ public class CompositeHealthCheck implem
 
         String[] tagsForIncludedChecksArr = PropertiesUtil.toStringArray(hcReference.getProperty(PROP_FILTER_TAGS), new String[0]);
         Set<String> tagsForIncludedChecks = new HashSet<String>(Arrays.asList(tagsForIncludedChecksArr));
-        
-        
+
+
         log.debug("HC {} has banned tags {}", thisCheckMetadata.getName(), bannedTagsForThisCompositeCheck);
         log.debug("tagsForIncludedChecks {}", tagsForIncludedChecks);
 
@@ -144,7 +145,7 @@ public class CompositeHealthCheck implem
         Set<String> intersection = new HashSet<String>();
         intersection.addAll(bannedTagsForThisCompositeCheck);
         intersection.retainAll(tagsForIncludedChecks);
-        
+
         if (!intersection.isEmpty()) {
             return new Result(Status.HEALTH_CHECK_ERROR,
                     "INVALID CONFIGURATION: Cycle detected in composite health check hierarchy. Health check '" + thisCheckMetadata.getName()
@@ -152,7 +153,7 @@ public class CompositeHealthCheck implem
                             + " as a composite check in the hierarchy is itself already tagged alike (tags assigned to composite checks: "
                             + bannedTagsForThisCompositeCheck + ")");
         }
-        
+
         // check each sub composite check
         ServiceReference[] hcRefsOfCompositeCheck = healthCheckFilter.getTaggedHealthCheckServiceReferences(tagsForIncludedChecksArr);
         for (ServiceReference hcRefOfCompositeCheck : hcRefsOfCompositeCheck) {
@@ -172,29 +173,6 @@ public class CompositeHealthCheck implem
 
     }
 
-    private ServiceReference getReferenceByPid(String servicePid) {
-
-        if (servicePid == null) {
-            return null;
-        }
-
-        String filterString = "(" + Constants.SERVICE_PID + "=" + servicePid + ")";
-        ServiceReference[] refs = null;
-        try {
-            refs = bundleContext.getServiceReferences(HealthCheck.class.getName(), filterString);
-        } catch (InvalidSyntaxException e) {
-            log.error("Invalid filter " + filterString, e);
-        }
-        if (refs == null || refs.length == 0) {
-            return null;
-        } else if (refs.length == 1) {
-            return refs[0];
-        } else {
-            throw new IllegalStateException("OSGi Framework returned more than one service reference for unique service pid =" + servicePid);
-        }
-
-    }
-
     void setHealthCheckFilter(HealthCheckFilter healthCheckFilter) {
         this.healthCheckFilter = healthCheckFilter;
     }
@@ -207,8 +185,7 @@ public class CompositeHealthCheck implem
         this.healthCheckExecutor = healthCheckExecutor;
     }
 
-    void setReferenceToThis(ServiceReference referenceToThis) {
-        this.referenceToThis = referenceToThis;
+    void setComponentContext(ComponentContext ctx) {
+        this.componentContext = ctx;
     }
-
 }

Modified: sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java?rev=1752363&r1=1752362&r2=1752363&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java (original)
+++ sling/trunk/bundles/extensions/healthcheck/core/src/test/java/org/apache/sling/hc/core/impl/CompositeHealthCheckTest.java Wed Jul 13 04:48:55 2016
@@ -45,6 +45,7 @@ import org.osgi.framework.Bundle;
 import org.osgi.framework.Constants;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentConstants;
+import org.osgi.service.component.ComponentContext;
 
 public class CompositeHealthCheckTest {
 
@@ -54,12 +55,14 @@ public class CompositeHealthCheckTest {
     @Mock
     private HealthCheckExecutor healthCheckExecutor;
 
+    @Mock
+    private ComponentContext componentContext;
     @Before
     public void setup() {
         MockitoAnnotations.initMocks(this);
         compositeHealthCheck.setHealthCheckExecutor(healthCheckExecutor);
         compositeHealthCheck.setFilterTags(new String[] {});
-
+        compositeHealthCheck.setComponentContext(componentContext);
     }
 
     @Test
@@ -69,17 +72,17 @@ public class CompositeHealthCheckTest {
         String[] testTags = new String[] { "tag1" };
         compositeHealthCheck.setFilterTags(testTags);
 
-        
+
         List<HealthCheckExecutionResult> executionResults = new LinkedList<HealthCheckExecutionResult>();
         executionResults.add(createExecutionResult("Check 1", testTags, new Result(Result.Status.INFO, "Good")));
         executionResults.add(createExecutionResult("Check 2", testTags, new Result(Result.Status.CRITICAL, "Bad")));
 
         when(healthCheckExecutor.execute(testTags)).thenReturn(executionResults);
-        
+
         Result result = compositeHealthCheck.execute();
 
         verify(healthCheckExecutor, times(1)).execute(testTags);
-        
+
         assertEquals(Result.Status.CRITICAL, result.getStatus());
 
     }
@@ -98,7 +101,7 @@ public class CompositeHealthCheckTest {
         final DummyHcServiceReference hcRef = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
 
         // test check is hcRef
-        compositeHealthCheck.setReferenceToThis(hcRef);
+        doReturn(hcRef).when(componentContext).getServiceReference();
         compositeHealthCheck.setFilterTags(filterTags);
 
         compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) { // not using @Spy because varargs matcher does not work with spies
@@ -132,7 +135,7 @@ public class CompositeHealthCheckTest {
         final DummyHcServiceReference hcRef3 = new DummyHcServiceReference("Check 3", new String[] { "check3" }, new String[] { "check1" });
 
         // test check is hcRef1
-        compositeHealthCheck.setReferenceToThis(hcRef1);
+        doReturn(hcRef1).when(componentContext).getServiceReference();
         compositeHealthCheck.setFilterTags(filterTags);
 
         compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) {