Search in sources :

Example 41 with BundleContext

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));
}
Also used : BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 42 with BundleContext

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));
}
Also used : BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 43 with BundleContext

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);
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BundleContext(org.osgi.framework.BundleContext)

Example 44 with BundleContext

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();
                }
            }
        }
    }
}
Also used : ServiceReference(org.osgi.framework.ServiceReference) BundleContext(org.osgi.framework.BundleContext)

Example 45 with BundleContext

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));
}
Also used : CommitRateLimiter(org.apache.jackrabbit.oak.plugins.observation.CommitRateLimiter) BundleContext(org.osgi.framework.BundleContext) Activate(org.osgi.service.component.annotations.Activate)

Aggregations

BundleContext (org.osgi.framework.BundleContext)1052 Test (org.junit.Test)361 Bundle (org.osgi.framework.Bundle)298 ServiceReference (org.osgi.framework.ServiceReference)229 File (java.io.File)119 Hashtable (java.util.Hashtable)114 HashMap (java.util.HashMap)98 ArrayList (java.util.ArrayList)97 IOException (java.io.IOException)75 ServiceRegistration (org.osgi.framework.ServiceRegistration)68 BundleException (org.osgi.framework.BundleException)60 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)59 Equinox (org.eclipse.osgi.launch.Equinox)51 ComponentContext (org.osgi.service.component.ComponentContext)50 Dictionary (java.util.Dictionary)48 Before (org.junit.Before)48 Properties (java.util.Properties)47 LinkedHashMap (java.util.LinkedHashMap)46 URL (java.net.URL)43 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)40