cziegeler    2002/09/12 07:14:36

  Modified:    src/java/org/apache/cocoon/serialization
                        AbstractTextSerializer.java
               src/java/org/apache/cocoon/environment/http
                        HttpEnvironment.java
               src/java/org/apache/cocoon/components/treeprocessor/sitemap
                        PipelineNode.java
               src/java/org/apache/cocoon/environment/commandline
                        AbstractCommandLineEnvironment.java
               src/java/org/apache/cocoon/environment
                        AbstractEnvironment.java Environment.java
               src/java/org/apache/cocoon Cocoon.java
  Added:       src/java/org/apache/cocoon/util BufferedOutputStream.java
  Log:
  First shot at the buffered output stream.
  Error handlers can now always produce output.
  Removed buffering in AbstractTextSerializer
  
  Revision  Changes    Path
  1.1                  
xml-cocoon2/src/java/org/apache/cocoon/util/BufferedOutputStream.java
  
  Index: BufferedOutputStream.java
  ===================================================================
  /*
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) 1999-2002 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
      [EMAIL PROTECTED]
  
   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  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
  */
  package org.apache.cocoon.util;
  
  import java.io.FilterOutputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  
  /**
   * This class is like the {@link java.io.BufferedOutputStream} but it
   * extends it with a logic to count the number of bytes written to
   * the output stream.
   * 
   * @author <a href="mailto:[EMAIL PROTECTED]";>Carsten Ziegeler</a>
   * @version CVS $Id: XMLCocoonLogFormatter.java,v 1.6 2002/02/22 07:03:58 cziegeler 
Exp $
   * @since   2.1
   */
  public final class BufferedOutputStream extends FilterOutputStream {
      
      protected byte buf[];
  
      protected int count;
      
      /**
       * Creates a new buffered output stream to write data to the 
       * specified underlying output stream with a default 512-byte 
       * buffer size.
       *
       * @param   out   the underlying output stream.
       */
      public BufferedOutputStream(OutputStream out) {
          this(out, 8192);
      }
  
      /**
       * Creates a new buffered output stream to write data to the 
       * specified underlying output stream with the specified buffer 
       * size. 
       *
       * @param   out    the underlying output stream.
       * @param   size   the buffer size.
       * @exception IllegalArgumentException if size <= 0.
       */
      public BufferedOutputStream(OutputStream out, int size) {
      super(out);
          if (size <= 0) {
              throw new IllegalArgumentException("Buffer size <= 0");
          }
          this.buf = new byte[size];
      }
  
      /**
       * Writes the specified byte to this buffered output stream. 
       *
       * @param      b   the byte to be written.
       * @exception  IOException  if an I/O error occurs.
       */
      public void write(int b) throws IOException {
          if (this.count >= this.buf.length) {
              this.incBuffer();
          }
          this.buf[count++] = (byte)b;
      }
  
      /**
       * Writes <code>len</code> bytes from the specified byte array 
       * starting at offset <code>off</code> to this buffered output stream.
       *
       * <p> Ordinarily this method stores bytes from the given array into this
       * stream's buffer, flushing the buffer to the underlying output stream as
       * needed.  If the requested length is at least as large as this stream's
       * buffer, however, then this method will flush the buffer and write the
       * bytes directly to the underlying output stream.  Thus redundant
       * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
       *
       * @param      b     the data.
       * @param      off   the start offset in the data.
       * @param      len   the number of bytes to write.
       * @exception  IOException  if an I/O error occurs.
       */
      public void write(byte b[], int off, int len) throws IOException {
          if (len >= buf.length) {
              this.incBuffer();
          }
          if (len > buf.length - count) {
              this.incBuffer();
          }
          System.arraycopy(b, off, buf, count, len);
          count += len;
      }
  
      /**
       * Flushes this buffered output stream. 
       * We don't flush here. 
       *
       * @exception  IOException  if an I/O error occurs.
       * @see        java.io.FilterOutputStream#out
       */
      public void flush() throws IOException {
      }
  
      /** 
       * Flushes this buffered output stream. 
       * We don't flush here. 
       */
      public void realFlush() throws IOException {
          this.writeBuffer();
          this.out.flush();
      }
      
      /**
       * Write the buffer
       */
      private void writeBuffer() 
      throws IOException {
          if (this.count > 0) {
              this.out.write(this.buf, 0, this.count);
          }
      }
  
      /**
       * Increment the buffer
       */
      private void incBuffer() {
          // currently we double the buffer size
          // this is not so fast but is a very simple logic
          byte[] newBuf = new byte[this.buf.length * 2];
          System.arraycopy(this.buf, 0, newBuf, 0, this.buf.length);
          this.buf = newBuf;
      }
      
      /**
       * Clear/reset the buffer
       */
      public void clearBuffer() {
          this.count = 0;
      }
      
  }
  
  
  
  
  1.16      +17 -17    
