myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ima...@apache.org
Subject svn commit: r399408 - in /myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation: ConversationExternalContext.java ConversationManager.java ConversationServletFilter.java FakeMap.java
Date Wed, 03 May 2006 20:30:20 GMT
Author: imario
Date: Wed May  3 13:29:59 2006
New Revision: 399408

URL: http://svn.apache.org/viewcvs?rev=399408&view=rev
Log:
provide a very limited external context if faces context is not there - eg. if called by methods
from the conversationServletFilter

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
  (with props)
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
  (with props)
Modified:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationServletFilter.java

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java?rev=399408&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
Wed May  3 13:29:59 2006
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.myfaces.custom.conversation;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * we need some information we normally get from the FacesContext.getExternalContext BEFORE
the FacesContext is active.
+ * So we require our own ExternalContext.
+ */
+public class ConversationExternalContext
+{
+	private final Map requestMap;
+	private final Map requestParameterMap;
+	
+	protected ConversationExternalContext(Map requestMap, Map requestParameterMap)
+	{
+		this.requestMap = requestMap;
+		this.requestParameterMap = requestParameterMap;
+	}
+	
+	public static ConversationExternalContext create(final HttpServletRequest httpRequest)
+	{
+		Map requestParameterMap = new FakeMap()
+		{
+			public boolean containsKey(Object key)
+			{
+				return httpRequest.getParameter((String) key) != null;
+			}
+
+			public Object get(Object key)
+			{
+				return httpRequest.getParameter((String) key);
+			}
+		};
+ 
+		Map requestMap = new FakeMap()
+		{
+			public boolean containsKey(Object key)
+			{
+				return httpRequest.getAttribute((String) key) != null;
+			}
+
+			public Object get(Object key)
+			{
+				return httpRequest.getAttribute((String) key);
+			}
+
+			public Object put(Object key, Object value)
+			{
+				Object prev = httpRequest.getAttribute((String) key);
+				httpRequest.setAttribute((String) key, value);
+				return prev;
+			}
+		};
+		
+		return new ConversationExternalContext(requestMap, requestParameterMap);
+	}
+
+	public Map getRequestMap()
+	{
+		return requestMap;
+	}
+
+	public Map getRequestParameterMap()
+	{
+		return requestParameterMap;
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationExternalContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java?rev=399408&r1=399407&r2=399408&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
(original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationManager.java
Wed May  3 13:29:59 2006
@@ -45,7 +45,6 @@
 	private PersistenceManagerFactory persistenceManagerFactory;
 	
 	private final Map conversationContexts = new HashMap();
-	
 	private final List registeredEndConversations = new ArrayList(10);
 	
 	protected ConversationManager()
@@ -103,14 +102,35 @@
 	 */
 	protected Long getConversationContextId()
 	{
+		Map requestMap;
+		Map requestParameterMap;
+		
 		FacesContext context = FacesContext.getCurrentInstance();
+		if (context != null)
+		{
+			requestMap = context.getExternalContext().getRequestMap();
+			requestParameterMap = context.getExternalContext().getRequestParameterMap();
+		}
+		else
+		{
+			ConversationExternalContext ccontext = ConversationServletFilter.getConversationExternalContext();
+			if (ccontext != null)
+			{
+				requestMap = ccontext.getRequestMap();
+				requestParameterMap = ccontext.getRequestParameterMap();
+			}
+			else
+			{
+				throw new IllegalStateException("cant find a requestMap or requestParameterMap");
+			}
+		}
 		
-		Long conversationContextId = (Long) context.getExternalContext().getRequestMap().get(CONVERSATION_CONTEXT_REQ);
+		Long conversationContextId = (Long) requestMap.get(CONVERSATION_CONTEXT_REQ);
 		if (conversationContextId == null)
 		{
-			if (context.getExternalContext().getRequestParameterMap().containsKey(CONVERSATION_CONTEXT_PARAM))
+			if (requestParameterMap.containsKey(CONVERSATION_CONTEXT_PARAM))
 			{
-				String urlConversationContextId = context.getExternalContext().getRequestParameterMap().get(CONVERSATION_CONTEXT_PARAM).toString();
+				String urlConversationContextId = requestParameterMap.get(CONVERSATION_CONTEXT_PARAM).toString();
 				conversationContextId = new Long(Long.parseLong(urlConversationContextId, Character.MAX_RADIX));
 			}
 			else
@@ -122,7 +142,7 @@
 				}
 			}
 			
-			context.getExternalContext().getRequestMap().put(CONVERSATION_CONTEXT_REQ, conversationContextId);
+			requestMap.put(CONVERSATION_CONTEXT_REQ, conversationContextId);
 		}
 		
 		return conversationContextId;
@@ -234,21 +254,6 @@
 	{
 		return registeredEndConversations;
 	}
-
-	/**
-	 * Inject all beans of the current conversation
-	 * @see ConversationContext#injectConversationBeans(FacesContext) 
-	protected void injectConversationBeans(FacesContext context)
-	{
-		ConversationContext conversationContext = getConversationContext();
-		if (conversationContext == null)
-		{
-			return;
-		}
-		
-		conversationContext.injectConversationBeans(context);
-	}
-	 */
 
 	/**
 	 * check if we have a conversation context

Modified: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationServletFilter.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationServletFilter.java?rev=399408&r1=399407&r2=399408&view=diff
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationServletFilter.java
(original)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/ConversationServletFilter.java
Wed May  3 13:29:59 2006
@@ -9,11 +9,14 @@
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 
 public class ConversationServletFilter implements Filter
 {
 	public final static String CONVERSATION_FILTER_CALLED = "org.apache.myfaces.conversation.ConversationServletFilter.CALLED";
-	
+
+	private final static ThreadLocal externalContext = new ThreadLocal();  
+
 	public void init(FilterConfig arg0) throws ServletException
 	{
 	}
@@ -26,10 +29,17 @@
 		{
 			if (request instanceof HttpServletRequest)
 			{
-				conversationManager = ConversationManager.getInstance(((HttpServletRequest) request).getSession(false));
-				if (conversationManager != null)
+				HttpServletRequest httpRequest = (HttpServletRequest) request;
+				HttpSession httpSession = httpRequest.getSession(false);
+				if (request != null)
 				{
-					conversationManager.attachPersistence();
+					conversationManager = ConversationManager.getInstance(httpSession);
+					if (conversationManager != null)
+					{
+						externalContext.set(ConversationExternalContext.create(httpRequest));
+						
+						conversationManager.attachPersistence();
+					}
 				}
 			}
 			
@@ -42,14 +52,26 @@
 		}
 		finally
 		{
-			if (conversationManager != null)
+			try
+			{
+				if (conversationManager != null)
+				{
+					conversationManager.detachPersistence();
+				}
+			}
+			finally
 			{
-				conversationManager.detachPersistence();
+				externalContext.set(null);
 			}
 		}
 	}
 
 	public void destroy()
 	{
+	}
+	
+	protected static ConversationExternalContext getConversationExternalContext()
+	{
+		return (ConversationExternalContext) externalContext.get();
 	}
 }

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java?rev=399408&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
(added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
Wed May  3 13:29:59 2006
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.apache.myfaces.custom.conversation;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * a fake map which implements all methods but throw an UnsupportedOperationException
+ * 
+ * @author imario@apache.org
+ */
+public class FakeMap implements Map
+{
+	public void clear()
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public boolean containsKey(Object key)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public boolean containsValue(Object value)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Set entrySet()
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Object get(Object key)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public boolean isEmpty()
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Set keySet()
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Object put(Object arg0, Object arg1)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public void putAll(Map arg0)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Object remove(Object key)
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public int size()
+	{
+		throw new UnsupportedOperationException();
+	}
+
+	public Collection values()
+	{
+		throw new UnsupportedOperationException();
+	}
+}

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/conversation/FakeMap.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message