Search in sources :

Example 61 with ServiceReference

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

the class HealthCheckMBeanCreator method registerHCMBean.

/**
     * Register an mbean for a health check service.
     * The mbean is only registered if
     * - the service has an mbean registration property
     * - if there is no other service with the same name but a higher service ranking
     *
     * @param bundleContext The bundle context
     * @param reference     The service reference to the health check service
     * @return The registered mbean or <code>null</code>
     */
private synchronized Object registerHCMBean(final BundleContext bundleContext, final ServiceReference reference) {
    final Registration reg = getRegistration(reference);
    if (reg != null) {
        this.registeredServices.put(reference, reg);
        List<ServiceReference> registered = this.sortedRegistrations.get(reg.name);
        if (registered == null) {
            registered = new ArrayList<ServiceReference>();
            this.sortedRegistrations.put(reg.name, registered);
        }
        registered.add(reference);
        // sort orders the references with lowest ranking first
        // we want the highest!
        Collections.sort(registered);
        final int lastIndex = registered.size() - 1;
        if (registered.get(lastIndex).equals(reference)) {
            if (registered.size() > 1) {
                final ServiceReference prevRef = registered.get(lastIndex - 1);
                final Registration prevReg = this.registeredServices.get(prevRef);
                prevReg.unregister();
            }
            reg.register(bundleContext);
        }
    }
    return reg;
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference)

Example 62 with ServiceReference

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

the class CompositeHealthCheckTest method testSimpleRecursion.

@Test
public void testSimpleRecursion() {
    // composite check referencing itself
    final String[] filterTags = new String[] { "check1" };
    final DummyHcServiceReference hcRef = new DummyHcServiceReference("Check 1", new String[] { "check1" }, filterTags);
    // test check is hcRef
    doReturn(hcRef).when(componentContext).getServiceReference();
    compositeHealthCheck.setFilterTags(filterTags);
    compositeHealthCheck.setHealthCheckFilter(new HealthCheckFilter(null) {

        @Override
        public ServiceReference[] getHealthCheckServiceReferences(HealthCheckSelector selector) {
            String[] tags = selector.tags();
            ServiceReference[] result = new ServiceReference[] {};
            if (tags.length > 0) {
                if (tags[0].equals(filterTags[0])) {
                    result = new ServiceReference[] { hcRef };
                }
            }
            return result;
        }
    });
    Result result = compositeHealthCheck.execute();
    verify(healthCheckExecutor, never()).execute(any(HealthCheckSelector.class));
    assertEquals(Result.Status.HEALTH_CHECK_ERROR, result.getStatus());
}
Also used : HealthCheckSelector(org.apache.sling.hc.api.execution.HealthCheckSelector) HealthCheckFilter(org.apache.sling.hc.util.HealthCheckFilter) ServiceReference(org.osgi.framework.ServiceReference) ExecutionResult(org.apache.sling.hc.core.impl.executor.ExecutionResult) Result(org.apache.sling.hc.api.Result) HealthCheckExecutionResult(org.apache.sling.hc.api.execution.HealthCheckExecutionResult) Test(org.junit.Test)

Example 63 with ServiceReference

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

the class HealthCheckExecutorImpl method getHealthCheckMetadata.

/**
     * Create the health check meta data
     */
private List<HealthCheckMetadata> getHealthCheckMetadata(final ServiceReference... healthCheckReferences) {
    final List<HealthCheckMetadata> descriptors = new LinkedList<HealthCheckMetadata>();
    for (final ServiceReference serviceReference : healthCheckReferences) {
        final HealthCheckMetadata descriptor = getHealthCheckMetadata(serviceReference);
        descriptors.add(descriptor);
    }
    return descriptors;
}
Also used : HealthCheckMetadata(org.apache.sling.hc.util.HealthCheckMetadata) LinkedList(java.util.LinkedList) ServiceReference(org.osgi.framework.ServiceReference)

Example 64 with ServiceReference

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

the class JmxAdjustableStatusForTestingIT method invokeMBean.

private void invokeMBean(String operation, Object[] args, String[] signature) throws Exception {
    ServiceReference[] serviceReference = bundleContext.getServiceReferences(DynamicMBean.class.getName(), "(jmx.objectname=org.apache.sling.healthcheck:type=AdjustableHealthCheckForTesting)");
    DynamicMBean mBean = (DynamicMBean) bundleContext.getService(serviceReference[0]);
    mBean.invoke(operation, args, signature);
}
Also used : DynamicMBean(javax.management.DynamicMBean) ServiceReference(org.osgi.framework.ServiceReference)

Example 65 with ServiceReference

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

the class ImplementsExtendsTest method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() throws ClassNotFoundException, MalformedURLException {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    when(bundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).then(new Answer<ServiceRegistration>() {

        @Override
        public ServiceRegistration answer(InvocationOnMock invocation) throws Throwable {
            final Dictionary<String, Object> props = (Dictionary<String, Object>) invocation.getArguments()[2];
            ServiceRegistration reg = mock(ServiceRegistration.class);
            ServiceReference ref = mock(ServiceReference.class);
            when(reg.getReference()).thenReturn(ref);
            when(ref.getProperty(anyString())).thenAnswer(new Answer<Object>() {

                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    String key = (String) invocation.getArguments()[0];
                    return props.get(key);
                }
            });
            return reg;
        }
    });
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2));
    factory.bindImplementationPicker(firstImplementationPicker, firstImplementationPickerProps);
    // simulate bundle add for ModelPackageBundleListener
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(ModelPackageBundleListener.PACKAGE_HEADER, "org.apache.sling.models.testmodels.classes.implextend");
    when(bundle.getHeaders()).thenReturn(headers);
    Vector<URL> classUrls = new Vector<URL>();
    classUrls.add(getClassUrl(ExtendsClassPropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel2.class));
    classUrls.add(getClassUrl(InvalidImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(InvalidSampleServiceInterface.class));
    classUrls.add(getClassUrl(SampleServiceInterface.class));
    classUrls.add(getClassUrl(SimplePropertyModel.class));
    when(bundle.findEntries(anyString(), anyString(), anyBoolean())).thenReturn(classUrls.elements());
    when(bundle.loadClass(anyString())).then(new Answer<Class<?>>() {

        @Override
        public Class<?> answer(InvocationOnMock invocation) throws ClassNotFoundException {
            String className = (String) invocation.getArguments()[0];
            return ImplementsExtendsTest.this.getClass().getClassLoader().loadClass(className);
        }
    });
    registeredAdapterFactories = (ServiceRegistration[]) factory.listener.addingBundle(bundle, bundleEvent);
}
Also used : Dictionary(java.util.Dictionary) SimplePropertyModel(org.apache.sling.models.testmodels.classes.implextend.SimplePropertyModel) ImplementsInterfacePropertyModel2(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel2) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) Vector(java.util.Vector) ServiceRegistration(org.osgi.framework.ServiceRegistration) ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) Hashtable(java.util.Hashtable) SampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.SampleServiceInterface) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) ImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) ServiceReference(org.osgi.framework.ServiceReference) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExtendsClassPropertyModel(org.apache.sling.models.testmodels.classes.implextend.ExtendsClassPropertyModel) Matchers.anyObject(org.mockito.Matchers.anyObject) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) Before(org.junit.Before)

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