Skip to content

Commit

Permalink
Passivating contexts + test
Browse files Browse the repository at this point in the history
still missing decorator/interceptor/EJB lifecycle dependent ones

WBRI-90

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@757 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Jan 4, 2009
1 parent 1a61565 commit 8caa0c2
Show file tree
Hide file tree
Showing 43 changed files with 1,120 additions and 160 deletions.
Expand Up @@ -21,12 +21,18 @@
import java.lang.reflect.Type;
import java.util.HashSet;

import javax.webbeans.BindingType;
import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.IllegalProductException;
import javax.webbeans.UnserializableDependencyException;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.MetaDataCache;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.jlr.AbstractAnnotatedMember;
import org.jboss.webbeans.util.Names;
import org.jboss.webbeans.util.Reflections;

Expand Down Expand Up @@ -170,6 +176,82 @@ protected Object getReceiver()
return getAnnotatedItem().isStatic() ? null : manager.getInstance(getDeclaringBean());
}

protected void checkInjectionPoints()
{
for (AnnotatedItem<?, ?> injectionPoint : getInjectionPoints())
{
Annotation[] bindings = injectionPoint.getMetaAnnotationsAsArray(BindingType.class);
Bean<?> bean = manager.resolveByType(injectionPoint.getType(), bindings).iterator().next();
if (Dependent.class.equals(bean.getScopeType()) && !bean.isSerializable())
{
throw new UnserializableDependencyException(bean + " is a non-serializable dependent injection for " + injectionPoint + " in " + this);
}
}
}

