KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > util > LoggerPluginWriter


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.logging.util;
23
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import org.jboss.logging.LoggerPlugin;
29
30 /** A subclass of PrintWriter that redirects its output to a LoggerPlugin at
31  * INFO level. The only usecase for this is legacy java apis which require
32  * integration with the logging layer through a Writer.
33  *
34  * @author David Jencks
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 1958 $
37  */

38 public class LoggerPluginWriter
39    extends PrintWriter JavaDoc
40 {
41    /**
42     *
43     * @param logger the logging plugin used to write messages
44     */

45    public LoggerPluginWriter(final LoggerPlugin logger)
46    {
47       super(new PluginWriter(logger), true);
48    }
49
50    static class PluginWriter extends Writer JavaDoc
51    {
52       private LoggerPlugin logger;
53       private boolean closed;
54
55       public PluginWriter(final LoggerPlugin logger)
56       {
57          lock = logger;
58          this.logger = logger;
59       }
60
61       public void write(char[] cbuf, int off, int len)
62          throws IOException JavaDoc
63       {
64          if (closed)
65          {
66             throw new IOException JavaDoc("Called write on closed Writer");
67          }
68          // Remove the end of line chars
69
while (len > 0 && (cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r'))
70          {
71             len--;
72          }
73          if (len > 0)
74          {
75             logger.info(String.copyValueOf(cbuf, off, len));
76          }
77       }
78
79       public void flush()
80          throws IOException JavaDoc
81       {
82          if (closed)
83          {
84             throw new IOException JavaDoc("Called flush on closed Writer");
85          }
86       }
87
88       public void close()
89       {
90          closed = true;
91       }
92    }
93
94 }
95
Popular Tags