[webbeans-commits] Webbeans SVN: r69 - in ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans: init and 1 other directories.
by webbeans-commits@lists.jboss.org
Author: shane.bryzak(a)jboss.com
Date: 2008-08-05 21:20:03 -0400 (Tue, 05 Aug 2008)
New Revision: 69
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/ServletRequestSessionMap.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansFilter.java
Modified:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ContainerImpl.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/init/Initialization.java
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java
Log:
initialization / context stuff
Modified: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ContainerImpl.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ContainerImpl.java 2008-08-06 01:07:01 UTC (rev 68)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/ContainerImpl.java 2008-08-06 01:20:03 UTC (rev 69)
@@ -4,11 +4,13 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.webbeans.ComponentInstance;
import javax.webbeans.Container;
import javax.webbeans.Context;
+import javax.webbeans.ContextNotActive;
import javax.webbeans.Observer;
import javax.webbeans.Standard;
import javax.webbeans.TypeLiteral;
@@ -24,6 +26,9 @@
private StereotypeManager stereotypeManager;
private EjbManager ejbLookupManager;
+ private ThreadLocal<Map<Class<Annotation>, Context>> contexts =
+ new ThreadLocal<Map<Class<Annotation>, Context>>();
+
private Set<ComponentInstance> components;
public ContainerImpl(List<Annotation> enabledDeploymentTypes)
@@ -78,10 +83,17 @@
public Context getContext(Class<Annotation> scopeType)
{
- // TODO Auto-generated method stub
- return null;
+ Context context = contexts.get().get(scopeType);
-
+ if (context == null)
+ {
+ // If context can't be found throw an exception (section 9.4 of spec)
+ throw new ContextNotActive();
+ }
+ else
+ {
+ return context;
+ }
}
public Object getInstanceByName(String name)
Modified: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/init/Initialization.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/init/Initialization.java 2008-08-06 01:07:01 UTC (rev 68)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/init/Initialization.java 2008-08-06 01:20:03 UTC (rev 69)
@@ -14,12 +14,12 @@
*/
public class Initialization
{
+ public static final String WEBBEANS_CONTAINER_KEY = "javax.webbeans.Container";
+
private ServletContext servletContext;
DeploymentStrategy deploymentStrategy;
-
- private static Container container;
-
+
public Initialization(ServletContext servletContext)
{
this.servletContext = servletContext;
@@ -27,7 +27,10 @@
public Initialization create()
{
- container = new ContainerImpl(null);
+ Container container = new ContainerImpl(null);
+
+ servletContext.setAttribute(WEBBEANS_CONTAINER_KEY, container);
+
deploymentStrategy = new DeploymentStrategy(Thread.currentThread().getContextClassLoader(), container);
return this;
}
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/ServletRequestSessionMap.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/ServletRequestSessionMap.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/ServletRequestSessionMap.java 2008-08-06 01:20:03 UTC (rev 69)
@@ -0,0 +1,115 @@
+package org.jboss.webbeans.servlet;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+/**
+ *
+ * @author Shane Bryzak
+ *
+ */
+public class ServletRequestSessionMap
+{
+ private HttpServletRequest request;
+
+ public ServletRequestSessionMap(HttpServletRequest request)
+ {
+ this.request = request;
+ }
+
+ public void clear()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public boolean containsKey(Object key)
+ {
+ HttpSession session = request.getSession(false);
+ return session==null ? false : session.getAttribute( (String) key )!=null;
+ }
+
+ public boolean containsValue(Object value)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Set<java.util.Map.Entry<String, Object>> entrySet()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object get(Object key)
+ {
+ HttpSession session = request.getSession(false);
+ return session==null ? null : session.getAttribute( (String) key );
+ }
+
+ public boolean isEmpty()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Set<String> keySet()
+ {
+ HttpSession session = request.getSession(false);
+ if (session==null)
+ {
+ return Collections.EMPTY_SET;
+ }
+ else
+ {
+ Set<String> keys = new HashSet<String>();
+ Enumeration<String> names = session.getAttributeNames();
+ while ( names.hasMoreElements() )
+ {
+ keys.add( names.nextElement() );
+ }
+ return keys;
+ }
+ }
+
+ public Object put(String key, Object value)
+ {
+ HttpSession session = request.getSession(true);
+ Object result = session.getAttribute(key);
+ session.setAttribute(key, value);
+ return result;
+ }
+
+ public void putAll(Map<? extends String, ? extends Object> t)
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object remove(Object key)
+ {
+ HttpSession session = request.getSession(false);
+ if (session==null)
+ {
+ return null;
+ }
+ else
+ {
+ Object result = session.getAttribute( (String) key );
+ session.removeAttribute( (String) key );
+ return result;
+ }
+ }
+
+ public int size()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Collection<Object> values()
+ {
+ throw new UnsupportedOperationException();
+ }
+}
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansFilter.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansFilter.java (rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansFilter.java 2008-08-06 01:20:03 UTC (rev 69)
@@ -0,0 +1,54 @@
+package org.jboss.webbeans.servlet;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.webbeans.Container;
+import javax.webbeans.Context;
+import javax.webbeans.RequestScoped;
+
+import org.jboss.webbeans.BasicContext;
+import org.jboss.webbeans.init.Initialization;
+
+/**
+ *
+ * @author Shane Bryzak
+ *
+ */
+public class WebBeansFilter implements Filter
+{
+ private Container container;
+
+ public void init(FilterConfig filterConfig) throws ServletException
+ {
+ container = (Container) filterConfig.getServletContext().getAttribute(
+ Initialization.WEBBEANS_CONTAINER_KEY);
+ }
+
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+ throws IOException, ServletException
+ {
+ BasicContext requestContext = new BasicContext(RequestScoped.class);
+
+ try
+ {
+ container.addContext(requestContext);
+
+ chain.doFilter(request, response);
+ }
+ finally
+ {
+ requestContext.destroy(container);
+ }
+ }
+
+ public void destroy()
+ {
+
+ }
+}
Modified: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java 2008-08-06 01:07:01 UTC (rev 68)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/servlet/WebBeansListener.java 2008-08-06 01:20:03 UTC (rev 69)
@@ -17,8 +17,8 @@
public void contextInitialized(ServletContextEvent event)
{
- ServletLifecycle.beginApplication( event.getServletContext() );
- new Initialization( event.getServletContext() ).create().init();
+ ServletLifecycle.beginApplication( event.getServletContext() );
+ new Initialization( event.getServletContext() ).create().init();
}
public void sessionCreated(HttpSessionEvent event)