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