Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some extensions to springbridge #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions beancontainer/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP1</version>
<type>jar</type>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
19 changes: 7 additions & 12 deletions spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@

<dependencies>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.0-SP1</version>
<type>jar</type>
<scope>compile</scope>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jcdi_1.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand All @@ -60,6 +59,7 @@
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
Expand All @@ -77,24 +77,19 @@
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions spring/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package org.cdisource.springintegration;

import java.lang.reflect.Type;
import java.util.Set;

import javax.enterprise.inject.spi.Bean;
import javax.inject.Named;

import org.cdisource.logging.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import static org.cdisource.logging.LogFactoryManager.logger;

public class CdiBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

private static Logger logger = logger(CdiBeanFactoryPostProcessor.class);

private boolean useLongName;

private BeanManagerLocationUtil beanManagerLocationUtil = new BeanManagerLocationUtil();
Expand All @@ -33,23 +38,40 @@ public void postProcessBeanFactory(
if (bean.getName()!=null && bean.getName().equals("Spring Injection")){
continue;
}
logger.debug("bean types = {}", bean.getTypes());
Class<?> beanClass = getBeanClass(bean);
BeanDefinitionBuilder definition = BeanDefinitionBuilder.rootBeanDefinition(CdiFactoryBean.class)
.addPropertyValue("beanClass", bean.getBeanClass())
.addPropertyValue("beanClass", beanClass)
.addPropertyValue("beanManager", beanManagerLocationUtil.beanManager())
.addPropertyValue("qualifiers", bean.getQualifiers())
.setLazyInit(true);
String name = generateName(bean);
factory.registerBeanDefinition(name, definition.getBeanDefinition());
logger.debug("bean name = {}, bean class = {}", bean.getName(), beanClass.getName());
}
}

private Class<?> getBeanClass(Bean<?> bean) {
Class<?> klass = Object.class;
for (Type type : bean.getTypes()) {
if (type instanceof Class) {
Class<?> currentClass = (Class<?>) type;
if (klass.isAssignableFrom(currentClass)) {
klass = currentClass;
}
}
}
return klass;
}

private String generateName(Bean<?> bean) {
Named named = (Named) bean.getBeanClass().getAnnotation(Named.class);
String name = named != null ? named.value() : generateNameBasedOnClassName(bean);
String name = bean.getName() != null ? bean.getName() : generateNameBasedOnClassName(bean);
return name;
}

private String generateNameBasedOnClassName(Bean<?> bean) {
return !useLongName ? bean.getBeanClass().getSimpleName() + "FactoryBean" : bean.getBeanClass().getName().replace(".", "_") + "FactoryBean";
Class<?> beanClass = getBeanClass(bean);
return !useLongName ? beanClass.getSimpleName() + "FactoryBean" : beanClass.getName().replace(".", "_") + "FactoryBean";
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package org.cdisource.springintegration;


import java.lang.annotation.Annotation;
import java.util.Set;

import javax.enterprise.inject.spi.BeanManager;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.cdisource.beancontainer.BeanContainer;
import org.cdisource.beancontainer.BeanContainerImpl;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class CdiFactoryBean implements FactoryBean<Object>, InitializingBean {

private Class<?> beanClass;
private boolean singleton = true;
private BeanContainer beanContainer;
private BeanManager beanManager;

private Set<Annotation> qualifiers;

@Override
public void afterPropertiesSet() throws Exception {
Expand All @@ -28,7 +31,7 @@ public void setBeanClass(Class<?> beanClass) {

@Override
public Object getObject() throws Exception {
return beanContainer.getBeanByType(beanClass);
return beanContainer.getBeanByType(beanClass, qualifiers.toArray(new Annotation[]{}));
}

@Override
Expand All @@ -49,4 +52,7 @@ public void setBeanManager(BeanManager beanManager) {
this.beanManager = beanManager;
}

public void setQualifiers(Set<Annotation> qualifiers) {
this.qualifiers = qualifiers;
}
}
2 changes: 1 addition & 1 deletion spring/src/main/resources/META-INF/web-fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">

<name>Spring CDI Bridge</name>
<name>SpringCDIBridge</name>


<!-- <ordering></ordering> -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.cdisource.springintegration;

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

public class ProducerBean {

@Produces @Named("mailHost")
public String mailHost() {
return "mail.example.com";
}

@Produces @Named("mailReceiver")
public String mailReceiver() {
return "[email protected]";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package org.cdisource.springintegration;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

public class SpringBeanUsingAutoWired {
Expand All @@ -8,6 +11,12 @@ public class SpringBeanUsingAutoWired {
@Autowired
ClassWithInjectionPoints foo;

@Inject @Named("mailHost")
String mailHost;

@Inject @Named("mailReceiver")
String mailReceiver;

public void validate() {
if (bean == null) {
throw new IllegalStateException("CDI bean is null");
Expand All @@ -21,6 +30,16 @@ public void validate() {
throw new IllegalStateException("you got no foo bean and I pity you!");

}

if (mailHost == null) {
throw new IllegalStateException("mailHost is null");

}

if (mailReceiver == null) {
throw new IllegalStateException("mailReceiver is null");

}
}

}