james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Harmeet Bedi" <harm...@kodemuse.com>
Subject Re: Easy protocol logging
Date Tue, 03 Dec 2002 19:49:51 GMT
----- Original Message -----
From: "Darrell DeBoer" <darrell@apache.org>
> I'm thinking of a SocketManager/ConnectionManager implementation that
returns
> a logging socket, so that the logging code doesn't need to be in the
actual
> protocol server at all.

This may help

Ability to log input or output stream data.
--------------------
    public static InputStream createSnoopStream(final InputStream inp,
                                                final OutputStream snoop) {
        return new FilterInputStream(inp) {
                public int read() throws IOException {
                    int d = super.read();
                    if ( d != -1 )
                        snoop.write(d);
                    return d;
                }
                public int read(byte[] ba,int offset,int count)
                    throws IOException
                {
                    count = super.read(ba,offset,count);
                    if ( count != -1 )
                        snoop.write(ba,offset,count);
                    return count;
                }
                public void close() throws IOException {
                    super.close();
                    snoop.close();
                }
            };
    }

    public static OutputStream createSnoopStream(final OutputStream out,
                                                final OutputStream snoop) {
        return new FilterOutputStream(out) {
                public void write(int b) throws IOException {
                    super.write(b);
                    snoop.write(b);
                }
                public void flush() throws IOException {
                    super.flush();
                    snoop.flush();
                  }
                public void close() throws IOException {
                    super.close();
                    snoop.close();
                  }
            };
    }
--------------------

JDK 1.3 code to create a create a logging socket
---------------------
    public static Socket createSnoopSocket
        (Socket sock,final OutputStream inpSnoop,final OutputStream
outSnoop)
        throws SocketException
    {
        SocketImpl impl = getImpl(sock);
        return new Socket(impl) {
                public InputStream getInputStream() throws IOException {
                    return
createSnoopStream(super.getInputStream(),inpSnoop);
                }
                public OutputStream getOutputStream() throws IOException {
                    return
createSnoopStream(super.getOutputStream(),outSnoop);
                }
            };
    }
   // this breaks with JDK 1.4
    public static SocketImpl getImpl(Socket sock) {
        SocketImpl impl = null;
        try {
            Field f = (Socket.class).getDeclaredField("impl");
            f.setAccessible(true);
            impl = (SocketImpl)f.get(sock);
        } catch( Exception ex ) {
            throw new RuntimeException(ex.toString());
        }
        return impl;
    }
---------------------------


I use this with a customized and very old version
SocketManager/ConnectionManager.


Another tack to record traffic that I thought was even more elegant was to
have a Socks server that records traffic or a NAT like socket dispatcher.
Most clients can be configured to have a Socks server. One advantage is that
not only can you get mail trace, but also other protocols like IM traffic.
:-) Another advantage is that the logging is handled by a separate process.

Harmeet


--
To unsubscribe, e-mail:   <mailto:james-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:james-dev-help@jakarta.apache.org>


Mime
View raw message