001: /*
002: * @(#)AbstractWriterServer.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Part of the GroboUtils package at:
009: * http://groboutils.sourceforge.net
010: *
011: * Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * The above copyright notice and this permission notice shall be included in
019: * all copies or substantial portions of the Software.
020: *
021: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
022: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
024: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
025: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
026: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
027: * DEALINGS IN THE SOFTWARE.
028: */
029: package net.sourceforge.groboutils.autodoc.v1.testserver;
030:
031: import java.io.IOException;
032: import java.io.Writer;
033:
034: import org.apache.log4j.Logger;
035:
036: /**
037: * An interface which corresponds to a part of the framework that knows how
038: * to deal with the framework's <tt>TestData</tt>. It may directly deal with
039: * the data, or pass it off to a remote server.
040: *
041: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
042: * @version $Date: 2003/02/10 22:52:13 $
043: * @since March 30, 2002
044: */
045: public abstract class AbstractWriterServer implements Server {
046: private static final Logger LOG = Logger
047: .getLogger(AbstractWriterServer.class);
048:
049: /**
050: * Receives <tt>td</tt> from a Monitor and handles it in an implementation
051: * specific manner.
052: *
053: * @param td the data to deal with.
054: */
055: public void addTestData(TestData td) {
056: if (td == null) {
057: // allow null aruments: fail gracefully.
058: LOG.warn("addTestData() received null TestData.");
059: return;
060: }
061:
062: // Write the result with all encountered logs.
063: Writer w;
064: try {
065: w = openOutput(td);
066: } catch (IOException e) {
067: LOG.error("Problem opening output.", e);
068:
069: // don't do any writing since the writer could not be opened.
070: return;
071: }
072:
073: try {
074: writeTestData(td, w);
075: } catch (IOException e) {
076: LOG.error("Problem writing to output.", e);
077: } finally {
078: // no matter what the write result may be, close the
079: // output at the end.
080: try {
081: closeOutput(w);
082: } catch (IOException e) {
083: LOG.error("Problem closing output.", e);
084: }
085: }
086: }
087:
088: /**
089: * Write the BugTestResult with all the result's encountered BugTestLog
090: * events.
091: */
092: protected abstract void writeTestData(TestData td, Writer w)
093: throws IOException;
094:
095: /**
096: * Open a writer stream. This will be done once per result, so log-like
097: * actions may need to append to the previous results.
098: */
099: protected abstract Writer openOutput(TestData td)
100: throws IOException;
101:
102: /**
103: * Close the given Writer, which was opened in the <tt>openOutput()</tt>
104: * method. Since nearly all implementations will be the same, the
105: * default implementation simply executes <code>w.close()</code>.
106: */
107: protected void closeOutput(Writer w) throws IOException {
108: w.close();
109: }
110: }
|