You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/08/18 16:14:27 UTC

svn commit: r567275 - in /myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra: conversation/jsf/filter/OrchestraServletFilter.java frameworkAdapter/FrameworkAdapter.java

Author: skitching
Date: Sat Aug 18 07:14:25 2007
New Revision: 567275

URL: http://svn.apache.org/viewvc?view=rev&rev=567275
Log:
Use thread-local for framework adapter location, set via a filter. Statics are not friendly for libraries.

Modified:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/jsf/filter/OrchestraServletFilter.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/FrameworkAdapter.java

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/jsf/filter/OrchestraServletFilter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/jsf/filter/OrchestraServletFilter.java?view=diff&rev=567275&r1=567274&r2=567275
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/jsf/filter/OrchestraServletFilter.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/conversation/jsf/filter/OrchestraServletFilter.java Sat Aug 18 07:14:25 2007
@@ -19,23 +19,34 @@
 
 package org.apache.myfaces.orchestra.conversation.jsf.filter;
 
-import org.apache.myfaces.orchestra.connectionManager.ConnectionManagerDataSource;
-import org.apache.myfaces.orchestra.conversation.ConversationContext;
-import org.apache.myfaces.orchestra.conversation.ConversationManager;
+import java.io.IOException;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
+import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
+
+import org.apache.myfaces.orchestra.connectionManager.ConnectionManagerDataSource;
+import org.apache.myfaces.orchestra.conversation.ConversationContext;
+import org.apache.myfaces.orchestra.conversation.ConversationManager;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapterInterface;
 
 /**
  * Perform a number of useful per-request tasks.
  * <p>
+ * <h2>Configures the orchestra FrameworkAdapter</h2>
+ * Orchestra accesses information about the request, response, session, etc via a
+ * FrameworkAdapter so that it can be used with multiple web tier frameworks. This
+ * class selects the correct adapter to use; the default is the JsfFrameworkAdapter
+ * but setting the appropriate servlet context parameter in web.xml allows other
+ * adapter classes to be selected.
+ * <p>
  * <h2>Make request/response objects accessible</h2>
  * The request and response objects for the current request are stored in thread-local
  * variables for the duration of this request. This makes it possible for any code to
@@ -63,8 +74,9 @@
  * implements that, by holding a lock on a mutex object held by the appropriate
  * conversationContext.
  * <p>
- * This filter can be enabled or disabled via a filter init parameter; setting
- * "serializeRequests" to "false" disables this filter. Default value: true (enabled).
+ * The serialize-requests functionality can be enabled or disabled via a filter init
+ * parameter; setting "serializeRequests" to "false" disables this filter. Default
+ * value: true (enabled).
  * <p>
  * <h2>JDBC Connection Management</h2>
  * Orchestra provides a special DataSource wrapper that can be configured for any
@@ -80,12 +92,29 @@
 	 * This filter init property can be set to "true" or "false". Default: "true".
 	 */
 	public final static String SERIALIZE_REQUESTS = "serializeRequests"; // NON-NLS
+
+	/**
+	 * A <i>servlet context</i> property that defines the name of a java class which maps from
+	 * the orchestra FrameworkAdapterInterface to the actual web tier environment 
+	 * that orchestra is running in. Defaults to the standard orchestra JSF adapter.
+	 * This only needs to be set if Orchestra is being used in a non-JSF environment.
+	 */
+	public final static String FRAMEWORK_ADAPTER_KEY = "org.apache.myfaces.orchestra.FRAMEWORK_ADAPTER_CLASS"; // NON-NLS
+
+	/** The default value for FRAMEWORK_ADAPTER_KEY. */
+	public final static String FRAMEWORK_ADAPTER_DFLT = "org.apache.myfaces.orchestra.frameworkAdapter.JsfFrameworkAdapter"; // NON-NLS
+
+	/**
+	 * Key of an attribute within the ConversationContext that is used as a lock to serialize
+	 * multiple http requests for the same conversation context.
+	 */
 	private final static String CONTEXT_MUTEXT_OBJECT = OrchestraServletFilter.class.getName() + ".SER_MUTEX";
 
 	private static ThreadLocal httpServletRequest = new ThreadLocal();
 	private static ThreadLocal httpServletResponse = new ThreadLocal();
 
 	private boolean serializeRequests = true;
