001: /*
002: * Copyright (C) The Apache Software Foundation. All rights reserved.
003: *
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008: package org.jivesoftware.util.log.output;
009:
010: import org.jivesoftware.util.log.LogEvent;
011: import org.jivesoftware.util.log.format.Formatter;
012:
013: /**
014: * Abstract output target.
015: * Any new output target that is writing to a single connected
016: * resource should extend this class directly or indirectly.
017: *
018: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
019: */
020: public abstract class AbstractOutputTarget extends AbstractTarget {
021: /**
022: * Formatter for target.
023: */
024: private Formatter m_formatter;
025:
026: /**
027: * Parameterless constructor.
028: */
029: public AbstractOutputTarget() {
030: }
031:
032: public AbstractOutputTarget(final Formatter formatter) {
033: m_formatter = formatter;
034: }
035:
036: /**
037: * Retrieve the associated formatter.
038: *
039: * @return the formatter
040: * @deprecated Access to formatter is not advised and this method will be removed
041: * in future iterations. It remains only for backwards compatability.
042: */
043: public synchronized Formatter getFormatter() {
044: return m_formatter;
045: }
046:
047: /**
048: * Set the formatter.
049: *
050: * @param formatter the formatter
051: * @deprecated In future this method will become protected access.
052: */
053: public synchronized void setFormatter(final Formatter formatter) {
054: writeTail();
055: m_formatter = formatter;
056: writeHead();
057: }
058:
059: /**
060: * Abstract method to send data.
061: *
062: * @param data the data to be output
063: */
064: protected void write(final String data) {
065: output(data);
066: }
067:
068: /**
069: * Abstract method that will output event.
070: *
071: * @param data the data to be output
072: * @deprecated User should overide send() instead of output(). Output exists
073: * for backwards compatability and will be removed in future.
074: */
075: protected void output(final String data) {
076: }
077:
078: protected void doProcessEvent(LogEvent event) {
079: final String data = format(event);
080: write(data);
081: }
082:
083: /**
084: * Startup log session.
085: */
086: protected synchronized void open() {
087: if (!isOpen()) {
088: super .open();
089: writeHead();
090: }
091: }
092:
093: /**
094: * Shutdown target.
095: * Attempting to send to target after close() will cause errors to be logged.
096: */
097: public synchronized void close() {
098: if (isOpen()) {
099: writeTail();
100: super .close();
101: }
102: }
103:
104: /**
105: * Helper method to format an event into a string, using the formatter if available.
106: *
107: * @param event the LogEvent
108: * @return the formatted string
109: */
110: private String format(final LogEvent event) {
111: if (null != m_formatter) {
112: return m_formatter.format(event);
113: } else {
114: return event.toString();
115: }
116: }
117:
118: /**
119: * Helper method to send out log head.
120: * The head initiates a session of logging.
121: */
122: private void writeHead() {
123: if (!isOpen())
124: return;
125:
126: final String head = getHead();
127: if (null != head) {
128: write(head);
129: }
130: }
131:
132: /**
133: * Helper method to send out log tail.
134: * The tail completes a session of logging.
135: */
136: private void writeTail() {
137: if (!isOpen())
138: return;
139:
140: final String 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 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 getTail() {
163: return null;
164: }
165: }
|