Skip to content

Commit

Permalink
make the contexts which are singletons really be singletons, instead …
Browse files Browse the repository at this point in the history
…of pretending they are not and then using singleton-ness of root manager to access them :)

git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@425 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
Gavin King authored and gavin.king@gmail.com committed Dec 6, 2008
1 parent 58a6bbe commit f9800a3
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 59 deletions.
6 changes: 3 additions & 3 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -156,9 +156,9 @@ protected void initContexts(Context... contexts)
if (contexts.length == 0)
{
addContext(new DependentContext());
addContext(new RequestContext());
addContext(new SessionContext());
addContext(new ApplicationContext());
addContext(RequestContext.INSTANCE);
addContext(SessionContext.INSTANCE);
addContext(ApplicationContext.INSTANCE);
}
else
{
Expand Down
Expand Up @@ -21,8 +21,6 @@

import javax.webbeans.ApplicationScoped;

import org.jboss.webbeans.ManagerImpl;

/**
* The Application context
*
Expand All @@ -32,6 +30,9 @@
*/
public class ApplicationContext extends AbstractContext
{

public static ApplicationContext INSTANCE = new ApplicationContext();

// The beans
private BeanMap beanMap;
// Is the context active?
Expand All @@ -40,7 +41,7 @@ public class ApplicationContext extends AbstractContext
/**
* Constructor
*/
public ApplicationContext()
protected ApplicationContext()
{
super(ApplicationScoped.class);
this.active = new AtomicBoolean(true);
Expand All @@ -67,16 +68,6 @@ public void setBeanMap(BeanMap applicationBeanMap)
this.beanMap = applicationBeanMap;
}

/**
* Helper method for accessing context
*
* @return The application context
*/
public static ApplicationContext instance()
{
return (ApplicationContext) ManagerImpl.rootManager().getBuiltInContext(ApplicationScoped.class);
}

/**
* Indicates if the context is active
*
Expand Down
Expand Up @@ -19,33 +19,22 @@

import javax.webbeans.RequestScoped;

import org.jboss.webbeans.ManagerImpl;

/**
* The request context
*
* @author Nicklas Karlsson
*/
public class RequestContext extends BasicContext
{

public static RequestContext INSTANCE = new RequestContext();

/**
* Constructor
*/
public RequestContext()
protected RequestContext()
{
super(RequestScoped.class);
}

/**
* Helper method for accessing context
*
* @return The request context
*/
public static RequestContext instance()
{
return (RequestContext) ManagerImpl.rootManager().getBuiltInContext(RequestScoped.class);
}

}

}
Expand Up @@ -19,7 +19,6 @@

import javax.webbeans.SessionScoped;

import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;

Expand All @@ -31,13 +30,16 @@
public class SessionContext extends AbstractContext
{
private static LogProvider log = Logging.getLogProvider(SessionContext.class);

public static SessionContext INSTANCE = new SessionContext();

// The beans
private ThreadLocal<BeanMap> beanMap;

/**
* Constructor
*/
public SessionContext()
protected SessionContext()
{
super(SessionScoped.class);
log.trace("Created session context");
Expand Down Expand Up @@ -65,14 +67,4 @@ public void setBeanMap(BeanMap beanMap)
this.beanMap.set(beanMap);
}

/**
* Helper method for accessing context
*
* @return The session context
*/
public static SessionContext instance()
{
return (SessionContext) ManagerImpl.rootManager().getBuiltInContext(SessionScoped.class);
}

}
Expand Up @@ -57,16 +57,16 @@ public static void beginApplication(ServletContext context)
servletContext = context;
Bootstrap bootstrap = new Bootstrap();
bootstrap.boot(getWebBeanDiscovery());
ApplicationContext.instance().setBeanMap(new ApplicationBeanMap(servletContext));
ApplicationContext.INSTANCE.setBeanMap(new ApplicationBeanMap(servletContext));
}

