Java

Metadata for Struts & Hibernate

Recent entries from Emmanuel and Cedric have me seriously thinking about the long term impact of JSR 175, the standard metadata interface for Java. I don’t think the average user will have much use for defining their own metadata interfaces, but I do think there is a great deal of room for application frameworks to rethink the way they configure their apps.

The use of XDoclet and the creation of this spec seem to be a reaction to the XML hell that many frameworks create. In frameworks like Struts and Hibernate, the idea of using XML to decouple your mappings from your objects is somewhat of a red herring, in that the very objects you’re writing are tied to that framework, tied to that metadata. What good is a StrutsAction without Struts? And for Hibernate, you have to include information about your class in the mappings, so they’re still tightly coupled. The only other flexibility given by xml configurations is the ability to tweak the application without rebuilding the app, but how often will you roll in a production change without first at least running some tests on it in another environment? Is typing “ant” really that hard?

Still, the inability of XDoclet to handle many scenarios for frameworks (Hibernate composite-id generation) and its non-standard nature have prevented its full adoption as a means of code generation. The metadata spec seems to offer enough flexibility to overcome this, and combined with the ability to inspect the metadata programmatically at runtime I believe it can be a powerful alternative to XML config files.

What if a Struts action looked like this?:

@StrutsAction(
  @ActionMappings({
    @ActionMapping(
      path=”index”,
      @ActionForwards({
        @ActionForward(name=”success”, path=”index.jsp”),
        @ActionForward(name=”failure”, path=”error.jsp”)
      })
    )
  })
)
public class IndexAction extends Action
{
….

So when ActionServlet initializes, it can search the classpath for classes with StrutsAction annotations and register them into its configuration. Additional metadata provided in an extension of ActionServlet could give instructions on what classpath to search for annotated struts elements. ActionForm could be similar designed and discovered.

Does it make your code pretty? Not necessarily. But are XML mapping files pretty? No. But metadata can simplify the number of files we need to maintain for these frameworks while still giving full control over the configuration. It’s more standard, more complete, and more strongly defined than XDoclet.

The following metadata interfaces would back up the suggested approach:

public @interface StrutsAction {
  ActionMappings mappings();
}

public @interface ActionMappings {
 ActionMapping[] value();
}
public @interface ActionMapping {
  String path();
  String form() default””;
  String input() default “”;
  ActionForwards value();
}

public @interface ActionForwards {
  ActionForward[] value();
}

public @interface ActionForward {
  String name();
  String path();
}

P.S. – If I’ve got the syntax wrong, let me know. There’s not much example code out there at the moment.

Update: After looking at Matt Raible’s thoughts on the look of this code, I cleaned it up a little bit – consolidating the lines for the ActionForwards.

Advertisements

One thought on “Metadata for Struts & Hibernate

Comments are closed.