Search in sources :

Example 31 with Dictionary

use of java.util.Dictionary in project jackrabbit-oak by apache.

the class OsgiUtilTest method testComponentLookupWithStringValue.

@Test
public void testComponentLookupWithStringValue() {
    Dictionary properties = mock(Dictionary.class);
    doReturn("  value   ").when(properties).get("name");
    ComponentContext context = mock(ComponentContext.class);
    doReturn(properties).when(context).getProperties();
    assertEquals("value", lookup(context, "name"));
}
Also used : Dictionary(java.util.Dictionary) ComponentContext(org.osgi.service.component.ComponentContext) Test(org.junit.Test)

Example 32 with Dictionary

use of java.util.Dictionary in project jackrabbit-oak by apache.

the class OsgiUtilTest method testFallbackLookupWithValueInComponent.

@Test
public void testFallbackLookupWithValueInComponent() {
    Dictionary dictionary = mock(Dictionary.class);
    doReturn("value").when(dictionary).get("cname");
    BundleContext bundleContext = mock(BundleContext.class);
    doReturn(null).when(bundleContext).getProperty("fname");
    ComponentContext componentContext = mock(ComponentContext.class);
    doReturn(dictionary).when(componentContext).getProperties();
    doReturn(bundleContext).when(componentContext).getBundleContext();
    assertEquals("value", lookupConfigurationThenFramework(componentContext, "cname", "fname"));
    assertEquals("value", lookupFrameworkThenConfiguration(componentContext, "cname", "fname"));
}
Also used : Dictionary(java.util.Dictionary) ComponentContext(org.osgi.service.component.ComponentContext) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test)

Example 33 with Dictionary

use of java.util.Dictionary in project karaf by apache.

the class UpdateCommand method doExecute.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object doExecute() throws Exception {
    Dictionary props = getEditedProps();
    if (props == null) {
        System.err.println("No configuration is being edited--run the edit command first");
        return null;
    }
    String pid = (String) this.session.get(PROPERTY_CONFIG_PID);
    boolean isFactory = this.session.get(PROPERTY_FACTORY) != null && (Boolean) this.session.get(PROPERTY_FACTORY);
    if (isFactory) {
        String alias = (String) this.session.get(PROPERTY_ALIAS);
        this.configRepository.createFactoryConfiguration(pid, alias, props);
    } else {
        this.configRepository.update(pid, props);
    }
    this.session.put(PROPERTY_CONFIG_PID, null);
    this.session.put(PROPERTY_FACTORY, null);
    this.session.put(PROPERTY_CONFIG_PROPS, null);
    if (this.session.get(PROPERTY_ALIAS) != null) {
        this.session.put(PROPERTY_ALIAS, null);
    }
    return null;
}
Also used : Dictionary(java.util.Dictionary)

Example 34 with Dictionary

use of java.util.Dictionary in project karaf by apache.

the class GreeterServiceFactoryManager method activate.

/**
     * Called when all of the SCR Components required dependencies have been satisfied.
     */
@Activate
public void activate() {
    LOG.info("Activating the {}", COMPONENT_LABEL);
    try {
        lock.readLock().lock();
        if (factory != null) {
            final Properties props = new Properties();
            props.setProperty("salutation", "Hello");
            props.setProperty("name", "User");
            instance = factory.newInstance((Dictionary) props);
            greeterService = (GreeterServiceComponentFactory) instance.getInstance();
            greeterService.startGreeter();
        }
    } finally {
        lock.readLock().unlock();
    }
}
Also used : Dictionary(java.util.Dictionary) Properties(java.util.Properties) Activate(org.osgi.service.component.annotations.Activate)

Example 35 with Dictionary

use of java.util.Dictionary in project karaf by apache.

the class GuardProxyCatalogTest method testCreateProxy.

