KickJava   Java API By Example, From Geeks To Geeks.

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


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.ErrorAware;
11 import org.jivesoftware.util.log.ErrorHandler;
12 import org.jivesoftware.util.log.LogEvent;
13 import org.jivesoftware.util.log.LogTarget;
14
15 /**
16  * Abstract target.
17  *
18  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
19  */

20 public abstract class AbstractTarget implements LogTarget, ErrorAware {
21
22     ///ErrorHandler used by target to delegate Error handling
23
private ErrorHandler m_errorHandler;
24
25     ///Flag indicating that log session is finished (aka target has been closed)
26
private boolean m_isOpen;
27
28     /**
29      * Provide component with ErrorHandler.
30      *
31      * @param errorHandler the errorHandler
32      */

33     public synchronized void setErrorHandler(final ErrorHandler errorHandler) {
34         m_errorHandler = errorHandler;
35     }
36
37     protected synchronized boolean isOpen() {
38         return m_isOpen;
39     }
40
41     /**
42      * Startup log session.
43      */

44     protected synchronized void open() {
45         if (!isOpen()) {
46             m_isOpen = true;
47         }
48     }
49
50     /**
51      * Process a log event, via formatting and outputting it.
52      *
53      * @param event the log event
54      */

55     public synchronized void processEvent(final LogEvent event) {
56         if (!isOpen()) {
57             getErrorHandler().error("Writing event to closed stream.", null, event);
58             return;
59         }
60
61         try {
62             doProcessEvent(event);
63         }
64         catch (final Throwable JavaDoc throwable) {
65             getErrorHandler().error("Unknown error writing event.", throwable, event);
66         }
67     }
68
69     /**
70      * Process a log event, via formatting and outputting it.
71      * This should be overidden by subclasses.
72      *
73      * @param event the log event
74      */

75     protected abstract void doProcessEvent(LogEvent event)
76             throws Exception JavaDoc;
77
78     /**
79      * Shutdown target.
80      * Attempting to send to target after close() will cause errors to be logged.
81      */

82     public synchronized void close() {
83         if (isOpen()) {
84             m_isOpen = false;
85         }
86     }
87
88     /**
89      * Helper method to retrieve ErrorHandler for subclasses.
90      *
91      * @return the ErrorHandler
92      */

93     protected final ErrorHandler getErrorHandler() {
94         return m_errorHandler;
95     }
96
97     /**
98      * Helper method to send error messages to error handler.
99      *
100      * @param message the error message
101      * @param throwable the exception if any
102      * @deprecated Use getErrorHandler().error(...) directly
103      */

104     protected final void error(final String JavaDoc message, final Throwable JavaDoc throwable) {
105         getErrorHandler().error(message, throwable, null);
106     }
107 }
108
Popular Tags