Showing posts with label tapestry. Show all posts
Showing posts with label tapestry. Show all posts

Friday, May 4, 2012

Persisting mutable objects in Tapestry

Since Tapestry makes it easy to persist application state by using the @Persist annotation, I tend to have a habit of using mutable state objects and then stumbling on problems when changing these mutable persisted values do not result in updates to the underlying session store.
Here's a sample page class:
On the page template, we simply display the current state and an action link to call the onActionFromChangeState event. Everything seems to work - the state object (List) is lazily initialized and new items are appended to it.
Long story short, Tapestry does not detect the changes to a mutable persisted state object. Unfortunately, the way HTTP sessions are usually implemented in many servlet containers, changes to the mutable state are still persisted (bypassing the Tapestry's persistent field invalidation mechanism). This can cause unexpected and hard-to-trace problems later, when trying to switch the persistence strategy for a specific state variable, e.g. from the session to the client strategy.
For instance, here we try to change the previous sample to use client-side persistence instead:
But when calling the onActionFromChangeState, nothing seems to happen. The state is transferred from the server to the client and back, but this always contains an empty List. This corresponds to the empty List initialized on the first render request.
To demonstrate that this is really the case, we can use a simple hack:
Now the changeState action works once again, since the PersistentFieldConduit responsible for recording the change of the field value is used. Although this kind of a "hack-y" approach works, the moral of the story seems to be quite clear - be very careful when persisting mutable values.

Thursday, February 2, 2012

Bean property expressions with Java and Tapestry

Tapestry has a couple of nice features for accessing bean properties in a loosely coupled way. One of them is a reflection-based PropertyAccess and the other one a runtime-generated proxy called PropertyConduit. Both of these are very comfortable to use - just specify a property expression and you're good to go. Unfortunately, in larger projects, these String-based property expressions tend to become a bit difficult to maintain as you're re-factoring the beans the expressions refer to.

So, just for fun, I decided to experiment with an alternative for PropertyConduit - to create the conduits during compilation and have an ability to chain these together into more complex expressions. For instance, if we would have two bean classes:

Then we could refer to and get the "bestFriend.name" property as:

This would ideally be type-safe and the *Properties classes auto-generated.

The initial proof-of-concept that I came up with contains two classes describing the property expressions:

For testing purposes, three classes were created:
To automatically generate property expressions for a set of bean classes, an annotation processor was implemented, which yields the following result (for the bean classes specified above):

And a simple test scenario to check that everything works as expected:

Will need to test this out on a larger scale now, for possible performance and memory usage problems :)

Wednesday, October 12, 2011

Dynamic dispatch with a twist

Had a silly idea for trying out Tapestry 5.3's new ASM wrapper called Plastic: what if we could implement message dispatchers in patterns like Visitor without ruining the forward compatibility of the Visitor SPI?
Let's say we have a generic message interface:
and a message dispatcher (visitor):
When implementing new MyMessage subtypes, we would create additional methods in the interface, but this would also require re-implementing all MyMessageDispatcher subtypes. Additionally, when we have a generic implementation for handling MyMessage and we do not care about the subtypes in a specific scenario, we would not want to implement every possible handle() method out there.
Let's say we have a bunch of message classes:


When creating a message handler for this tree, we might want to handle the messages like this:



One option would be to have a reflection-based check in the handle methods defined by the MyMessageDispatcher interface. Another would be to avoid reflection and add a bunch of instanceof statements in the same method, achieving some form of type-safety. The (twisted) option described here would essentially be the second solution, but with a proxy class created on-the-fly. The main requirements for the proxy are:

  1. The proxy must implement a given dispatcher interface (MyMessageDispatcher)
  2. When handle(MyMessage) is called, the proxy must:
    1. Determine the runtime class of the argument
    2. Find an implementation of the handle() method with the first parameter closest to the runtime class of the message in type hierarchy. Excluding the first argument, the handler must have the same signature and return type as the method specified by the interface
    3. Call the implementation and return the result.


Let's implement a service for creating this kind of a proxy:


The transformer responsible for implementing this proxy is:


Internally, a DispatchTarget class is used:


A dispatcher proxy can be initialized, using the createProxy() method, passing the implementation instance to the method:



I'm actually quite interested in the performance of this bytecode mutant, compared to a reflection-based approach, so there could be some optimization and performance testing waiting ahead.

Monday, September 26, 2011

Integrating YUI3 with Tapestry 5.2

I have become a fan of the YUI's patterns for modular architecture and the idea of executing JavaScript in a restricted "sandbox". Unfortunately, I haven't found many examples for integrating this pattern in Tapestry 5.2. The closest example for integrating YUI at the time was the tapx-yui module, but as the project is more related to YUI 2, I thought to give it a go and try to implement something for YUI 3 myself.

Step 1: importing YUI JavaScript modules and CSS
As far as I've understood, there are two ways for importing YUI modules:
  1. By importing (creating a script tag for) every YUI JavaScript asset that is required on a page.
  2. By importing the yui(-min).js asset and using the auto-loading feature.
As tapx-yui already had a great example on how to go about importing the assets one-by-one, I decided to focus on using the auto-loader approach. In the following example, I define the yui.js asset as a JavaScriptStack, which also contains 4 YUI CSS assets: cssreset, cssbase, cssfonts and cssgrids. No real reason to do so, the CSS assets could as well be auto-loaded.



In this case, all assets from the YUI build are expected to be stored under the src/main/webapp/yui directory.
The CoreYuiStack class needs to be contributed to the JavaScriptStackSource service:



Step 2: Creating a component that defines a YUI module
As soon as YUI libraries can be auto-loaded, we can start defining our own custom modules. Here is an example of a component that wraps the YUI Panel functionality. Not much augmentation here compared to the YUI's Panel, but the idea should be clear enough.


A new JavaScript namespace, appcore is defined, containing a component called Panel. The corresponding Tapestry component class looks like this:



After the component's body is rendered, we add an initialization call for passing configuration data to the new component. There is no special API here, just a simple code snippet rendered into the page body. There is some room for improvement here. Although we would very much like the initialization call to be wrapped in a sandbox, the dependency on a local variable called Y looks a bit iffy design-wise.
The YuiSupport service is responsible for wrapping the initialization calls into a YUI sandbox:



Step 3: Using the new component
The Panel component can now be used like any other Tapestry component:
The output should now contain a panel similar to this: