use of org.apache.aries.blueprint.reflect.BeanMetadataImpl in project aries by apache.
the class LifecyclePolicyTest method testStatic.
@Test
public void testStatic() throws Exception {
final ReferenceMetadataImpl ref = new ReferenceMetadataImpl();
ref.setId("ref");
ref.setRuntimeInterface(TestItf.class);
ref.setLifecycle(ExtendedReferenceMetadata.LIFECYCLE_STATIC);
final BeanMetadataImpl bean1 = new BeanMetadataImpl();
bean1.setId("bean1");
bean1.setRuntimeClass(Bean1.class);
bean1.setInitMethod("init");
bean1.setDestroyMethod("destroy");
bean1.addProperty("itf", new RefMetadataImpl("ref"));
final BeanMetadataImpl bean2 = new BeanMetadataImpl();
bean2.setId("bean2");
bean2.setRuntimeClass(Bean2.class);
bean2.setInitMethod("init");
bean2.setDestroyMethod("destroy");
bean2.addProperty("bean1", new RefMetadataImpl("bean1"));
Bundle bundle = EasyMock.createMock(Bundle.class);
BundleContext bundleContext = EasyMock.createMock(BundleContext.class);
Bundle extenderBundle = EasyMock.createMock(Bundle.class);
BundleContext extenderBundleContext = EasyMock.createMock(BundleContext.class);
BlueprintListener eventDispatcher = EasyMock.createMock(BlueprintListener.class);
NamespaceHandlerRegistry namespaceHandlerRegistry = EasyMock.createMock(NamespaceHandlerRegistry.class);
ProxyManager proxyManager = EasyMock.createMock(ProxyManager.class);
NamespaceHandlerSet namespaceHandlerSet = EasyMock.createMock(NamespaceHandlerSet.class);
TestItf itf = EasyMock.createMock(TestItf.class);
ServiceRegistration registration = EasyMock.createMock(ServiceRegistration.class);
ExecutorService executorService = Executors.newFixedThreadPool(1);
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
List<URL> pathList = new ArrayList<URL>();
Set<URI> namespaces = new HashSet<URI>();
BlueprintContainerImpl container = new BlueprintContainerImpl(bundle, bundleContext, extenderBundle, eventDispatcher, namespaceHandlerRegistry, executorService, timer, pathList, proxyManager, namespaces) {
private boolean repoCreated = false;
@Override
public BlueprintRepository getRepository() {
if (!repoCreated) {
getComponentDefinitionRegistry().registerComponentDefinition(ref);
getComponentDefinitionRegistry().registerComponentDefinition(bean1);
getComponentDefinitionRegistry().registerComponentDefinition(bean2);
repoCreated = true;
}
return super.getRepository();
}
};
ServiceReference svcRef1 = EasyMock.createMock(ServiceReference.class);
EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
EasyMock.expect(bundle.getState()).andReturn(Bundle.ACTIVE).anyTimes();
EasyMock.expect(bundle.getBundleContext()).andReturn(bundleContext).anyTimes();
Hashtable<String, String> headers = new Hashtable<String, String>();
headers.put(Constants.BUNDLE_SYMBOLICNAME, "bundleSymbolicName;blueprint.aries.xml-validation:=false");
EasyMock.expect(bundle.getHeaders()).andReturn(headers).anyTimes();
eventDispatcher.blueprintEvent(EasyMock.<BlueprintEvent>anyObject());
EasyMock.expectLastCall().anyTimes();
EasyMock.expect(namespaceHandlerRegistry.getNamespaceHandlers(namespaces, bundle)).andReturn(namespaceHandlerSet).anyTimes();
EasyMock.expect(namespaceHandlerSet.getNamespaces()).andReturn(namespaces).anyTimes();
namespaceHandlerSet.addListener(container);
EasyMock.expectLastCall();
EasyMock.expect(bundleContext.getProperty(BlueprintConstants.XML_VALIDATION_PROPERTY)).andReturn(null);
Properties props = new Properties();
props.put("osgi.blueprint.container.version", Version.emptyVersion);
props.put("osgi.blueprint.container.symbolicname", "bundleSymbolicName");
EasyMock.expect(bundleContext.registerService(EasyMock.aryEq(new String[] { BlueprintContainer.class.getName() }), EasyMock.same(container), EasyMock.eq((Dictionary) props))).andReturn(registration);
bundleContext.addServiceListener(EasyMock.<org.osgi.framework.ServiceListener>anyObject(), EasyMock.<String>anyObject());
EasyMock.expectLastCall();
EasyMock.expect(bundleContext.getServiceReferences((String) null, "(objectClass=" + TestItf.class.getName() + ")")).andReturn(new ServiceReference[] { svcRef1 });
EasyMock.expect(bundleContext.getService(svcRef1)).andReturn(itf);
EasyMock.expect(bundle.loadClass("java.lang.Object")).andReturn((Class) Object.class).anyTimes();
EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
container.run();
ReferenceRecipe recipe = (ReferenceRecipe) container.getRepository().getRecipe("ref");
recipe.start(container);
Bean2 bean2i = (Bean2) container.getRepository().create("bean2");
Assert.assertNotNull(bean2i);
Assert.assertEquals(1, Bean2.initialized);
Assert.assertEquals(0, Bean2.destroyed);
EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
//
// Unregister the service
//
// Given the lifecycle is 'static', this should cause the Bean1 and Bean2
// to be destroyed
//
EasyMock.reset(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
EasyMock.expect(bundleContext.ungetService(svcRef1)).andReturn(false);
EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
recipe.serviceChanged(new ServiceEvent(ServiceEvent.UNREGISTERING, svcRef1));
Assert.assertEquals(1, Bean2.initialized);
Assert.assertEquals(1, Bean2.destroyed);
EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
//
// Re-register the service
//
// Given the lifecycle is 'static', this should cause the Bean1 and Bean2
// to be recreated
//
EasyMock.reset(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
EasyMock.expect(bundle.getSymbolicName()).andReturn("bundleSymbolicName").anyTimes();
EasyMock.expect(bundle.getVersion()).andReturn(Version.emptyVersion).anyTimes();
EasyMock.expect(bundleContext.getService(svcRef1)).andReturn(itf);
EasyMock.expect(bundle.loadClass("java.lang.Object")).andReturn((Class) Object.class).anyTimes();
EasyMock.replay(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
recipe.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, svcRef1));
Assert.assertEquals(2, Bean2.initialized);
Assert.assertEquals(1, Bean2.destroyed);
EasyMock.verify(bundle, bundleContext, extenderBundle, extenderBundleContext, eventDispatcher, namespaceHandlerRegistry, namespaceHandlerSet, proxyManager, svcRef1, registration);
}
use of org.apache.aries.blueprint.reflect.BeanMetadataImpl in project aries by apache.
the class Parser method parseBeanMetadata.
private ComponentMetadata parseBeanMetadata(Element element, boolean topElement) {
BeanMetadataImpl metadata = new BeanMetadataImpl();
if (topElement) {
metadata.setId(getId(element));
if (element.hasAttribute(SCOPE_ATTRIBUTE)) {
metadata.setScope(getScope(element.getAttributeNode(SCOPE_ATTRIBUTE)));
if (!metadata.getScope().equals(BeanMetadata.SCOPE_SINGLETON)) {
if (element.hasAttribute(ACTIVATION_ATTRIBUTE)) {
if (element.getAttribute(ACTIVATION_ATTRIBUTE).equals(ACTIVATION_EAGER)) {
throw new ComponentDefinitionException("A <bean> with a prototype or custom scope can not have an eager activation");
}
}
metadata.setActivation(ComponentMetadata.ACTIVATION_LAZY);
} else {
metadata.setActivation(parseActivation(element));
}
} else {
metadata.setActivation(parseActivation(element));
}
} else {
metadata.setActivation(ComponentMetadata.ACTIVATION_LAZY);
}
if (element.hasAttribute(CLASS_ATTRIBUTE)) {
metadata.setClassName(element.getAttribute(CLASS_ATTRIBUTE));
}
if (element.hasAttribute(DEPENDS_ON_ATTRIBUTE)) {
metadata.setDependsOn(parseList(element.getAttribute(DEPENDS_ON_ATTRIBUTE)));
}
if (element.hasAttribute(INIT_METHOD_ATTRIBUTE)) {
metadata.setInitMethod(element.getAttribute(INIT_METHOD_ATTRIBUTE));
}
if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
metadata.setDestroyMethod(element.getAttribute(DESTROY_METHOD_ATTRIBUTE));
}
if (element.hasAttribute(FACTORY_REF_ATTRIBUTE)) {
metadata.setFactoryComponent(new RefMetadataImpl(element.getAttribute(FACTORY_REF_ATTRIBUTE)));
}
if (element.hasAttribute(FACTORY_METHOD_ATTRIBUTE)) {
String factoryMethod = element.getAttribute(FACTORY_METHOD_ATTRIBUTE);
metadata.setFactoryMethod(factoryMethod);
}
// Do some validation
if (metadata.getClassName() == null && metadata.getFactoryComponent() == null) {
throw new ComponentDefinitionException("Bean class or factory-ref must be specified");
}
if (metadata.getFactoryComponent() != null && metadata.getFactoryMethod() == null) {
throw new ComponentDefinitionException("factory-method is required when factory-component is set");
}
if (MetadataUtil.isPrototypeScope(metadata) && metadata.getDestroyMethod() != null) {
throw new ComponentDefinitionException("destroy-method must not be set for a <bean> with a prototype scope");
}
// Parse elements
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element e = (Element) node;
if (isBlueprintNamespace(node.getNamespaceURI())) {
if (nodeNameEquals(node, ARGUMENT_ELEMENT)) {
metadata.addArgument(parseBeanArgument(metadata, e));
} else if (nodeNameEquals(node, PROPERTY_ELEMENT)) {
metadata.addProperty(parseBeanProperty(metadata, e));
}
}
}
}
MetadataUtil.validateBeanArguments(metadata.getArguments());
ComponentMetadata m = metadata;
// Parse custom scopes
m = handleCustomScope(element.getAttributeNode(SCOPE_ATTRIBUTE), element, m);
// Parse custom attributes
m = handleCustomAttributes(element.getAttributes(), m);
// Parse custom elements;
m = handleCustomElements(element, m);
return m;
}
use of org.apache.aries.blueprint.reflect.BeanMetadataImpl in project geronimo-xbean by apache.
the class RestaurantUsingXBeanTest method testPizza.
public void testPizza() throws Exception {
super.testPizza();
BeanMetadataImpl restaurant = (BeanMetadataImpl) reg.getComponentDefinition("restaurant");
ValueMetadata uri = (ValueMetadata) propertyByName("uri", restaurant).getValue();
assertNotNull("URI is null", uri);
assertEquals("URI", "http://cheese.com", uri.getStringValue());
log.info("Successfully converted the property to a URI: " + uri);
}
use of org.apache.aries.blueprint.reflect.BeanMetadataImpl in project geronimo-xbean by apache.
the class SaladUsingBlueprintTest method testSalad.
public void testSalad() throws Exception {
BeanMetadataImpl salad = (BeanMetadataImpl) reg.getComponentDefinition("saladService");
checkArgumentValue(0, "Cesar", salad, false);
checkArgumentValue(1, "Small", salad, false);
checkArgumentValue(2, "true", salad, false);
}
use of org.apache.aries.blueprint.reflect.BeanMetadataImpl in project geronimo-xbean by apache.
the class SocketAddressBlueprintTest method testSocketAddress.
public void testSocketAddress() throws Exception {
BeanMetadataImpl socketAddress = (BeanMetadataImpl) reg.getComponentDefinition("socketAddress");
// System.out.println();
// System.out.println("===========================");
// System.out.println(socketAddress);
// System.out.println("===========================");
// System.out.println();
assertNotNull(socketAddress);
}
Aggregations