Wednesday, July 7, 2010

Using Bean Validation with the OSGi/JPA 2.0 Feature Pack

Do you have applications littered with layers of inconsistent validation logic or are writing a new application that will require data validation? Bean validation (JSR-303 - part of the Java EE6 family of specifications) defines an API for providing runtime validation of data within Java beans. In addition, other EE6 specifications such as JPA 2.0, JSF, and JCA provide tight integration with bean validation, allowing runtime validation to occur seemlessly with your persistence, resource/connectivity, and UI logic.

Using bean validation within the app server requires a JSR-303 spec API library, the bean validation provider library(ies), and any supporting libraries. If you'd like to test drive bean validation with JPA 2.0, the
WebSphere Application Server V7 Feature Pack for OSGi Applications and Java Persistence API 2.0 does not include a bean validation API or provider, but it does include a provision for wiring one in yourself. The wiring is pretty simple and is briefly explained in this IBM Infocenter document Using Bean Validation with JPA. With the information provided in the article, I was able to configure WebSphere Application Server to use Apache's Bean Validation provider using these steps.
  1. Install WebSphere Application Server V7 with the Fixpack required for the feature pack (currently 7.0.0.9). There is a free Developer's Edition if you don't have the V7 test server for RAD 7.5 or an extra license lying about. :-)
  2. Install the WebSphere Application Server V7 Feature Pack for OSGi Applications and Java Persistence API 2.0.
  3. Download the Apache Bean Validation Spec API jar. Save to a location such as:
    /opt/apache/specs/geronimo-validation_1.0_spec-1.1.jar
  4. Download the Apache Bean Validation Core jar. Save to a location such as:
    /opt/apache/bval/bval-core-0.1-incubating.jar
  5. Download the Apache Bean Valdation JSR-303 jar. Save to a location such as:
    /opt/apache/bval/bval-jsr303-0.1-incubating.jar
  6. Download Apache Commons Bean Utils. Extract the distribution and locate commons-beanutils-1.8.3.jar. Copy the jar to a location such as:
    /opt/apache/commons/commons-beanutils-1.8.3.jar
  7. Download Apache Commons Lang 2.4. Extract the distribution and locate commons-lang-2.4.jar. Copy the jar to a location such as:
    /opt/apache/commons/commons-lang-2.4.jar
  8. Start the app server.
  9. Open the WebSphere Integrated Solutions Console in your browser (http://<server:port>/admin).
  10. In the navigation pane expand Servers -> Server Types and click on WebSphere application servers.
  11. Select the server profile to configure. Typically server1 in a default single server environment.
  12. Under the Server Infrastructure section, select Process Definition.
  13. Under the Additional Properties section, select Java Virtual Machine.
  14. Under the Additional Properties section of the JVM settings, select Custom properties.
  15. Click New and add a property named com.ibm.websphere.validation.api.jar.path and set the value to the location of the bean validation API jar. (ex. /opt/apache/specs/geronimo-validation_1.0_spec-1.1.jar)
  16. Restart the application server.

The application server is now configured to locate and load the bean validation spec API jar. The spec API classes are now available, but the application server (being an EE5 app server) does include a bean validation provider. You need to make a provider available to your application by either bundling it with your application archive (in the proper location within a war or ear) or via a shared library. Include the bean validation core, bean validation jsr-303 component, commons bean utils, and commons lang 2.4 jars within the application or shared library and you'll be off to the races.

That's all there is to configuring the application server. To actually build applications which take advantage of bean validation constraints, you'll need to specify the spec API library in the build path of your tooling. The examples in Apache Bean Validation Primer will help you get started. The example in the primer is targeted for a JSE environment (I'm working on a JEE version -- no ETA), but the JPA entities, constraints, validators, and logic can be easily ported to an EE environment. One additional very important item to note is that your persistence.xml must be updated to version 2.0 in order for bean validation to be enabled by the JPA provider (OpenJPA). Since the 2.0 schema is backward compatible, it is typically a simple one line XML change. The JPA Integration section of the primer contains the rationale for this requirement and an example.

-Jeremy