Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Expand All @@ -18,21 +18,22 @@
*/
package org.apache.aries.blueprint.plugin.model;

import org.ops4j.pax.cdi.api.OsgiService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.blueprint.container.BlueprintContainer;
import org.osgi.service.blueprint.container.Converter;

import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.enterprise.inject.Produces;

import org.ops4j.pax.cdi.api.OsgiService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.blueprint.container.BlueprintContainer;
import org.osgi.service.blueprint.container.Converter;

public class Context implements Matcher {

SortedSet<BeanRef> reg;
Expand Down Expand Up @@ -70,9 +71,19 @@ private void addBean(Class<?> clazz) {
private void addProducedBeans(Class<?> clazz, BeanRef factoryBean) {
for (Method method : clazz.getMethods()) {
Produces produces = method.getAnnotation(Produces.class);
Named named = method.getAnnotation(Named.class);
Singleton singleton = method.getAnnotation(Singleton.class);
if (produces != null) {
Class<?> producedClass = method.getReturnType();
ProducedBean producedBean = new ProducedBean(producedClass, factoryBean, method.getName());
ProducedBean producedBean;
if (named != null) {
producedBean = new ProducedBean(producedClass, named.value(), factoryBean, method.getName());
} else {
producedBean = new ProducedBean(producedClass, factoryBean, method.getName());
}
if (singleton != null) {
producedBean.setSingleton();
}
reg.add(producedBean);
}
}
Expand Down Expand Up @@ -108,7 +119,7 @@ public SortedSet<Bean> getBeans() {
TreeSet<Bean> beans = new TreeSet<Bean>();
for (BeanRef ref : reg) {
if (ref instanceof Bean) {
beans.add((Bean)ref);
beans.add((Bean) ref);
}
}
return beans;
Expand All @@ -118,7 +129,7 @@ public SortedSet<OsgiServiceRef> getServiceRefs() {
TreeSet<OsgiServiceRef> serviceRefs = new TreeSet<OsgiServiceRef>();
for (BeanRef ref : reg) {
if (ref instanceof OsgiServiceRef) {
serviceRefs.add((OsgiServiceRef)ref);
serviceRefs.add((OsgiServiceRef) ref);
}
}
return serviceRefs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
public class ProducedBean extends Bean {
public String factoryMethod;
public BeanRef factoryBean;

public ProducedBean(Class<?> clazz, BeanRef factoryBean, String factoryMethod) {
super(clazz);
this.factoryBean = factoryBean;
this.factoryMethod = factoryMethod;
}

public ProducedBean(Class<?> clazz, String id, BeanRef factoryBean, String factoryMethod) {
super(clazz);
this.id = id;
this.factoryBean = factoryBean;
this.factoryMethod = factoryMethod;
}

public void setSingleton(){
this.isPrototype = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ public void testGenerateReferenceWithFilter() throws Exception {
assertEquals("(mode=123)", xpath.evaluate("@filter", ser2));
}

@Test
public void testProducesNamedBeans() throws Exception {
Node bean1 = getBeanById("produced1");
assertEquals("org.apache.aries.blueprint.plugin.test.MyProduced", xpath.evaluate("@class", bean1));
assertEquals("myFactoryNamedBean", xpath.evaluate("@factory-ref", bean1));
assertEquals("createBean1", xpath.evaluate("@factory-method", bean1));
assertEquals("prototype", xpath.evaluate("@scope", bean1));

Node bean2 = getBeanById("produced2");
assertEquals("org.apache.aries.blueprint.plugin.test.MyProduced", xpath.evaluate("@class", bean1));
assertEquals("myFactoryNamedBean", xpath.evaluate("@factory-ref", bean2));
assertEquals("createBean2", xpath.evaluate("@factory-method", bean2));
assertEquals("", xpath.evaluate("@scope", bean2));

Node myBean5 = getBeanById("myBean5");
assertEquals("produced2", xpath.evaluate("argument[8]/@ref", myBean5));
}

private static Document readToDocument(ByteArrayOutputStream os) throws ParserConfigurationException,
SAXException, IOException {
InputStream is = new ByteArrayInputStream(os.toByteArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,15 @@ public void testParseBeanWithConstructorInject() {
assertNull("There should be no destroyMethod", bean.destroyMethod);
assertTrue("There should be no persistenceUnit", bean.persistenceFields.isEmpty());
assertEquals(0, bean.properties.size());
assertEquals(7, bean.constructorArguments.size());
assertEquals(8, bean.constructorArguments.size());
assertEquals("my2", bean.constructorArguments.get(0).getRef());
assertEquals("serviceA", bean.constructorArguments.get(1).getRef());
assertEquals("serviceB", bean.constructorArguments.get(2).getRef());
assertEquals("100", bean.constructorArguments.get(3).getValue());
assertEquals("ser1", bean.constructorArguments.get(4).getRef());
assertEquals("ser2", bean.constructorArguments.get(5).getRef());
assertEquals("serviceA", bean.constructorArguments.get(6).getRef());
assertEquals("produced2", bean.constructorArguments.get(7).getRef());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public MyBean5(@Named("my2") ServiceA serviceA1,
@Value("100") int bla,
@OsgiService(filter = "myRef") @Named("ser1") ServiceC myReference,
@OsgiService(filter = "(mode=123)") @Named("ser2") ServiceC myReference2,
@AnnotatedService ServiceA serviceAAnnotated) {
@AnnotatedService ServiceA serviceAAnnotated,
@Named("produced2") MyProduced myProduced) {
this.serviceA1 = serviceA1;
this.serviceA2 = serviceA2;
this.serviceB = serviceB;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.apache.aries.blueprint.plugin.test;

import javax.enterprise.inject.Produces;
import javax.inject.Named;
import javax.inject.Singleton;

@Singleton
public class MyFactoryNamedBean {

@Produces
@Named("produced1")
public MyProduced createBean1() {
return new MyProduced("My message");
}

@Produces
@Named("produced2")
@Singleton
public MyProduced createBean2() {
return new MyProduced("My message");
}
}