You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@excalibur.apache.org by cr...@apache.org on 2004/12/04 19:47:37 UTC

svn commit: r109818 - in excalibur/trunk/framework/impl: . src/java/org/apache/avalon/framework/logger src/test/org/apache/avalon/framework/logger src/test/org/apache/avalon/framework/logger/test

Author: crafterm
Date: Sat Dec  4 10:47:35 2004
New Revision: 109818

URL: http://svn.apache.org/viewcvs?view=rev&rev=109818
Log:
Applied patch from EXLBR-16.

This new class provides a bridge between Logger and OutputStream objects.
It's useful if you have a library that uses an OutputStream or a Writer
destination for output, and you want this to go to a Logger object.

Added test case to validate, and rolled junit version number to be the latest.


Added:
   excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/logger/LoggerAwareOutputStream.java
   excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/
   excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/test/
   excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/test/LoggerAwareOutputStreamTestCase.java
Modified:
   excalibur/trunk/framework/impl/project.xml

Modified: excalibur/trunk/framework/impl/project.xml
Url: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/project.xml?view=diff&rev=109818&p1=excalibur/trunk/framework/impl/project.xml&r1=109817&p2=excalibur/trunk/framework/impl/project.xml&r2=109818
==============================================================================
--- excalibur/trunk/framework/impl/project.xml	(original)
+++ excalibur/trunk/framework/impl/project.xml	Sat Dec  4 10:47:35 2004
@@ -58,7 +58,7 @@
     </dependency>
     <dependency>
       <id>junit</id>
-      <version>3.7</version>
+      <version>3.8.1</version>
     </dependency>
   </dependencies>
 

Added: excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/logger/LoggerAwareOutputStream.java
Url: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/logger/LoggerAwareOutputStream.java?view=auto&rev=109818
==============================================================================
--- (empty file)
+++ excalibur/trunk/framework/impl/src/java/org/apache/avalon/framework/logger/LoggerAwareOutputStream.java	Sat Dec  4 10:47:35 2004
@@ -0,0 +1,116 @@
+/*
+ * Copyright 1997-2004 The Apache Software Foundation
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avalon.framework.logger;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/**
+ * Logger aware output stream, characters written to this {@link OutputStream}
+ * are buffered until a newline character is encountered, or a flush() is called.
+ * 
+ * <p>
+ * Extend to specify the log method that the message should be invoked. eg:
+ * </p>
+ * 
+ * <pre>
+ * setOutputStream( new LoggerAwareOutputStream( getLogger() ) {
+ *     protected void logMessage( String message )
+ *     {
+ *         if ( m_logger.isDebugEnabled() )
+ *         {
+ *             m_logger.debug( message );
+ *         }
+ *     }
+ * } );
+ * </pre>
+ * 
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @version $Revision:$
+ * @since Nov 19, 2004 7:03:50 PM
+ */
+public abstract class LoggerAwareOutputStream extends OutputStream
+{
+    /**
+     * Constructor, creates instance of class.
+     * 
+     * @param logger logger this output stream should use
+     */
+    public LoggerAwareOutputStream( Logger logger ) {
+        m_logger = logger;
+    }
+
+    /**
+     * Writes a byte to the internal buffer. If a newline character is
+     * encountered, then the buffer is sent to the logger.
+     * 
+     * @param b character to write
+     * @throws IOException if an error occurs
+     * @see java.io.OutputStream#write(int)
+     */
+    public void write( int b ) throws IOException
+    {
+        if ( b == '\n' )
+        {
+            final byte[] content = bos.toByteArray();
+            logMessage( new String( content ) );
+            bos.reset();
+            return;
+        }
+
+        bos.write( b );
+    }
+
+    /**
+     * Flushes this output stream, writing any buffered content to the log
+     * 
+     * @throws IOException on error
+     * @see java.io.OutputStream#flush()
+     */
+    public void flush() throws IOException
+    {
+        final byte[] content = bos.toByteArray();
+        logMessage( new String( content ) );
+        bos.reset();
+    }
+
+    /**
+     * Purposely flushes the stream, but doesn't close anything since the logger
+     * is managed by another class.
+     * 
+     * @throws IOException if an IO error occurs
+     * @see java.io.OutputStream#close()
+     */
+    public void close() throws IOException
+    {
+        flush();
+    }
+
+    /**
+     * Writes the message to the log. Subclasses should override this method to
+     * send the message to the log level they require.
+     * 
+     * @param message message to be written
+     */
+    protected abstract void logMessage( String message );
+
+    /** Message buffer */
+    private final ByteArrayOutputStream bos = new ByteArrayOutputStream();
+
+    /** {@link Logger} reference */
+    protected final Logger              m_logger;
+}
\ No newline at end of file

