Skip to content

Commit

Permalink
Structures for passivating scopes.
Browse files Browse the repository at this point in the history
Got tests but need to sort them out first...

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@750 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Jan 2, 2009
1 parent b47d858 commit 8bf4b56
Show file tree
Hide file tree
Showing 17 changed files with 272 additions and 72 deletions.
53 changes: 45 additions & 8 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -42,6 +42,7 @@
import javax.webbeans.Standard;
import javax.webbeans.TypeLiteral;
import javax.webbeans.UnsatisfiedDependencyException;
import javax.webbeans.UnserializableDependencyException;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Context;
import javax.webbeans.manager.Decorator;
Expand Down Expand Up @@ -98,7 +99,7 @@ public class ManagerImpl implements Manager, Serializable
private Set<Interceptor> interceptors;

private EjbDescriptorCache ejbDescriptorCache;

// The Naming (JNDI) access
private Naming naming;

Expand Down Expand Up @@ -711,32 +712,68 @@ public String toDetailedString()
public Manager parse(InputStream xmlStream)
{
// TODO Implement XML parsing
return null;
return this;
}

public Manager validate()
{
// TODO Implement XML parsing
return null;
checkPassivation();
return this;
}

private void checkPassivation()
{
for (Bean<?> bean : beans)
{
boolean passivatingScoped = MetaDataCache.instance().getScopeModel(bean.getScopeType()).isPassivating();
if (passivatingScoped && !bean.isSerializable())
{
throw new UnserializableDependencyException(bean + " is not serializable or has unserializable dependencies");
}
// // TODO: Not that pretty. Can this logic be moved to the
// isSerializable()
// // of SimpleBean and EnterpriseBean or are they too strict there?
// if (bean instanceof EnterpriseBean)
// {
// boolean stateful =
// getEjbDescriptorCache().containsKey(((EnterpriseBean<?>)
// bean).getType());
// if (stateful && !bean.isSerializable())
// {
// throw new UnserializableDependencyException(bean +
// " is not serializable or has unserializable dependencies");
// }
// }
// else if (bean instanceof SimpleBean)
// {
// boolean passivating =
// MetaDataCache.instance().getScopeModel(bean.getScopeType()).isPassivating();
// if (passivating && !bean.isSerializable())
// {
// throw new UnserializableDependencyException(bean +
// " is not serializable or has unserializable dependencies");
// }
// }
}
}

public Manager createChildManager()
{
// TODO Implement hierarchical managers
return null;
return this;
}

public Manager setCurrent()
{
// TODO Implement hierarchical managers
return null;
return this;
}

public Naming getNaming()
{
return naming;
}

