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>());
}
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;
}
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);
}
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());
}
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
}
}
Aggregations