use of org.osgi.framework.BundleContext in project sling by apache.
the class AdapterImplementationsTest method testResourceTypeRegistrationForRequest.
@Test
public void testResourceTypeRegistrationForRequest() {
when(resource.getResourceType()).thenReturn("sling/rt/one");
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.getParentResourceType(resource)).thenReturn(null);
when(resourceResolver.getSearchPath()).thenReturn(new String[] { "/apps/", "/libs/" });
when(request.getResource()).thenReturn(resource);
// ensure we don't have any registrations for 'sling/rt/one'
assertNull(underTest.getModelClassForRequest(request));
assertNull(underTest.getModelClassForResource(resource));
// now add a mapping for SlingHttpServletRequest -> String
BundleContext bundleContext = MockOsgi.newBundleContext();
underTest.registerModelToResourceType(bundleContext.getBundle(), "sling/rt/one", SlingHttpServletRequest.class, String.class);
underTest.registerModelToResourceType(bundleContext.getBundle(), "sling/rt/one", Resource.class, Integer.class);
assertEquals(String.class, underTest.getModelClassForRequest(request));
assertEquals(Integer.class, underTest.getModelClassForResource(resource));
// ensure that trying to reregister the resource type is a no-op
BundleContext secondBundleContext = MockOsgi.newBundleContext();
underTest.registerModelToResourceType(secondBundleContext.getBundle(), "sling/rt/one", SlingHttpServletRequest.class, Integer.class);
assertEquals(String.class, underTest.getModelClassForRequest(request));
underTest.removeResourceTypeBindings(bundleContext.getBundle());
assertNull(underTest.getModelClassForRequest(request));
assertNull(underTest.getModelClassForResource(resource));
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class AdapterImplementationsTest method testResourceTypeRegistrationForResource.
@Test
public void testResourceTypeRegistrationForResource() {
when(resource.getResourceType()).thenReturn("sling/rt/one");
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(childResource.getResourceType()).thenReturn("sling/rt/child");
when(childResource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.getParentResourceType(resource)).thenReturn(null);
when(resourceResolver.getParentResourceType(childResource)).thenReturn("sling/rt/one");
when(resourceResolver.getSearchPath()).thenReturn(new String[] { "/apps/", "/libs/" });
// ensure we don't have any registrations for 'sling/rt/one'
assertNull(underTest.getModelClassForResource(resource));
assertNull(underTest.getModelClassForResource(childResource));
// now add a mapping for Resource -> String
BundleContext bundleContext = MockOsgi.newBundleContext();
underTest.registerModelToResourceType(bundleContext.getBundle(), "sling/rt/one", Resource.class, String.class);
assertEquals(String.class, underTest.getModelClassForResource(resource));
assertEquals(String.class, underTest.getModelClassForResource(childResource));
// ensure that trying to reregister the resource type is a no-op
BundleContext secondBundleContext = MockOsgi.newBundleContext();
underTest.registerModelToResourceType(secondBundleContext.getBundle(), "sling/rt/one", Resource.class, Integer.class);
assertEquals(String.class, underTest.getModelClassForResource(resource));
assertEquals(String.class, underTest.getModelClassForResource(childResource));
underTest.removeResourceTypeBindings(bundleContext.getBundle());
assertNull(underTest.getModelClassForResource(resource));
assertNull(underTest.getModelClassForResource(childResource));
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class SlingSettingsServiceImplTest method createSlingSettingsService.
private SlingSettingsService createSlingSettingsService(final File slingIdFile, final File optionsFile) throws IOException {
BundleContext context = mock(BundleContext.class);
when(context.getDataFile(SLING_ID_FILE_NAME)).thenReturn(slingIdFile);
when(context.getDataFile(OPTIONS_FILE_NAME)).thenReturn(optionsFile);
// write options
final List<SlingSettingsServiceImpl.Options> options = new ArrayList<SlingSettingsServiceImpl.Options>();
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream(optionsFile);
oos = new ObjectOutputStream(fos);
oos.writeObject(options);
} catch (final IOException ioe) {
throw new RuntimeException("Unable to write to options data file.", ioe);
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException ignore) {
// ...
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException ignore) {
// ...
}
}
}
return new SlingSettingsServiceImpl(context);
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class ServiceUserMappedBundleFilter method event.
@Override
public void event(ServiceEvent serviceEvent, Map map) {
ServiceReference serviceReference = serviceEvent.getServiceReference();
if (isServiceMappingReference(serviceReference)) {
Object serviceName = serviceReference.getProperty(Mapping.SERVICENAME);
if (serviceName != null && serviceName instanceof String) {
Iterator<Map.Entry<BundleContext, Collection<ListenerHook.ListenerInfo>>> it = map.entrySet().iterator();
while (it.hasNext()) {
BundleContext ctx = it.next().getKey();
String bundleServiceName = ServiceUserMapperImpl.getServiceName(ctx.getBundle());
if (!serviceName.equals(bundleServiceName)) {
it.remove();
}
}
}
}
}
use of org.osgi.framework.BundleContext in project sling by apache.
the class OakSlingRepositoryManager method activate.
@Activate
private void activate(final OakSlingRepositoryManagerConfiguration configuration, final ComponentContext componentContext) {
this.configuration = configuration;
this.componentContext = componentContext;
final BundleContext bundleContext = componentContext.getBundleContext();
final String defaultWorkspace = configuration.defaultWorkspace();
final boolean disableLoginAdministrative = !configuration.admin_login_enabled();
if (configuration.oak_observation_limitCommitRate()) {
commitRateLimiter = new CommitRateLimiter();
}
this.threadPool = threadPoolManager.get("oak-observation");
this.nodeAggregatorRegistration = bundleContext.registerService(NodeAggregator.class.getName(), getNodeAggregator(), null);
super.start(bundleContext, new Config(defaultWorkspace, disableLoginAdministrative));
}
Aggregations