You are viewing a plain text version of this content. The canonical link for it is here.
Posted to adffaces-commits@incubator.apache.org by jw...@apache.org on 2007/01/02 23:55:09 UTC

svn commit: r491976 - in /incubator/adffaces/branches/jwaldman-portal/trinidad: trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/ trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/ trinidad-impl/src/main/java/org/apach...

Author: jwaldman
Date: Tue Jan  2 15:55:08 2007
New Revision: 491976

URL: http://svn.apache.org/viewvc?view=rev&rev=491976
Log:
ADFFACES-234 Filter logic for Portlets (commit to jwaldman-portal branch for Scott OBryan)

Modified:
    incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/Configurator.java
    incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java
    incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/FacesContextFactoryImpl.java
    incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java

Modified: incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/Configurator.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/Configurator.java?view=diff&rev=491976&r1=491975&r2=491976
==============================================================================
--- incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/Configurator.java (original)
+++ incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-api/src/main/java/org/apache/myfaces/trinidad/config/Configurator.java Tue Jan  2 15:55:08 2007
@@ -15,19 +15,9 @@
  */
 package org.apache.myfaces.trinidad.config;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 import javax.faces.context.ExternalContext;
 import javax.servlet.ServletRequest;
 
-import org.apache.myfaces.trinidad.logging.TrinidadLogger;
-import org.apache.myfaces.trinidad.util.ClassLoaderUtils;
-
 /**
  * This defines an abstract class for the Configurator. Classes implementing
  * this abstraction should be listed in the jar's /META-INF/services folder
@@ -51,78 +41,22 @@
  * run in a portal and should therefore be considered optional to the run of
  * the renderkit or application if Portal compatibility is important.
  *  
- *
  * @author Scott O'Bryan (latest modification by $Author$)
+ * @author Adam Weiner
+ * 
  * @version $Revision$ $Date$
  */
