jvanzyl     02/01/17 19:34:59

  Added:       src/java/org/apache/turbine/pipeline
                        DetermineActionValve.java DetermineTargetValve.java
                        RunModulesValve.java
  Log:
  - More work by James Taylor :-)
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-3/src/java/org/apache/turbine/pipeline/DetermineActionValve.java
  
  Index: DetermineActionValve.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  import org.apache.turbine.Turbine;
  import org.apache.turbine.TurbineConstants;
  import org.apache.turbine.RunData;
  import org.apache.turbine.TurbineException;
  import org.apache.turbine.Valve;
  import org.apache.turbine.ValveContext;
  import org.apache.log4j.Category;
  
  /**
   * This valve is responsible for setting the 'action' property of RunData based
   * on request parameter. There is no default action, since a null action is
   * perfectly valid.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Taylor</a>
   */
  public class DetermineActionValve 
      implements Valve
  {
      private static final Category log = 
          Category.getInstance( DetermineActionValve.class );
          
      /**
       * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
       */
      public void invoke( RunData data, ValveContext context )
          throws IOException, TurbineException
      {
          if ( ! data.hasAction() )
          {
              String action = 
                  data.getParameters().getString( TurbineConstants.ACTION );
  
              if ( action != null )
              {
                  data.setAction( action );
                  
                  log.debug( "Set action from request parameter" );
              }
              else
              {                    
                  log.debug( "No action" );
              }
          }
          
          if ( log.isDebugEnabled() )
          {
              log.debug( "Action is now: " + data.getAction() );
          }
  
          // Pass control to the next Valve in the Pipeline
          context.invokeNext( data );
      }
  }
  
  
  
  1.1                  
jakarta-turbine-3/src/java/org/apache/turbine/pipeline/DetermineTargetValve.java
  
  Index: DetermineTargetValve.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  import org.apache.turbine.Turbine;
  import org.apache.turbine.TurbineConstants;
  import org.apache.turbine.RunData;
  import org.apache.turbine.TurbineException;
  import org.apache.turbine.Valve;
  import org.apache.turbine.ValveContext;
  import org.apache.log4j.Category;
  
  /**
   * This valve is responsible for setting the 'target' property of the RunData.
   * If it is not already set it attempts to get the target from the request
   * parameter 'template'. If the parameter is not set, we use the homepage
   * specified by the configuration property Turbine.TEMPLATE_HOMEPAGE.
   *
   * FIXME: The request parameter which determines the template should be
   *        configurable. 
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Taylor</a>
   */
  public class DetermineTargetValve 
      implements Valve
  {
      private static final Category log = 
          Category.getInstance( DetermineTargetValve.class );
          
      /**
       * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
       */
      public void invoke( RunData data, ValveContext context )
          throws IOException, TurbineException
      {
          if ( ! data.hasTarget() )
          {
              String target = data.getParameters().getString( "template" );
  
              if ( target != null )
              {
                  data.setTarget( target );
                  
                  log.debug( "Set target from request parameter" );
              }
              else
              {
                  data.setTarget( Turbine.getConfiguration().getString(
                      Turbine.TEMPLATE_HOMEPAGE ) );
                      
                  log.debug( "Set target using default value" );
              }
          }
          
          if ( log.isDebugEnabled() )
          {
              log.debug( "Target is now: " + data.getTarget() );
          }
  
          // Pass control to the next Valve in the Pipeline
          context.invokeNext( data );
      }
  }
  
  
  
  1.1                  
