KickJava   Java API By Example, From Geeks To Geeks.

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


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.LogEvent;
20 import org.apache.log.format.Formatter;
21
22 /** Abstract output target.
23  *
24  * Any new output target that is writing to a single connected
25  * resource should extend this class directly or indirectly.
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @author Peter Donald
29  */

30 public abstract class AbstractOutputTarget
31     extends AbstractTarget
32 {
33     /** Formatter for target. */
34     private Formatter m_formatter;
35
36     /** Parameterless constructor. */
37     public AbstractOutputTarget()
38     {
39     }
40
41     /**
42      * Creation of a new abstract output target instance.
43      * @param formatter the formatter to apply
44      */

45     public AbstractOutputTarget( final Formatter formatter )
46     {
47         m_formatter = formatter;
48     }
49
50     /** Returns the Formatter.
51      */

52     protected Formatter getFormatter()
53     {
54         return m_formatter;
55     }
56     
57     /**
58      * Abstract method to write data.
59      *
60      * @param data the data to be output
61      */

62     protected void write( final String JavaDoc data )
63     {
64     }
65
66     /**
67      * Process a log event.
68      * @param event the event to process
69      */

70     protected void doProcessEvent( LogEvent event )
71     {
72         final String JavaDoc data = format( event );
73         write( data );
74     }
75
76     /**
77      * Startup log session.
78      *
79      */

80     protected synchronized void open()
81     {
82         if( !isOpen() )
83         {
84             super.open();
85             writeHead();
86         }
87     }
88
89     /**
90      * Shutdown target.
91      * Attempting to write to target after close() will cause errors to be logged.
92      *
93      */

94     public synchronized void close()
95     {
96         if( isOpen() )
97         {
98             writeTail();
99             super.close();
100         }
101     }
102
103     /**
104      * Helper method to format an event into a string, using the formatter if available.
105      *
106      * @param event the LogEvent
107      * @return the formatted string
108      */

109     private String JavaDoc format( final LogEvent event )
110     {
111         if( null != m_formatter )
112         {
113             return m_formatter.format( event );
114         }
115         else
116         {
117             return event.toString();
118         }
119     }
120
121     /**
122      * Helper method to write out log head.
123      * The head initiates a session of logging.
124      */

125     private void writeHead()
126     {
127         if( !isOpen() )
128         {
129             return;
130         }
131
132         final String JavaDoc head = getHead();
133         if( null != head )
134         {
135             write( head );
136         }
137     }
138
139     /**
140      * Helper method to write out log tail.
141      * The tail completes a session of logging.
142      */

143     private void writeTail()
144     {
145         if( !isOpen() )
146         {
147             return;
148         }
149
150         final String JavaDoc tail = getTail();
151         if( null != tail )
152         {
153             write( tail );
154         }
155     }
156
157     /**
158      * Helper method to retrieve head for log session.
159      * TODO: Extract from formatter
160      *
161      * @return the head string
162      */

163     private String JavaDoc getHead()
164     {
165         return null;
166     }
167
168     /**
169      * Helper method to retrieve tail for log session.
170      * TODO: Extract from formatter
171      *
172      * @return the head string
173      */

174     private String JavaDoc getTail()
175     {
176         return null;
177     }
178 }
179
Popular Tags