Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Wicket's serialization test

I'm working on a large Java application that uses Wicket 1.5 together with Hibernate / JPA 2. Wicket has a standard rule that objects stored in the session must implement Serializable. We have an extra rule that JPA managed objects must not be stored in the session. Instead, JPA managed objects are loaded on each request through detachable models.

To complicate matters, it is legitimate to store an Entity object in the session provided that it has not been persisted yet. As a result, some of our classes already implement Serializable.

If I wanted to extend Wicket's serialization test to detect objects owned by JPA, how would I go about it? Is this possible without a local fork of Wicket?

like image 731
Adrian Cox Avatar asked Nov 30 '11 08:11

Adrian Cox


2 Answers

At my $dayjob we use something what you describe, and what I have presented at several meetups (see slide 23 and on). You don't have to fork Wicket for that.

Basically what you do is copy the serializer checker code and modify it to include your check as well as checking for serialization errors. Then in the last phase of the request cycle you run your own serializer checker on the affected pages.

The check we created checks for our common base class, and wether or not the entity has ben persisted. If so, we fail the request. Additionally we have an Ajax callback in our base page that checks a session attribute to see if there was a serialization error. If so, we redirect to the error page with the serialization failure, to ensure that developers don't ignore the entity in page hierarchy.

Here's the meat of our checker (the check method rewritten from Wicket's serializer check):

private void check(Object obj)
{
    if (obj == null || obj.getClass().isAnnotationPresent(Deprecated.class)
        || obj.getClass().isAnnotationPresent(SkipClass.class))
    {
        return;
    }

    Class< ? > cls = obj.getClass();
    nameStack.add(simpleName);
    traceStack.add(new TraceSlot(obj, fieldDescription));

    if (!(obj instanceof Serializable) && (!Proxy.isProxyClass(cls)))
    {
        throw new WicketNotSerializableException(toPrettyPrintedStack(obj.getClass().getName())
            .toString(), exception);
    }
    if (obj instanceof IdObject)
    {
        Serializable id = ((IdObject) obj).getIdAsSerializable();
        if (id != null && !(id instanceof Long && ((Long) id) <= 0))
        {
            throw new WicketContainsEntityException(toPrettyPrintedStack(
                obj.getClass().getName()).toString(), exception);
        }
    }
    if (obj instanceof LoadableDetachableModel)
    {
        LoadableDetachableModel< ? > ldm = (LoadableDetachableModel< ? >) obj;
        if (ldm.isAttached())
        {
            throw new WicketContainsAttachedLDMException(toPrettyPrintedStack(
                obj.getClass().getName()).toString(), exception);
        }
    }

For Wicket 1.5 we created our own PageStoreManager that performs these checks (and a lot of other things, like enabling a server side browsing history for our users). We provided our own RequestAdapter by overriding PageStoreManager#newRequestAdapter(IPageManagerContext context) and doing the serialization check in the adapter:

class DetachCheckingRequestAdapter extends RequestAdapter
{
    public DetachCheckingRequestAdapter(IPageManagerContext context)
    {
        super(context);
    }

    @Override
    protected void storeTouchedPages(List<IManageablePage> touchedPages)
    {
        super.storeTouchedPages(touchedPages);
        if (Application.get().usesDevelopmentConfig())
        {
            for (IManageablePage curPage : touchedPages)
            {
                if (!((Page) curPage).isErrorPage())
                    testDetachedObjects(curPage);
            }
        }
    }

    private void testDetachedObjects(final IManageablePage page)
    {
        try
        {
            NotSerializableException exception = new NotSerializableException();
            EntityAndSerializableChecker checker = new EntityAndSerializableChecker(exception);
            checker.writeObject(page);
        }
        catch (Exception ex)
        {
            log.error("Couldn't test/serialize the Page: " + page + ", error: " + ex);
            Session.get().setDetachException(ex);
        }
    }
}
like image 119
Martijn Dashorst Avatar answered Nov 01 '22 08:11

Martijn Dashorst


For a simpler solution in Wicket 6.3.0+ see https://cwiki.apache.org/confluence/display/WICKET/Serialization+Checker

like image 27
martin-g Avatar answered Nov 01 '22 08:11

martin-g