jakarta-turbine-3/src/java/org/apache/turbine/pipeline/RunModulesValve.java
  
  Index: RunModulesValve.java
  ===================================================================
  package org.apache.turbine.pipeline;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Turbine" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Turbine", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import java.io.IOException;
  
  import org.apache.turbine.Turbine;
  import org.apache.turbine.TurbineConstants;
  import org.apache.turbine.RunData;
  import org.apache.turbine.TurbineException;
  import org.apache.turbine.Valve;
  import org.apache.turbine.ValveContext;
  import org.apache.turbine.Resolver;
  import org.apache.turbine.modules.Module;
  
  import org.apache.log4j.Category;
  
  /**
   * Valve which determines the target for a request and runs the modules
   * associated with that target. This valve is intended to model a portion
   * of the 'Turbine classic pipeline' as defined by the behavior of turbine
   * release 2 
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>James Taylor</a>
   * @version $Id: RunModulesValve.java,v 1.1 2002/01/18 03:34:59 jvanzyl Exp $
   */
  public class RunModulesValve 
      implements Valve
  {
      private static final Category log = 
          Category.getInstance( RunModulesValve.class );
      
      private String targetModuleType = Turbine.getConfiguration()
          .getString( "pipeline.default.targetModuleType" );
      
      private String defaultTarget = Turbine.getConfiguration()
          .getString( Turbine.TEMPLATE_HOMEPAGE );
        
      /**
       * @see org.apache.turbine.Valve#invoke(RunData, ValveContext)
       */
      public void invoke( RunData data, ValveContext context )
          throws IOException, TurbineException
      {
          try
          {
              runModules( targetModuleType, data );
          }
          catch ( Exception e )
          {
              throw new TurbineException( "Failure in RunModulesValve", e );
          }
  
          // Pass control to the next Valve in the Pipeline
          context.invokeNext( data );
      }
      
      /**
       * Runs the module chain associated with the target
       */
      public void runModules( String moduleType, RunData data )
          throws Exception
      {
          String target; 
          String oldTarget = "";
          
          Resolver resolver = Turbine.getResolver();
          
          Module module; 
          
          determineTarget( data );
  
          target = normalizeTarget( data.getTarget() );
          
          while( ! oldTarget.equals( target ) ) 
          {
              module = resolver.getModule( moduleType, target );
  
              if ( log.isDebugEnabled() )
              {
                  log.debug( "Executing module " + module.getClass().getName() 
                             + " for target: " + target );
              }
  
              module.execute( data );
  
              // Modules might change the target:
              
              oldTarget = target;
              target = normalizeTarget( data.getTarget() );
          }
          
          // Set the target to the final normalized path, which will be used by
          // the resolver and the appropriate TemplateEngineService to locate the
          // template.
          
          data.setTarget( target );
      }
      
      /**
       * Responsible for determining the initial target from the request 
       * parameters. If no target is found the 'homepage' will be used.
       */
      public void determineTarget( RunData data )
      {
          if ( ! data.hasTarget() )
          {
              String target = data.getParameters().getString( "template" );
  
              if ( target != null )
              {
                  data.setTarget( target );
                  
                  log.debug( "Set target from request parameter" );
              }
              else
              {
                  data.setTarget( defaultTarget );
                      
                  log.debug( "Set target using default value" );
              }
          }
          
          if ( log.isDebugEnabled() )
          {
              log.debug( "Target is now: " + data.getTarget() );
          }
      }
      
      /**
       * Normalizes the target path to the format used internally by the turbine 
       * classic style valves and resolvers. ',' will be translated to '/', 
       * multiple path seperators will be collapsed, and the result is guranteed
       * not to begin with a path seperator.
       */
      private String normalizeTarget( String target )
          throws Exception
      {
          StringBuffer buffer = new StringBuffer( target );
  
          char c;        
  
          int len = buffer.length();
          
          boolean lastWasDelim = true;
          
          for ( int j = 0; j < len; j++ )
          {
              c = buffer.charAt( j );
              
              if ( c == ',' || c == '/' )
              {
                  if ( lastWasDelim )
                  {
                      buffer.deleteCharAt( j );
                      
                      j--;
                      len--;
                  }
                  else
                  {
                      buffer.setCharAt( j, '/' );
                  }
                  
                  lastWasDelim = true;
              }
              else
              {   
                  lastWasDelim = false;
              }
          }
          
          if (len == 0) 
          {
              new Exception( "Syntax error in target: '" + target + '\'');
          }
          
          return buffer.toString();
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to