You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Anil <an...@wonderschools.com> on 2001/04/02 12:00:42 UTC

I have got a query regarding PrintWriter,

I have been trying to intercept all the System.out and System.err request
given in the tomcat. Here is the excerpts of the Code.

I wrote a class WSPrintStream that extends PrintStream and overided the
commonly used println(..) statements.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>
/*
 * WSPrintStream.java
 *
 * Created on March 31, 2001, 1:13 PM
 */

package org.weume;

/**
 *
 * @author  anil oommen
 * @version
 */
public class WSPrintStream extends java.io.PrintStream {

    /** Holds value of property outputText. */
    private java.lang.String outputText;
    private java.lang.StringBuffer new_outputText;
    /** Utility field used by bound properties. */
    private java.beans.PropertyChangeSupport propertyChangeSupport =  new
java.beans.PropertyChangeSupport (this);




    /** Creates new WSPrintStream */
    public WSPrintStream(java.io.OutputStream out){
        super(out);
        outputText = new java.lang.String();
        new_outputText = new java.lang.StringBuffer();
    }


    public void print(boolean param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void print(char param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void print(char[] values) {
        new_outputText.append(values);
        updateOutputText();
    }

    public void print(double param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void print(float param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void print(int param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void print(java.lang.Object obj) {
        new_outputText.append(obj);
        updateOutputText();
    }

    public void print(java.lang.String str) {
        new_outputText.append(str);
        updateOutputText();
    }

    public void print(long param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void println() {
    }

    public void println(boolean param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void println(char param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void println(char[] values) {
        new_outputText.append(values);
        updateOutputText();
    }

    public void println(double param) {
        new_outputText.append(param);
        updateOutputText();

    }

    public void println(float param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void println(int param) {
        new_outputText.append(param);
        updateOutputText();
    }

    public void println(java.lang.Object obj) {
        new_outputText.append(obj);
        updateOutputText();
    }

    public void println(java.lang.String str) {
        new_outputText.append(str + "\n" );
        updateOutputText();
    }

    public void println(long param) {
        new_outputText.append(param);
        updateOutputText();
    }

    /** Add a PropertyChangeListener to the listener list.
     * @param l The listener to add.
 */
    public void addPropertyChangeListener(java.beans.PropertyChangeListener
l) {
        propertyChangeSupport.addPropertyChangeListener (l);
    }

    /** Removes a PropertyChangeListener from the listener list.
     * @param l The listener to remove.
 */
    public void
removePropertyChangeListener(java.beans.PropertyChangeListener l) {
        propertyChangeSupport.removePropertyChangeListener (l);
    }

    /** Getter for property outputText.
     * @return Value of property outputText.
 */
    public java.lang.String getOutputText() {
        return outputText;
    }

    /** Setter for property outputText.
     * @param outputText New value of property outputText.
 */
    public synchronized void updateOutputText() {
        String old_outputText = this.outputText;
        this.outputText = this.outputText + new_outputText.toString();
        this.new_outputText = new java.lang.StringBuffer();
        propertyChangeSupport.firePropertyChange ("outputText",
old_outputText, outputText);
    }

}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>


now before the execution of tomcat server I added this code

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
       oldSystem_out = System.out;
        WSPrintStream wsp = new WSPrintStream(System.out);
       System.setOut(wsp);
       wsp.addPropertyChangeListener(this);

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Now I can catch all the println statements in the
>>>>>>>>public void propertyChange(java.beans.PropertyChangeEvent evt){
when ever the System.out.println(....) is used in the System

But,,


This is a problem I  encountered

If I use
>>>>>>>>>>
java.io.PrintStream pw = new java.io.PrintStream(System.out);
pw.println("helo try reading this");
>>>>>>>

I am not able to catch the message from going to the default out put stream.
i.e my WSPrintStream does not get invoked.

It would be nice if somebody told me as to what I am doing wrong. And what
would be the best way to do it.
I have been trying to do GUI for TOMCAT and want to redirect all the Stream
output to my display without editing or modifying any of the tomcat original
source.


Thanx in Advance,

Anil Oommen
Technical Member,
Banyanlabs , Chennai India
anil@banyanlabs.com
anil@wonderschools.com









RE: I have got a query regarding PrintWriter,

Posted by Anil <an...@wonderschools.com>.
Thank you, Dr Mel Martinez,
	I have tried it and it did work.

I believe you have asked aw why I have gone for this solution. Well I was
trying to capture all the messages sent to the Std.output stream in tomcat.
The code has snippets like
>		java.io.PrintStream pw = new
>           java.io.PrintStream(System.out);
>           pw.println("helo try reading this");

instead of

>		System.out.println("helo try reading this");


and I just could not capture them.

So this seemed the correct solution.



Regards,

Anil Oommen
Technical Member
Banyanlabs ,  India
anil@banyanlabs.com
anil@wonderschools.com




Re: I have got a query regarding PrintWriter,

Posted by Mel Martinez <me...@yahoo.com>.
--- Anil <an...@wonderschools.com> wrote:
> I have been trying to intercept all the System.out
> and System.err request
> given in the tomcat. Here is the excerpts of the
> Code.
> 
> I wrote a class WSPrintStream that extends
> PrintStream and overided the
> commonly used println(..) statements.
> 
>

[definition of WSPrintStream trimmed except
to note that it is implemented by overriding
each println() method to fire a PropertyChangeEvent.]
 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> >>>>>>>
> 
> now before the execution of tomcat server I added
> this code
> 
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> oldSystem_out = System.out;
> WSPrintStream wsp = new
> WSPrintStream(System.out);
> System.setOut(wsp);
> wsp.addPropertyChangeListener(this);
> 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
> Now I can catch all the println statements in the
> >>>>>>>public void  
> propertyChange(
> java.beans.PropertyChangeEvent evt){
> when ever the System.out.println(....) is used in
the
> System
> 
> But,,
>
> 
> This is a problem I  encountered
> If I use
> >>>>>>>>>> java.io.PrintStream pw = new 
>               java.io.PrintStream(System.out);
>            pw.println("helo try reading this");
>>>>>>

Umm... just curious, but why not use 
    System.out.println("helo...");
instead of wrapping it here?  Since (according
to your use of System.setOut(), then the following
should
be true:

    System.out instanceof WSPrintStream
    
and System.out.println() -> WSPrintStream.println()

this is not the real, problem though (see below).

> I am not able to catch the message from going to the
> default out put 
> stream.
> i.e my WSPrintStream does not get invoked.
> 


Your WSPrintStream subclass needs to override the 
write(byte b) method.  When you wrap one OutputStream 
around another, the data is passed from one stream to 
another through the OutputStream write() method.  
The println() method is not part of OutputStream
interface 
and so as far as the wrapping PrintStream object is 
concerned they (your println methods) do not exist.

I hope this helps.

Cheers,

Dr. Mel Martinez
G1440, Inc.



__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/?.refer=text