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;
}
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());
}
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;
}
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);
}
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);
}
Aggregations