Don't bother. Here is it for the archives.

package x;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionAttributeListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
    Listener that complains if you try to put nonserializable elements
    into your session.
*/
public class SessionListener implements HttpSessionAttributeListener  {
    private static Log log = LogFactory.getLog(SessionListener.class);

    public SessionListener() {
    }

    public void attributeAdded(HttpSessionBindingEvent se) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objStream = new ObjectOutputStream(os);
            objStream.writeObject(se.getValue());
        } catch(IOException e) {
            log.warn("Can't serialize attribute[" + se.getName() + "]", e);
        }
    }

    public void attributeRemoved(HttpSessionBindingEvent se) {
    }

    public void attributeReplaced(HttpSessionBindingEvent se) {
    }
}

-Tim


Ian F.Darwin wrote:

The original user was having trouble figuring out which class(es) in their application were causing NotSerializableExceptions.
And, in fact, I was starting to think about the Serializable issue for a client...


And then Tim wrote:

------- Additional Comments From [EMAIL PROTECTED] 2004-10-11 22:36 -------
You can determine which attributes are not serializable by writing your own
HttpSessionAttributeListener and checking if the attribute implements serializable.


http://jakarta.apache.org/tomcat/tomcat-5.0-doc/servletapi/javax/ servlet/http/HttpSessionAttributeListener.html


This is actually a good idea, Tim :-) So I've written one and will polish it up a bit (it goes recursively, first complaining if the object is not serializable, then testing if the object being added is a Collection...).

Since a number of Tomcat users are not Java programmers, I would like to commit this for general use.

But there doesn't seem a good place for it. What would y'all think of creating a package called, say, o.a.c.userdiagnostic or o.a.tomcat.util.userdiagnostic to serve as a place to store diagnostic tools for end-users? Or is there a good place already?

To be maximally useful it wants to be part of what gets shipped with TC, so we can just tell people something like:

You can do this with an HttpSessionAttributeListener; to use Tomcat's default "serializable attributes" diagnostic HttpSessionAttributeListener just add these lines in your web.xml, immediately before the first <servlet> tag:

<listener>
<!-- Used to warn about non-Serializable objects being put in the session. -->
<listener- class>org.apache.catalina.userdiagnostic.SerializableCheckAttributeListe ner
</listener-class>
</listener>


Ian


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to