Search in sources :

Example 11 with ServiceFactory

use of org.osgi.framework.ServiceFactory in project aries by apache.

the class ProviderBundleTrackerCustomizerGenericCapabilityTest method mockSPIBundleContext4.

@SuppressWarnings("unchecked")
private BundleContext mockSPIBundleContext4() {
    BundleContext implBC = EasyMock.createNiceMock(BundleContext.class);
    implBC.registerService(EasyMock.anyString(), EasyMock.anyObject(), (Dictionary<String, ?>) EasyMock.anyObject());
    EasyMock.expectLastCall().andAnswer(new IAnswer<ServiceRegistration<Object>>() {

        @Override
        public ServiceRegistration<Object> answer() throws Throwable {
            final String className = (String) EasyMock.getCurrentArguments()[0];
            final Object serviceObject = EasyMock.getCurrentArguments()[1];
            final Dictionary<String, Object> registrationProps = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
            return new ServiceRegistrationImpl(className, serviceObject, registrationProps);
        }
    }).anyTimes();
    implBC.getService(EasyMock.anyObject(ServiceReference.class));
    EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            ServiceRegistrationImpl reg = (ServiceRegistrationImpl) EasyMock.getCurrentArguments()[0];
            Object svc = reg.getServiceObject();
            if (svc instanceof ServiceFactory) {
                return ((ServiceFactory) svc).getService(null, reg);
            } else {
                return svc;
            }
        }
    }).anyTimes();
    EasyMock.replay(implBC);
    return implBC;
}
Also used : IAnswer(org.easymock.IAnswer) Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 12 with ServiceFactory

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

the class LogbackManager method registerEventHandler.

private void registerEventHandler() {
    Properties props = new Properties();
    props.put(Constants.SERVICE_VENDOR, "Apache Software Foundation");
    props.put(Constants.SERVICE_DESCRIPTION, "Sling Log Reset Event Handler");
    props.put("event.topics", new String[] { RESET_EVENT_TOPIC });
    registrations.add(bundleContext.registerService("org.osgi.service.event.EventHandler", new ServiceFactory() {

        private Object instance;

        @Override
        public Object getService(Bundle bundle, ServiceRegistration serviceRegistration) {
            synchronized (this) {
                if (this.instance == null) {
                    this.instance = new ConfigResetRequestHandler(LogbackManager.this);
                }
                return instance;
            }
        }

        @Override
        public void ungetService(Bundle bundle, ServiceRegistration serviceRegistration, Object o) {
        }
    }, props));
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) Properties(java.util.Properties) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 13 with ServiceFactory

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

the class MockedResourceResolverImplTest method before.