Added: excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/test/LoggerAwareOutputStreamTestCase.java
Url: http://svn.apache.org/viewcvs/excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/test/LoggerAwareOutputStreamTestCase.java?view=auto&rev=109818
==============================================================================
--- (empty file)
+++ excalibur/trunk/framework/impl/src/test/org/apache/avalon/framework/logger/test/LoggerAwareOutputStreamTestCase.java	Sat Dec  4 10:47:35 2004
@@ -0,0 +1,243 @@
+/*
+ * Copyright 1997-2004 The Apache Software Foundation
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avalon.framework.logger.test;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import junit.framework.TestCase;
+
+import org.apache.avalon.framework.logger.Logger;
+import org.apache.avalon.framework.logger.LoggerAwareOutputStream;
+
+/**
+ * Test case for LoggerAwareOutputStream class.
+ * 
+ * @author <a href="mailto:crafterm@apache.org">Marcus Crafter</a>
+ * @since Dec 4, 2004 6:53:48 PM
+ */
+public class LoggerAwareOutputStreamTestCase extends TestCase
+{
+    /**
+     * Tests the logger aware output stream by sending a test string to the
+     * output stream, and checking whether the logger logged the same string. 
+     * 
+     * @throws Exception if an error occurs
+     */
+    public void testLoggerAwareOutputStream() throws Exception
+    {
+        final TestLogger logger = new TestLogger();
+        final String testString = "a test string";
+        
+        final OutputStream os =
+            new LoggerAwareOutputStream( logger ) {
+                protected void logMessage( String message )
+                {
+                    m_logger.debug( message );
+                }
+            };
+
+        final OutputStreamWriter writer = new OutputStreamWriter(os);
+        writer.write(testString, 0, testString.length());
+        writer.close();
+        final String message = logger.getDebugMessage();
+
+        // check that string printed to the output stream is the same in the log
+        assertEquals("Logged message doesn't equal source string", testString, message);
+
+    }
+
+    /**
+     * Based on NullLogger. 
+     */
+    private class TestLogger implements Logger
+    {
+        /**
+         * Creates a new <code>NullLogger</code>.
+         */
+        public TestLogger()
+        {
+        }
+
+        /**
+         * Stores debug message inside of this test logger.
+         *
+         * @param message ignored
+         */
+        public void debug( String message )
+        {
+            this.message = message;
+        }
+        
+        private String message;
+        
+        /**
+         * Get the saved message from the last call to debug
+         * @return
+         */
+        public String getDebugMessage() 
+        {
+            return message;
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         * @param throwable ignored
+         */
+        public void debug( String message, Throwable throwable )
+        {
+        }
+
+        /**
+         * Always returns true
+         *
+         * @return <code>true</code>
+         */
+        public boolean isDebugEnabled()
+        {
+            return true;
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         */
+        public void info( String message )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         * @param throwable ignored
+         */
+        public void info( String message, Throwable throwable )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @return <code>false</code>
+         */
+        public boolean isInfoEnabled()
+        {
+            return false;
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         */
+        public void warn( String message )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         * @param throwable ignored
+         */
+        public void warn( String message, Throwable throwable )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @return <code>false</code>
+         */
+        public boolean isWarnEnabled()
+        {
+            return false;
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         */
+        public void error( String message )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         * @param throwable ignored
+         */
+        public void error( String message, Throwable throwable )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @return <code>false</code>
+         */
+        public boolean isErrorEnabled()
+        {
+            return false;
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         */
+        public void fatalError( String message )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @param message ignored
+         * @param throwable ignored
+         */
+        public void fatalError( String message, Throwable throwable )
+        {
+        }
+
+        /**
+         * No-op.
+         *
+         * @return <code>false</code>
+         */
+        public boolean isFatalErrorEnabled()
+        {
+            return false;
+        }
+
+        /**
+         * Returns this <code>NullLogger</code>.
+         *
+         * @param name ignored
+         * @return this <code>NullLogger</code>
+         */
+        public Logger getChildLogger( String name )
+        {
+            return this;
+        }
+    }
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: scm-unsubscribe@excalibur.apache.org
For additional commands, e-mail: scm-help@excalibur.apache.org