Skip to content

Commit

Permalink
context hierarchy changes
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@189 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
nickarls committed Oct 28, 2008
1 parent ea0d3ac commit 70c7c75
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 38 deletions.
Expand Up @@ -3,7 +3,6 @@
import java.lang.annotation.Annotation;
import java.util.HashMap;

import javax.webbeans.ContextNotActiveException;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Context;
import javax.webbeans.manager.Manager;
Expand All @@ -21,7 +20,7 @@
public abstract class AbstractContext implements Context
{

private class BeanMap extends MapWrapper<Bean<? extends Object>, Object>
protected class BeanMap extends MapWrapper<Bean<? extends Object>, Object>
{

public BeanMap()
Expand All @@ -37,49 +36,17 @@ public <T extends Object> T get(Bean<? extends T> key)

}

private BeanMap beans;
protected BeanMap beans;
private Class<? extends Annotation> scopeType;
private boolean active;
protected boolean active;

public AbstractContext(Class<? extends Annotation> scopeType)
{
this.scopeType = scopeType;
beans = new BeanMap();
active = true;
}

public <T> T get(Bean<T> bean, boolean create)
{
if (!active)
{
throw new ContextNotActiveException();
}

if (beans == null)
{
// Context has been destroyed
return null;
}

T instance = beans.get(bean);

if (instance != null)
{
return instance;
}

if (!create)
{
return null;
}

// TODO should bean creation be synchronized?

instance = bean.create();

beans.put(bean, instance);
return instance;
}
public abstract <T> T get(Bean<T> bean, boolean create);

public Class<? extends Annotation> getScopeType()
{
Expand Down
Expand Up @@ -2,20 +2,33 @@

import java.lang.annotation.Annotation;

import javax.webbeans.ContextNotActiveException;
import javax.webbeans.Dependent;
import javax.webbeans.manager.Bean;

public class DependentContext extends PseudoContext
{

public DependentContext(Class<? extends Annotation> scopeType)
{
super(Dependent.class);
active = false;
}

@Override
public <T> T get(Bean<T> bean, boolean create)
{
if (!active)
{
throw new ContextNotActiveException();
}

return create == false ? null : bean.create();
}

@Override
public String toString()
{
return "Dependent context";
}

}
Expand Up @@ -2,12 +2,49 @@

import java.lang.annotation.Annotation;

import javax.webbeans.ContextNotActiveException;
import javax.webbeans.manager.Bean;

public abstract class NormalContext extends AbstractContext
{

public NormalContext(Class<? extends Annotation> scopeType)
{
super(scopeType);
active = true;
}

public <T> T get(Bean<T> bean, boolean create)
{
if (!active)
{
throw new ContextNotActiveException();
}

if (beans == null)
{
// Context has been destroyed
return null;
}

T instance = beans.get(bean);

if (instance != null)
{
return instance;
}

if (!create)
{
return null;
}

// TODO should bean creation be synchronized?

instance = bean.create();

beans.put(bean, instance);
return instance;
}

}

0 comments on commit 70c7c75

Please sign in to comment.