xml-cocoon2/src/java/org/apache/cocoon/serialization/AbstractTextSerializer.java
  
  Index: AbstractTextSerializer.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/serialization/AbstractTextSerializer.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- AbstractTextSerializer.java       23 Aug 2002 11:06:15 -0000      1.15
  +++ AbstractTextSerializer.java       12 Sep 2002 14:14:36 -0000      1.16
  @@ -105,12 +105,12 @@
       /**
        * The default output buffer size.
        */
  -    private static final int DEFAULT_BUFFER_SIZE = 8192;
  +    //private static final int DEFAULT_BUFFER_SIZE = 8192;
   
       /**
        * The output buffer size to use.
        */
  -    private int outputBufferSize = DEFAULT_BUFFER_SIZE;
  +    //private int outputBufferSize = DEFAULT_BUFFER_SIZE;
   
       /**
        * Cache for avoiding unnecessary checks of namespaces abilities.
  @@ -194,12 +194,12 @@
            * transfer encoding this would otherwise lead to a whopping 6-fold
            * increase of data on the wire.
            */
  -        if (outputBufferSize > 0) {
  -            super.setOutputStream(
  -              new BufferedOutputStream(out, outputBufferSize));
  -        } else {
  +      //  if (outputBufferSize > 0) {
  +      //      super.setOutputStream(
  +      //        new BufferedOutputStream(out, outputBufferSize));
  +      //  } else {
               super.setOutputStream(out);
  -        }
  +      //  }
       }
   
       /**
  @@ -209,9 +209,9 @@
         throws ConfigurationException {
   
           // configure buffer size
  -        Configuration bsc = conf.getChild("buffer-size", false);
  -        if(null != bsc)
  -          outputBufferSize = bsc.getValueAsInteger(DEFAULT_BUFFER_SIZE);
  +     //   Configuration bsc = conf.getChild("buffer-size", false);
  +     //   if(null != bsc)
  +      //    outputBufferSize = bsc.getValueAsInteger(DEFAULT_BUFFER_SIZE);
   
           // configure xalan
           Configuration cdataSectionElements = 
conf.getChild("cdata-section-elements");
  @@ -269,12 +269,12 @@
       }
   
       public void recycle() {
  -        if (this.output != null) {
  -            try {
  -                this.output.flush();
  -            } catch (IOException ignored) {
  -            }
  -        }
  +     //   if (this.output != null) {
  +     //       try {
  +     //           this.output.flush();
  +     //       } catch (IOException ignored) {
  +     //       }
  +     //   }
           
           super.recycle();
   
  
  
  
  1.14      +3 -13     
xml-cocoon2/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java
  
  Index: HttpEnvironment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/environment/http/HttpEnvironment.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- HttpEnvironment.java      6 Jun 2002 14:43:40 -0000       1.13
  +++ HttpEnvironment.java      12 Sep 2002 14:14:36 -0000      1.14
  @@ -85,9 +85,6 @@
       /** The HttpContext */
       private HttpContext webcontext = null;
   
  -    /** The OutputStream */
  -    private OutputStream outputStream = null;
  -
       /** Cache content type as there is no getContentType() in reponse object */
       private String contentType = null;
   
  @@ -232,13 +229,6 @@
       }
   
       /**
  -     * Get the OutputStream
  -     */
  -    public OutputStream getOutputStream() throws IOException {
  -        return this.outputStream;
  -    }
  -
  -    /**
        * Check if the response has been modified since the same
        * "resource" was requested.
        * The caller has to test if it is really the same "resource"
  @@ -267,7 +257,7 @@
        *
        * @return true if the response was successfully reset
       */
  -    public boolean tryResetResponse() {
  +/*    public boolean tryResetResponse() {
           try {
               if ( !this.response.isCommitted() ) {
                   this.response.reset();
  @@ -280,6 +270,6 @@
           }
           getLogger().debug("Response wasn't reset");
           return false;
  -    }
  +    }*/
   
   }
  
  
  
  1.15      +2 -2      
xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelineNode.java
  
  Index: PipelineNode.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/treeprocessor/sitemap/PipelineNode.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- PipelineNode.java 11 Sep 2002 10:08:35 -0000      1.14
  +++ PipelineNode.java 12 Sep 2002 14:14:36 -0000      1.15
  @@ -187,7 +187,7 @@
           try {
               // Try to reset the response to avoid mixing already produced output
               // and error page.
  -            env.tryResetResponse();
  +            env.resetResponse();
   
               // Build a new context
               errorContext = new InvokeContext();
  
  
  
  1.10      +5 -6      
xml-cocoon2/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java
  
  Index: AbstractCommandLineEnvironment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/environment/commandline/AbstractCommandLineEnvironment.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- AbstractCommandLineEnvironment.java       31 Jul 2002 13:13:27 -0000      1.9
  +++ AbstractCommandLineEnvironment.java       12 Sep 2002 14:14:36 -0000      1.10
  @@ -79,7 +79,6 @@
   implements Redirector {
   
       protected String contentType;
  -    protected OutputStream stream;
       protected int contentLength;
       protected boolean hasRedirected = false;
   
  @@ -91,7 +90,7 @@
       throws MalformedURLException {
           super(uri, view, context);
           this.enableLogging(log);
  -        this.stream = stream;
  +        this.outputStream = stream;
       }
   
       /**
  @@ -128,7 +127,7 @@
               // so we create one without Avalon...
               org.apache.cocoon.serialization.LinkSerializer ls =
                   new org.apache.cocoon.serialization.LinkSerializer();
  -            ls.setOutputStream(this.stream);
  +            ls.setOutputStream(this.outputStream);
   
               Source redirectSource = null;
               try {
  @@ -152,7 +151,7 @@
                   int length = -1;
   
                   while ((length = is.read(buffer)) > -1) {
  -                    this.stream.write(buffer, 0, length);
  +                    this.outputStream.write(buffer, 0, length);
                   }
               } catch (SourceException se) {
                   throw new IOException("SourceException: " + se);
  @@ -191,7 +190,7 @@
        * Get the OutputStream
        */
       public OutputStream getOutputStream() throws IOException {
  -        return this.stream;
  +        return this.outputStream;
       }
   }
   
  
  
  
  1.27      +40 -2     
xml-cocoon2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java
  
  Index: AbstractEnvironment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/environment/AbstractEnvironment.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- AbstractEnvironment.java  8 Aug 2002 02:10:40 -0000       1.26
  +++ AbstractEnvironment.java  12 Sep 2002 14:14:36 -0000      1.27
  @@ -59,6 +59,7 @@
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.components.source.SourceHandler;
   import org.apache.cocoon.components.source.SourceUtil;
  +import org.apache.cocoon.util.BufferedOutputStream;
   
   import org.apache.excalibur.source.SourceException;
   import org.apache.excalibur.xmlizer.XMLizer;
  @@ -68,6 +69,7 @@
   
   import java.io.File;
   import java.io.IOException;
  +import java.io.OutputStream;
   import java.net.MalformedURLException;
   import java.net.URL;
   import java.util.Enumeration;
  @@ -116,6 +118,12 @@
       /** The current manager */
       protected ComponentManager manager = null;
   
  +    /** The secure Output Stream */
  +    protected BufferedOutputStream secureOutputStream;
  +    
  +    /** The real output stream */
  +    protected OutputStream outputStream;
  +    
       /**
        * The sitemap processor sets up new managers per sitemap. Get the
        * "current" one for this environment.
  @@ -426,16 +434,46 @@
       }
   
       /**
  +     * Get the OutputStream
  +     */
  +    public OutputStream getOutputStream() throws IOException {
  +        if (this.secureOutputStream == null) {
  +            this.secureOutputStream = new BufferedOutputStream(this.outputStream);
  +        }
  +        return this.secureOutputStream;
  +    }
  +
  +    /**
        * Reset the response if possible. This allows error handlers to have
        * a higher chance to produce clean output if the pipeline that raised
        * the error has already output some data.
        *
  +     * @deprecated This is obsoleted by {@link #resetResponse}
        * @return true if the response was successfully reset
       */
       public boolean tryResetResponse() {
  -        return false;
  +        this.resetResponse();
  +        return true;
       }
   
  +    /**
  +     * Reset the response. This allows error handlers to have
  +     * a higher chance to produce clean output if the pipeline that raised
  +     * the error has already output some data.
  +     *
  +     */
  +    public void resetResponse() {
  +        this.secureOutputStream.clearBuffer();
  +    }
  +
  +    /**
  +     * Commit the response
  +     */
  +    public void commitResponse() 
  +    throws IOException {
  +        this.secureOutputStream.realFlush();
  +    }
  +    
       /**
        * Get a <code>Source</code> object.
        */
  
  
  
  1.13      +16 -2     
xml-cocoon2/src/java/org/apache/cocoon/environment/Environment.java
  
  Index: Environment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/environment/Environment.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Environment.java  2 Jul 2002 08:32:06 -0000       1.12
  +++ Environment.java  12 Sep 2002 14:14:36 -0000      1.13
  @@ -239,9 +239,23 @@
        * a higher chance to produce clean output if the pipeline that raised
        * the error has already output some data.
        *
  +     * @deprecated This is obsoleted by {@link #resetResponse}
        * @return true if the response was successfully reset
  -    */
  +     */
       boolean tryResetResponse();
   
  +    /**
  +     * Reset the response. This allows error handlers to have
  +     * a higher chance to produce clean output if the pipeline that raised
  +     * the error has already output some data.
  +     *
  +     */
  +    void resetResponse();
  +
  +    /**
  +     * Commit the response
  +     */
  +    void commitResponse() throws IOException;
  +    
   }
   
  
  
  
  1.36      +11 -3     xml-cocoon2/src/java/org/apache/cocoon/Cocoon.java
  
  Index: Cocoon.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/Cocoon.java,v
  retrieving revision 1.35
  retrieving revision 1.36
  diff -u -r1.35 -r1.36
  --- Cocoon.java       11 Sep 2002 10:08:36 -0000      1.35
  +++ Cocoon.java       12 Sep 2002 14:14:36 -0000      1.36
  @@ -569,22 +569,30 @@
   
           environment.setComponents( this.sourceResolver, this.xmlizer );
           try {
  +            boolean result;
               if (this.getLogger().isDebugEnabled()) {
                   ++activeRequestCount;
                   this.debug(environment, false);
               }
   
               if (this.threadSafeProcessor != null) {
  -                return this.threadSafeProcessor.process(environment);
  +                result = this.threadSafeProcessor.process(environment);
               } else {
                   Processor processor = 
(Processor)this.componentManager.lookup(Processor.ROLE);
                   try {
  -                    return processor.process(environment);
  +                    result = processor.process(environment);
                   }
                   finally {
                       this.componentManager.release(processor);
                   }
               }
  +            // commit response on success
  +            environment.commitResponse();
  +            return result;
  +        } catch (Exception any) {
  +            // reset response on error
  +            environment.resetResponse();
  +            throw any;
           } finally {
               if (this.getLogger().isDebugEnabled()) {
                   --activeRequestCount;
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     [EMAIL PROTECTED]
To unsubscribe, e-mail:          [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to