/**
* Ends the application
*/
public static void endApplication()
{
ApplicationContext.instance().destroy();
ApplicationContext.instance().setBeanMap(null);
ApplicationContext.INSTANCE.destroy();
ApplicationContext.INSTANCE.setBeanMap(null);
servletContext = null;
}

Expand All @@ -86,8 +86,8 @@ public static void beginSession(HttpSession session)
*/
public static void endSession(HttpSession session)
{
SessionContext.instance().destroy();
SessionContext.instance().setBeanMap(null);
SessionContext.INSTANCE.destroy();
SessionContext.INSTANCE.setBeanMap(null);
}

/**
Expand All @@ -99,7 +99,7 @@ public static void endSession(HttpSession session)
*/
public static void beginRequest(HttpServletRequest request)
{
SessionContext.instance().setBeanMap(new SessionBeanMap(request.getSession()));
SessionContext.INSTANCE.setBeanMap(new SessionBeanMap(request.getSession()));
}

/**
Expand All @@ -109,8 +109,8 @@ public static void beginRequest(HttpServletRequest request)
*/
public static void endRequest(HttpServletRequest request)
{
RequestContext.instance().destroy();
SessionContext.instance().setBeanMap(null);
RequestContext.INSTANCE.destroy();
SessionContext.INSTANCE.setBeanMap(null);
}

/**
Expand Down
Expand Up @@ -17,15 +17,14 @@ public class AbstractTest

protected MockManagerImpl manager;
protected Bootstrap bootstrap;


@BeforeMethod
public final void before()
{
manager = new MockManagerImpl();
MockManagerImpl.setInstance(manager);
// Mock the ApplicationContext as a simple map
ApplicationContext.instance().setBeanMap(new SimpleBeanMap());
ApplicationContext.INSTANCE.setBeanMap(new SimpleBeanMap());
bootstrap = new MockBootstrap();
init();
}
Expand Down
Expand Up @@ -21,7 +21,7 @@ public void testInjectingManager()
@Test(expectedExceptions={ContextNotActiveException.class}, groups={"manager"}) @SpecAssertion(section="8.6")
public void testGetContextWithNoActiveContextsFails()
{
Context requestContext = new RequestContext();
Context requestContext = new RequestContext() {};
((AbstractContext)requestContext).setActive(false);
manager.setContexts(requestContext);
manager.getContext(RequestScoped.class);
Expand All @@ -30,8 +30,8 @@ public void testGetContextWithNoActiveContextsFails()
@Test(expectedExceptions={IllegalArgumentException.class}, groups={"manager"}) @SpecAssertion(section="8.6")
public void testGetContextWithTooManyActiveContextsFails()
{
Context firstContext = new RequestContext();
Context secondContext = new RequestContext();
Context firstContext = new RequestContext() {};
Context secondContext = new RequestContext() {};
manager.setContexts(firstContext, secondContext);
manager.getContext(RequestScoped.class);
assert true;
Expand All @@ -48,7 +48,7 @@ public void testGetContextWithNoRegisteredContextsFails()
@Test(groups={"manager"}) @SpecAssertion(section="8.6")
public void testGetContextReturnsActiveContext()
{
Context requestContext = new RequestContext();
Context requestContext = new RequestContext() {};
manager.setContexts(requestContext);
Context testContext = manager.getContext(RequestScoped.class);
assert testContext == requestContext;
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class NormalContextTest extends AbstractTest

@BeforeMethod
public void initContext() {
context = new RequestContext();
context = new RequestContext() {};
}

@Test(groups="contexts") @SpecAssertion(section="8.1")
Expand Down
Expand Up @@ -19,7 +19,7 @@ public class PassivatingContextTest extends AbstractTest
@BeforeMethod
public void initContext()
{
context = new RequestContext();
context = new RequestContext() {};
}

@Test(groups = {"stub", "contexts", "passivation" }) @SpecAssertion(section = "8.4")
Expand Down

0 comments on commit f9800a3

Please sign in to comment.