use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class ITFilterSupport method testNormalFilter.
@Test
public void testNormalFilter() {
TestAppender ta = registerAppender("filter", "TestAppender");
org.slf4j.Logger bar = LoggerFactory.getLogger("filter.foo.bar");
assertTrue(bar.isDebugEnabled());
bar.debug("Test");
assertEquals(1, ta.events.size());
SimpleFilter stf = new SimpleFilter();
Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("appenders", "TestAppender");
ServiceRegistration sr = bundleContext.registerService(Filter.class.getName(), stf, props);
delay();
assertNotNull("Filter should have context set", stf.getContext());
assertTrue("Filter should be started", stf.isStarted());
ta.events.clear();
//A filter attached to an appender cannot influence isXXXEnabled call
assertTrue(bar.isDebugEnabled());
//No events should be logged as filter would have rejected that
bar.debug("Test");
assertTrue(ta.events.isEmpty());
//Now unregister and earlier asserts should work
sr.unregister();
delay();
ta.events.clear();
assertTrue(bar.isDebugEnabled());
bar.debug("Test");
assertEquals(1, ta.events.size());
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class ITFilterSupport method registerAppender.
private TestAppender registerAppender(String prefix, String appenderName) {
TestAppender ta = new TestAppender(appenderName);
Dictionary<String, Object> props = new Hashtable<String, Object>();
String[] loggers = { prefix + ".foo.bar", prefix + ".foo.baz" };
ch.qos.logback.classic.Logger bar = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(loggers[0]);
bar.setLevel(Level.DEBUG);
ch.qos.logback.classic.Logger baz = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(loggers[1]);
baz.setLevel(Level.INFO);
props.put("loggers", loggers);
ServiceRegistration sr = bundleContext.registerService(Appender.class.getName(), ta, props);
delay();
return ta;
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class RequestLogger method createRequestLoggerService.
private static void createRequestLoggerService(Map<ServiceRegistration, RequestLoggerService> services, final BundleContext bundleContext, final boolean onEntry, final String format, final String output, final int outputType) {
final RequestLoggerService service = new RequestLoggerService(bundleContext, new RequestLoggerService.Config() {
@Override
public Class<? extends Annotation> annotationType() {
return RequestLoggerService.Config.class;
}
@Override
public int request_log_service_outputtype() {
return outputType;
}
@Override
public String request_log_service_output() {
return output;
}
@Override
public boolean request_log_service_onentry() {
return onEntry;
}
@Override
public String request_log_service_format() {
return format;
}
});
final ServiceRegistration reg = bundleContext.registerService(service.getClass().getName(), service, null);
services.put(reg, service);
}
use of org.osgi.framework.ServiceRegistration in project sling by apache.
the class Activator method start.
public void start(BundleContext bundleContext) {
// install handler for uncaught exceptions
oldHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(this);
// install thread handler shell command
register(bundleContext, new String[] { "org.apache.felix.shell.Command" }, new ServiceFactory() {
public void ungetService(final Bundle bundle, final ServiceRegistration reg, final Object consoleObject) {
// nothing to do
}
public Object getService(final Bundle bundle, final ServiceRegistration reg) {
return new ThreadDumpCommand();
}
}, null);
// install Web Console configuration printer
final Dictionary<String, Object> props = new Hashtable<String, Object>();
props.put("felix.webconsole.label", "slingthreads");
props.put("felix.webconsole.title", "Threads");
props.put("felix.webconsole.configprinter.modes", "always");
final ThreadDumperPanel tdp = new ThreadDumperPanel();
register(bundleContext, new String[] { tdp.getClass().getName() }, tdp, props);
}
use of org.osgi.framework.ServiceRegistration 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