Skip to content

Commit

Permalink
WELD-460 this also fixed adding multiple ejb's with the same java typ…
Browse files Browse the repository at this point in the history
…e through BeforeBeanDiscovery.addAnnotatedType()
  • Loading branch information
stuartwdouglas committed Mar 4, 2010
1 parent 37c4d89 commit f0a1522
Show file tree
Hide file tree
Showing 12 changed files with 337 additions and 2 deletions.
29 changes: 28 additions & 1 deletion impl/src/main/java/org/jboss/weld/bean/SessionBean.java
Expand Up @@ -72,6 +72,7 @@
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;
import org.jboss.weld.serialization.spi.helpers.SerializableContextual;
import org.jboss.weld.util.AnnotatedTypes;
import org.jboss.weld.util.Beans;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Proxies.TypeInfo;
Expand Down Expand Up @@ -106,13 +107,39 @@ public class SessionBean<T> extends AbstractClassBean<T>
public static <T> SessionBean<T> of(InternalEjbDescriptor<T> ejbDescriptor, BeanManagerImpl beanManager)
{
WeldClass<T> type = beanManager.getServices().get(ClassTransformer.class).loadClass(ejbDescriptor.getBeanClass());
return new SessionBean<T>(type, ejbDescriptor, createId(SessionBean.class.getSimpleName(), ejbDescriptor) , beanManager);
return new SessionBean<T>(type, ejbDescriptor, createId(SessionBean.class.getSimpleName(), ejbDescriptor, type), beanManager);
}

/**
* Creates a simple, annotation defined Enterprise Web Bean using the annotations specified on type
*
* @param <T> The type
* @param clazz The class
* @param beanManager the current manager
* @param type the AnnotatedType to use
* @return An Enterprise Web Bean
*/
public static <T> SessionBean<T> of(InternalEjbDescriptor<T> ejbDescriptor, BeanManagerImpl beanManager, WeldClass<T> type)
{
return new SessionBean<T>(type, ejbDescriptor, createId(SessionBean.class.getSimpleName(), ejbDescriptor, type), beanManager);
}

protected static String createId(String beanType, InternalEjbDescriptor<?> ejbDescriptor)
{
return new StringBuilder().append(beanType).append(BEAN_ID_SEPARATOR).append(ejbDescriptor.getEjbName()).toString();
}

protected static String createId(String beanType, InternalEjbDescriptor<?> ejbDescriptor, WeldClass<?> type)
{
if (type.isDiscovered())
{
return createId(beanType, ejbDescriptor);
}
else
{
return new StringBuilder().append(beanType).append(BEAN_ID_SEPARATOR).append(ejbDescriptor.getEjbName()).append(AnnotatedTypes.createTypeId(type)).toString();
}
}

