1 17 package org.apache.log.output.io; 18 19 import java.io.IOException ; 20 import java.io.Writer ; 21 import org.apache.log.format.Formatter; 22 import org.apache.log.output.AbstractOutputTarget; 23 24 29 public class WriterTarget 30 extends AbstractOutputTarget 31 { 32 private Writer m_output; 33 34 40 public WriterTarget( final Writer writer, final Formatter formatter ) 41 { 42 super( formatter ); 43 44 if( null != writer ) 45 { 46 setWriter( writer ); 47 open(); 48 } 49 } 50 51 57 protected synchronized void setWriter( final Writer writer ) 58 { 59 if( null == writer ) 60 { 61 throw new NullPointerException ( "writer property must not be null" ); 62 } 63 64 m_output = writer; 65 } 66 67 72 protected void write( final String data ) 73 { 74 try 75 { 76 m_output.write( data ); 77 m_output.flush(); 78 } 79 catch( final IOException ioe ) 80 { 81 getErrorHandler().error( "Caught an IOException", ioe, null ); 82 } 83 } 84 85 89 public synchronized void close() 90 { 91 super.close(); 92 shutdownWriter(); 93 } 94 95 98 protected synchronized void shutdownWriter() 99 { 100 final Writer writer = m_output; 101 m_output = null; 102 103 try 104 { 105 if( null != writer ) 106 { 107 writer.close(); 108 } 109 } 110 catch( final IOException ioe ) 111 { 112 getErrorHandler().error( "Error closing Writer", ioe, null ); 113 } 114 } 115 } 116 | Popular Tags |