public void setNaming(Naming naming)
{
this.naming = naming;
Expand Down
Expand Up @@ -90,7 +90,7 @@ public static Class<? extends Annotation> getDeploymentType(List<Class<? extends
private Set<Annotation> bindingTypes;
// The name
protected String name;
// The scope type
// The scope type
protected Class<? extends Annotation> scopeType;
// The merged stereotypes
private MergedStereotypes<T, E> mergedStereotypes;
Expand Down Expand Up @@ -212,7 +212,7 @@ protected void initDeploymentType()
*/
protected void initInjectionPoints()
{
injectionPoints = new HashSet<AnnotatedItem<?,?>>();
injectionPoints = new HashSet<AnnotatedItem<?, ?>>();
}

/**
Expand Down Expand Up @@ -503,18 +503,11 @@ public boolean isPrimitive()
return primitive;
}

/**
* Indicates if bean is serializable
*
* @return True if serializable, false otherwise
*
* @see @see javax.webbeans.manager.Bean#isSerializable()
*/
@Override
public boolean isSerializable()
{
// TODO Implement passivating scopes
return false;
// TODO: OK?
return true;
}

/**
Expand Down Expand Up @@ -544,5 +537,5 @@ public String toDetailedString()
buffer.append(mergedStereotypes.toString() + "\n");
return buffer.toString();
}

}
Expand Up @@ -23,19 +23,26 @@

import javax.webbeans.BindingType;
import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.Destructor;
import javax.webbeans.Disposes;
import javax.webbeans.IllegalProductException;
import javax.webbeans.Initializer;
import javax.webbeans.Observes;
import javax.webbeans.Produces;
import javax.webbeans.Production;
import javax.webbeans.UnproxyableDependencyException;
import javax.webbeans.UnserializableDependencyException;
import javax.webbeans.manager.Bean;

import org.jboss.webbeans.CurrentManager;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.MetaDataCache;
import org.jboss.webbeans.introspector.AnnotatedClass;
import org.jboss.webbeans.introspector.AnnotatedField;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.AnnotatedMethod;
import org.jboss.webbeans.introspector.jlr.AbstractAnnotatedMember;
import org.jboss.webbeans.introspector.jlr.AnnotatedClassImpl;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
Expand Down Expand Up @@ -87,6 +94,23 @@ protected void init()
initInitializerMethods();
}

protected void checkPassivation()
{
for (AnnotatedField<Object> injectableField : injectableFields)
{
if (injectableField.isTransient())
{
continue;
}

Bean<?> bean = CurrentManager.rootManager().resolveByType(injectableField).iterator().next();
if (Dependent.class.equals(bean.getScopeType()) && !bean.isSerializable())
{
throw new UnserializableDependencyException("Dependent Web Beans cannot be injected into non-transient fields of beans declaring a passivating scope");
}
}
}

/**
* Initializes the bean type
*/
Expand Down Expand Up @@ -121,7 +145,8 @@ public Set<AnnotatedField<Object>> getProducerFields()
/**
* Gets the observer methods
*
* @return A set of observer methods. An empty set is returned if there are no matches.
* @return A set of observer methods. An empty set is returned if there are
* no matches.
*/
public Set<AnnotatedMethod<Object>> getObserverMethods()
{
Expand Down Expand Up @@ -303,7 +328,7 @@ public String toDetailedString()
}

@Override
/**
/*
* Gets the default deployment type
*
* @return The default deployment type
Expand All @@ -312,4 +337,38 @@ protected Class<? extends Annotation> getDefaultDeploymentType()
{
return Production.class;
}

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 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);
}
}
}
}
Expand Up @@ -26,7 +26,9 @@
import javax.webbeans.IllegalProductException;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.MetaDataCache;
import org.jboss.webbeans.util.Names;
import org.jboss.webbeans.util.Reflections;

/**
* The implicit producer bean
Expand Down Expand Up @@ -151,6 +153,11 @@ protected void checkReturnValue(T instance)
{
throw new IllegalProductException("Cannot return null from a non-dependent producer method");
}
boolean passivating = MetaDataCache.instance().getScopeModel(getScopeType()).isPassivating();
if (passivating && !Reflections.isSerializable(instance.getClass()))
{
throw new IllegalProductException("Producers cannot declare passivating and return non-serializable class");
}
}

/**
Expand Down
Expand Up @@ -54,13 +54,12 @@
public class EnterpriseBean<T> extends AbstractClassBean<T>
{
private LogProvider log = Logging.getLogProvider(EnterpriseBean.class);

// The EJB descriptor
private EjbDescriptor<T> ejbDescriptor;

// The remove method on the bean class (do not call!)
private AnnotatedMethod<?> removeMethod;


/**
* Constructor
Expand Down Expand Up @@ -94,7 +93,7 @@ protected void init()
}
else
{
throw new RuntimeException("TODO Multiple EJBs have the same bean class! " + getType() );
throw new RuntimeException("TODO Multiple EJBs have the same bean class! " + getType());
}
}
initRemoveMethod();
Expand Down Expand Up @@ -180,7 +179,7 @@ protected void initRemoveMethod()
{
throw new DefinitionException("Multiple @Destructor methods not allowed on " + getAnnotatedItem());
}

if (getAnnotatedItem().getAnnotatedMethods(Destructor.class).size() == 1)
{
AnnotatedMethod<?> destructorMethod = getAnnotatedItem().getAnnotatedMethods(Destructor.class).iterator().next();
Expand All @@ -207,14 +206,14 @@ protected void initRemoveMethod()
this.removeMethod = annotatedItem.getMethod(noArgsRemoveMethods.iterator().next());
return;
}

if (!getScopeType().equals(Dependent.class))
{
throw new DefinitionException("Only @Dependent scoped enterprise beans can be without remove methods " + toString());
}

}

/**
* Validates the remove method
*/
Expand Down Expand Up @@ -257,7 +256,8 @@ public T create()
try
{
DependentContext.INSTANCE.setActive(true);
// TODO Implement enterprise bean proxies and select the correct jndiName
// TODO Implement enterprise bean proxies and select the correct
// jndiName
return (T) manager.getNaming().lookup(ejbDescriptor.getLocalJndiName(), getType());
}
catch (Exception e)
Expand All @@ -283,7 +283,7 @@ public void destroy(T instance)
DependentContext.INSTANCE.setActive(true);
removeMethod.invokeOnInstance(instance, manager);
}
catch (Exception e)
catch (Exception e)
{
log.error("Error destroying " + toString(), e);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ protected AbstractBean<? extends T, Class<T>> getSpecializedType()
}

}

public AnnotatedMethod<?> getRemoveMethod()
{
return removeMethod;
Expand Down Expand Up @@ -396,6 +396,10 @@ public void postConstruct(T instance)
try
{
DependentContext.INSTANCE.setActive(true);
if (ejbDescriptor.isStateful())
{
checkProducedInjectionPoints();
}
bindDecorators();
bindInterceptors();
injectEjbAndCommonFields();
Expand All @@ -406,12 +410,26 @@ public void postConstruct(T instance)
{
DependentContext.INSTANCE.setActive(false);
}

}

public void preDestroy(Object target)
{


}

@Override
public boolean isSerializable()
{
if (ejbDescriptor.isStateful())
{
checkInjectionPoints();
return true;
}
else
{
return true;
}
}

}

0 comments on commit 8bf4b56

Please sign in to comment.