/**
* Constructor
Expand Down
Expand Up @@ -265,6 +265,15 @@ protected <T> SessionBean<T> createSessionBean(InternalEjbDescriptor<T> ejbDescr
return bean;
}

protected <T> SessionBean<T> createSessionBean(InternalEjbDescriptor<T> ejbDescriptor, WeldClass<T> weldClass)
{
// TODO Don't create enterprise bean if it has no local interfaces!
SessionBean<T> bean = SessionBean.of(ejbDescriptor, manager, weldClass);
getEnvironment().addSessionBean(bean);
createObserversProducersDisposers(bean);
return bean;
}

protected <T> void createNewSessionBean(InternalEjbDescriptor<T> ejbDescriptor)
{
getEnvironment().addSessionBean(NewSessionBean.of(ejbDescriptor, manager));
Expand Down
20 changes: 19 additions & 1 deletion impl/src/main/java/org/jboss/weld/bootstrap/BeanDeployer.java
Expand Up @@ -34,6 +34,9 @@
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.resources.ClassTransformer;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

/**
* @author pmuir
*
Expand Down Expand Up @@ -92,6 +95,7 @@ public BeanDeployer addClasses(Iterable<Class<?>> classes)

public BeanDeployer createBeans()
{
Multimap<Class<?>, WeldClass<?>> otherWeldClasses = HashMultimap.create();
for (WeldClass<?> clazz : classes)
{
boolean managedBeanOrDecorator = !getEnvironment().getEjbDescriptors().contains(clazz.getJavaClass()) && isTypeManagedBeanOrDecoratorOrInterceptor(clazz);
Expand All @@ -109,12 +113,26 @@ else if (managedBeanOrDecorator && !clazz.isAbstract())
{
createManagedBean(clazz);
}
else
{
otherWeldClasses.put(clazz.getJavaClass(), clazz);
}
}
for (InternalEjbDescriptor<?> ejbDescriptor : getEnvironment().getEjbDescriptors())
{
if (ejbDescriptor.isSingleton() || ejbDescriptor.isStateful() || ejbDescriptor.isStateless())
{
createSessionBean(ejbDescriptor);
if (otherWeldClasses.containsKey(ejbDescriptor.getBeanClass()))
{
for (WeldClass<?> c : otherWeldClasses.get(ejbDescriptor.getBeanClass()))
{
createSessionBean(ejbDescriptor, (WeldClass) c);
}
}
else
{
createSessionBean(ejbDescriptor);
}
}
}

Expand Down
@@ -0,0 +1,60 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;
import javax.enterprise.util.AnnotationLiteral;

import org.jboss.weld.tests.util.annotated.TestAnnotatedTypeBuilder;

public class AnnotatedTypeEjbExtension implements Extension
{
/**
* Adds two ejb beans
*/
public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery beforeBeanDiscovery)
{
TestAnnotatedTypeBuilder<Lathe> builder = new TestAnnotatedTypeBuilder<Lathe>(Lathe.class);
builder.addToClass(new AnnotationLiteral<SmallLathe>() { });
beforeBeanDiscovery.addAnnotatedType(builder.create());
builder = new TestAnnotatedTypeBuilder<Lathe>(Lathe.class);
builder.addToClass(new AnnotationLiteral<BigLathe>() { });
beforeBeanDiscovery.addAnnotatedType(builder.create());
}
/**
* Adds annotations to an EJB
*/
public void overrideLatheAnnotations(@Observes ProcessAnnotatedType<Lathe> event) throws SecurityException, NoSuchMethodException
{
TestAnnotatedTypeBuilder<Lathe> builder = new TestAnnotatedTypeBuilder<Lathe>(Lathe.class);
for(Annotation a : event.getAnnotatedType().getAnnotations())
{
builder.addToClass(a);
}
Method method = Lathe.class.getMethod("doWork");
builder.addToMethod(method, new AnnotationLiteral<ConveyorShaft>() {});
builder.addToMethod(method, new AnnotationLiteral<Produces>() {});
event.setAnnotatedType(builder.create());
}
}
@@ -0,0 +1,56 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import javax.enterprise.util.AnnotationLiteral;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.Classes;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.testharness.impl.packaging.Packaging;
import org.jboss.testharness.impl.packaging.PackagingType;
import org.jboss.testharness.impl.packaging.jsr299.Extension;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;
/**
* Tests that it is possible to override ejb annotations through the SPI
* @author Stuart Douglas <stuart@baileyroberts.com.au>
*
*/
@Artifact
@Packaging(PackagingType.EAR)
@IntegrationTest
@Extension("javax.enterprise.inject.spi.Extension")
@Classes(packages = { "org.jboss.weld.tests.util.annotated" })
public class AnnotatedTypeSessionBeanTest extends AbstractWeldTest
{
@Test
public void testOverridingEjbAnnotations()
{
Shaft conveyerShaft = getReference(Shaft.class, new AnnotationLiteral<ConveyorShaft>() { });
assert conveyerShaft != null;
}

@Test
public void testAddingBultipleBeansPerEjbClass()
{
LatheLocal bigLathe = getReference(LatheLocal.class, new AnnotationLiteral<BigLathe>() { });
assert bigLathe != null;
LatheLocal smallLathe = getReference(LatheLocal.class, new AnnotationLiteral<SmallLathe>() { });
assert smallLathe != null;
}
}
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface BigLathe
{

}
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface ConveyorShaft
{

}
@@ -0,0 +1,28 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import javax.ejb.Stateless;

@Stateless
public class Lathe implements LatheLocal
{
public Shaft doWork()
{
return new Shaft();
}
}
@@ -0,0 +1,27 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import javax.ejb.Local;

@Local
public interface LatheLocal
{

public Shaft doWork();

}
@@ -0,0 +1,22 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

public class Shaft
{

}
@@ -0,0 +1,29 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat, Inc., and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed 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
* 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 KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.tests.extensions.annotatedType.ejb;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Qualifier;

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface SmallLathe
{

}

0 comments on commit f0a1522

Please sign in to comment.