Search in sources :

Example 56 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class JobConsumerManagerTest method testSubCategoryMappingExecutor.

@Test
public void testSubCategoryMappingExecutor() {
    final BundleContext bc = Mockito.mock(BundleContext.class);
    final JobConsumerManager jcs = new JobConsumerManager();
    jcs.activate(bc, getDefaultConfig());
    final JobExecutor jc1 = Mockito.mock(JobExecutor.class);
    final ServiceReference ref1 = Mockito.mock(ServiceReference.class);
    Mockito.when(ref1.getProperty(JobExecutor.PROPERTY_TOPICS)).thenReturn("a/**");
    Mockito.when(ref1.getProperty(Constants.SERVICE_RANKING)).thenReturn(1);
    Mockito.when(ref1.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
    Mockito.when(bc.getService(ref1)).thenReturn(jc1);
    jcs.bindJobExecutor(ref1);
    assertNotNull(jcs.getExecutor("a/b"));
    assertNull(jcs.getExecutor("a"));
    assertNotNull(jcs.getExecutor("a/c"));
    assertNotNull(jcs.getExecutor("a/b/a"));
}
Also used : JobExecutor(org.apache.sling.event.jobs.consumer.JobExecutor) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 57 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class HealthCheckExecutorImpl method execute.

@Override
public List<HealthCheckExecutionResult> execute(HealthCheckSelector selector, HealthCheckExecutionOptions options) {
    logger.debug("Starting executing checks for filter selector {} and execution options {}", selector, options);
    final HealthCheckFilter filter = new HealthCheckFilter(this.bundleContext);
    try {
        final ServiceReference[] healthCheckReferences = filter.getHealthCheckServiceReferences(selector, options.isCombineTagsWithOr());
        return this.execute(healthCheckReferences, options);
    } finally {
        filter.dispose();
    }
}
Also used : HealthCheckFilter(org.apache.sling.hc.util.HealthCheckFilter) ServiceReference(org.osgi.framework.ServiceReference)

Example 58 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class CompositeHealthCheck method execute.

@Override
public Result execute() {
    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;
    }
    FormattingResultLog resultLog = new FormattingResultLog();
    List<HealthCheckExecutionResult> executionResults = healthCheckExecutor.execute(HealthCheckSelector.tags(filterTags));
    resultLog.debug("Executing {} HealthChecks selected by tags {}", executionResults.size(), Arrays.asList(filterTags));
    result = new CompositeResult(resultLog, executionResults);
    return result;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ServiceReference(org.osgi.framework.ServiceReference) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) HashSet(java.util.HashSet)

Example 59 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class CompositeHealthCheck method checkForRecursion.

Result checkForRecursion(ServiceReference hcReference, Set<String> alreadyBannedTags) {
    HealthCheckMetadata thisCheckMetadata = new HealthCheckMetadata(hcReference);
    Set<String> bannedTagsForThisCompositeCheck = new HashSet<String>();
    bannedTagsForThisCompositeCheck.addAll(alreadyBannedTags);
    bannedTagsForThisCompositeCheck.addAll(thisCheckMetadata.getTags());
    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);
    // is this HC ok?
    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() + "' (" + hcReference.getProperty(Constants.SERVICE_ID) + ") must not have tag(s) " + intersection + " 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.getHealthCheckServiceReferences(HealthCheckSelector.tags(tagsForIncludedChecksArr));
    for (ServiceReference hcRefOfCompositeCheck : hcRefsOfCompositeCheck) {
        if (CompositeHealthCheck.class.getName().equals(hcRefOfCompositeCheck.getProperty(ComponentConstants.COMPONENT_NAME))) {
            log.debug("Checking sub composite HC {}, {}", hcRefOfCompositeCheck, hcRefOfCompositeCheck.getProperty(ComponentConstants.COMPONENT_NAME));
            Result result = checkForRecursion(hcRefOfCompositeCheck, bannedTagsForThisCompositeCheck);
            if (result != null) {
                // found recursion
                return result;
            }
        }
    }
    // no recursion detected
    return null;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) HashSet(java.util.HashSet) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) ServiceReference(org.osgi.framework.ServiceReference)

Example 60 with ServiceReference

use of org.osgi.framework.ServiceReference in project sling by apache.

the class HealthCheckMBeanCreator method unregisterHCMBean.

private synchronized void unregisterHCMBean(final BundleContext bundleContext, final ServiceReference ref) {
    final Registration reg = registeredServices.remove(ref);
    if (reg != null) {
        final boolean registerFirst = reg.unregister();
        final List<ServiceReference> registered = this.sortedRegistrations.get(reg.name);
        registered.remove(ref);
        if (registered.size() == 0) {
            this.sortedRegistrations.remove(reg.name);
        } else if (registerFirst) {
            final ServiceReference newRef = registered.get(0);
            final Registration newReg = this.registeredServices.get(newRef);
            newReg.register(bundleContext);
        }
        bundleContext.ungetService(ref);
    }
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65