-abstract public class Configurator
+public abstract class Configurator
 {
   /**
-   * Returns the proper instance of the Configurator.
-   * An ExternalContext is generally passed to ensure that
-   * a Configurator can properly be created, if necessary.
-   * However, a null ExternalContext may be passed if
-   * there is no requirement that a Configurator be created
-   * if not already loaded - calls to destroy() are the
-   * most obvious scenario.
-   */
-  static public Configurator getInstance(ExternalContext ec)
-  {
-    ClassLoader loader = Thread.currentThread().getContextClassLoader();
-    
-    if (loader == null)
-      _LOG.severe("Could not find context class loader.");
-    else
-    {
-      // FIXME: would probably be better to just use ConcurrentHashMap
-      synchronized(_CONFIGURATORS)
-      {
-        Configurator config = _CONFIGURATORS.get(loader);
-        if ((config == null) && (ec != null))
-        {
-          try
-          {
-            List<Configurator> configs = ClassLoaderUtils.getServices(
-               _GLOBAL_CONFIGURATOR);
-            if (configs.isEmpty())
-            {
-              _LOG.severe("Could not load a global configurator");
-              // Create a null configurator (which is an empty subclass),
-              // just so we don't have to re-warn the user every time
-              config = new Configurator() {};
-            }
-            else
-            {
-              config = configs.get(0);
-            }
-
-            config.init(ec);
-            _CONFIGURATORS.put(loader, config);
-            _LOG.fine("GlobalConfigurator has been created.");
-          }
-          catch (RuntimeException e)
-          {
-            // OC4J was not reporting these errors properly:
-            _LOG.severe(e);
-            throw e;
-          }
-        }
-        return config;
-      }
-    } 
-
-    return null;
-  }
-
-  /**
    * Initializes the Configurator.  This method is guaranteed to run before
-   * any other method within this Configurator and will be called by the
-   * GlobalConfigurator only once per webapp context.  This init is
-   * guaranteed to be executed before completion of the first call to the
-   * {@link javax.faces.FacesContextFactory#getFacesContext#getFacesContext(Object, Object, Object,javax.faces.lifecycle.Lifecycle)}
+   * any other method within this Configurator and will be called only once 
+   * per webapp context.  This init is guaranteed to be executed before 
+   * completion of the first call to the
+   * {@link javax.faces.FacesContextFactory#getFacesContext(Object, Object, Object,javax.faces.lifecycle.Lifecycle)}
    * is completed.
    *
-   * Note:the ExternalContext provided to this method may not
+   * <strong>Note:</strong>the ExternalContext provided to this method may not
    * contain any of the Request/Response functionality within the external
    * context and will NOT contain any ExternalContext wrapping provided by
    * the {@link #getExternalContext(ExternalContext)} method.  This object
@@ -136,9 +70,8 @@
 
   /**
    * Cleans up the Configurator.  This method is guaranteed to run at some
-   * point after the Servlet or Portlet context falls out of scope although
-   * the exact time of it's execution is not guaranteed and will typically
-   * be determined by the garbage collector.
+   * point after the Servlet or Portlet context falls out of scope and will 
+   * typically be determined by the context listener.
    */
   public void destroy() {}
 
@@ -148,19 +81,20 @@
    * {@link #endRequestRequest(ExternalContext)}.  When using the
    * TrinidadFilter, this will be called during filter execution, but
    * is not guaranteed to happen until just before the creation of the
-   * Trinidad FacesContext wrapper.  In a portlet environment this will
-   * be called at the beginning of each request where the current portlet
-   * is the acting portlet.
+   * Trinidad FacesContext wrapper.
    *
    * All Configurator services will have thier beginRequest() methods
    * called before any calls to getExternalContext().  So any context
    * wrapping done by this method will not happen until after the
-   * beginRequest() is called.
+   * beginRequest() is called.</p>
    *
    * It is also important to note that the ExternalContext provided
    * may or may not be the same as the ExternalContext provided to
    * getExternalContext().  But it will have a valid request and response
    * object.
+   * 
+   * By contract, the {@link org.apache.myfaces.trinidad.context.RequestContext}
+   * object will be initialized and available when this method is run.
    *
    * @param externalContext a mocked up or life externalContext providing
    *                        access to the native request, response, and context
@@ -179,8 +113,11 @@
    *
    * Please note that it is important that this method returns a wrapper of
    * the original context, or the behavior provided by other services and by
-   * Trinidad may well not function
+   * Trinidad may not function
    *
+   * By contract, the {@link org.apache.myfaces.trinidad.context.RequestContext}
+   * object will be initialized and available when this method is run.
+   * 
    * @param externalContext the ExternalContext to wrap
    * @return a wrapper of the ExternalContext
    */
@@ -197,6 +134,9 @@
    * may or may not be the same as the ExternalContext provided to
    * getExternalContext().  But it will have a valid request and response
    * object.
+   * 
+   * By contract, the {@link org.apache.myfaces.trinidad.context.RequestContext}
+   * object will be initialized and available when this method is run.
    *
    * @param externalContext the external context
    */
@@ -204,41 +144,43 @@
 
   /**
    * Disables Configurator services for the current request.  When this method
-   * has been called on a request, then calls to beginRequest, endRequest, and
-   * getExternalContext will not execute any of the associated services or
-   * initialize the RequestContext.
+   * has been called on a request, then the {{@link #beginRequest(ExternalContext)},
+   * {@link #endRequest(ExternalContext)}, and 
+   * {@link #getExternalContext(ExternalContext)} methods will not be called durring 
+   * a request.
    * 
-   * <p><string>Note:</strong> that this command should be executed before
-   * {@link Configurator#beginRequest(ExternalContext)}.</p>
+   * <strong>Note:</strong> is this method is called after the beginRequest() method,
+   * an error will be logged in the trinidad logger and the services will continue to
+   * execute.
    * 
-   * @param ec the ExternalContext
-   */
-  public static final void disableConfiguratorServices(ExternalContext ec)
-  {
-    ec.getRequestMap().put(_DISABLE_SERVICES, Boolean.TRUE);
-  }
-
-  /**
-   * Utility to disable configurator services without access to
-   * an ExternalContext.
+   * @param srq the servlet request
    */
   public static final void disableConfiguratorServices(ServletRequest srq)
   {
     srq.setAttribute(_DISABLE_SERVICES, Boolean.TRUE);
   }
 
-  public static final boolean isConfiguratorServiceDisabled(ExternalContext ec)
+  /**
+   * Returns <code>true</code> if the {@link #disableConfiguratorServices(ServletRequest)}
+   * has been executed on the current request and <code>false</code> if it has not.
+   * 
+   * <strong>Note:</strong>it is important to understand that this method will not
+   * properly reflect if the services have actually been disabled or not.  It simply
+   * returns whether they "should" have been disabled.  If the disableConfiguratorServices
+   * was executed after the beginRequest methods have been executed, then the services
+   * will continue to function so that {{@link #getExternalContext(ExternalContext)}
+   * and {@link #endRequest(ExternalContext)} will still be called.
+   * 
+   * @param ec the ExternalContext object
+   * 
+   * @return a <code>boolean</code> containing <code>true</code> if the 
+   *         <code>disableConfiguratorServices()</code> method has been 
+   *         called and <code>false</code> if it has not.
+   */
+  protected static final boolean isConfiguratorServiceDisabled(ExternalContext ec)
   {
     return Boolean.TRUE.equals(ec.getRequestMap().get(_DISABLE_SERVICES));
   }
   
-  private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(Configurator.class);
-  private static final Class[] _SERVLET_REQUEST_ARRAY = {ServletRequest.class};
-
-  static private final String _GLOBAL_CONFIGURATOR =
-    "org.apache.myfaces.trinidad.config.GlobalConfigurator";
-  static private final String _DISABLE_SERVICES =
-    "org.apache.myfaces.trinidad.config.DISABLE_SERVICES";
-  static private final Map<ClassLoader, Configurator> _CONFIGURATORS
-   = new HashMap<ClassLoader, Configurator>();
+  static private final String _DISABLE_SERVICES =  Configurator.class.getName()+".DISABLE_SERVICES";
 }

Modified: incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java?view=diff&rev=491976&r1=491975&r2=491976
==============================================================================
--- incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java (original)
+++ incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/GlobalConfiguratorImpl.java Tue Jan  2 15:55:08 2007
@@ -20,7 +20,6 @@
 import java.util.Map;
 
 import javax.faces.context.ExternalContext;
-import javax.servlet.ServletRequest;
 
 import org.apache.myfaces.trinidad.config.Configurator;
 import org.apache.myfaces.trinidad.context.RequestContext;
@@ -32,137 +31,270 @@
 import org.apache.myfaces.trinidadinternal.context.TrinidadPhaseListener;
 import org.apache.myfaces.trinidadinternal.skin.SkinFactoryImpl;
 import org.apache.myfaces.trinidadinternal.skin.SkinUtils;
+import org.apache.myfaces.trinidadinternal.util.ExternalContextUtils;
 
 /**
- * This is the implementation of the Trinidad GlobalConfigurator.
+ * This is the implementation of the Trinidad's Global configurator. It provides the entry point for
+ * all other configurators. This class is responsible for enforcing the contract outlined by the
+ * Configurator abstract class, but allows a more "relaxed" implementation of the APIs called for by
+ * the Configurator class, making it more convenient to use ConfiguratorServices from within the
+ * Trinidad renderkit. Where appropriate, these differences will be documented for the benifit of
+ * the Trindad developer.
  * 
- * @see org.apache.myfaces.trinidad.config.GlobalConfigurator
+ * @see org.apache.myfaces.trinidad.config.Configurator
  * @author Scott O'Bryan (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
-//FIXME: destroy() and endRequest() should go in inverse order
-// from init() and startRequest()
 public class GlobalConfiguratorImpl extends Configurator
 {
   /**
-   * Returns <code>true</code> is {@link #beginRequest(ExternalContext)} has been called and
-   * {@link #endRequest(ExternalContext)} has not. It will return <code>false</code> under all
-   * other circumstances.
+   * Returns a GlobalConfigurator instance for the current context's class loader. The
+   * GlobalConfigurator is responsible for enforcing the contract on the other methods of this
+   * class. This means that if {@link #init(ExternalContext)} is called multiple times, the global
+   * configurator will call all subordinate configurators only once.
    * 
-   * @param ec
-   *          the current externalContext
-   * @return a boolean saying whether a particular request has begun but has not ended.
+   * Likewise, the GlobalConfigurator will return exceptions when the contract is expressly violated
+   * (like if {@link #getExternalContext(ExternalContext)} is called before a {{@link #beginRequest(ExternalContext)}.
+   * 
+   * @return a GlobalConfigurator or <code>null</code> is one was unable to be obtained.
    */
-  public static boolean isRequestBegun(final ExternalContext ec)
+  static public final GlobalConfiguratorImpl getInstance()
   {
-    return Boolean.TRUE.equals(ec.getRequestMap().get(_IN_REQUEST));
+    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+
+    if (loader == null)
+    {
+      _LOG.severe("Could not find context class loader.");
+    }
+    else
+    {
+      synchronized (_CONFIGURATORS)
+      {
+        GlobalConfiguratorImpl config = _CONFIGURATORS.get(loader);
+        if (config == null)
+        {
+          try
+          {
+            config = new GlobalConfiguratorImpl();
+            _CONFIGURATORS.put(loader, config);
+          }
+          catch (final RuntimeException e)
+          {
+            // OC4J was not reporting these errors properly:
+            _LOG.severe(e);
+            throw e;
+          }
+          _LOG.fine("GlobalConfigurator has been created.");
+        }
+        return config;
+      }
+    }
+    return null;
   }
   
+  /**
+   * Returns true if the request has not been stated for the current "virtual"
+   * request.  In the servlet environment this will be true after 
+   * {@link #beginRequest(ExternalContext)} is executed and before 
+   * {@link #endRequest(ExternalContext)} is executed.  This will generally
+   * happen once per request.  In the Portlet Environment, the request must be
+   * be started and ended at the beginning and end of both the actionRequest
+   * and the RenderRequest.
+   *  
+   * @param ec
+   * @return
+   */
+  static public boolean isRequestStarted(final ExternalContext ec)
+  {
+    return (RequestType.getType(ec) != null);
+  }
 
   /**
-   * Default constructor
+   * Returns "true" if the services should be considered enabled or disabled.
+   * 
+   * @param ec
+   * @return
    */
-  public GlobalConfiguratorImpl()
+  static private final boolean _isDisabled(final ExternalContext ec)
   {
+    final Boolean inRequest = (Boolean) ec.getRequestMap().get(_IN_REQUEST);
+
+    if (inRequest == null)
+    {
+      return isConfiguratorServiceDisabled(ec);
+    }
+    else
+    {
+      final boolean disabled = inRequest.booleanValue();
+      if (disabled != isConfiguratorServiceDisabled(ec))
+      {
+        _LOG
+            .warning("Configurator services were disabled after beginRequest was executed.  Cannot disable configurator services");
+      }
+
+      return disabled;
+    }
   }
 
-  /*
-   * (non-Javadoc)
+  /**
+   * Private default constructor. Right now this class is not serializable. If serialization is
+   * required, we may wish to make this public. We really don't want people using this though.
+   */
+  private GlobalConfiguratorImpl()
+  {}
+
+  /**
+   * Executes the beginRequest methods of all of the configurator services. This method will also
+   * initizlize the configurator if it has not already been initialized, so there may be no need to
+   * call the {@link #init(ExternalContext)} method directly.
+   * 
+   * This method also ensures that the requestContext is attached before the beginRequest methods
+   * are called, so there is no reason to initialize the request context before calling this method.
+   * In portal environments, it is important to execute this method once for each Portlet action and
+   * render request so that the requestContext may be properly initialized even though the
+   * underlying services will be called only once per physical request.
+   * 
+   * @param externalContext
+   *          the externalContext to use to begin the request.
    * 
    * @see org.apache.myfaces.trinidad.config.Configurator#beginRequest(javax.faces.context.ExternalContext)
    */
-  @SuppressWarnings("unchecked")
-  public void beginRequest(ExternalContext externalContext)
+  @SuppressWarnings("unchecked") // TODO: remove this for Faces 1.2
+  @Override
+  public void beginRequest(final ExternalContext externalContext)
   {
-    _checkInitialized();
-    
-    if(!isConfiguratorServiceDisabled(externalContext))
+    // asserts for debug which disappear in production
+    assert externalContext != null;
+
+    // Do per-virtual request stuff
+    if (RequestType.getType(externalContext) == null)
     {
-      _attachRequestContext(externalContext);
-  
+      // RequestType may change in a portal environment. Make sure it's set up to enforce the
+      // contracts
+      RequestType.setType(externalContext);
+
       // By contract, Configurators beginRequest is only called once per physical request.
       // The globalConfigurator may be called multiple times, however, so we need to enforce
       // the contract.
-      if (!isRequestBegun(externalContext))
+      if (!_isDisabled(externalContext))
       {
-        externalContext.getRequestMap().put(_IN_REQUEST, Boolean.TRUE);
-  
-        // If request map is present, we don't have a new request
-        // Launch Configurator Services
-        for (final Configurator config : _services)
+        if (!_initialized)
         {
-          config.beginRequest(externalContext);
+          init(externalContext);
+        }
+
+        // If this hasn't been initialized then please initialize
+        _attachRequestContext(externalContext);
+
+        if (externalContext.getRequestMap().get(_IN_REQUEST) == null)
+        {
+          _startConfiguratorServiceRequest(externalContext);
         }
       }
+      else
+      {
+        _LOG.fine("GlobalConfigurator: Configurators have been disabled for this request.");
+      }
+    }
+    else if (!RequestType.isCorrectType(externalContext))
+    {
+      // This will happen if the actionRequest was not ended before dispatching to the render
+      // request
+      throw new IllegalStateException("The previous action request was not ended.");
     }
     else
     {
-      _LOG.fine("GlobalConfigurator: Configurators have been disabled for this request.");
+      _LOG.fine("BeginRequest called multiple times for this request");
     }
   }
 
-  /*
-   * (non-Javadoc)
+  /**
+   * Cleans up the current configurator. This will execute the destroy method on all of the
+   * configurator services. Generally this will be called by Trinidad's context listener when the
+   * context is destroyed, but it may be used manually to allow the configurator to be
+   * re-initialized.
+   * 
+   * Calling this method while the configurator is not initialized will not re-execute the destroy
+   * methods on the services.
    * 
    * @see org.apache.myfaces.trinidad.config.Configurator#destroy()
    */
+  @Override
   public void destroy()
   {
-    _checkInitialized();
-    for (final Configurator config : _services)
+    if (_initialized)
     {
-      try
-      {
-        config.destroy();
-      }
-      catch (final Throwable t)
+      for (final Configurator config : _services)
       {
-        // we always want to continue to destroy things, so log errors and continue
-        _LOG.severe(t);
+        try
+        {
+          config.destroy();
+        }
+        catch (final Throwable t)
+        {
+          // we always want to continue to destroy things, so log errors and continue
+          _LOG.severe(t);
+        }
       }
+      _services = null;
+      _initialized = false;
     }
   }
 
-  /*
-   * (non-Javadoc)
+  /**
+   * Ends the currently begun request. It is important to note that this should be executed only
+   * once per physical request.
    * 
    * @see org.apache.myfaces.trinidad.config.Configurator#endRequest(javax.faces.context.ExternalContext)
    */
+  @Override
   public void endRequest(final ExternalContext externalContext)
   {
-    _checkInitialized();
-    
-    if (!isConfiguratorServiceDisabled(externalContext))
+    // do per virtual-request stuff
+    if (RequestType.getType(externalContext) != null)
     {
-      if (isRequestBegun(externalContext))
+      if (!_isDisabled(externalContext))
       {
-        for (final Configurator config : _services)
+        final RequestType type = RequestType.getType(externalContext);
+
+        // Do not end services at the end of a portal action request
+        if (type != RequestType.PORTAL_ACTION)
         {
-          config.endRequest(externalContext);
+          _endConfiguratorServiceRequest(externalContext);
+        }
+
+        final RequestContext context = RequestContext.getCurrentInstance();
+        if (context != null)
+        {
+          context.release();
+          assert RequestContext.getCurrentInstance() == null;
         }
-        
-        externalContext.getRequestMap().remove(_IN_REQUEST);
-      }
-  
-      final RequestContext context = RequestContext.getCurrentInstance();
-  
-      if (context != null)
-      {
-        context.release();
-        assert RequestContext.getCurrentInstance() == null;
       }
+      RequestType.clearType(externalContext);
     }
-    else
-    {
-      externalContext.getRequestMap().remove(_IN_REQUEST);
-    }    
   }
 
+  /**
+   * Returns an externalContext for this configurator and all of the configurator services. If this
+   * method is executed before {@link #beginRequest(ExternalContext)} then this method will call
+   * beginRequest(). It is important to note, however, that even though beginRequest does not need
+   * to be explicitly called, {{@link #endRequest(ExternalContext)} does need to be called when the
+   * request has completed or the contract to the configurators will be broken.
+   * 
+   * @param externalContext
+   *          the ExternalContext object that should be wrapped.
+   * 
+   * @return a decorated ExternalContext object
+   */
+  @Override
   public ExternalContext getExternalContext(ExternalContext externalContext)
   {
-    _checkInitialized();
-    
-    if(!isConfiguratorServiceDisabled(externalContext))
+    if (RequestType.getType(externalContext) == null)
+    {
+      beginRequest(externalContext);
+    }
+
+    if (!_isDisabled(externalContext))
     {
       // Wrap ExternalContexts
       for (final Configurator config : _services)
@@ -170,44 +302,63 @@
         externalContext = config.getExternalContext(externalContext);
       }
     }
-    
+
     return externalContext;
   }
 
