Search in sources :

Example 41 with ServiceRegistration

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());
}
Also used : MatchingFilter(ch.qos.logback.classic.turbo.MatchingFilter) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(ch.qos.logback.core.filter.Filter) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 42 with ServiceRegistration

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;
}
Also used : Appender(ch.qos.logback.core.Appender) Hashtable(java.util.Hashtable) Logger(ch.qos.logback.classic.Logger) Logger(ch.qos.logback.classic.Logger) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 43 with ServiceRegistration

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);
}
Also used : Annotation(java.lang.annotation.Annotation) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 44 with ServiceRegistration

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);
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 45 with ServiceRegistration

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

ServiceRegistration (org.osgi.framework.ServiceRegistration)188 Test (org.junit.Test)80 Hashtable (java.util.Hashtable)61 BundleContext (org.osgi.framework.BundleContext)35 Bundle (org.osgi.framework.Bundle)26 Dictionary (java.util.Dictionary)23 Properties (java.util.Properties)22 HashMap (java.util.HashMap)21 ServiceReference (org.osgi.framework.ServiceReference)21 ArrayList (java.util.ArrayList)18 Map (java.util.Map)13 IOException (java.io.IOException)8 ServiceFactory (org.osgi.framework.ServiceFactory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 URL (java.net.URL)6 HashSet (java.util.HashSet)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 ObjectName (javax.management.ObjectName)5 InitialContext (javax.naming.InitialContext)5