[webbeans-commits] Webbeans SVN: r321 - in ri/trunk/webbeans-ri/src: main/java/org/jboss/webbeans/servlet and 1 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Tue Nov 18 02:49:06 EST 2008
Author: nickarls
Date: 2008-11-18 02:49:06 -0500 (Tue, 18 Nov 2008)
New Revision: 321
Added:
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockHttpSession.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockServletContext.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java
Log:
Minor context tunings + mocks
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java 2008-11-18 07:14:06 UTC (rev 320)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/PrivateContext.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -6,7 +6,7 @@
public class PrivateContext extends AbstractContext
{
private ThreadLocal<AtomicBoolean> active;
- private ThreadLocal<BeanMap> beans;
+ protected ThreadLocal<BeanMap> beans;
public PrivateContext(Class<? extends Annotation> scopeType)
{
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java 2008-11-18 07:14:06 UTC (rev 320)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionBeanMap.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -28,10 +28,17 @@
{
this.session = session;
}
+
+ private void checkSession() {
+ if (session == null) {
+ throw new IllegalArgumentException("Session has not been initialized in SessionBeanMap");
+ }
+ }
@SuppressWarnings("unchecked")
public <T> T get(Bean<? extends T> bean)
{
+ checkSession();
T instance = cache.get(bean);
if (instance != null)
{
@@ -48,6 +55,7 @@
public <T> T remove(Bean<? extends T> bean)
{
+ checkSession();
T instance = get(bean);
String id = KEY_PREFIX + manager.getBeans().indexOf(bean);
session.removeAttribute(id);
@@ -58,6 +66,7 @@
@SuppressWarnings("unchecked")
public void clear()
{
+ checkSession();
Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
@@ -66,8 +75,11 @@
cache.clear();
}
+ @SuppressWarnings("unchecked")
public Iterable<Bean<? extends Object>> keySet()
{
+ checkSession();
+
List<Bean<?>> beans = new ArrayList<Bean<?>>();
Enumeration names = session.getAttributeNames();
@@ -85,6 +97,7 @@
public <T> T put(Bean<? extends T> bean, T instance)
{
+ checkSession();
String id = KEY_PREFIX + manager.getBeans().indexOf(bean);
session.setAttribute(id, instance);
return cache.put(bean, instance);
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java 2008-11-18 07:14:06 UTC (rev 320)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/contexts/SessionContext.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -1,29 +1,25 @@
package org.jboss.webbeans.contexts;
+import javax.servlet.http.HttpSession;
import javax.webbeans.SessionScoped;
import org.jboss.webbeans.ManagerImpl;
public class SessionContext extends PrivateContext {
- private ThreadLocal<BeanMap> beans;
-
public SessionContext(ManagerImpl manager)
{
super(SessionScoped.class);
- beans = new ThreadLocal<BeanMap>();
beans.set(new SessionBeanMap(manager));
}
@Override
- public BeanMap getBeanMap()
- {
- return beans.get();
- }
-
- @Override
public String toString()
{
return "Session context";
}
+
+ public void setSession(HttpSession session) {
+ ((SessionBeanMap)getBeanMap()).setSession(session);
+ }
}
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java 2008-11-18 07:14:06 UTC (rev 320)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/servlet/ServletLifecycle.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -8,9 +8,12 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.contexts.SessionBeanMap;
import org.jboss.webbeans.contexts.SessionContext;
+import org.jboss.webbeans.util.JNDI;
public class ServletLifecycle
{
+ private static final String MANAGER_JNDI_KEY = "java:comp/Manager";
+
private static ServletContext servletContext;
public static void beginApplication(ServletContext context)
@@ -30,10 +33,9 @@
}
public static void beginRequest(HttpServletRequest request) {
- ManagerImpl manager = new ManagerImpl();
+ ManagerImpl manager = (ManagerImpl) JNDI.lookup(MANAGER_JNDI_KEY);
SessionContext sessionContext = (SessionContext) manager.getContext(SessionScoped.class);
- SessionBeanMap sessionBeanMap = (SessionBeanMap) sessionContext.getBeanMap();
- sessionBeanMap.setSession(request.getSession(true));
+ sessionContext.setSession(request.getSession(true));
}
public static void endRequest(HttpServletRequest request) {
Added: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockHttpSession.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockHttpSession.java (rev 0)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockHttpSession.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -0,0 +1,152 @@
+package org.jboss.webbeans.test.mock;
+
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+
+ at SuppressWarnings("deprecation")
+public class MockHttpSession implements HttpSession
+{
+ private Map<String, Object> attributes = new HashMap<String, Object>();
+ private boolean isInvalid;
+ private ServletContext servletContext;
+
+ @SuppressWarnings("unchecked")
+ public class IteratorEnumeration implements Enumeration
+ {
+
+ private Iterator iterator;
+
+ public IteratorEnumeration(Iterator iterator)
+ {
+ this.iterator = iterator;
+ }
+
+ public boolean hasMoreElements()
+ {
+ return iterator.hasNext();
+ }
+
+ public Object nextElement()
+ {
+ return iterator.next();
+ }
+
+ }
+
+ public MockHttpSession(ServletContext servletContext)
+ {
+ this.servletContext = servletContext;
+ }
+
+ public boolean isInvalid()
+ {
+ return isInvalid;
+ }
+
+ public long getCreationTime()
+ {
+ return 0;
+ }
+
+ public String getId()
+ {
+ return null;
+ }
+
+ public long getLastAccessedTime()
+ {
+ return 0;
+ }
+
+ private int maxInactiveInterval;
+
+ public void setMaxInactiveInterval(int max)
+ {
+ maxInactiveInterval = max;
+ }
+
+ public int getMaxInactiveInterval()
+ {
+ return maxInactiveInterval;
+ }
+
+ public HttpSessionContext getSessionContext()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ public Object getAttribute(String att)
+ {
+ return attributes.get(att);
+ }
+
+ public Object getValue(String att)
+ {
+ return getAttribute(att);
+ }
+
+ public Enumeration getAttributeNames()
+ {
+ return new IteratorEnumeration( attributes.keySet().iterator() );
+ }
+
+ public String[] getValueNames()
+ {
+ return attributes.keySet().toArray( new String[0] );
+ }
+
+ public void setAttribute(String att, Object value)
+ {
+ if (value==null)
+ {
+ attributes.remove(att);
+ }
+ else
+ {
+ attributes.put(att, value);
+ }
+ }
+
+ public void putValue(String att, Object value)
+ {
+ setAttribute(att, value);
+ }
+
+ public void removeAttribute(String att)
+ {
+ attributes.remove(att);
+ }
+
+ public void removeValue(String att)
+ {
+ removeAttribute(att);
+ }
+
+ public void invalidate()
+ {
+ attributes.clear();
+ isInvalid = true;
+ }
+
+ public boolean isNew()
+ {
+ return false;
+ }
+
+ public Map<String, Object> getAttributes()
+ {
+ return attributes;
+ }
+
+ public ServletContext getServletContext()
+ {
+ return servletContext;
+ }
+
+}
Added: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockServletContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockServletContext.java (rev 0)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockServletContext.java 2008-11-18 07:49:06 UTC (rev 321)
@@ -0,0 +1,167 @@
+package org.jboss.webbeans.test.mock;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+public class MockServletContext implements ServletContext
+{
+
+ public Object getAttribute(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getAttributeNames()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public ServletContext getContext(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getContextPath()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getInitParameter(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getInitParameterNames()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMajorVersion()
+ {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public String getMimeType(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getMinorVersion()
+ {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ public RequestDispatcher getNamedDispatcher(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getRealPath(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public RequestDispatcher getRequestDispatcher(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public URL getResource(String arg0) throws MalformedURLException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public InputStream getResourceAsStream(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set getResourcePaths(String arg0)
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServerInfo()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Servlet getServlet(String arg0) throws ServletException
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public String getServletContextName()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServletNames()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Enumeration getServlets()
+ {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void log(String arg0)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(Exception arg0, String arg1)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void log(String arg0, Throwable arg1)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void removeAttribute(String arg0)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void setAttribute(String arg0, Object arg1)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+}
More information about the weld-commits
mailing list