You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by cz...@apache.org on 2003/10/19 16:34:40 UTC

cvs commit: cocoon-2.2/src/java/org/apache/cocoon/environment EnvironmentHelper.java Environment.java AbstractEnvironment.java

cziegeler    2003/10/19 07:34:40

  Modified:    src/java/org/apache/cocoon/environment Environment.java
                        AbstractEnvironment.java
  Added:       src/java/org/apache/cocoon/environment
                        EnvironmentHelper.java
  Log:
  Adding experimental EnvironmentHelper to clean up environment
  handling a little bit and avoid unnecessary uri calculations
  
  Revision  Changes    Path
  1.7       +1 -7      cocoon-2.2/src/java/org/apache/cocoon/environment/Environment.java
  
  Index: Environment.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/environment/Environment.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- Environment.java	15 Oct 2003 18:03:52 -0000	1.6
  +++ Environment.java	19 Oct 2003 14:34:40 -0000	1.7
  @@ -133,12 +133,6 @@
   
       /**
        * Get the output stream where to write the generated resource.
  -     * @deprecated Use {@link #getOutputStream(int)} instead.
  -     */
  -    OutputStream getOutputStream() throws IOException;
  -
  -    /**
  -     * Get the output stream where to write the generated resource.
        * The returned stream is buffered by the environment. If the
        * buffer size is -1 then the complete output is buffered.
        * If the buffer size is 0, no buffering takes place.
  
  
  
  1.19      +24 -27    cocoon-2.2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java
  
  Index: AbstractEnvironment.java
  ===================================================================
  RCS file: /home/cvs/cocoon-2.2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- AbstractEnvironment.java	19 Oct 2003 14:02:47 -0000	1.18
  +++ AbstractEnvironment.java	19 Oct 2003 14:34:40 -0000	1.19
  @@ -53,7 +53,6 @@
   import java.io.File;
   import java.io.IOException;
   import java.io.OutputStream;
  -import java.lang.reflect.Method;
   import java.net.MalformedURLException;
   import java.util.Enumeration;
   import java.util.HashMap;
  @@ -83,31 +82,32 @@
       /** The current uri in progress */
       protected String uris;
   
  -    /** The current prefix to strip off from the request uri */
  +    /** The current prefix to strip off from the request uri  - TODO (CZ) Remove this*/
       protected StringBuffer prefix = new StringBuffer();
   
       /** The View requested */
  -    protected String view = null;
  +    protected String view;
   
       /** The Action requested */
  -    protected String action = null;
  +    protected String action;
   
  -     /** The Context path */
  -    protected String context = null;
  +     /** The Context path  - TODO (CZ) Remove this*/
  +    protected String context;
   
  -    /** The context path stored temporarily between constructor and initComponents */
  -    private String tempInitContext = null;
  +    /** The context path stored temporarily between constructor and initComponents 
  +     *    - TODO (CZ) Remove this*/
  +    private String tempInitContext;
   
  -    /** The root context path */
  -    protected String rootContext = null;
  +    /** The root context path  - TODO (CZ) Remove this*/
  +    protected String rootContext;
   
       /** The servlet object model */
  -    protected HashMap objectModel = null;
  +    protected HashMap objectModel;
   
  -    /** The real source resolver */
  +    /** The real source resolver  - TODO (CZ) Remove this*/
       protected org.apache.excalibur.source.SourceResolver sourceResolver;
   
  -    /** The service manager */
  +    /** The service manager - TODO (CZ) Remove this */
       protected ServiceManager manager;
   
       /** The attributes */
  @@ -119,10 +119,7 @@
       /** The real output stream */
       protected OutputStream outputStream;
   
  -    /** The AvalonToCocoonSourceWrapper (this is for the deprecated support) */
  -    static protected Method avalonToCocoonSourceWrapper;
  -
  -    /** Do we have our components ? */
  +    /** Do we have our components ?  - TODO (CZ) Remove this */
       protected boolean initializedComponents = false;
       
       /**
  @@ -164,6 +161,7 @@
   
       /**
        * Get the Root Context
  +     * TODO (CZ) Remove this method
        */
       public String getRootContext() {
           if ( !this.initializedComponents) {
  @@ -174,6 +172,7 @@
   
       /**
        * Get the current Context
  +     * TODO (CZ) Remove this method
        */
       public String getContext() {
           if ( !this.initializedComponents) {
  @@ -184,6 +183,7 @@
   
       /**
        * Get the prefix of the URI in progress
  +     * TODO (CZ) Remove this method
        */
       public String getURIPrefix() {
           return this.prefix.toString();
  @@ -191,6 +191,7 @@
   
       /**
        * Set the prefix of the URI in progress
  +     * TODO (CZ) Remove this method
        */
       protected void setURIPrefix(String prefix) {
           if (getLogger().isDebugEnabled()) {
  @@ -201,6 +202,7 @@
   
       /**
        * Set the context.
  +     * TODO (CZ) Remove this method
        */
       protected void setContext(String context) {
           this.context = context;
  @@ -209,6 +211,7 @@
       /**
        * Set the context. This is similar to changeContext()
        * except that it is absolute.
  +     * TODO (CZ) Remove this method
        */
       public void setContext(String prefix, String uri, String context) {
           this.setContext(context);
  @@ -221,6 +224,7 @@
   
       /**
        * Adds an prefix to the overall stripped off prefix from the request uri
  +     * TODO (CZ) Remove this method
        */
       public void changeContext(String prefix, String newContext)
       throws IOException {
  @@ -379,15 +383,6 @@
   
       /**
        * Get the output stream where to write the generated resource.
  -     * @deprecated Use {@link #getOutputStream(int)} instead.
  -     */
  -    public OutputStream getOutputStream() throws IOException {
  -        // by default we use the complete buffering output stream
  -        return this.getOutputStream(-1);
  -    }
  -
  -    /**
  -     * Get the output stream where to write the generated resource.
        * The returned stream is buffered by the environment. If the
        * buffer size is -1 then the complete output is buffered.
        * If the buffer size is 0, no buffering takes place.
  @@ -439,6 +434,7 @@
       /**
        * Initialize the components for the environment
        * This gets the source resolver and the xmlizer component
  +     * TODO (CZ) Remove this method
        */
       protected void initComponents() {
           this.initializedComponents = true;
  @@ -478,6 +474,7 @@
        * This can be used to cleanup the environment object
        */
       public void finishingProcessing() {
  +        // TODO (CZ) Remove this
           if ( null != this.manager ) {
               this.manager.release( this.sourceResolver );
               this.manager = null;
  
  
  
  1.1                  cocoon-2.2/src/java/org/apache/cocoon/environment/EnvironmentHelper.java
  
  Index: EnvironmentHelper.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, 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 Cocoon" and  "Apache Software Foundation" must  not  be
      used to  endorse or promote  products derived from  this software without
      prior written permission. For written permission, please contact
      apache@apache.org.
  
   5. Products  derived from this software may not  be called "Apache", 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 (INCLU-
   DING, 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 and was  originally created by
   Stefano Mazzocchi  <st...@apache.org>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.environment;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.util.Map;
  
  import org.apache.avalon.framework.activity.Disposable;
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.framework.service.ServiceException;
  import org.apache.avalon.framework.service.ServiceManager;
  import org.apache.avalon.framework.service.Serviceable;
  import org.apache.cocoon.components.source.SourceUtil;
  import org.apache.excalibur.source.Source;
  
  /**
   * Experimental code for cleaning up the environment handling
   * 
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Id: EnvironmentHelper.java,v 1.1 2003/10/19 14:34:40 cziegeler Exp $
   * @since 2.2
   */
  
  public class EnvironmentHelper
  extends AbstractLogEnabled
  implements SourceResolver, Serviceable, Disposable {
  
      /** The real source resolver */
      protected org.apache.excalibur.source.SourceResolver resolver;
      
      /** The service manager */
      protected ServiceManager manager;
      
      /** The current prefix to strip off from the request uri 
       *  TODO (CZ) Convert this to a String */
      protected StringBuffer prefix = new StringBuffer();
  
       /** The Context path */
      protected String context;
  
      /** The root context path */
      protected String rootContext;
  
      /**
       * Constructor
       *
       */
      public EnvironmentHelper(String context) {
          this.context = context;
      }
      
      
      /* (non-Javadoc)
       * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
       */
      public void service(ServiceManager manager) throws ServiceException {
          this.manager = manager;
          this.resolver = (org.apache.excalibur.source.SourceResolver)
                            this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE);
          if (this.context != null) {
              Source source = null;
              try {
                  source = this.resolver.resolveURI(this.context);
                  this.context = source.getURI();
                      
                  if (this.rootContext == null) // hack for EnvironmentWrapper
                      this.rootContext = this.context;
              } catch (IOException ioe) {
                  throw new ServiceException("Unable to resolve environment context. ", ioe);
              } finally {
                  this.resolver.release(source);
              }
              this.context = null;
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.avalon.framework.activity.Disposable#dispose()
       */
      public void dispose() {
          if ( this.manager != null ) {
              this.manager.release( this.resolver );
              this.resolver = null;
              this.manager = null;
          }
      }
  
      /* (non-Javadoc)
       * @see org.apache.excalibur.source.SourceResolver#release(org.apache.excalibur.source.Source)
       */
      public void release(Source source) {
          this.resolver.release(source);
      }
  
      /* (non-Javadoc)
       * @see org.apache.excalibur.source.SourceResolver#resolveURI(java.lang.String, java.lang.String, java.util.Map)
       */
      public Source resolveURI(final String location,
                               String baseURI,
                               final Map    parameters)
      throws MalformedURLException, IOException {
          return this.resolver.resolveURI(location, 
                                          (baseURI == null ? this.context : baseURI),
                                          parameters);
      }
  
      /* (non-Javadoc)
       * @see org.apache.excalibur.source.SourceResolver#resolveURI(java.lang.String)
       */
      public Source resolveURI(final String location)
      throws MalformedURLException, IOException {
          return this.resolveURI(location, null, null);
      }
  
      public void changeContext(Environment env) {
          String uris = env.getURI();
          final int l = this.prefix.length();
          if ( l >= 1 ) {
              if (!uris.startsWith(this.prefix.toString())) {
                  String message = "The current URI (" + uris +
                                   ") doesn't start with given prefix (" + prefix + ")";
                  getLogger().error(message);
                  throw new RuntimeException(message);
              }      
              // we don't need to check for slash at the beginning
              // of uris - the prefix always ends with a slash!
              uris = uris.substring(l);
              // TODO (CZ) Implement this in the environment
              // env.setURI(uris);
          }
      }
      /**
       * Adds an prefix to the overall stripped off prefix from the request uri
       */
      public void changeContext(String prefix, String newContext)
      throws IOException {
          if (getLogger().isDebugEnabled()) {
              getLogger().debug("Changing Cocoon context");
              getLogger().debug("  from context(" + this.context + ") and prefix(" + this.prefix + ")");
              getLogger().debug("  to context(" + newContext + ") and prefix(" + prefix + ")");
          }
          int l = prefix.length();
          if (l >= 1) {
              this.prefix.append(prefix);
              // check for a slash at the beginning to avoid problems with subsitemaps
              if ( !this.prefix.toString().endsWith("/") ) {
                  this.prefix.append('/');
              }
          }
  
          if (SourceUtil.getScheme(this.context).equals("zip")) {
              // if the resource is zipped into a war file (e.g. Weblogic temp deployment)
              // FIXME (VG): Is this still required? Better to unify both cases.
              if (getLogger().isDebugEnabled()) {
                  getLogger().debug("Base context is zip: " + this.context);
              }
              
              org.apache.excalibur.source.Source source = null;
              try {
                  source = this.resolver.resolveURI(this.context + newContext);
                  this.context = source.getURI();
              } finally {
                  this.resolver.release(source);
              }
          } else {
              String sContext;
              // if we got a absolute context or one with a protocol resolve it
              if (newContext.charAt(0) == '/') {
                  // context starts with the '/' - absolute file URL
                  sContext = "file:" + newContext;
              } else if (newContext.indexOf(':') > 1) {
                  // context have ':' - absolute URL
                  sContext = newContext;
              } else {
                  // context is relative to old one
                  sContext = this.context + '/' + newContext;
              }
  
              // Cut the file name part from context (if present)
              int i = sContext.lastIndexOf('/');
              if (i != -1 && i + 1 < sContext.length()) {
                  sContext = sContext.substring(0, i + 1);
              }
              
              Source source = null;
              try {
                  source = this.resolver.resolveURI(sContext);
                  this.context = source.getURI();
              } finally {
                  this.resolver.release(source);
              }
          }
  
          if (getLogger().isDebugEnabled()) {
              getLogger().debug("New context is " + this.context);
          }
      }
      
  }