avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From leosu...@apache.org
Subject cvs commit: avalon-logkit/src/java/org/apache/log/output AbstractWrappingTarget.java AsyncLogTarget.java
Date Tue, 04 Mar 2003 14:34:15 GMT
leosutic    2003/03/04 06:34:14

  Modified:    src/java/org/apache/log/output AsyncLogTarget.java
  Added:       src/test/org/apache/log/test WrappingTargetTestCase.java
               src/java/org/apache/log/output AbstractWrappingTarget.java
  Log:
  Factored out handling of wrapped, Closeable targets to AbstractWrappingTarget,
  and modified the AsyncLogTarget class to use the new base class.
  
  Revision  Changes    Path
  1.1                  avalon-logkit/src/test/org/apache/log/test/WrappingTargetTestCase.java
  
  Index: WrappingTargetTestCase.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 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 "Jakarta", "Avalon", 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 (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/>.
   */
  package org.apache.log.test;
  
  import junit.framework.TestCase;
  import org.apache.log.Hierarchy;
  import org.apache.log.Logger;
  import org.apache.log.LogEvent;
  import org.apache.log.LogTarget;
  import org.apache.log.output.AbstractWrappingTarget;
  import org.apache.log.util.Closeable;
  
  /**
   * Test suite for wrapping targets.
   *
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   */
  public final class WrappingTargetTestCase
      extends TestCase
  {
      
      static private class DummyTarget implements LogTarget
      {
          private boolean closed = false;
          
          public void close()
          {
              closed = true;
          }
          
          public boolean isClosed()
          {
              return closed;
          }
          
          public void processEvent( LogEvent event )
          {
              // Do nothing
          }
      }
      
      static private class CloseableDummyTarget extends DummyTarget implements Closeable
      {
      }
      
      static private class DummyTargetWrapper extends AbstractWrappingTarget
      {
          public DummyTargetWrapper( final LogTarget logTarget )
          {
              super( logTarget );
          }
          
          public DummyTargetWrapper( final LogTarget logTarget, final boolean closeWrappedTarget
)
          {
              super( logTarget, closeWrappedTarget );
          }
          
          public void doProcessEvent( LogEvent event )
          {
              // Do nothing
          }
      }
      
      public void testNonCloseable()
      {
          DummyTarget dummyTargetNonClose = new DummyTarget();
          DummyTarget dummyTargetNonClose2 = new DummyTarget();
          DummyTarget dummyTargetClose = new DummyTarget();
          
          DummyTargetWrapper wrapperNonClose = new DummyTargetWrapper(dummyTargetNonClose,
false);
          DummyTargetWrapper wrapperNonClose2 = new DummyTargetWrapper(dummyTargetNonClose2);
// should default to false
          DummyTargetWrapper wrapperClose = new DummyTargetWrapper(dummyTargetClose, true);
          
          assertTrue( !dummyTargetNonClose.isClosed() );
          assertTrue( !dummyTargetNonClose2.isClosed() );
          assertTrue( !dummyTargetClose.isClosed() );
          
          wrapperNonClose.close();
          wrapperNonClose2.close();
          wrapperClose.close();
          
          // The close() should have no effect, since neither target implements closeable.
          
          assertTrue( !dummyTargetNonClose.isClosed() );
          assertTrue( !dummyTargetNonClose2.isClosed() );
          assertTrue( !dummyTargetClose.isClosed() );        
      }
      
      public void testCloseable()
      {
          DummyTarget dummyTargetNonClose = new CloseableDummyTarget();
          DummyTarget dummyTargetNonClose2 = new CloseableDummyTarget();
          DummyTarget dummyTargetClose = new CloseableDummyTarget();
          
          DummyTargetWrapper wrapperNonClose = new DummyTargetWrapper(dummyTargetNonClose,
false);
          DummyTargetWrapper wrapperNonClose2 = new DummyTargetWrapper(dummyTargetNonClose2);
// should default to false
          DummyTargetWrapper wrapperClose = new DummyTargetWrapper(dummyTargetClose, true);
          
          assertTrue( !dummyTargetNonClose.isClosed() );
          assertTrue( !dummyTargetNonClose2.isClosed() );
          assertTrue( !dummyTargetClose.isClosed() );
          
          wrapperNonClose.close();
          wrapperNonClose2.close();
          wrapperClose.close();
          
          // Only the target that was wrapped with the closeWrapped parameter
          // set to true should be closed.
          
          assertTrue( !dummyTargetNonClose.isClosed() );
          assertTrue( !dummyTargetNonClose2.isClosed() );
          assertTrue( dummyTargetClose.isClosed() );        
      }
  }
  
  
  
  1.15      +26 -2     avalon-logkit/src/java/org/apache/log/output/AsyncLogTarget.java
  
  Index: AsyncLogTarget.java
  ===================================================================
  RCS file: /home/cvs/avalon-logkit/src/java/org/apache/log/output/AsyncLogTarget.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- AsyncLogTarget.java	26 Feb 2003 09:53:11 -0000	1.14
  +++ AsyncLogTarget.java	4 Mar 2003 14:34:13 -0000	1.15
  @@ -79,7 +79,7 @@
    * @author <a href="mailto:peter@apache.org">Peter Donald</a>
    */
   public class AsyncLogTarget
  -    extends AbstractTarget
  +    extends AbstractWrappingTarget
       implements Runnable
   {
       private final LinkedList m_list;
  @@ -102,12 +102,36 @@
       */
       public AsyncLogTarget( final LogTarget logTarget, final int queueSize )
       {
  +        this( logTarget, queueSize, false );
  +    }
  +
  +    /**
  +     * Creation of a new async log target.
  +     * @param logTarget the underlying target
  +     * @param closeTarget close the underlying target when this target is closed. This
flag
  +     *        has no effect unless the logTarget implements Closeable.
  +     */
  +    public AsyncLogTarget( final LogTarget logTarget, final boolean closeTarget )
  +    {
  +        this( logTarget, 15, closeTarget );
  +    }
  +    
  +    /**
  +     * Creation of a new async log target.
  +     * @param logTarget the underlying target
  +     * @param queueSize the queue size
  +     * @param closeTarget close the underlying target when this target is closed. This
flag
  +     *        has no effect unless the logTarget implements Closeable.
  +     */
  +    public AsyncLogTarget( final LogTarget logTarget, final int queueSize, final boolean
closeTarget )
  +    {
  +        super( logTarget, closeTarget );
           m_logTarget = logTarget;
           m_list = new LinkedList();
           m_queueSize = queueSize;
           open();
       }
  -
  +    
       /**
        * Provide component with ErrorHandler.
        *
  
  
  
  1.1                  avalon-logkit/src/java/org/apache/log/output/AbstractWrappingTarget.java
  
  Index: AbstractWrappingTarget.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1997-2003 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 "Jakarta", "Avalon", 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 (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/>.
   */
  package org.apache.log.output;
  
  import java.util.LinkedList;
  import org.apache.log.ErrorAware;
  import org.apache.log.ErrorHandler;
  import org.apache.log.LogEvent;
  import org.apache.log.LogTarget;
  import org.apache.log.util.Closeable;
  
  /**
   * Abstract base class for targets that wrap other targets. The class
   * provides functionality for optionally closing a wrapped target that
   * implements  <code>org.apache.log.util.Closeable</code>.
   *
   * @see org.apache.log.util.Closeable
   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
   */
  public abstract class AbstractWrappingTarget
      extends AbstractTarget
  {
      private final boolean m_closeWrapped;
      private final LogTarget m_wrappedLogTarget;
      
      /**
       * Creation of a new wrapping log target.
       * 
       * @param logTarget the underlying target
       * @param closeWrappedTarget boolean flag indicating whether the wrapped log target
       *        should be closed when this target is closed. Note: This flag has no
       *        effect unless the underlying target implements <code>org.apache.log.util.Closeable</code>.
       * @see org.apache.log.util.Closeable
       */
      public AbstractWrappingTarget( final LogTarget logTarget, final boolean closeWrappedTarget
)
      {
          m_wrappedLogTarget = logTarget;
          m_closeWrapped = closeWrappedTarget;
      }
      
      /**
       * Creation of a new wrapping log target. The underlying log target will
       * <b>not</b> be closed when this target is closed.
       * 
       * @param logTarget the underlying target
       */
      public AbstractWrappingTarget( final LogTarget logTarget )
      {
          this( logTarget, false );
      }
      
      public void close()
      {
          super.close();
          
          if( m_closeWrapped && m_wrappedLogTarget instanceof Closeable )
          {
              ((Closeable) m_wrappedLogTarget).close();
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message