Sunday, May 31, 2009

Spring, Flex, and other things


I finally just did another release for Spring by Example. One example is Simple Flex Webapp and the other is Simple Spring Integration.

I just did the Flex example recently and the Spring Integration one was done a little while ago, but I never wrote about it for the site. There's also a more advanced example using queues and splitting, but I'm not sure if the flow really makes sense although it needs to be the way it is to split and aggregate things.

I've been working with Flex for a few months now and I still think it's really nice. It's the best thought out UI framework I've ever worked with. Besides it looking very nice with minimal effort, it's very easy to do many things, ActionScript is a very comfortable environment for someone that knows Java, JavaScript, and is used to doing web development. Also, I'm impressed with how many things Adobe has open sourced. The Adobe BlazeDS provides a really good bridge between Java and Flex for remoting and messaging. Also the Spring BlazeDS Integration project from SpringSource really simplifies configuration and ease of use as you would expect. I think they've done a really great job again. Spring Integration also has integration with Spring BlazeDS Integration so from a message flow you could send a message to a Flex UI and the flow could wait for a response to come back.

The Adobe Cairngorm project for providing client side MVC in Flex is nice too. It helps provide separation of business logic from UI components and encourages using Flex's data binding to transfer information from the controller to the view using a bound model. Adobe suggests having the model a singleton, but it would be nice if a dependency injection framework was used instead. Spring ActionScript gives basic functionality, but just the bare minimum compared to Spring's Java implementation. Although I think it could become a lot nicer without too much effort.

I'm going to try to post more Flex examples including more advanced Flex usage as well as ones using messaging. A bridge can be created between JMS queue and a Flex queue or messages can be sent directly to the Flex queue. Also, I'd like to start spending more time using the SpringSource dm Server. The new web features that are being added to version 2.0 really interest me.


Sunday, April 19, 2009

Maven JAXB Generation Part II

I've already done a posting on using Maven to generate JAXB with the Fluent API and this adds making the JAXB beans serializable and setters for lists. Add the file jaxb-bindings.xjb to the 'src/main/resources' with the config below and JAXB beans will implement serializable.

Also by default, collections only have getters. The JAXB 2.1 Collection setter Injector Plugin will also generate a setter for any collection. I found this useful while serializing JAXB beans between Java and ActionScript with BlazeDS (while using Spring BlazeDS Integration. BlazeDS only serializes something if it has a getter and a setter. Unfortunately I couldn't find this in a Maven repo, but you can install it locally or into your Nexus server.




<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xfluent-api</arg>
<arg>-Xcollection-setter-injector</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>
<dependencies>
<!-- Had to manually install in repo, not in main java.net repo. -->
<dependency>
<groupId>org.jvnet.jaxb2-commons</groupId>
<artifactId>collection-setter-injector</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</plugin>

src/main/resources/jaxb-bindings.xjb
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="1.0">

<jxb:bindings>
<jxb:globalBindings>
<jxb:serializable/>
</jxb:globalBindings>
</jxb:bindings>

</jxb:bindings>

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.