-  /*
+  /**
+   * Initializes the global configurator and the configurator services. This method need not be
+   * called directly as it will be called from {@link #beginRequest(ExternalContext)} if needed. It
+   * is also possible to execute this method more then once, although if initialization has already
+   * happened then a call to this method will not do anything. To re-initialize this class, call
+   * {@link #destroy()} first and then call this method.
+   * 
+   * @param externalContext
+   *          the externalContext needed to initialize this class
+   * 
    * @see org.apache.myfaces.trinidad.config.Configurator#init(javax.faces.context.ExternalContext)
    */
-  public void init(ExternalContext externalContext)
+  @Override
+  public void init(final ExternalContext externalContext)
   {
-    _services = ClassLoaderUtils.getServices(Configurator.class.getName());
+    assert externalContext != null;
 
-    // Create a new RequestContextFactory is needed
-    if (RequestContextFactory.getFactory() == null)
+    if (!_initialized)
     {
-      RequestContextFactory.setFactory(new RequestContextFactoryImpl());
-    }
+      _services = ClassLoaderUtils.getServices(Configurator.class.getName());
 
-    // Create a new SkinFactory if needed.
-    if (SkinFactory.getFactory() == null)
-    {
-      SkinFactory.setFactory(new SkinFactoryImpl());
-    }
+      // Create a new RequestContextFactory is needed
+      if (RequestContextFactory.getFactory() == null)
+      {
+        RequestContextFactory.setFactory(new RequestContextFactoryImpl());
+      }
 
-    // register the base skins
-    SkinUtils.registerBaseSkins();
-    
-    for (final Configurator config : _services)
+      // Create a new SkinFactory if needed.
+      if (SkinFactory.getFactory() == null)
+      {
+        SkinFactory.setFactory(new SkinFactoryImpl());
+      }
+
+      // register the base skins
+      SkinUtils.registerBaseSkins();
+
+      for (final Configurator config : _services)
+      {
+        config.init(externalContext);
+      }
+
+      // after the 'services' filters are initialized, then register
+      // the skin extensions found in trinidad-skins.xml. This
+      // gives a chance to the 'services' filters to create more base
+      // skins that the skins in trinidad-skins.xml can extend.
+      SkinUtils.registerSkinExtensions(externalContext);
+
+      _initialized = true;
+    }
+    else
     {
-      config.init(externalContext);
+      _LOG.warning("Configurator services already initialized.");
     }
