Skip to content

Commit

Permalink
WELDX-144 rename @ManagedProducer to @Unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas authored and pmuir committed Sep 21, 2010
1 parent a6dfa95 commit 2dac538
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 51 deletions.

This file was deleted.

27 changes: 27 additions & 0 deletions impl/src/main/java/org/jboss/weld/extensions/unwraps/Unwraps.java
@@ -0,0 +1,27 @@
package org.jboss.weld.extensions.unwraps;

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

/**
* This allows for producer methods that emulate a stateless scope.
*
* If a method is annotated with @Unwraps then a bean is registered with CDI
* with the same types and qualifiers as the @Unwraps method. This bean produces
* a proxy, whenever a method is invoked on the proxy the @Unwraps method is
* called, and the method invocation is forwarded to the result.
*
* This allows you to manually control the lifecycle of an object while still
* allowing it to be injected.
*
* @author Stuart Douglas
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Unwraps
{

}
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.managedproducer;
package org.jboss.weld.extensions.unwraps;

import java.util.HashSet;
import java.util.Set;
Expand All @@ -27,34 +27,33 @@
import javax.enterprise.inject.spi.ProcessAnnotatedType;

/**
* An extension that allows the use of @ManagedProducer methods
* An extension that allows the use of {@link Unwraps} methods
*
* these methods work in a similar manner to @Unwrap methods in seam 2
*
* @author stuart
* @author Stuart Douglas
*
*/
public class ManagedProducerExtension implements Extension
public class UnwrapsExtension implements Extension
{

final private Set<ManagedProducerBean<?>> beans = new HashSet<ManagedProducerBean<?>>();
final private Set<UnwrapsProducerBean<?>> beans = new HashSet<UnwrapsProducerBean<?>>();

public void processAnnotatedType(@Observes ProcessAnnotatedType<?> type, BeanManager manager)
{
for (AnnotatedMethod<?> m : type.getAnnotatedType().getMethods())
{
if (m.isAnnotationPresent(ManagedProducer.class))
if (m.isAnnotationPresent(Unwraps.class))
{
// we have a managed producer
// lets make a note of it and register it later
beans.add(new ManagedProducerBean(m, manager));
beans.add(new UnwrapsProducerBean(m, manager));
}
}
}

public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBean)
{
for (ManagedProducerBean<?> b : beans)
for (UnwrapsProducerBean<?> b : beans)
{
afterBean.addBean(b);
}
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.managedproducer;
package org.jboss.weld.extensions.unwraps;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -34,7 +34,7 @@

import org.jboss.weld.extensions.bean.InjectionPointImpl;

public class ManagedProducerInvocationHandler implements MethodHandler
public class UnwrapsInvocationHandler implements MethodHandler
{

final private BeanManager manager;
Expand All @@ -46,7 +46,7 @@ public class ManagedProducerInvocationHandler implements MethodHandler
final private InjectionPoint injectionPoint;


public ManagedProducerInvocationHandler(BeanManager manager, AnnotatedMethod<?> method, Bean<?> bean, InjectionPoint injectionPoint)
public UnwrapsInvocationHandler(BeanManager manager, AnnotatedMethod<?> method, Bean<?> bean, InjectionPoint injectionPoint)
{
this.manager = manager;
this.method = method.getJavaMember();
Expand All @@ -69,11 +69,11 @@ public ManagedProducerInvocationHandler(BeanManager manager, AnnotatedMethod<?>
Set<Bean<?>> beans = manager.getBeans(mainType, mainClassQualifiers.toArray(new Annotation[0]));
if (beans.isEmpty())
{
throw new UnsatisfiedResolutionException("could not find declaring bean for managed producer method " + method.getDeclaringType().getJavaClass() + "." + this.method.getName());
throw new UnsatisfiedResolutionException("Could not find declaring bean for @Unwrap method " + method.getDeclaringType().getJavaClass() + "." + this.method.getName());
}
else if (beans.size() > 1)
{
throw new AmbiguousResolutionException("could not find declaring bean for managed producer method " + method.getDeclaringType().getJavaClass() + "." + this.method.getName());
throw new AmbiguousResolutionException("Could not find declaring bean for @Unwrap method " + method.getDeclaringType().getJavaClass() + "." + this.method.getName());
}
mainClassBean = beans.iterator().next();
this.injectionPoint = injectionPoint;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.managedproducer;
package org.jboss.weld.extensions.unwraps;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -41,14 +41,13 @@
/**
* Bean implementation that produces a JDK proxy
*
* when a method is invoked on the proxy it calls the managed producer method
* when a method is invoked on the proxy it calls the Unwraps producer method
* and invokes the method on the returned object
*
* @author stuart
* @author Stuart Douglas
*
* @param <M>
*/
public class ManagedProducerBean<M> implements Bean<M>
public class UnwrapsProducerBean<M> implements Bean<M>
{

final private Class<?> beanClass;
Expand All @@ -69,7 +68,7 @@ public class ManagedProducerBean<M> implements Bean<M>
{
} };

public ManagedProducerBean(AnnotatedMethod<?> method, BeanManager manager)
public UnwrapsProducerBean(AnnotatedMethod<?> method, BeanManager manager)
{
this.method = method;
beanClass = method.getDeclaringType().getJavaClass();
Expand Down Expand Up @@ -201,7 +200,7 @@ public M create(CreationalContext<M> creationalContext)
Set<Bean<?>> beans = manager.getBeans(InjectionPoint.class, defaultQualifiers);
Bean<?> injectionPointBean = (Bean<?>) beans.iterator().next();
InjectionPoint injectionPoint = (InjectionPoint) manager.getReference(injectionPointBean, InjectionPoint.class, creationalContext);
ManagedProducerInvocationHandler hdl = new ManagedProducerInvocationHandler(manager, this.method, this, injectionPoint);
UnwrapsInvocationHandler hdl = new UnwrapsInvocationHandler(manager, this.method, this, injectionPoint);
try
{
M obj = proxyClass.newInstance();
Expand Down
Expand Up @@ -2,7 +2,7 @@ org.jboss.weld.extensions.bean.generic.GenericBeanExtension
org.jboss.weld.extensions.resourceLoader.servlet.ServletResourceExtension
org.jboss.weld.extensions.core.CoreExtension
org.jboss.weld.extensions.interceptor.InterceptorExtension
org.jboss.weld.extensions.managedproducer.ManagedProducerExtension
org.jboss.weld.extensions.unwraps.UnwrapsExtension
org.jboss.weld.extensions.defaultbean.DefaultBeanExtension
org.jboss.weld.extensions.log.LoggerExtension
org.jboss.weld.extensions.servicehandler.ServiceHandlerExtension
Expand Down
Expand Up @@ -14,15 +14,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import javax.enterprise.inject.spi.InjectionPoint;

import org.jboss.weld.extensions.managedproducer.ManagedProducer;
import org.jboss.weld.extensions.unwraps.Unwraps;

public class BeanProducer
{
@ManagedProducer
@Unwraps
public ProducedInterface produce(InjectionPoint injectionPoint)
{
ProducedBean b = new ProducedBean();
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import org.jboss.weld.extensions.core.Veto;

Expand Down
@@ -1,9 +1,9 @@
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;

import org.jboss.weld.extensions.managedproducer.ManagedProducer;
import org.jboss.weld.extensions.unwraps.Unwraps;

/*
* JBoss, Home of Professional Open Source
Expand Down Expand Up @@ -32,7 +32,7 @@ public void changeLion()
lion = new Lion("lion two");
}

@ManagedProducer
@Unwraps
@Named("lion")
public Lion produceLion()
{
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import javax.inject.Inject;

Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import org.jboss.weld.extensions.core.Veto;

Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

public interface ProducedInterface
{
Expand Down
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.weld.extensions.test.managedproducer;
package org.jboss.weld.extensions.test.unwraps;

import static org.jboss.weld.extensions.test.util.Deployments.baseDeployment;

Expand All @@ -28,13 +28,13 @@
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class ManagedProducerTest
public class UnwrapsTest
{

@Deployment
public static Archive<?> deployment()
{
return baseDeployment().addPackage(ManagedProducerTest.class.getPackage());
return baseDeployment().addPackage(UnwrapsTest.class.getPackage());
}

@Inject
Expand All @@ -48,14 +48,14 @@ public static Archive<?> deployment()
LionTamer lionTamer;

@Test
public void testManagedProducersInjectionPoint()
public void testUnwrapsInjectionPoint()
{
assert bean.getBean1().getValue().equals("bean1") : " value: " + bean.getBean1().getValue();
assert bean.getBean2().getValue().equals("bean2") : " value: " + bean.getBean2().getValue();
}

@Test
public void testManagedProducers()
public void testUnwraps()
{
assert lion.getName().equals("lion one");
lionTamer.changeLion();
Expand Down

0 comments on commit 2dac538

Please sign in to comment.