001: /**
002: * $Id: StreamLogger.java,v 1.6 2006/09/04 11:15:13 mr161608 Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.fabric.util;
014:
015: import java.io.BufferedReader;
016: import java.io.InputStream;
017: import java.io.InputStreamReader;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020: import java.util.logging.LogRecord;
021:
022: import com.sun.portal.log.common.PortalLogger;
023:
024: /**
025: * This class implements the logic for reading an Input Stream
026: * that contains data being pushed from a process output or error streams.
027: * The data is read from the stream and logged using a the logger
028: */
029: class StreamLogger implements Runnable {
030:
031: static final String nl = System.getProperty("line.separator");
032:
033: private InputStreamReader isr = null;
034: private StringBuffer result = null;
035: private static Logger logger = PortalLogger
036: .getLogger(StreamLogger.class);
037: private String sname = null;
038:
039: /**
040: * Constructor for the StreamLogger class
041: *
042: * @param is Input Stream object passed by the caller to be read and logged.
043: * @param streamName A String that will be used as a Tag when logging the
044: * output.
045: * @param storeContent This boolean flag is used by this class to determine
046: * if the content in the Stream should be preserved and
047: * returned as a String. This flag should be used with
048: * caution. If it is set to <B>true</B> and the Stream
049: * contains a lot of data, the size of the StringBuffer
050: * that stores the output will grow very large and can
051: * cause out of memory conditions.
052: */
053: public StreamLogger(InputStream is, String streamName,
054: boolean storeContent) {
055:
056: isr = new InputStreamReader(is);
057: sname = streamName;
058: result = storeContent ? new StringBuffer() : null;
059: }
060:
061: /**
062: * Returns the content in the stream as a String
063: * @return Content of this stream as a String.
064: * Returns a null if storeContent is set to false
065: */
066: public String getContent() {
067: return (result != null) ? result.toString() : null;
068: }
069:
070: /**
071: * Implements the run method of the Runnable interface.
072: * This method reads the InputStream in a Buffered Reader and logs the
073: * each line in the BufferedReader into the Logger object.
074: */
075: public void run() {
076: try {
077:
078: BufferedReader br = new BufferedReader(isr);
079: String str = null;
080:
081: while ((str = br.readLine()) != null) {
082:
083: String[] args0 = new String[] { sname, str };
084: logger.log(Level.FINEST, "PSFB_CSPFU0004", args0);
085:
086: if (result != null) {
087: result.append(str);
088: result.append(nl);
089:
090: }
091:
092: }
093:
094: if (result != null) {
095: result.append(nl);
096: }
097:
098: // Close the Buffered Reader
099: br.close();
100: } catch (Exception e) {
101:
102: if (logger.isLoggable(Level.WARNING)) {
103: LogRecord record = new LogRecord(Level.WARNING,
104: "PSFB_CSPFU0002");
105: record.setParameters(new String[] { sname });
106: record.setThrown(e);
107: record.setLoggerName(logger.getName());
108: logger.log(record);
109: }
110: }
111: }
112: }
|