/**
* Creates an instance of the bean
*
* @returns The instance
*/
@Override
public T create()
{
try
{
DependentContext.INSTANCE.setActive(true);
boolean passivating = MetaDataCache.instance().getScopeModel(scopeType).isPassivating();
if (passivating)
{
checkProducedInjectionPoints();
}
T instance = produceInstance();
checkReturnValue(instance);
return instance;
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

@Override
public void destroy(T instance)
{
try
{
DependentContext.INSTANCE.setActive(true);
// TODO Implement any cleanup needed
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

protected void checkProducedInjectionPoints()
{
for (AnnotatedItem<?, ?> injectionPoint : getInjectionPoints())
{
if (injectionPoint instanceof AbstractAnnotatedMember)
{
if (((AbstractAnnotatedMember<?, ?>) injectionPoint).isTransient())
{
continue;
}
}
Annotation[] bindings = injectionPoint.getMetaAnnotationsAsArray(BindingType.class);
Bean<?> bean = manager.resolveByType(injectionPoint.getType(), bindings).iterator().next();
boolean producerBean = (bean instanceof ProducerMethodBean || bean instanceof ProducerFieldBean);
if (producerBean && Dependent.class.equals(bean.getScopeType()) && !bean.isSerializable())
{
throw new IllegalProductException("Dependent-scoped producer bean " + producerBean + " produces a non-serializable product for injection for " + injectionPoint + " in " + this);
}
}
}

protected abstract T produceInstance();

/**
* Gets a string representation
*
Expand All @@ -193,4 +275,29 @@ public String toString()
return buffer.toString();
}

@Override
public boolean isSerializable()
{
boolean normalScoped = MetaDataCache.instance().getScopeModel(scopeType).isNormal();
if (normalScoped)
{
boolean passivatingScoped = MetaDataCache.instance().getScopeModel(scopeType).isPassivating();
if (passivatingScoped)
{
checkInjectionPoints();
return true;
}
else
{
return true;
}
}
else
{
return isProductSerializable();
}
}

protected abstract boolean isProductSerializable();

}
Expand Up @@ -20,7 +20,6 @@
import java.lang.reflect.Field;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.jlr.AnnotatedFieldImpl;
import org.jboss.webbeans.util.Names;
Expand Down Expand Up @@ -64,41 +63,14 @@ public ProducerFieldBean(AnnotatedField<T> field, AbstractClassBean<?> declaring
init();
}

/**
* Creates an instance of the bean
*
* @returns The instance
*/
@Override
public T create()
{
try
{
DependentContext.INSTANCE.setActive(true);
T instance = field.get(getReceiver());
checkReturnValue(instance);
return instance;
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

@Override
public void destroy(T instance)
public T produceInstance()
{
try
{
DependentContext.INSTANCE.setActive(true);
// TODO Implement any cleanup needed
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
return field.get(getReceiver());
}


/**
* Gets the annotated item representing the field
*
Expand Down Expand Up @@ -144,18 +116,8 @@ public String toString()
return buffer.toString();
}

public String toDetailedString()
{
StringBuilder buffer = new StringBuilder();
buffer.append("ProducerFieldBean:\n");
buffer.append(super.toString() + "\n");
buffer.append("Declaring bean: " + declaringBean.toString() + "\n");
buffer.append("Field: " + field.toString() + "\n");
return buffer.toString();
}

@Override
public boolean isSerializable()
public boolean isProductSerializable()
{
return Reflections.isSerializable(field.getAnnotatedField().getClass());
}
Expand Down
Expand Up @@ -27,7 +27,6 @@
import javax.webbeans.Observes;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.context.DependentContext;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.AnnotatedParameter;
import org.jboss.webbeans.introspector.jlr.AnnotatedMethodImpl;
Expand Down Expand Up @@ -74,39 +73,9 @@ public ProducerMethodBean(AnnotatedMethod<T> method, AbstractClassBean<?> declar
init();
}

/**
* Creates an instance of the bean
*
* @returns The instance
*/
@Override
public T create()
{
try
{
DependentContext.INSTANCE.setActive(true);
T instance = method.invoke(getReceiver(), manager);
checkReturnValue(instance);
return instance;
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
}

@Override
public void destroy(T instance)
public T produceInstance()
{
try
{
DependentContext.INSTANCE.setActive(true);
// TODO Implement any cleanup needed
}
finally
{
DependentContext.INSTANCE.setActive(false);
}
return method.invoke(getReceiver(), manager);
}

/**
Expand Down Expand Up @@ -232,19 +201,10 @@ public String toString()
return buffer.toString();
}

public String toDetailedString()
{
StringBuilder buffer = new StringBuilder();
buffer.append("ProducerMethodBean:\n");
buffer.append(super.toString() + "\n");
buffer.append("Declaring bean: " + declaringBean.toString() + "\n");
buffer.append("Method: " + method.toString() + "\n");
return buffer.toString();
}

@Override
public boolean isSerializable()
protected boolean isProductSerializable()
{
return Reflections.isSerializable(method.getAnnotatedMethod().getReturnType());
}

}
@@ -1,5 +1,10 @@
package org.jboss.webbeans.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;

import javax.webbeans.Production;
Expand Down Expand Up @@ -27,6 +32,7 @@ public final void before()
addStandardDeploymentTypesForTests();
}

@SuppressWarnings("unchecked")
protected void addStandardDeploymentTypesForTests()
{
manager.setEnabledDeploymentTypes(Arrays.asList(Standard.class, Production.class, AnotherDeploymentType.class, HornedAnimalDeploymentType.class));
Expand All @@ -37,4 +43,16 @@ protected <T> void addToEjbCache(Class<T> clazz)
manager.getEjbDescriptorCache().add(new MockEjbDescriptor<T>(clazz));
}

protected byte[] serialize(Object instance) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(instance);
return bytes.toByteArray();
}

protected Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
return in.readObject();
}

}
@@ -1,10 +1,6 @@
package org.jboss.webbeans.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import javax.webbeans.DefinitionException;
import javax.webbeans.UnproxyableDependencyException;
Expand Down Expand Up @@ -49,18 +45,6 @@ public void testClientProxyNotUsedForPseudoScope()
DependentContext.INSTANCE.setActive(false);
}
}

private byte[] serializeBean(Object instance) throws IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(instance);
return bytes.toByteArray();
}

private Object deserializeBean(byte[] bytes) throws IOException, ClassNotFoundException {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
return in.readObject();
}

@Test(groups = "Reflections")
@SpecAssertion(section = "5.4")
Expand All @@ -70,8 +54,8 @@ public void testSimpleWebBeanClientProxyIsSerializable() throws IOException, Cla
manager.addBean(tunaBean);
TunedTuna tuna = manager.getInstance(tunaBean);
assert Reflections.isProxy(tuna);
byte[] bytes = serializeBean(tuna);
tuna = (TunedTuna) deserializeBean(bytes);
byte[] bytes = serialize(tuna);
tuna = (TunedTuna) deserialize(bytes);
assert Reflections.isProxy(tuna);
assert tuna.getState().equals("tuned");
}
Expand All @@ -82,6 +66,7 @@ public void testInjectionPointWithUnproxyableTypeWhichResolvesToNormalScopedWebB
{
Bean<FinalTuna> tunaBean = BeanFactory.createSimpleBean(FinalTuna.class, manager);
manager.addBean(tunaBean);
@SuppressWarnings("unused")
FinalTuna tuna = manager.getInstanceByType(FinalTuna.class);
assert false;
}
Expand All @@ -100,6 +85,7 @@ public void testClientProxyInvocation()
@Test(groups = "Reflections", expectedExceptions=DefinitionException.class)
public void testGettingUnknownBeanFails() {
Bean<Tuna> tunaBean = BeanFactory.createSimpleBean(Tuna.class, manager);
@SuppressWarnings("unused")
Tuna tuna = manager.getInstance(tunaBean);
assert false;
}
Expand Down

0 comments on commit 8caa0c2

Please sign in to comment.