Monday, February 16, 2009

Spring Modules Valang

I've been working on adding enhancements to Spring Modules Valang. I've added a number of new features so far and I have two versions uploaded to the Spring by Example Maven Repository (org.springbyexample.validation:sbe-validation). I have it under a different group id and artifact name so there aren't' any collisions in the future with Spring Modules releases.

I'm also working on documentation, but that's going to take a little longer to finish.

Version 0.91

  • Bytecode generation added to DefaultVisitor as a replacement for reflection accessing simple properties (BeanPropertyFunction) for a significant performance improvement.
  • Basic enum comparison support. In the expression below the personType is an enum and the value STUDENT will be convereted to an enum for comparison. The value must match an enum value on the type being compared or an exception will be thrown.

    personType EQUALS ['STUDENT']
    For better performance the full class name can be specified so the enum can be retrieved during parsing. The first example is for standard enum and the second one is for an inner enum class .

    personType EQUALS ['org.springmodules.validation.example.PersonType.STUDENT']

    personType EQUALS ['org.springmodules.validation.example.Person$PersonType.STUDENT']
  • Where clause support. In the expression below, the part of the expression price will only be evaluated if the personType is 'STUDENT'. Otherwise the validation will be skipped.

    price < 100 WHERE personType EQUALS ['STUDENT']
  • Improved performance of 'IN'/'NOT IN' if comparing a value to a java.util.Set it will use Set.contains(value). Static lists of Strings (ex: 'A', 'B', 'C') are now stored in a Set instead of an ArrayList.
  • Functions can be configured in Spring, but need to have their scope set as prototype and use a FunctionWrapper that is also a prototype bean with set on it.
  • Removed servlet dependency from Valang project except for the custom JSP tag ValangValidateTag needing it, but running Valang no longer requires it. This involved removing ServletContextAware from it's custom dependency injection. If someone was using this in a custom function, the function can now be configured directly in Spring and Spring can inject any "aware" values.

Version 0.92

  • Removed custom dependency injection since functions can be configured in Spring.
  • Added auto-discovery of FunctionWrapper beans from the Spring context to go with existing auto-discovery of FunctionDefinition beans.

Generating Bytecode


I've been looking at the Javassist project. It's really nice. It's a higher level API for manipulating and generating bytecode. Below is an example generating a new class that implements and interface, and then implements the interface. By generating this, it avoids reflection and is significantly faster in tests I've run.



ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(classFunctionName);

cc.addInterface(pool.get("org.springmodules.validation.valang.functions.Function"));

StringBuilder generatedMethod = new StringBuilder();
generatedMethod.append("public Object getResult(Object target) {");
generatedMethod.append(" return ");
generatedMethod.append(" ((" + className + ")target).");
generatedMethod.append(property);
generatedMethod.append("();");

CtMethod m = CtNewMethod.make(generatedMethod.toString(), cc);
cc.addMethod(m);

result = (Function)cc.toClass().newInstance();

Sunday, February 1, 2009

Spring by Example Update

I've been working for a while on an update to Spring by Example. It just happens to be the 1.00 release so I wanted it to be a little bit bigger. It's over a years worth of work at this point and I think it shows how much work I've put into it. I'm happy that it's helping people and will continue to work on it as I have time. I wish I had more. Especially to keep doing more work with the Spring dm Server. Below is a list of the lastest examples posted and I'm working on other ones that I'll hopefully be able to finalize over the next month.

Tuesday, December 9, 2008

Maven generating JAXB with the Fluent API

I thought this was interesting and there wasn't a lot of information on this online. It's using the Maven JAXB plugin to generate JAXB classes with the Fluent API. All the regular methods for getting and setting values are available, but there is also an API to chain together setting and creating classes. Thanks to Steve Berman for the Maven config.

Standard API

PersonResponse personList = new PersonResponse();

Person person = new Person();
person.setId(ID);
person.setFirstName(FIRST_NAME);
person.setLastName(LAST_NAME);

Person person2 = new Person();
person2.setId(SECOND_ID);
person2.setFirstName(SECOND_FIRST_NAME);
person2.setLastName(SECOND_LAST_NAME);

personList.getPerson().add(person);
personList.getPerson().add(person2);


Fluent API

PersonResponse personList = new PersonResponse().withPerson(
    new Person().withId(ID).withFirstName(FIRST_NAME).withLastName(LAST_NAME),
    new Person().withId(SECOND_ID).withFirstName(SECOND_FIRST_NAME).withLastName(SECOND_LAST_NAME));


Inside the build element's plugins section, this can be added to generate JAXB beans with the Fluent API.

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-Xfluent-api</arg>
        </args>
        <schemaDirectory>src/main/resources</schemaDirectory>
        <plugins>
            <plugin>
                <groupId>net.java.dev.jaxb2-commons</groupId>
                <artifactId>jaxb-fluent-api</artifactId>
                <version>2.1.8</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<repositories>
    <repository>
        <id>maven2-repository.dev.java.net</id>
        <name>Java.net Maven 2 Repository</name>
        <url>http://download.java.net/maven/2</url>
    </repository>
</repositories>

Friday, December 5, 2008

Spring by Example Wins

I was going to really try to get back into working on the book, but I think I realistically don't have the time. I did make a small update to Spring In-depth, In Context before SpringOne started, but I was thinking about how Spring 3.0 is coming out soon so I would need to go through everything again. Posting examples on Spring by Example is less effort since each example is more or less standalone and I'm primarily writing up something specifically on the example. Instead of trying to explain the subject completely, which involves a lot more research and effort. Maybe one day I'll try to get back into working on it, but I actually think hands on examples are probably more valuable. At least that what I always like to see. I typically just want to get going on a project and I'll learn all the nuances of the technology as I go. Possibly the preface and intro from the book can become part of the Spring by Example documentation at some point.

Based on this decision I moved the work I had done for the book to Spring by Example.

Tuesday, December 2, 2008

SpringOne Americas 2008 Presentation

SpringOne has been really interesting so far. Rossen Stoyanchev gave a really excellent presentation on all the nuances and best practices for using Spring MVC Annotations. I'll need to update all my web examples, because there's at least one thing if not more that could be done more elegantly. Like using the @ModelAttribute on a method to populate the model for a form instead of using a dummy method just for a create (so the form has an instance to bind to).

My presentation on GWT & Dojo Cometd with Spring Bayeux is tomorrow morning. I've put a lot of work into the presentation, writing up what I could for Spring In Depth, In Context, and getting the examples cleaned up as much as possible and checked into Subversion. Hopefully the presentation will go well and everyone will find it interesting.

Spring by Example & 'Spring In-depth, In Context'

I've made updates to both sites. Spring by Example doesn't have anything too major, but the last site release updated the Spring by Example Web Module to version 1.1.1. For Spring In-depth, In Context, I finally had time to get the Chat and Trade Monitor examples I made for my SpringOne presentation posted and written up for Jetty. The examples are checked in except for the Trade Monitor example running an embedded Jetty in Tomcat. This is the same as the Trade Monitor that runs under Jetty, was just to show that you could still use Jetty's Bayeux implementation even if you were on Tomcat.

Also I finally had time to post a simple Spring dm Server example showing side-by-side versioning. It has a version 1.0 and 1.1 of a message service. The version 1.0 uses Commons Lang 2.1.0 and version 1.1 uses Commons Lang 2.4.0. There is a web module that just displays the data and you can switch back and forth between the two different message services at runtime. I'll work on this section more to add screen shots and a more detailed explanation of things, but I wanted to get what was ready posted.