Search in sources :

Example 6 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class BlueprintURLContextTest method setup.

@BeforeClass
public static void setup() throws Exception {
    System.setProperty("org.apache.aries.jndi.disable.builder", "false");
    bundle = Skeleton.newMock(new BundleMock("aBundle", new Hashtable<String, String>()), Bundle.class);
    BundleContext bc = bundle.getBundleContext();
    new org.apache.aries.jndi.startup.Activator().start(bc);
    Activator a = new Activator();
    a.start(bc);
    ProxyManager pm = (ProxyManager) Proxy.newProxyInstance(BlueprintURLContext.class.getClassLoader(), new Class[] { ProxyManager.class }, (proxy, method, args) -> null);
    a.serviceChanged(null, pm);
    // Register a BlueprintContainer mock that will answer getComponentInstance(String id) calls
    BlueprintContainer bpc = Skeleton.newMock(new BlueprintContainerStub(), BlueprintContainer.class);
    bc.registerService("org.osgi.service.blueprint.container.BlueprintContainer", bpc, new Hashtable<String, String>());
}
Also used : Arrays(java.util.Arrays) Proxy(java.lang.reflect.Proxy) BeforeClass(org.junit.BeforeClass) Constants(org.osgi.framework.Constants) Skeleton(org.apache.aries.unittest.mocks.Skeleton) NamingException(javax.naming.NamingException) HashSet(java.util.HashSet) BundleContextMock(org.apache.aries.mocks.BundleContextMock) After(org.junit.After) ProxyManager(org.apache.aries.proxy.ProxyManager) Context(javax.naming.Context) Bundle(org.osgi.framework.Bundle) Method(java.lang.reflect.Method) NameNotFoundException(javax.naming.NameNotFoundException) Hashtable(java.util.Hashtable) Before(org.junit.Before) NoSuchComponentException(org.osgi.service.blueprint.container.NoSuchComponentException) InitialContext(javax.naming.InitialContext) NameClassPair(javax.naming.NameClassPair) AfterClass(org.junit.AfterClass) Properties(java.util.Properties) Assert.assertNotNull(org.junit.Assert.assertNotNull) Set(java.util.Set) Test(org.junit.Test) BundleContext(org.osgi.framework.BundleContext) BundleMock(org.apache.aries.mocks.BundleMock) Binding(javax.naming.Binding) NamingEnumeration(javax.naming.NamingEnumeration) InvocationHandler(java.lang.reflect.InvocationHandler) BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Assert.assertEquals(org.junit.Assert.assertEquals) Bundle(org.osgi.framework.Bundle) ProxyManager(org.apache.aries.proxy.ProxyManager) BundleMock(org.apache.aries.mocks.BundleMock) BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) BeforeClass(org.junit.BeforeClass) AfterClass(org.junit.AfterClass) BundleContext(org.osgi.framework.BundleContext) BeforeClass(org.junit.BeforeClass)

Example 7 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class BlueprintURLContext method getBlueprintContainerRef.

/**
 * Obtain a BlueprintContainerService for the given bundle. If the service isn't there, wait for up
 * to the blueprint.graceperiod defined for that bundle for one to be published.
 *
 * @param b The Bundle to look in
 * @return BlueprintContainerService instance for that bundle
 * @throws ServiceUnavailableException If no BlueprinContainerService found
 */
private static ServiceReference<BlueprintContainer> getBlueprintContainerRef(Bundle b) throws ServiceUnavailableException {
    ServiceReference<BlueprintContainer> result = findBPCRef(b);
    if (result == null) {
        Semaphore s = new Semaphore(0);
        AtomicReference<ServiceReference<BlueprintContainer>> bpcRef = new AtomicReference<>();
        ServiceTracker<BlueprintContainer, BlueprintContainer> st = new ServiceTracker<>(b.getBundleContext(), BlueprintContainer.class, new BlueprintContainerServiceTrackerCustomizer(b, s, bpcRef));
        st.open();
        // Make another check for the BlueprintContainer service in case it came up just before our tracker engaged
        int graceperiod = getGracePeriod(b);
        result = findBPCRef(b);
        if (result == null && graceperiod >= 0) {
            if (graceperiod == 0) {
                // Wait for an unlimited period
                try {
                    s.acquire();
                } catch (InterruptedException ix) {
                }
            } else {
                try {
                    s.tryAcquire(graceperiod, TimeUnit.MILLISECONDS);
                } catch (InterruptedException ix) {
                }
            }
        }
        result = bpcRef.get();
        st.close();
    }
    if (result == null) {
        throw new ServiceUnavailableException("The BlueprintContainer service for bundle " + b.getSymbolicName() + '/' + b.getVersion() + " can not be located");
    }
    return result;
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) ServiceTracker(org.osgi.util.tracker.ServiceTracker) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) ServiceReference(org.osgi.framework.ServiceReference)

