1 8 package org.jivesoftware.util.log.output.io; 9 10 import org.jivesoftware.util.log.format.Formatter; 11 import org.jivesoftware.util.log.output.AbstractOutputTarget; 12 import java.io.IOException ; 13 import java.io.Writer ; 14 15 20 public class WriterTarget extends AbstractOutputTarget { 21 22 private Writer m_output; 23 24 30 public WriterTarget(final Writer writer, final Formatter formatter) { 31 super(formatter); 32 33 if (null != writer) { 34 setWriter(writer); 35 open(); 36 } 37 } 38 39 45 protected synchronized void setWriter(final Writer writer) { 46 if (null == writer) { 47 throw new NullPointerException ("writer property must not be null"); 48 } 49 50 m_output = writer; 51 } 52 53 58 protected void write(final String data) { 59 try { 60 m_output.write(data); 61 m_output.flush(); 62 } 63 catch (final IOException ioe) { 64 getErrorHandler().error("Caught an IOException", ioe, null); 65 } 66 } 67 68 72 public synchronized void close() { 73 super.close(); 74 shutdownWriter(); 75 } 76 77 80 protected synchronized void shutdownWriter() { 81 final Writer writer = m_output; 82 m_output = null; 83 84 try { 85 if (null != writer) { 86 writer.close(); 87 } 88 } 89 catch (final IOException ioe) { 90 getErrorHandler().error("Error closing Writer", ioe, null); 91 } 92 } 93 } 94 | Popular Tags |