1 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 20 public abstract class AbstractOutputTarget 21 extends AbstractTarget { 22 25 private Formatter m_formatter; 26 27 30 public AbstractOutputTarget() { 31 } 32 33 public AbstractOutputTarget(final Formatter formatter) { 34 m_formatter = formatter; 35 } 36 37 44 public synchronized Formatter getFormatter() { 45 return m_formatter; 46 } 47 48 54 public synchronized void setFormatter(final Formatter formatter) { 55 writeTail(); 56 m_formatter = formatter; 57 writeHead(); 58 } 59 60 65 protected void write(final String data) { 66 output(data); 67 } 68 69 76 protected void output(final String data) { 77 } 78 79 protected void doProcessEvent(LogEvent event) { 80 final String data = format(event); 81 write(data); 82 } 83 84 87 protected synchronized void open() { 88 if (!isOpen()) { 89 super.open(); 90 writeHead(); 91 } 92 } 93 94 98 public synchronized void close() { 99 if (isOpen()) { 100 writeTail(); 101 super.close(); 102 } 103 } 104 105 111 private String 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 124 private void writeHead() { 125 if (!isOpen()) return; 126 127 final String head = getHead(); 128 if (null != head) { 129 write(head); 130 } 131 } 132 133 137 private void writeTail() { 138 if (!isOpen()) return; 139 140 final String tail = getTail(); 141 if (null != tail) { 142 write(tail); 143 } 144 } 145 146 152 private String getHead() { 153 return null; 154 } 155 156 162 private String getTail() { 163 return null; 164 } 165 } 166 | Popular Tags |