Re: logging outgoing requests from Tomcat to SQL

2009-01-29 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Gergely,

Gergely Paljak wrote:
> And it looks like more wrapping than before christmas, but I have never
> taken a close look on JDBC implementations (at least not this close).
> 
> So, i'd be interested in that *super-sexy *class wrapper generator of yours!
> Also in any experience, pitfalls or sources for manuals if you might have
> some ideas!

As with most super-sexy code (!), it comes without documentation and
without warranty. It is fairly readable, though.

Enjoy!
- -chris

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

import java.lang.reflect.*;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class WrapperGenerator
{
public static void main(String[] args)
throws Exception
{
String className = args[0];

Class clazz = Class.forName(className);

new WrapperGenerator(clazz).generate(System.out);
}

private static String INDENT = "  ";
private final Class _class;
private final String _bareName;
public WrapperGenerator(Class clazz)
{
if(Modifier.isFinal(clazz.getModifiers()))
throw new IllegalArgumentException("Cannot wrap "
   + clazz.getName() + ":"
   + " class is final.");

_class = clazz;

_bareName = getShortName(_class);
}

public void generate(OutputStream out)
throws IOException
{
this.generate(new OutputStreamWriter(out));
}

public void generate(Writer out)
throws IOException
{
PrintWriter pout;

if(out instanceof PrintWriter)
pout = (PrintWriter)out;
else
pout = new PrintWriter(out);

generateHeader(pout);
generateWrapperMethods(pout);
generateFooter(pout);

pout.flush();
}

private void generateHeader(PrintWriter out)
throws IOException
{
// package declaration
out.println("// package ;");
out.println();

// imports
// out.print("import ");
// out.print(_class.getName());
// out.println(";");
// out.println();

// class declaration
out.println("/**");
out.print(" * A wrapper class for ");
out.print(_bareName);
out.println(" objects.");
out.println(" */");

out.print("public ");

if(Modifier.isStrict(_class.getModifiers()))
out.print("strictfp ");

out.print("class ");
out.print(_bareName);
out.println("Wrapper");
out.print(INDENT);

if(Modifier.isInterface(_class.getModifiers()))
out.print("implements ");
else
out.print("extends ");

out.println(_class.getName());
out.println("{");

// wrapped member
out.print(INDENT);
out.println("/**");
out.print(INDENT);
out.print(" * The ");
out.print(_bareName);
out.println(" being wrapped by this class.");
out.print(INDENT);
out.println(" */");

out.print(INDENT);
out.print("private ");
out.print(_class.getName());
out.println(" _wrapped;");
out.println();

out.print(INDENT);
out.println("/**");
out.print(INDENT);
out.print(" * Creates a new ");
out.print(_bareName);
out.print("Wrapper with the specified wrapped ");
out.print(_bareName);
out.println(".");
out.print(INDENT);
out.println(" *");
out.print(INDENT);
out.print(" * @param wrapped The ");
out.print(_bareName);
out.print(" being wrapped by this");
out.print("_bareName");
out.println("Wrapper.");
out.print(INDENT);
out.println(" */");

out.print(INDENT);
out.print("public ");
out.print(_bareName);
out.print("Wrapper(");
out.print(_class.getName());
out.println(" wrapped)");
out.print(INDENT);
out.println("{");
out.print(INDENT);
out.print(INDENT);
out.println("_wrapped = wrapped;");
out.print(INDENT);
out.println("}");
}

private void generateWrapperMethods(PrintWriter out)
throws IOException
{
Method[] methods = _class.getDeclaredMethods();

for(int i=0; i usedArgNames = new HashSet();
ArrayList argNames = new ArrayList();
Class[] argTypes = method.getParameterTypes();
for(int i=0; i i=argNames.iterator(); i.hasNext(); )
{
out.print(i.next());
if(i.hasNext())
out.print(", ");
}
out.println(");");

o

Re: logging outgoing requests from Tomcat to SQL

2009-01-29 Thread Gergely Paljak
Hi Christopher,

thanks for the comment, it's really interesting!

And it looks like more wrapping than before christmas, but I have never
taken a close look on JDBC implementations (at least not this close).

So, i'd be interested in that *super-sexy *class wrapper generator of yours!
Also in any experience, pitfalls or sources for manuals if you might have
some ideas!

Thank you,
Gergely Paljak

2009/1/28 Christopher Schultz :
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> Gergely,
>
> Gergely Paljak wrote:
>> A want to realize a logging system for tomcat that is capable of:
>> - logging the incoming request for Servlets (easy)
>> - and logging the time when the Tomcat response is sent (easy as well)
>
> As you say, these are relatively easy to accomplish.
>
>> - logging the outgoing SQL queries inside Tomcat while maintaining a
mapping
>> between the requested Servlet and the sent SQL queries
>> - logging the incoming SQL query results
>
> I think your best bet is to implement your own DataSource that extends
> Tomcat's built-in datasource (or some other one, if you like). Have your
> DataSource return Connection objects that wrap the ones provided by
> Tomcat's DataSource object.
>
> Your Connection class should produce wrapped Statement classes that log
> the statements as they are executed, and wrapped ResultSet objects that
> do ... something else. Due to the specific JDBC spec requirements about
> how results are read, you may have to settle for only reading those
> values /actually read/ by the application, instead of the entire result
set.
>
> I have implemented JDBC interface wrappers before, and they are
> completely miserable to do (just sooo much plumbing code). I wrote a
> super-sexy class wrapper generator if you're interested in doing
> something like this.
>
> - -chris
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v1.4.9 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iEYEARECAAYFAkmAplsACgkQ9CaO5/Lv0PAySgCfZSTz2qkBq4yZD3HCuvAYDTBb
> 2ygAnR3SMz+fR0Ow1rBF+lJcfjm5YJEU
> =wTg7
> -END PGP SIGNATURE-
>
> -
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


Re: logging outgoing requests from Tomcat to SQL

2009-01-28 Thread Christopher Schultz
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Gergely,

Gergely Paljak wrote:
> A want to realize a logging system for tomcat that is capable of:
> - logging the incoming request for Servlets (easy)
> - and logging the time when the Tomcat response is sent (easy as well)

As you say, these are relatively easy to accomplish.

> - logging the outgoing SQL queries inside Tomcat while maintaining a mapping
> between the requested Servlet and the sent SQL queries
> - logging the incoming SQL query results

I think your best bet is to implement your own DataSource that extends
Tomcat's built-in datasource (or some other one, if you like). Have your
DataSource return Connection objects that wrap the ones provided by
Tomcat's DataSource object.

Your Connection class should produce wrapped Statement classes that log
the statements as they are executed, and wrapped ResultSet objects that
do ... something else. Due to the specific JDBC spec requirements about
how results are read, you may have to settle for only reading those
values /actually read/ by the application, instead of the entire result set.

I have implemented JDBC interface wrappers before, and they are
completely miserable to do (just sooo much plumbing code). I wrote a
super-sexy class wrapper generator if you're interested in doing
something like this.

- -chris
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkmAplsACgkQ9CaO5/Lv0PAySgCfZSTz2qkBq4yZD3HCuvAYDTBb
2ygAnR3SMz+fR0Ow1rBF+lJcfjm5YJEU
=wTg7
-END PGP SIGNATURE-

-
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org



logging outgoing requests from Tomcat to SQL

2009-01-28 Thread Gergely Paljak
Hi,

I have encountered the following challenge regarding which I would like to
ask you!

A want to realize a logging system for tomcat that is capable of:
- logging the incoming request for Servlets (easy)
- logging the outgoing SQL queries inside Tomcat while maintaining a mapping
between the requested Servlet and the sent SQL queries
- logging the incoming SQL query results
- and logging the time when the Tomcat response is sent (easy as well)

I would like to ask for you advice on how to log the SQL part? How to match
the SQL queries with Servlets without modifying the Servlet code? Could it
be realized by a valve? I have taken a look at the valves, but I haven't
found anything useful.

All comments are welcome!

Thank you,
Gergely Paljak