Search in sources :

Example 56 with BundleContext

use of org.osgi.framework.BundleContext in project jackrabbit-oak by apache.

the class SecurityProviderRegistration method maybeRegister.

private void maybeRegister() {
    BundleContext context;
    log.info("Trying to register a SecurityProvider...");
    synchronized (this) {
        if (this.context == null) {
            log.info("Aborting: no BundleContext is available");
            return;
        }
        if (!preconditions.areSatisfied()) {
            log.info("Aborting: preconditions are not satisfied: {}", preconditions);
            return;
        }
        if (registration != null) {
            log.info("Aborting: a SecurityProvider is already registered");
            return;
        }
        if (registering) {
            log.info("Aborting: a SecurityProvider is already being registered");
            return;
        }
        // Mark the start of a registration process.
        registering = true;
        // Save the BundleContext for local usage.
        context = this.context;
    }
    // Register the SecurityProvider.
    Dictionary<String, Object> properties = new Hashtable<String, Object>();
    properties.put("type", "default");
    ServiceRegistration registration = context.registerService(SecurityProvider.class.getName(), createSecurityProvider(context), properties);
    synchronized (this) {
        this.registration = registration;
        this.registering = false;
    }
    log.info("SecurityProvider instance registered");
}
Also used : Hashtable(java.util.Hashtable) SecurityProvider(org.apache.jackrabbit.oak.spi.security.SecurityProvider) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 57 with BundleContext

use of org.osgi.framework.BundleContext in project jackrabbit-oak by apache.

the class ExternalPrincipalConfigurationTest method testRemoveSyncHandler.

@Test
public void testRemoveSyncHandler() throws Exception {
    Dictionary<String, Object> enableProps = new Hashtable(ImmutableMap.<String, Object>of(DefaultSyncConfigImpl.PARAM_USER_DYNAMIC_MEMBERSHIP, true));
    Dictionary<String, Object> disableProps = new Hashtable(ImmutableMap.<String, Object>of(DefaultSyncConfigImpl.PARAM_USER_DYNAMIC_MEMBERSHIP, false));
    DefaultSyncHandler sh = new DefaultSyncHandler();
    BundleContext bundleContext = context.bundleContext();
    ServiceRegistration registration1 = bundleContext.registerService(SyncHandler.class.getName(), sh, enableProps);
    ServiceRegistration registration2 = bundleContext.registerService(SyncHandler.class.getName(), sh, enableProps);
    ServiceRegistration registration3 = bundleContext.registerService(SyncHandler.class.getName(), sh, disableProps);
    assertIsEnabled(principalConfiguration, true);
    registration2.unregister();
    assertIsEnabled(principalConfiguration, true);
    registration1.unregister();
    assertIsEnabled(principalConfiguration, false);
    registration3.unregister();
    assertIsEnabled(principalConfiguration, false);
}
Also used : SyncHandler(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncHandler) DefaultSyncHandler(org.apache.jackrabbit.oak.spi.security.authentication.external.impl.DefaultSyncHandler) Hashtable(java.util.Hashtable) DefaultSyncHandler(org.apache.jackrabbit.oak.spi.security.authentication.external.impl.DefaultSyncHandler) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) AbstractExternalAuthTest(org.apache.jackrabbit.oak.spi.security.authentication.external.AbstractExternalAuthTest) Test(org.junit.Test)

Example 58 with BundleContext

use of org.osgi.framework.BundleContext in project jackrabbit-oak by apache.

the class BaseDocumentDiscoveryLiteServiceTest method createInstance.

SimplifiedInstance createInstance(DocumentNodeStore ns, String workingDir) throws NoSuchFieldException {
    DocumentDiscoveryLiteService discoveryLite = new DocumentDiscoveryLiteService();
    PrivateAccessor.setField(discoveryLite, "nodeStore", ns);
    BundleContext bc = mock(BundleContext.class);
    ComponentContext c = mock(ComponentContext.class);
    when(c.getBundleContext()).thenReturn(bc);
    final Map<String, Object> registeredServices = new HashMap<String, Object>();
    when(bc.registerService(anyString(), anyObject(), (Properties) anyObject())).then(new Answer<ServiceRegistration>() {

        @Override
        public ServiceRegistration answer(InvocationOnMock invocation) {
            registeredServices.put((String) invocation.getArguments()[0], invocation.getArguments()[1]);
            return null;
        }
    });
    discoveryLite.activate(c);
    Descriptors d = (Descriptors) registeredServices.get(Descriptors.class.getName());
    final SimplifiedInstance result = new SimplifiedInstance(discoveryLite, ns, d, registeredServices, 500, workingDir);
    allInstances.add(result);
    logger.info("Created " + result);
    return result;
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyObject(org.mockito.Matchers.anyObject) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) Descriptors(org.apache.jackrabbit.oak.api.Descriptors) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 59 with BundleContext

use of org.osgi.framework.BundleContext in project jackrabbit-oak by apache.

the class DocumentDiscoveryLiteServiceTest method testActivateDeactivate.

@Test
public void testActivateDeactivate() throws Exception {
    // then test normal start with a DocumentNodeStore
    DocumentMK mk1 = createMK(1, 0);
    DocumentDiscoveryLiteService discoveryLite = new DocumentDiscoveryLiteService();
    PrivateAccessor.setField(discoveryLite, "nodeStore", mk1.nodeStore);
    BundleContext bc = mock(BundleContext.class);
    ComponentContext c = mock(ComponentContext.class);
    when(c.getBundleContext()).thenReturn(bc);
    discoveryLite.activate(c);
    verify(c, times(0)).disableComponent(DocumentDiscoveryLiteService.COMPONENT_NAME);
    discoveryLite.deactivate();
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 60 with BundleContext

use of org.osgi.framework.BundleContext in project karaf by apache.

the class TestBundleFactory method createBundleContext.

public BundleContext createBundleContext() {
    BundleContext bundleContext = createMock(BundleContext.class);
    Bundle[] bundles = createBundles();
    expect(bundleContext.getProperty("karaf.systemBundlesStartLevel")).andReturn(Integer.toString(50)).anyTimes();
    expect(bundleContext.getBundles()).andReturn(bundles).anyTimes();
    expect(bundleContext.getBundle(0)).andReturn(null).anyTimes();
    expect(bundleContext.getBundle(1)).andReturn(bundles[0]).anyTimes();
    expect(bundleContext.getBundle(2)).andReturn(bundles[1]).anyTimes();
    replay(bundleContext);
    return bundleContext;
}
Also used : Bundle(org.osgi.framework.Bundle) BundleContext(org.osgi.framework.BundleContext)

Aggregations

BundleContext (org.osgi.framework.BundleContext)524 Test (org.junit.Test)186 Bundle (org.osgi.framework.Bundle)175 ServiceReference (org.osgi.framework.ServiceReference)126 File (java.io.File)82 Hashtable (java.util.Hashtable)75 HashMap (java.util.HashMap)70 Equinox (org.eclipse.osgi.launch.Equinox)51 BundleException (org.osgi.framework.BundleException)51 ArrayList (java.util.ArrayList)50 LinkedHashMap (java.util.LinkedHashMap)45 ServiceRegistration (org.osgi.framework.ServiceRegistration)41 IOException (java.io.IOException)40 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)38 URL (java.net.URL)33 Dictionary (java.util.Dictionary)32 Matchers.anyString (org.mockito.Matchers.anyString)28 Before (org.junit.Before)26 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)26 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)18