Author: kylem
Date: Mon Apr 25 09:00:24 2005
New Revision: 164594

URL: http://svn.apache.org/viewcvs?rev=164594&view=rev
Log:
Added initialization parameter to servlet filter for Controls that makes the 
BeanContext class to use for containing web tier controls configurable.   
Resolves BEEHIVE-395.

Patch submitted by Brett Bennett.

Modified:
    
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/servlet/ControlFilter.java

Modified: 
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/servlet/ControlFilter.java
URL: 
http://svn.apache.org/viewcvs/incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/servlet/ControlFilter.java?rev=164594&r1=164593&r2=164594&view=diff
==============================================================================
--- 
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/servlet/ControlFilter.java
 (original)
+++ 
incubator/beehive/trunk/controls/src/runtime/org/apache/beehive/controls/runtime/servlet/ControlFilter.java
 Mon Apr 25 09:00:24 2005
@@ -34,22 +34,51 @@
  * been set up prior to forwarding the request to the actual target servlet, 
and does 
  * post-processing to ensure that resources have been properly released.
  *
- * This filter supports the following initialization parameters:
- * 
- * useSession - a boolean value that indicates whether the bean context should 
be stored in
- *              session.  This makes the context (and any contained controls) 
accessible
- *              across multiple http requests.
- *
  * This filter needs to be configured in web.xml for URL mappings that will be 
hosting servlets.
  */
 public class ControlFilter implements Filter
 {
     public static String BEAN_CONTEXT_ATTRIBUTE = 
ServletBeanContext.class.getName();
 
+    /*
+     * THIS FILTER RECOGNIZES THE FOLLOWING INITIALIZATION PARAMETERS:
+     */
+
+    /**
+     * The useSession init parameter is a boolean value that indicates whether 
the bean context 
+     * should be stored in session.  This makes the context (and any contained 
controls) 
+     * accessible across multiple http requests.
+     */
+    public static String INIT_PARAM_USE_SESSION = "useSession";
+
+    /**
+     * The contextClass init parameter is a class name that defines the 
BeanContext class to use 
+     * for containing Controls in the servlet container.  This class <b>must 
be</b> a subclass of 
+     * the org.apache.beehive.runtime.servlet.ServletBeanContext class.
+     */
+    public static String INIT_PARAM_CONTEXT_CLASS = "contextClass";
+
     public void init(FilterConfig filterConfig) throws ServletException 
     {
         _filterConfig = filterConfig;
-        _useSessionContext = new 
Boolean(filterConfig.getInitParameter("useSession")).booleanValue();
+        _useSessionContext = new 
Boolean(filterConfig.getInitParameter(INIT_PARAM_USE_SESSION)).booleanValue();
+        String contextClassName = 
filterConfig.getInitParameter(INIT_PARAM_CONTEXT_CLASS);
+        if(contextClassName != null)
+        {
+            try
+            {
+                _contextClass = Class.forName(contextClassName);
+            }
+            catch(Exception e)
+            {
+                throw new ServletException("Cannot load container context 
class '"+contextClassName+"'");
+            }
+            if(!ServletBeanContext.class.isAssignableFrom(_contextClass))
+            {
+                throw new ServletException("'"+contextClassName+"' is not a 
ServletBeanContext sub-class");
+            }
+            
+        }
     }
 
     public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain)
@@ -77,7 +106,15 @@
             //
             if (beanContext == null)
             {
-                beanContext = new ServletBeanContext();
+                try
+                {
+                    beanContext = 
(ServletBeanContext)_contextClass.newInstance();
+                }
+                catch(Exception e)
+                {
+                    throw new ServletException("Cannot construct BeanContext 
class instance: " +
+                                               _contextClass.getName());
+                }
                 if (httpSession != null)
                     httpSession.setAttribute(BEAN_CONTEXT_ATTRIBUTE, 
beanContext);
             }
@@ -122,4 +159,9 @@
      * The resolved value of the 'useSession' filter attribute.
      */
     private boolean      _useSessionContext = false;
+
+    /**
+     * The BeanContext class to use as the container for controls running in 
the ServletContainer
+     */
+    private Class        _contextClass = ServletBeanContext.class;
 }


Reply via email to