@SuppressWarnings("unchecked")
@Before
public void before() throws LoginException {
    activator = new ResourceResolverFactoryActivator();
    // system bundle access
    final Bundle systemBundle = Mockito.mock(Bundle.class);
    Mockito.when(systemBundle.getState()).thenReturn(Bundle.ACTIVE);
    Mockito.when(bundleContext.getBundle(Constants.SYSTEM_BUNDLE_LOCATION)).thenReturn(systemBundle);
    activator.resourceAccessSecurityTracker = new ResourceAccessSecurityTracker();
    activator.resourceProviderTracker = resourceProviderTracker;
    activator.changeListenerWhiteboard = resourceChangeListenerWhiteboard;
    activator.serviceUserMapper = Mockito.mock(ServiceUserMapper.class);
    handlers.add(createRPHandler(resourceProvider, "org.apache.sling.resourceresolver.impl.DummyTestProvider", 10L, "/single"));
    // setup mapping resources at /etc/map to exercise vanity etc.
    // hmm, can't provide the resolver since its not up and ready.
    // mapping almost certainly work properly until this can be setup correctly.
    buildMappingResource("/etc/map", mappingResourceProvider, null);
    handlers.add(createRPHandler(mappingResourceProvider, "org.apache.sling.resourceresolver.impl.MapProvider", 11L, "/etc"));
    handlers.add(createRPHandler(appsResourceProvider, "org.apache.sling.resourceresolver.impl.AppsProvider", 12L, "/libs"));
    handlers.add(createRPHandler(appsResourceProvider, "org.apache.sling.resourceresolver.impl.AppsProvider", 13L, "/apps"));
    handlers.add(createRPHandler(queriableResourceProviderA, "org.apache.sling.resourceresolver.impl.QueriableResourceProviderA", 14L, "/searchA"));
    Mockito.when(queriableResourceProviderA.getQueryLanguageProvider()).thenReturn(queryProvider);
    ResourceProviderStorage storage = new ResourceProviderStorage(handlers);
    Mockito.when(resourceProviderTracker.getResourceProviderStorage()).thenReturn(storage);
    // activate the components.
    activator.activate(bundleContext, new ResourceResolverFactoryConfig() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return null;
        }

        @Override
        public String[] resource_resolver_virtual() {
            return new String[] { "/:/" };
        }

        @Override
        public String[] resource_resolver_vanitypath_whitelist() {
            return null;
        }

        @Override
        public boolean resource_resolver_vanitypath_maxEntries_startup() {
            return true;
        }

        @Override
        public int resource_resolver_vanitypath_maxEntries() {
            return -1;
        }

        @Override
        public int resource_resolver_vanitypath_bloomfilter_maxBytes() {
            return 1024000;
        }

        @Override
        public String[] resource_resolver_vanitypath_blacklist() {
            return null;
        }

        @Override
        public boolean resource_resolver_vanity_precedence() {
            return false;
        }

        @Override
        public String[] resource_resolver_searchpath() {
            return new String[] { "/apps", "/libs" };
        }

        @Override
        public String[] resource_resolver_required_providers() {
            return new String[] { "org.apache.sling.resourceresolver.impl.DummyTestProvider" };
        }

        @Override
        public String[] resource_resolver_required_providernames() {
            return null;
        }

        @Override
        public boolean resource_resolver_providerhandling_paranoid() {
            return false;
        }

        @Override
        public boolean resource_resolver_optimize_alias_resolution() {
            return true;
        }

        @Override
        public String[] resource_resolver_mapping() {
            return new String[] { "/:/", "/content/:/", "/system/docroot/:/", "/content.html-/$" };
        }

        @Override
        public String[] resource_resolver_map_observation() {
            return new String[] { "/" };
        }

        @Override
        public String resource_resolver_map_location() {
            return MapEntries.DEFAULT_MAP_ROOT;
        }

        @Override
        public boolean resource_resolver_manglenamespaces() {
            return true;
        }

        @Override
        public boolean resource_resolver_log_closing() {
            return false;
        }

        @Override
        public boolean resource_resolver_enable_vanitypath() {
            return true;
        }

        @Override
        public int resource_resolver_default_vanity_redirect_status() {
            return 302;
        }

        @Override
        public boolean resource_resolver_allowDirect() {
            return true;
        }

        @Override
        public boolean resource_resolver_log_unclosed() {
            return true;
        }
    });
    // configure using Bundle
    Mockito.when(usingBundle.getBundleContext()).thenReturn(usingBundleContext);
    Mockito.when(usingBundleContext.getBundle()).thenReturn(usingBundle);
    // extract any services that were registered into a map.
    ArgumentCaptor<Class> classesCaptor = ArgumentCaptor.forClass(Class.class);
    ArgumentCaptor<ServiceFactory> serviceCaptor = ArgumentCaptor.forClass(ServiceFactory.class);
    @SuppressWarnings("rawtypes") ArgumentCaptor<Dictionary> propertiesCaptor = ArgumentCaptor.forClass(Dictionary.class);
    Mockito.verify(bundleContext, Mockito.atLeastOnce()).registerService((Class<ResourceResolverFactory>) classesCaptor.capture(), (ServiceFactory<ResourceResolverFactory>) serviceCaptor.capture(), propertiesCaptor.capture());
    int si = 0;
    List<ServiceFactory> serviceList = serviceCaptor.getAllValues();
    @SuppressWarnings({ "unused", "rawtypes" }) List<Dictionary> servicePropertiesList = propertiesCaptor.getAllValues();
    for (Class serviceClass : classesCaptor.getAllValues()) {
        services.put(serviceClass.getName(), serviceList.get(si));
        serviceProperties.put(serviceClass.getName(), serviceProperties.get(si));
        si++;
    }
    // verify that a ResourceResolverFactoryImpl was created and registered.
    Assert.assertNotNull("" + services, services.get(ResourceResolverFactory.class.getName()));
    Object rrf = services.get(ResourceResolverFactory.class.getName());
    if (rrf instanceof ServiceFactory) {
        rrf = ((ServiceFactory) rrf).getService(usingBundle, null);
    }
    Assert.assertTrue(rrf instanceof ResourceResolverFactoryImpl);
    resourceResolverFactory = (ResourceResolverFactoryImpl) rrf;
}
Also used : Dictionary(java.util.Dictionary) ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) ServiceUserMapper(org.apache.sling.serviceusermapping.ServiceUserMapper) Annotation(java.lang.annotation.Annotation) ResourceProviderStorage(org.apache.sling.resourceresolver.impl.providers.ResourceProviderStorage) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) Before(org.junit.Before)

Aggregations

ServiceFactory (org.osgi.framework.ServiceFactory)13 Bundle (org.osgi.framework.Bundle)11 ServiceRegistration (org.osgi.framework.ServiceRegistration)10 Dictionary (java.util.Dictionary)5 Hashtable (java.util.Hashtable)5 IAnswer (org.easymock.IAnswer)4 BundleContext (org.osgi.framework.BundleContext)4 ServiceReference (org.osgi.framework.ServiceReference)4 LinkedHashMap (java.util.LinkedHashMap)3 ProxyManager (org.apache.aries.proxy.ProxyManager)3 AsmProxyManager (org.apache.aries.proxy.impl.AsmProxyManager)3 CreateProxyRunnable (org.apache.karaf.service.guard.impl.GuardProxyCatalog.CreateProxyRunnable)3 ServiceRegistrationHolder (org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder)3 BundleWiring (org.osgi.framework.wiring.BundleWiring)3 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Test (org.junit.Test)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1