+	private FrameworkAdapterInterface frameworkAdapterObj;
 
 	public void init(FilterConfig filterConfig) throws ServletException
 	{
@@ -94,6 +123,22 @@
 		{
 			serializeRequests = false;
 		}
+		
+		ServletContext servletContext = filterConfig.getServletContext();
+		String adapterClassName = servletContext.getInitParameter(FRAMEWORK_ADAPTER_KEY);
+		if (adapterClassName == null) {
+			adapterClassName = FRAMEWORK_ADAPTER_DFLT;
+		}
+		try {
+			Class adapterClass = this.getClass().getClassLoader().loadClass(adapterClassName);
+			frameworkAdapterObj = (FrameworkAdapterInterface) adapterClass.newInstance();
+		} catch(ClassNotFoundException e) {
+			throw new ServletException("Unable to load Orchestra FrameworkAdapter class " + adapterClassName, e);
+		} catch(InstantiationException e) {
+			throw new ServletException("Unable to load Orchestra FrameworkAdapter class " + adapterClassName, e);
+		} catch(IllegalAccessException e) {
+			throw new ServletException("Unable to load Orchestra FrameworkAdapter class " + adapterClassName, e);
+		}
 	}
 
 	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
@@ -109,6 +154,7 @@
 				httpServletResponse.set(servletResponse);
 			}
 
+			FrameworkAdapter.setInstance(frameworkAdapterObj);
 			Object mutex = null;
 			if (serializeRequests)
 			{
@@ -150,6 +196,7 @@
 			}
 			finally
 			{
+				FrameworkAdapter.setInstance(null);
 				httpServletRequest.set(null);
 				httpServletResponse.set(null);
 			}

Modified: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/FrameworkAdapter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/FrameworkAdapter.java?view=diff&rev=567275&r1=567274&r2=567275
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/FrameworkAdapter.java (original)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/FrameworkAdapter.java Sat Aug 18 07:14:25 2007
@@ -26,6 +26,8 @@
  * Provides a static method for returning the singleton FrameworkAdapterInterface, which allows Orchestra to
  * support MVC frameworks other than JSF.
  * <p>
+ * A request filter must be configured which calls setInstance at the start of each request, and
+ * setInstance(null) at the end of each request.
  * This class can be configured to return any implementation of FrameworkAdapterInterface that is desired,
  * simply by calling setFrameworkAdapter before any Orchestra code calls getInstance on this class. If no
  * configuration is done then getInstance defaults to returning a FrameworkAdapterInterface which supports
@@ -33,25 +35,23 @@
  */
 public final class FrameworkAdapter
 {
-	private static FrameworkAdapterInterface INSTANCE;
+	private final static ThreadLocal instance = new ThreadLocal();
 
+	/** No instances of this class should be created. */
 	private FrameworkAdapter()
 	{
 	}
 
-	static void setFrameworkAdapter(FrameworkAdapterInterface frameworkAdapterInterface)
+	/**
+	 * Expected to be called only by a servlet filter at the start and end of each request.
+	 */
+	public static void setInstance(FrameworkAdapterInterface frameworkAdapterInterface)
 	{
-		INSTANCE = frameworkAdapterInterface;
+		instance.set(frameworkAdapterInterface);
 	}
 
 	public static FrameworkAdapterInterface getInstance()
 	{
-		if (INSTANCE == null)
-		{
-			INSTANCE = new JsfFrameworkAdapter();
-		}
-
-		return INSTANCE;
+		return (FrameworkAdapterInterface) instance.get();
 	}
-
 }