KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > log > output > AbstractOutputTarget


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8 package org.jivesoftware.util.log.output;
9
10 import org.jivesoftware.util.log.LogEvent;
11 import org.jivesoftware.util.log.format.Formatter;
12
13 /**
14  * Abstract output target.
15  * Any new output target that is writing to a single connected
16  * resource should extend this class directly or indirectly.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public abstract class AbstractOutputTarget
21         extends AbstractTarget {
22     /**
23      * Formatter for target.
24      */

25     private Formatter m_formatter;
26
27     /**
28      * Parameterless constructor.
29      */

30     public AbstractOutputTarget() {
31     }
32
33     public AbstractOutputTarget(final Formatter formatter) {
34         m_formatter = formatter;
35     }
36
37     /**
38      * Retrieve the associated formatter.
39      *
40      * @return the formatter
41      * @deprecated Access to formatter is not advised and this method will be removed
42      * in future iterations. It remains only for backwards compatability.
43      */

44     public synchronized Formatter getFormatter() {
45         return m_formatter;
46     }
47
48     /**
49      * Set the formatter.
50      *
51      * @param formatter the formatter
52      * @deprecated In future this method will become protected access.
53      */

54     public synchronized void setFormatter(final Formatter formatter) {
55         writeTail();
56         m_formatter = formatter;
57         writeHead();
58     }
59
60     /**
61      * Abstract method to send data.
62      *
63      * @param data the data to be output
64      */

65     protected void write(final String JavaDoc data) {
66         output(data);
67     }
68
69     /**
70      * Abstract method that will output event.
71      *
72      * @param data the data to be output
73      * @deprecated User should overide send() instead of output(). Output exists
74      * for backwards compatability and will be removed in future.
75      */

76     protected void output(final String JavaDoc data) {
77     }
78
79     protected void doProcessEvent(LogEvent event) {
80         final String JavaDoc data = format(event);
81         write(data);
82     }
83
84     /**
85      * Startup log session.
86      */

87     protected synchronized void open() {
88         if (!isOpen()) {
89             super.open();
90             writeHead();
91         }
92     }
93
94     /**
95      * Shutdown target.
96      * Attempting to send to target after close() will cause errors to be logged.
97      */

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

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

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

137     private void writeTail() {
138         if (!isOpen()) return;
139
140         final String JavaDoc tail = getTail();
141         if (null != tail) {
142             write(tail);
143         }
144     }
145
146     /**
147      * Helper method to retrieve head for log session.
148      * TODO: Extract from formatter
149      *
150      * @return the head string
151      */

152     private String JavaDoc getHead() {
153         return null;
154     }
155
156     /**
157      * Helper method to retrieve tail for log session.
158      * TODO: Extract from formatter
159      *
160      * @return the head string
161      */

162     private String JavaDoc getTail() {
163         return null;
164     }
165 }
166
Popular Tags