-    
-    // after the 'services' filters are initialized, then register
-    // the skin extensions found in trinidad-skins.xml. This
-    // gives a chance to the 'services' filters to create more base
-    // skins that the skins in trinidad-skins.xml can extend.
-    SkinUtils.registerSkinExtensions(externalContext);
-
-    _inited = true;
   }
 
   /**
@@ -254,23 +405,93 @@
     assert RequestContext.getCurrentInstance() == context;
   }
 
-  private void _checkInitialized()
+  private void _endConfiguratorServiceRequest(final ExternalContext ec)
   {
-    if (!_inited)
+    // Physical request has now ended
+    // Clear the in-request flag
+    ec.getRequestMap().remove(_IN_REQUEST);
+    for (final Configurator config : _services)
     {
-      throw new IllegalStateException("Configurator not initialized!");
+      config.endRequest(ec);
     }
   }
-  
-  /*
-   * (non-Javadoc)
-   * 
-   * @see org.apache.myfaces.trinidad.config.GlobalConfigurator#getPersistantRequestMap(javax.faces.context.ExternalContext)
-   */
 
-  private boolean                     _inited;
-  private List<Configurator>          _services;
-  
-  static private final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(GlobalConfiguratorImpl.class);
-  static private final String _IN_REQUEST = GlobalConfiguratorImpl.class.getName()+".IN_REQUEST";
- }
+  @SuppressWarnings("unchecked")
+  private void _startConfiguratorServiceRequest(final ExternalContext ec)
+  {
+    // Physical request has now begun
+    final boolean disabled = isConfiguratorServiceDisabled(ec);
+
+    // Tell whether the services were disabled when the requests had begun
+    ec.getRequestMap().put(_IN_REQUEST, disabled);
+
+    // If this hasn't been initialized then please initialize
+    for (final Configurator config : _services)
+    {
+      config.beginRequest(ec);
+    }
+  }
+
+  private boolean                                               _initialized;
+
+  private List<Configurator>                                    _services;
+
+  static private final Map<ClassLoader, GlobalConfiguratorImpl> _CONFIGURATORS = new HashMap<ClassLoader, GlobalConfiguratorImpl>();
+
+  static private final String                                   _IN_REQUEST    = GlobalConfiguratorImpl.class
+                                                                                   .getName()
+                                                                                   + ".IN_REQUEST";
+
+  static private final TrinidadLogger                           _LOG           = TrinidadLogger
+                                                                                   .createTrinidadLogger(GlobalConfiguratorImpl.class);
+
+  private enum RequestType
+  {
+    PORTAL_ACTION, 
+    PORTAL_RENDER, 
+    SERVLET;
+
+    public static void clearType(final ExternalContext ec)
+    {
+      ec.getRequestMap().remove(_REQUEST_TYPE);
+    }
+
+    public static RequestType getType(final ExternalContext ec)
+    {
+      return (RequestType) ec.getRequestMap().get(_REQUEST_TYPE);
+    }
+
+    public static boolean isCorrectType(final ExternalContext ec)
+    {
+      return _findType(ec) == getType(ec);
+    }
+
+    @SuppressWarnings("unchecked")
+    public static void setType(final ExternalContext ec)
+    {
+      ec.getRequestMap().put(_REQUEST_TYPE, _findType(ec));
+    }
+
+    private static final RequestType _findType(final ExternalContext ec)
+    {
+      if (ExternalContextUtils.isPortlet(ec))
+      {
+        if (ExternalContextUtils.isAction(ec))
+        {
+          return PORTAL_ACTION;
+        }
+        else
+        {
+          return PORTAL_RENDER;
+        }
+      }
+      else
+      {
+        return SERVLET;
+      }
+    }
+
+    static private final String _REQUEST_TYPE = GlobalConfiguratorImpl.class.getName()
+                                                  + ".REQUEST_TYPE";
+  }
+}

