1 17 package org.apache.log.output; 18 19 import org.apache.log.ErrorAware; 20 import org.apache.log.ErrorHandler; 21 import org.apache.log.LogEvent; 22 import org.apache.log.LogTarget; 23 import org.apache.log.util.Closeable; 24 import org.apache.log.util.DefaultErrorHandler; 25 26 32 public abstract class AbstractTarget 33 implements LogTarget, ErrorAware, Closeable 34 { 35 private static final ErrorHandler DEFAULT_ERROR_HANDLER = new DefaultErrorHandler(); 36 37 private ErrorHandler m_errorHandler = DEFAULT_ERROR_HANDLER; 39 40 private boolean m_isOpen; 42 43 46 public AbstractTarget() 47 { 48 } 49 50 54 public AbstractTarget( final ErrorHandler errorHandler ) 55 { 56 if( errorHandler == null ) 57 { 58 throw new NullPointerException ( "errorHandler specified cannot be null" ); 59 } 60 setErrorHandler( errorHandler ); 61 } 62 63 68 public synchronized void setErrorHandler( final ErrorHandler errorHandler ) 69 { 70 m_errorHandler = errorHandler; 71 } 72 73 77 protected synchronized boolean isOpen() 78 { 79 return m_isOpen; 80 } 81 82 85 protected synchronized void open() 86 { 87 if( !isOpen() ) 88 { 89 m_isOpen = true; 90 } 91 } 92 93 98 public synchronized void processEvent( final LogEvent event ) 99 { 100 if( !isOpen() ) 101 { 102 getErrorHandler().error( "Writing event to closed stream.", null, event ); 103 return; 104 } 105 106 try 107 { 108 doProcessEvent( event ); 109 } 110 catch( final Throwable throwable ) 111 { 112 getErrorHandler().error( "Unknown error writing event.", throwable, event ); 113 } 114 } 115 116 123 protected abstract void doProcessEvent( LogEvent event ) 124 throws Exception ; 125 126 131 public synchronized void close() 132 { 133 if( isOpen() ) 134 { 135 m_isOpen = false; 136 } 137 } 138 139 144 protected final ErrorHandler getErrorHandler() 145 { 146 return m_errorHandler; 147 } 148 149 } 150 | Popular Tags |