KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j.Category;
29 import org.apache.log4j.Priority;
30
31 /**
32  * A subclass of PrintWriter that redirects its output to a log4j Category. <p>
33  *
34  * This class is used to have something to give api methods that require a
35  * PrintWriter for logging. JBoss-owned classes of this nature generally ignore
36  * the PrintWriter and do their own log4j logging.
37  *
38  * @deprecated Use {@link LoggerWriter} instead.
39  *
40  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
41  * .
42  * @created August 19, 2001
43  * @version $$
44  */

45 public class CategoryWriter
46        extends PrintWriter JavaDoc {
47    private Category category;
48    private Priority priority;
49    private boolean inWrite;
50    private boolean issuedWarning;
51
52    /**
53     * Redirect logging to the indicated category using Priority.INFO
54     *
55     * @param category Description of Parameter
56     */

57    public CategoryWriter( final Category category ) {
58       this( category, Priority.INFO );
59    }
60
61    /**
62     * Redirect logging to the indicated category using the given priority. The
63     * ps is simply passed to super but is not used.
64     *
65     * @param category Description of Parameter
66     * @param priority Description of Parameter
67     */

68    public CategoryWriter( final Category category,
69          final Priority priority ) {
70       super( new InternalCategoryWriter( category, priority ), true );
71    }
72
73    /**
74     * @created August 19, 2001
75     */

76    static class InternalCategoryWriter extends Writer JavaDoc {
77       private Category category;
78       private Priority priority;
79       private boolean closed;
80
81       public InternalCategoryWriter( final Category category, final Priority priority ) {
82          lock = category;
83          //synchronize on this category
84
this.category = category;
85          this.priority = priority;
86       }
87
88       public void write( char[] cbuf, int off, int len )
89          throws IOException JavaDoc {
90          if ( closed ) {
91             throw new IOException JavaDoc( "Called write on closed Writer" );
92          }
93          // Remove the end of line chars
94
while ( len > 0 && ( cbuf[len - 1] == '\n' || cbuf[len - 1] == '\r' ) ) {
95             len--;
96          }
97          if ( len > 0 ) {
98             category.log( priority, String.copyValueOf( cbuf, off, len ) );
99          }
100       }
101
102
103       public void flush()
104          throws IOException JavaDoc {
105          if ( closed ) {
106             throw new IOException JavaDoc( "Called flush on closed Writer" );
107          }
108       }
109
110       public void close() {
111          closed = true;
112       }
113    }
114
115 }
116
Popular Tags