@SuppressWarnings({ "unchecked", "rawtypes" })
public Dictionary<String, Object> testCreateProxy(BundleContext bc, Class intf, final Class proxyRegClass, Object testService) throws Exception {
    // Create the object that is actually being tested here
    GuardProxyCatalog gpc = new GuardProxyCatalog(bc);
    // The service being proxied has these properties
    long serviceID = 456L;
    final Hashtable<String, Object> serviceProps = new Hashtable<>();
    serviceProps.put(Constants.OBJECTCLASS, new String[] { intf.getName() });
    serviceProps.put(Constants.SERVICE_ID, serviceID);
    serviceProps.put(".foo", 123L);
    final Map<ServiceReference<?>, Object> serviceMap = new HashMap<>();
    // The mock bundle context for the bundle providing the service is set up here
    BundleContext providerBC = EasyMock.createMock(BundleContext.class);
    // These are the expected service properties of the proxy registration. Note the proxy marker...
    final Hashtable<String, Object> expectedProxyProps = new Hashtable<>(serviceProps);
    expectedProxyProps.put(GuardProxyCatalog.PROXY_SERVICE_KEY, Boolean.TRUE);
    // This will check that the right proxy is being registered.
    EasyMock.expect(providerBC.registerService(EasyMock.isA(String[].class), EasyMock.anyObject(), EasyMock.isA(Dictionary.class))).andAnswer((IAnswer) () -> {
        if (!runningUnderCoverage) {
            assertArrayEquals(new String[] { proxyRegClass.getName() }, (String[]) EasyMock.getCurrentArguments()[0]);
            Object svc = EasyMock.getCurrentArguments()[1];
            assertTrue(svc instanceof ServiceFactory);
        }
        Dictionary<String, Object> props = (Dictionary<String, Object>) EasyMock.getCurrentArguments()[2];
        for (String key : expectedProxyProps.keySet()) {
            assertEquals(expectedProxyProps.get(key), props.get(key));
        }
        ServiceRegistration reg = EasyMock.createMock(ServiceRegistration.class);
        ServiceReference sr = mockServiceReference(props);
        EasyMock.expect(reg.getReference()).andReturn(sr).anyTimes();
        reg.unregister();
        EasyMock.expectLastCall().once();
        EasyMock.replay(reg);
        serviceMap.put(sr, EasyMock.getCurrentArguments()[1]);
        return reg;
    }).once();
    EasyMock.expect(providerBC.getService(EasyMock.isA(ServiceReference.class))).andAnswer(() -> serviceMap.get(EasyMock.getCurrentArguments()[0])).anyTimes();
    EasyMock.replay(providerBC);
    // In some cases the proxy-creating code is looking for a classloader (e.g. when run through
    // a coverage tool such as EclEmma). This will satisfy that.
    BundleWiring bw = EasyMock.createMock(BundleWiring.class);
    EasyMock.expect(bw.getClassLoader()).andReturn(getClass().getClassLoader()).anyTimes();
    EasyMock.replay(bw);
    // The mock bundle that provides the original service (and also the proxy is registered with this)
    Bundle providerBundle = EasyMock.createNiceMock(Bundle.class);
    EasyMock.expect(providerBundle.getBundleContext()).andReturn(providerBC).anyTimes();
    EasyMock.expect(providerBundle.adapt(BundleWiring.class)).andReturn(bw).anyTimes();
    EasyMock.replay(providerBundle);
    ServiceReference sr = mockServiceReference(providerBundle, serviceProps);
    assertEquals("Precondition", 0, gpc.proxyMap.size());
    assertEquals("Precondition", 0, gpc.createProxyQueue.size());
    // Create the proxy for the service
    gpc.proxyIfNotAlreadyProxied(sr);
    assertEquals(1, gpc.proxyMap.size());
    // The actual proxy creation is done asynchronously.
    GuardProxyCatalog.ServiceRegistrationHolder holder = gpc.proxyMap.get(serviceID);
    assertNull("The registration shouldn't have happened yet", holder.registration);
    assertEquals(1, gpc.createProxyQueue.size());
    // Mimic the thread that works the queue to create the proxy
    GuardProxyCatalog.CreateProxyRunnable runnable = gpc.createProxyQueue.take();
    ProxyManager pm = getProxyManager();
    runnable.run(pm);
    // The runnable should have put the actual registration in the holder
    ServiceReference<?> proxySR = holder.registration.getReference();
    for (String key : expectedProxyProps.keySet()) {
        assertEquals(expectedProxyProps.get(key), proxySR.getProperty(key));
    }
    // Check that the proxy registration was done on the original provider bundle's context
    EasyMock.verify(providerBC);
    // Test that the actual proxy invokes the original service...
    Object proxyService = serviceMap.get(proxySR);
    assertNotSame("The proxy should not be the same object as the original service", testService, proxyService);
    // Attempt to proxy the service again, make sure that no re-proxying happens
    assertEquals("Precondition", 1, gpc.proxyMap.size());
    assertEquals("Precondition", 0, gpc.createProxyQueue.size());
    gpc.proxyIfNotAlreadyProxied(sr);
    assertEquals("No additional proxy should have been created", 1, gpc.proxyMap.size());
    assertEquals("No additional work on the queue is expected", 0, gpc.createProxyQueue.size());
    Dictionary<String, Object> proxyProps = getServiceReferenceProperties(proxySR);
    gpc.close();
    // checks that the unregister call was made
    EasyMock.verify(holder.registration);
    return proxyProps;
}
Also used : Dictionary(java.util.Dictionary) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ServiceFactory(org.osgi.framework.ServiceFactory) CreateProxyRunnable(org.apache.karaf.service.guard.impl.GuardProxyCatalog.CreateProxyRunnable) Hashtable(java.util.Hashtable) Bundle(org.osgi.framework.Bundle) ServiceRegistrationHolder(org.apache.karaf.service.guard.impl.GuardProxyCatalog.ServiceRegistrationHolder) BundleWiring(org.osgi.framework.wiring.BundleWiring) ProxyManager(org.apache.aries.proxy.ProxyManager) AsmProxyManager(org.apache.aries.proxy.impl.AsmProxyManager) ServiceReference(org.osgi.framework.ServiceReference) IAnswer(org.easymock.IAnswer) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

Dictionary (java.util.Dictionary)198 Hashtable (java.util.Hashtable)91 Test (org.junit.Test)48 Configuration (org.osgi.service.cm.Configuration)33 BundleDescription (org.eclipse.osgi.service.resolver.BundleDescription)28 State (org.eclipse.osgi.service.resolver.State)28 Enumeration (java.util.Enumeration)27 Properties (java.util.Properties)27 BundleContext (org.osgi.framework.BundleContext)24 ArrayList (java.util.ArrayList)22 ServiceReference (org.osgi.framework.ServiceReference)21 HashMap (java.util.HashMap)20 ServiceRegistration (org.osgi.framework.ServiceRegistration)20 IOException (java.io.IOException)17 Map (java.util.Map)17 Bundle (org.osgi.framework.Bundle)16 LinkedHashMap (java.util.LinkedHashMap)13 List (java.util.List)13 Matchers.anyString (org.mockito.Matchers.anyString)11 MinionIdentity (org.opennms.minion.core.api.MinionIdentity)11