Example 8 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class FragmentTest method testFragmentProvidesBlueprintFile.

@Test
public void testFragmentProvidesBlueprintFile() throws Exception {
    Runnable r = context().getService(Runnable.class);
    Assert.assertNotNull("Could not find blueprint registered service", r);
    BlueprintContainer bc = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.test.host");
    Assert.assertNotNull("Could not find blueprint container for bundle", bc);
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Test(org.junit.Test)

Example 9 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class Helper method testBlueprintContainer.

public static void testBlueprintContainer(RichBundleContext context, Bundle bundle) throws Exception {
    BlueprintContainer blueprintContainer = getBlueprintContainerForBundle(context, SAMPLE_SYM_NAME);
    assertNotNull(blueprintContainer);
    Bar bar = getInstance(blueprintContainer, "bar", Bar.class);
    checkBar(bar);
    Foo foo = getInstance(blueprintContainer, "foo", Foo.class);
    checkFoo(bar, foo);
    Foo fooService = context.getService(Foo.class);
    assertNotNull(fooService);
    checkFoo(bar, fooService);
    // TODO Does not work
    // assertEquals(obj, foo);
    Account account = getInstance(blueprintContainer, "accountOne", Account.class);
    assertEquals(1, account.getAccountNumber());
    Account account2 = getInstance(blueprintContainer, "accountTwo", Account.class);
    assertEquals(2, account2.getAccountNumber());
    Account account3 = getInstance(blueprintContainer, "accountThree", Account.class);
    assertEquals(3, account3.getAccountNumber());
    AccountFactory accountFactory = getInstance(blueprintContainer, "accountFactory", AccountFactory.class);
    assertEquals("account factory", accountFactory.getFactoryName());
    bundle.stop();
    Thread.sleep(1000);
    try {
        blueprintContainer = getBlueprintContainerForBundle(context, SAMPLE_SYM_NAME, 1);
        fail("BlueprintContainer should have been unregistered");
    } catch (Exception e) {
    // Expected, as the module container should have been unregistered
    }
    assertTrue(foo.isInitialized());
    assertTrue(foo.isDestroyed());
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Account(org.apache.aries.blueprint.sample.Account) AccountFactory(org.apache.aries.blueprint.sample.AccountFactory) Bar(org.apache.aries.blueprint.sample.Bar) Foo(org.apache.aries.blueprint.sample.Foo) ParseException(java.text.ParseException)

Example 10 with BlueprintContainer

use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.

the class SpringTest method testSpringBundle.

@Test
public void testSpringBundle() throws Exception {
    Bundle bundles = context().getBundleByName("org.apache.aries.blueprint.testbundles");
    assertNotNull(bundles);
    bundles.start();
    BlueprintContainer container = startBundleBlueprint("org.apache.aries.blueprint.testbundles");
    List list = (List) container.getComponentInstance("springList");
    System.out.println(list);
    BeanCItf beanC = (BeanCItf) list.get(4);
    assertEquals(1, beanC.getInitialized());
    try {
        beanC.doSomething();
        fail("Should have thrown an exception because the transaction manager is not defined");
    } catch (NoSuchBeanDefinitionException e) {
    // expected
    }
}
Also used : BlueprintContainer(org.osgi.service.blueprint.container.BlueprintContainer) Helper.mvnBundle(org.apache.aries.blueprint.itests.Helper.mvnBundle) CoreOptions.mavenBundle(org.ops4j.pax.exam.CoreOptions.mavenBundle) Bundle(org.osgi.framework.Bundle) List(java.util.List) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) BeanCItf(org.apache.aries.blueprint.testbundles.BeanCItf) Test(org.junit.Test)

Aggregations

BlueprintContainer (org.osgi.service.blueprint.container.BlueprintContainer)29 Test (org.junit.Test)14 Bundle (org.osgi.framework.Bundle)11 ServiceReference (org.osgi.framework.ServiceReference)6 Helper.mvnBundle (org.apache.aries.blueprint.itests.Helper.mvnBundle)5 List (java.util.List)4 BundleContext (org.osgi.framework.BundleContext)4 DestroyTest (org.apache.aries.blueprint.sample.DestroyTest)3 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)3 URL (java.net.URL)2 Arrays (java.util.Arrays)2 HashSet (java.util.HashSet)2 Hashtable (java.util.Hashtable)2 Map (java.util.Map)2 BindingListener (org.apache.aries.blueprint.sample.BindingListener)2 Foo (org.apache.aries.blueprint.sample.Foo)2 InterfaceA (org.apache.aries.blueprint.sample.InterfaceA)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 ServiceMetadata (org.osgi.service.blueprint.reflect.ServiceMetadata)2