01: /******************************************************************************
02: * JBoss, a division of Red Hat *
03: * Copyright 2006, Red Hat Middleware, LLC, and individual *
04: * contributors as indicated by the @authors tag. See the *
05: * copyright.txt in the distribution for a full listing of *
06: * individual contributors. *
07: * *
08: * This is free software; you can redistribute it and/or modify it *
09: * under the terms of the GNU Lesser General Public License as *
10: * published by the Free Software Foundation; either version 2.1 of *
11: * the License, or (at your option) any later version. *
12: * *
13: * This software is distributed in the hope that it will be useful, *
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16: * Lesser General Public License for more details. *
17: * *
18: * You should have received a copy of the GNU Lesser General Public *
19: * License along with this software; if not, write to the Free *
20: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
21: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
22: ******************************************************************************/package org.jboss.portal.migration.helper;
23:
24: import org.apache.log4j.Level;
25: import org.apache.log4j.Logger;
26:
27: import java.io.IOException;
28: import java.io.Writer;
29:
30: /**
31: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
32: * @version $Revision: 8784 $
33: */
34: public class Log4JWriter extends Writer {
35:
36: /** The open/closed status. */
37: private boolean open;
38:
39: /** The logger. */
40: private Logger log;
41:
42: /** The logging level. */
43: private Level level;
44:
45: /** @throws IllegalArgumentException if log or level is null */
46: public Log4JWriter(Logger log, Level level)
47: throws IllegalArgumentException {
48: if (log == null) {
49: throw new IllegalArgumentException("No logger provided");
50: }
51: if (level == null) {
52: throw new IllegalArgumentException("No level provided");
53: }
54: this .open = false;
55: this .log = log;
56: this .level = level;
57: }
58:
59: public void write(char cbuf[], int off, int len) throws IOException {
60: if (open) {
61: log.log(level, new String(cbuf, off, len));
62: } else {
63: throw new IOException("Stream closed");
64: }
65: }
66:
67: public void flush() throws IOException {
68: }
69:
70: public void close() throws IOException {
71: open = false;
72: }
73: }
|