Modified: incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/FacesContextFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/FacesContextFactoryImpl.java?view=diff&rev=491976&r1=491975&r2=491976
==============================================================================
--- incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/FacesContextFactoryImpl.java (original)
+++ incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/context/FacesContextFactoryImpl.java Tue Jan  2 15:55:08 2007
@@ -29,7 +29,6 @@
 import javax.faces.lifecycle.Lifecycle;
 import javax.faces.render.RenderKit;
 
-import org.apache.myfaces.trinidad.config.Configurator;
 import org.apache.myfaces.trinidad.context.ExternalContextDecorator;
 import org.apache.myfaces.trinidad.context.RequestContext;
 import org.apache.myfaces.trinidadinternal.config.GlobalConfiguratorImpl;
@@ -60,9 +59,14 @@
     FacesContext fc = _factory.getFacesContext(context, request, response, lifecycle);
     ExternalContext ec = fc.getExternalContext();
 
-    Configurator config = Configurator.getInstance(ec);    
-    //The global configurator will make sure other configurators are run only once
-    if(!GlobalConfiguratorImpl.isRequestBegun(ec))
+    GlobalConfiguratorImpl config = GlobalConfiguratorImpl.getInstance();    
+
+    //This should be done only if the filter or other logic was not done before this
+    //we try to retrieve the FacesContext.  If this is the case then we'll need to handle
+    //cleanup on the release of the FacesContext.  Otherwise the endRequest should be
+    //called by whatever did he origional beginRequest.
+    
+    if(!GlobalConfiguratorImpl.isRequestStarted(ec))
     {
       config.beginRequest(ec);
       ec.getApplicationMap().put(_CONFIG_IN_CONTEXT, Boolean.TRUE);
@@ -76,6 +80,7 @@
    * 
    * @param fc
    */
+  @SuppressWarnings("unchecked")
   static void endRequestIfNecessary(FacesContext fc)
   {
     ExternalContext ec = fc.getExternalContext();
@@ -94,7 +99,7 @@
       //SMO: TODO: is this still needed?
       ExternalContext baseExternal = base.getExternalContext();
       ExternalContext external = 
-        Configurator.getInstance(baseExternal).getExternalContext(baseExternal);
+        GlobalConfiguratorImpl.getInstance().getExternalContext(baseExternal);
       _external = new OverrideDispatch(external);
       setCurrentInstance(this);
     }
@@ -243,7 +248,7 @@
       ExternalContext ec = getExternalContext();
       if(Boolean.TRUE.equals(ec.getApplicationMap().remove(_READY_FOR_CLEANUP)))
       {
-        Configurator.getInstance(ec).endRequest(ec);
+        GlobalConfiguratorImpl.getInstance().endRequest(ec);
       }
       
       _base.release();

Modified: incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java?view=diff&rev=491976&r1=491975&r2=491976
==============================================================================
--- incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java (original)
+++ incubator/adffaces/branches/jwaldman-portal/trinidad/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java Tue Jan  2 15:55:08 2007
@@ -33,9 +33,9 @@
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponseWrapper;
 
-import org.apache.myfaces.trinidad.config.Configurator;
 import org.apache.myfaces.trinidad.logging.TrinidadLogger;
 import org.apache.myfaces.trinidad.util.ClassLoaderUtils;
+import org.apache.myfaces.trinidadinternal.config.GlobalConfiguratorImpl;
 import org.apache.myfaces.trinidadinternal.config.dispatch.DispatchResponseConfiguratorImpl;
 import org.apache.myfaces.trinidadinternal.config.dispatch.DispatchServletResponse;
 import org.apache.myfaces.trinidadinternal.config.upload.FileUploadConfiguratorImpl;
@@ -80,8 +80,7 @@
       context.getExternalContext().getRequestMap().get(_IS_RETURNING_KEY));
   }
 
-  public void init(
-    FilterConfig filterConfig) throws ServletException
+  public void init(FilterConfig filterConfig) throws ServletException
   {
       _servletContext = filterConfig.getServletContext();
             
@@ -102,10 +101,9 @@
       }
       _filters = null;
       
-      //Destroy container agnostic services
-      Configurator config = Configurator.getInstance(null);
-      if (config != null)
-        config.destroy();
+      //=-= Scott O'Bryan =-=
+      //Configurator services are destroyed by the Context Listener
+      
   }
 
   @SuppressWarnings("unchecked")
@@ -123,7 +121,7 @@
     request.setAttribute(_FILTER_EXECUTED_KEY, Boolean.TRUE);
 
     ExternalContext externalContext = new ServletExternalContext(_servletContext, request, response);    
-    Configurator config = Configurator.getInstance(externalContext);
+    GlobalConfiguratorImpl config = GlobalConfiguratorImpl.getInstance();
     config.beginRequest(externalContext);
     
     //To maintain backward compatibilty, wrap the request at the filter level