KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > output > AbstractTarget


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
27  * Abstract target.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @author Peter Donald
31  */

32 public abstract class AbstractTarget
33     implements LogTarget, ErrorAware, Closeable
34 {
35     private static final ErrorHandler DEFAULT_ERROR_HANDLER = new DefaultErrorHandler();
36
37     ///ErrorHandler used by target to delegate Error handling
38
private ErrorHandler m_errorHandler = DEFAULT_ERROR_HANDLER;
39
40     ///Flag indicating that log session is finished (aka target has been closed)
41
private boolean m_isOpen;
42
43     /**
44      * AbstractTarget constructor.
45      */

46     public AbstractTarget()
47     {
48     }
49
50     /**
51      * AbstractTarget constructor.
52      * @param errorHandler the error handler
53      */

54     public AbstractTarget( final ErrorHandler errorHandler )
55     {
56         if( errorHandler == null )
57         {
58             throw new NullPointerException JavaDoc( "errorHandler specified cannot be null" );
59         }
60         setErrorHandler( errorHandler );
61     }
62
63     /**
64      * Provide component with ErrorHandler.
65      *
66      * @param errorHandler the errorHandler
67      */

68     public synchronized void setErrorHandler( final ErrorHandler errorHandler )
69     {
70         m_errorHandler = errorHandler;
71     }
72
73     /**
74      * Return the open state of the target.
75      * @return TRUE if the target is open else FALSE
76      */

77     protected synchronized boolean isOpen()
78     {
79         return m_isOpen;
80     }
81
82     /**
83      * Startup log session.
84      */

85     protected synchronized void open()
86     {
87         if( !isOpen() )
88         {
89             m_isOpen = true;
90         }
91     }
92
93     /**
94      * Process a log event, via formatting and outputting it.
95      *
96      * @param event the log event
97      */

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 JavaDoc throwable )
111         {
112             getErrorHandler().error( "Unknown error writing event.", throwable, event );
113         }
114     }
115
116     /**
117      * Process a log event, via formatting and outputting it.
118      * This should be overidden by subclasses.
119      *
120      * @param event the log event
121      * @exception Exception if an event processing error occurs
122      */

123     protected abstract void doProcessEvent( LogEvent event )
124         throws Exception JavaDoc;
125
126     /**
127      * Shutdown target.
128      * Attempting to write to target after close() will cause errors to be logged.
129      *
130      */

131     public synchronized void close()
132     {
133         if( isOpen() )
134         {
135             m_isOpen = false;
136         }
137     }
138
139     /**
140      * Helper method to retrieve ErrorHandler for subclasses.
141      *
142      * @return the ErrorHandler
143      */

144     protected final ErrorHandler getErrorHandler()
145     {
146         return m_errorHandler;
147     }
148
149 }
150
Popular Tags