7/12/10

Wicket, Spring 3, JPA2 & Hibernate OSGi Application on Apache Karaf

EDIT: Hibernate is now OSGi ready so most of those stuff are now completely outdated.

The full source for this post has been move to github.


Prologue
Recently I attempted to modify an existing crud web application for OSGi deployment. During the process I encountered a lot of issues such as
  • Lack of OSGi bundles.
  • Troubles wiring the tiers of the application together.
  • Issues on the OSGi container configuration.
  • Lack of detailed examples on the web.
 So, I decided to create such a guide & provide full source for a working example (A very simple person crud application).

The first part of this guide is Creating custom Hibernate 3.5 OSGi bundles. This part provides an example project (which includes the bundles source) that describes how to use the custom hibernate bundles in order to build a wicket, spring 3, hibernate 3.5 jpa 2 and deploy it to Karaf.

Among others it describes:
  • How to wire database and web tier using the OSGi blueprint.
  • How to deploy web applications to Karaf 1.6.0.
  • A small wicket crud application.
Note: This demo application does not make use OSGi Enterprise Spec, since its an OSGi-fication of an existing application. The use of the spec will be a subject for future posts.

Enjoy!

Environment Preparation
The OSGi run-time that will be used in this post is Felix/Karaf version 1.6.0.
This section describes the required configuration for deploying web applications.

Once, karaf is downloaded and extracted, it can be started by typing
bin/karaf
from inside the karaf root folder.

Now, we are going to install karaf webconsole and war deployer that will allow us to deploy web applications to karaf.
features:install webconsole
features:install war

Note: In the background karaf fetches all the required bundles from maven repositories. You are going to need internet access for this. Moreover, if you are behind a proxy you will need to set up your jvm net.properties accordingly. Having the proxy configured in maven settings.xml is not enough.

Custom Bundles
Most of the bundles required for this project are available either in public maven repositories or inside Spring Enterprise Bundle Repository. However, hibernate 3.5.x which is one of the key dependencies for this project is not available as OSGi bundle (note: earlier version of hibernate can be found in Spring EBR). More details on OSGi-fying Hibernate 3.5.x in the previous part of the guide "Creating custom Hibernate 3.5 OSGi bundles".


Creating the application itself
The actual demo application will be the simplest possible wicket crud for persons (a killer application that stores/delete/updates a persons first name and last name to the database).

Database
The create schema script of such application in mysql would look like this:
CREATE TABLE person (
    ID MEDIUMINT NOT NULL AUTO_INCREMENT,
    FIRST_NAME VARCHAR(40) NOT NULL,
    LAST_NAME VARCHAR(40) NOT NULL,
    PRIMARY KEY (ID)
);
Database Tier
For the database tier we are going to create a simple bundle that will contain the entity, the dao interface and the dao implementation. The bundle will contain the necessary persistence descriptor for JPA 2.0 with hibernate as persistence provider. Finally it will use spring to create the data source, entity manager factory & JPA transaction manager. This bundle will export the dao as a service to the OSGi Registry using Spring dynamic modules.


The Person entity for the example can look like:
package net.iocanel.database.entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author iocanel
 */
@Entity
@Table(name = "Person")
@NamedQueries({
 @NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),
 @NamedQuery(name = "Person.findById", query = "SELECT p FROM Person p WHERE p.id = :id"),
 @NamedQuery(name = "Person.findByFirstName", query = "SELECT p FROM Person p WHERE p.firstName = :firstName"),
 @NamedQuery(name = "Person.findByLastName", query = "SELECT p FROM Person p WHERE p.lastName = :lastName")})
 public class Person implements Serializable {

 private static final long serialVersionUID = 1L;
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 @Basic(optional = false)
 @Column(name = "ID")
 private Integer id;
 @Column(name = "FIRST_NAME")
 private String firstName;
 @Column(name = "LAST_NAME")
 private String lastName;

 public Person() {
 }

 public Person(Integer id) {
  this.id = id;
 }

 public Integer getId() {
  return id;
 }

 public void setId(Integer id) {
  this.id = id;
 }

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 @Override
 public int hashCode() {
  int hash = 0;
  hash += (id != null ? id.hashCode() : 0);
  return hash;
 }

 @Override
 public boolean equals(Object object) {
  // TODO: Warning - this method won't work in the case the id fields are not set
  if (!(object instanceof Person)) {
   return false;
  }
  Person other = (Person) object;
  if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
   return false;
  }
  return true;
 }

 @Override
 public String toString() {
  return "net.iocanel.database.entities.Person[id=" + id + "]";
 }
}

For this entity we will create a dao interface, through which the rest of the bundles in the container can track/lookup the dao service (the actual implementation).

We want the dao service to provide simple crud operations such as, create, delete, find & findAll, so the dao interface can be something like:
package net.iocanel.database.dao;

import java.util.List;
import net.iocanel.database.entities.Person;

/**
 *
 * @author iocanel
 */
public interface PersonDAO {

 public void create(Person person) throws Exception;
 public void edit(Person person) throws Exception;
 public void destroy(Integer id) throws Exception;
 public Person findPerson(Integer id);
 public List findAllPersons();
}

The actual jpa implementation of the dao will obtain the EntityManager via Spring (it will be injected by Spring) and for transaction demarcation will use Spring's Transactional annotation:
package net.iocanel.database.dao;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.PersistenceContext;
import net.iocanel.database.entities.Person;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author iocanel
 */
@Transactional
public class PersonJpaDAO implements PersonDAO {

 @PersistenceContext
 private EntityManager entityManager;

 public void create(Person person) throws Exception {
  entityManager.persist(person);
  entityManager.flush();
 }

 public void edit(Person person) throws Exception {
  entityManager.merge(person);
  entityManager.flush();
 }

 public void destroy(Integer id) throws Exception {
  entityManager.remove(findPerson(id));
  entityManager.flush();
 }

 public List findPersonEntities(int maxResults, int firstResult) {
  return findPersonEntities(false, maxResults, firstResult);
 }

 private List findPersonEntities(boolean all, int maxResults, int firstResult) {
  Query q = entityManager.createQuery("select object(o) from Person as o");
  if (!all) {
   q.setMaxResults(maxResults);
   q.setFirstResult(firstResult);
  }
  return q.getResultList();
 }

 public Person findPerson(Integer id) {
  return entityManager.find(Person.class, id);
 }

 public int getPersonCount() {
  Query q = entityManager.createQuery("select count(o) from Person as o");
  return ((Long) q.getSingleResult()).intValue();
 }

 public List findAllPersons() {
  Query q = entityManager.createNamedQuery("Person.findAll");
  return q.getResultList();
 }
}


For the EntityManager injection and Spring Transactions, we need need a spring context. Since we are going to use Spring Dynamic Modules, the spring context needs to be placed under META-INF/spring/.


 
 
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 

 
 
  
  
 

 
  
 

 
 

 
 

 



For the creation of the EntityManagerFactory Spring will need a persistence.xml file located under META-INF:

  
    org.hibernate.ejb.HibernatePersistence
    net.iocanel.database.entities.Person
    
      
      
      
      
      
      
      
      
      
      
      
      
      
    
  


So far in the database tier we did what we would do in a typical application. Now we will add OSGi flavor to our module.

Creating the DAO OSGi Service
As mentioned above for the creation of the dao service we will use spring dynamic modules. So all we need is to add a descriptor under META-INF/spring that will instruct Spring's OSGi exporter to export bean personDAO as OSGi service:


 
 


Finally, we need to perform a small hack. In the previous part of this guide, we created an OSGi fragment for Hibernate Validator. This fragment is attached on the validation api host, so that the validation api can find the classes of hibernate validator. However, we still need to instruct the validation api, to look for Hibernate Validator classes. In an non-OSGi world the validation api will lookup in the classpath for the following file META-INF/services/javax.validation.spi.ValidationProvider and read the actual validation provider class name from this file.

Passing the Validation Provider to Validation API
In the OSGi world the validation api, will delegate that call to the calling bundle (in our case the database tier bundle) so we are going to make sure that it finds it. How we are going to do so? We are going to copy it from Hibernate Validator and add it in our bundle. This approach might not seem that elegant, however it has two great advantages:
  • Its simple
  • It works
If you are aware of more elegant alternative feel free to communicate them.

The final obstacle is creating the bundle itself.The bundle will be created using maven-bundle-plugin. As maven dependencies it will contain only whatever it requires for the compile scope and its run-time dependencies(hibernate,spring,jpa spec, cglib etc) will be declared as OSGi Import-Packages.


  net.iocanel.*
  
   *,
   javax.sql,
   javax.transaction,
   javax.persistence,
   javax.persistence.*,
   javax.persistence.spi,
   javax.persistence.metamodel,
   javax.persistence.criteria,
   org.hibernate,
   org.hibernate.annotations,
   org.hibernate.annotations.common,
   org.hibernate.annotations.common.reflection,
   org.hibernate.ejb,
   org.hibernate.cfg,
   org.hibernate.cfg.annotations,
   org.hibernate.cfg.annotations.reflection,
   org.hibernate.cache,
   org.hibernate.hql,
   org.hibernate.hql.ast,
   org.hibernate.validator,
   org.hibernate.validator.constraints,
   org.hibernate.validator.constraints.impl,
   org.hibernate.validator.engine,
   org.hibernate.validator.engine.groups,
   org.hibernate.validator.engine.resolver,
   org.hibernate.validator.jtype,
   org.hibernate.validator.metadata,
   org.hibernate.validator.util,
   org.hibernate.validator.util.annotationfactory,
   org.hibernate.validator.xml,
   org.hibernate.proxy,
   com.mysql.jdbc,
   javassist.util.proxy,
   org.aopalliance.aop,
   org.springframework.aop,
   org.springframework.aop.framework,
   net.sf.cglib.core,
   net.sf.cglib.reflect,
   net.sf.cglib.proxy
  
  !*
  ${pom.groupId}.${pom.artifactId}
  false
 

 
  
   
    org.apache.felix
    maven-bundle-plugin
    true
    
     
      ${artifactId}
      ${symbolic.name}
      ${pom.name}
      .
      ${import.packages}
      ${export.packages}
      ${private.packages}
     
    
   



Presentation/Web Tier
For the presentation tier we are going to be a Wicket OSGi application. This application will be integrated with Spring using @SpringBean annotation (more details on this on Wicket/Spring Wiki).

Since we are interested in taking advantage of Spring Dynamic Modules, we are going to instruct to load its context from OsgiBundleXmlWebApplicationContext inside the web.xml.

  contextClassorg.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

So the full web deployment descriptor for Wicket/Spring/OSGi could look like this(yes I know I am starting to sound like Bob Ross):


 web-tier

 
  wicket.configurationdevelopment

 
  contextClassorg.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext

 
  org.springframework.web.context.ContextLoaderListener
 
 
  wicket.wicket-spring
  org.apache.wicket.protocol.http.WicketFilter
  
   applicationFactoryClassNameorg.apache.wicket.spring.SpringWebApplicationFactory
  
   applicationClassNamenet.iocanel.WicketApplication
 
 
  wicket.wicket-spring
  /*
 


The Spring context file (/WEB-INF/applicationContext.xml)that will be loaded needs to define two simple things:
  • The Wicket Application Object.
  • The PersonDAO OSGi service.

The PersonDAO service will be looked up using Spring Dynamic Modules. Inside the wicket application the PersonDAO service will be injected as if it was a normal spring bean using the @SpringBean annotation.



 
 


We are almost there. All that's left is the coding of the actual crud. I will not go into much detail, since its beyond the scope of this blog post. However, I am going to list the key points of the crud.

The C.R.U.D.
For the CRUD part we will create a single ajax page that will display:
  • A list of all persons in the database.
  • A small form to insert/edit person details.
  • Buttons for each record to edit/remove persons in the database.
The List Component that will be used is PropertyListView, and the model attached to list will be a LoadableDetachableModel that will load all persons from the database. Finally the person details form will consist of 2 text fields First Name & Last Name.


Enjoy!
The full source for this example (including the custom bundles) can be found at my github repository. Once you unpack it you can mvn clean install and it will package the project bundles, the custom bundles and all the required bundles under target/wicket-osgi.dir/deploy folder. Just copy the contents of this folder to $KARAF_HOME/deploy and you are ready launch the application at http://localhost:8181/web-tier/.

Final thoughts
I hope you find this useful.
Once my schedule allows, I will blog on how to add JTA transactions on the example above, so stay tuned(The Hibernate bundle is JTA ready, however we need a JTA transaction manager bundle).
Feel free to send comments or suggestions.

7/9/10

Creating custom Hibernate OSGi bundles for JPA 2.0

Edit: I am more than happy that this post is now completely obsolete. Hibernate is now OSGi ready, Yay!


Prologue
I was trying to migrate an application that uses JPA 2.0 / Hibernate to OSGi. I found out that hibernate does not provide OSGi bundles. There are some Hibernate bundles provided in the Spring Enterprise Bundle repository, however they are none available for Hibernate 3.5.x which implements JPA 2.0. So I decided to create them myself and share the experience with you.

This post describes how to OSGi-fy Hibernate 3.5.2-Final with EhCache and JTA transaction support. The bundles that were created were tested on Felix Karaf, but they will probably work in other containers too.

Introduction
A typical JPA 2.0 application with Hibernate as persistence provider will probably require among other the following dependencies
  • hibernate-core
  • hibernate-annotations
  • hibernate-entitymanager
  • hibernate-validator
  • ehcache
Unfortunately, at the time this post was written none of the above was available as OSGi bundle. To make OSGi bundles for the above one needs to overcome the following problems
  • Cyclic dependencies inside Hibernate artifacts.
  • 3rd party dependencies (e.g. Weblogic/Websphere Transaction Manager).
  • Common api / impl issues for validation api and hibernate cache.
The last bullet which may not be that clear points to a problem where an api loads classes from the implementation using Class.forName() or similar approaches. In the OSGi world that means that the api must import packages from the implementation.

Hibernate cyclic dependencies
The creation of an OSGi bundle for each hibernate artifact is possible. However, when the bundles get deployed to an OSGi container, they will fail to resolve due to cyclic package imports.

The easiest way to overcome this issue is to merge hibernate core artifacts into one bundle. Below I am going to provide an example of how to use maven bundle plug-in to merge hibernate-core, hibernate-annotations & hibernate-entitymanager into one bundle.

A common way to use the maven-bundle-plugin to merge jars into artifacts is to instruct it to embed the dependencies of a project into a bundle. However, this is not very handy in cases where you need to add custom code into the final bundle. In that case you can use the maven dependency plug-in to unpack the dependencies, bundle plug-in to create the manifest and jar plug-in to instruct it to use the generated manifest in the package phase.

  
   org.apache.maven.plugins
   maven-dependency-plugin
   
    
     unpack
     generate-resources
     
      unpack
     
     
      
       
        org.hibernate
        hibernate-core
        false
        target
       
       
        org.hibernate
        hibernate-annotations
        false
        target
       
       
        org.hibernate
        hibernate-entitymanager
        false
        target
       
      
      false
      true
     
    
   
  
  
   org.apache.felix
   maven-bundle-plugin
   2.0.1
   true
   
    
     create-manifest
     process-classes
     
      manifest
     
    
   
   
    
     jar
     bundle
    
    ${unpack.bundle}
    
     ${artifactId}
     ${groupId},${artifactId}
     ${project.name}
     *
     *
    
   
  
  
   maven-jar-plugin
   
    
     target/META-INF/MANIFEST.MF
    
   
  
 

Hibernate & 3rd Party dependencies
Hibernate has a lot of 3rd party dependencies. Some of them are available as OSGi bundles, some need to be created and some can be excluded.

Examples of 3rd party dependencies that are available as OSGi bundle in the Spring Enterprise Repository are:
  • antlr
  • dom4j
  • cglib
Dependencies that are not available are:
  • jacc (javax.security.jacc)
 Dependencies that can be excluded vary depending on the needs. In my case I could exclude Weblogic/Websphere transaction manager, since I didn't intend to use them. To exclude a dependency just add the packages that are to be excluded in the import packages section using the ! operator (e.g. !com.ibm.*,*)

Hibernate validator and Validation API
As mentioned above the validation api provides a factory that build the validator by loading the implementing class using Class.forName(). This issue can be solved with 2 ways
  • Use dynamic imports in the API bundle to import the Implementation at runtime.
  • Make the implementation OSGi Fragment that will get attached to the API.
In this example the validation api is the one provided by the Spring Enterprise Bundle Repository, so the second approach was easier to apply.

More details on this issue can be found at this excellent blog post:
Having “fun” with JSR-303 Beans Validation and OSGi + Spring DM

Hibernate & EhCache
More or less the same applies to EhCache. Hibernate provides an interface which is implemented by EhCache. Hibernate loads that implementation in runtime. We will do exactly the same thing  we did for hibernate validator. We will convert ehcache jar to fragement bundle so that it gets attached to the merged hibernate bundle.

Hibernate & JTA Transactions
I kept for last the most interesting part. This part describes what needs to be added inside the bundle so that it can support JTA transactions.

For JTA transactions Hibernate needs a reference to the transaction manager. That reference is returned by the TransactionManagerLookup class specified in the persistence.xml. In a typical JEE container the lookup class just performs a JNDI to get the TransactionManager. In an OSGi container the transaction manager is very likely to be exported as an OSGi service.

This section describes how to build an OSGi based TransactionManagerLookup class. The solution presented is very simple and uses only the OSGi core framework (no blueprint implementation required).

We will add to the hibernate bundle 3 new classes:
  • TransactionManagerLocator (Service Locator).
  • OsgiTransactionManagerLookup (Lookup implementation).
  • Activator (Hibernate Bundle Activator).
TransactionManagerLocator is a ServiceLocator that uses OSGi's ServiceTracker to get a reference to the TransactionManager service.
package org.hibernate.transaction;

import javax.transaction.TransactionManager;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

/**
 *
 * @author iocanel
 */
public class TransactionManagerLocator {

    private final String lookupFilter = "(objectClass=javax.transaction.TransactionManager)";
    private static BundleContext context;
    private static TransactionManagerLocator instance;
    private ServiceTracker serviceTracker;

    //Constructor
    private TransactionManagerLocator() throws Exception {
        if (context == null) {
            throw new Exception("Bundle Context is null");
        } else {
            serviceTracker = new ServiceTracker(context, context.createFilter(lookupFilter), null);
        }
    }

    public static synchronized TransactionManagerLocator getInstance() throws Exception {
        if (instance == null) {
            instance = new TransactionManagerLocator();
        }
        return instance;
    }


    public static void setContext(BundleContext context) {
        TransactionManagerLocator.context = context;
    }

    public TransactionManager getTransactionManager() {
        return (TransactionManager) serviceTracker.getService();

    }
}

OsgiTransactionManagerLookup is an implementation of Hibernates TransactionManagerLookup that delegates the look
up to the TransactionManagerLocator.
package org.hibernate.transaction;

import java.util.Properties;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import org.hibernate.HibernateException;

/**
 *
 * @author iocanel
 */
public class OsgiTransactionManagerLookup implements TransactionManagerLookup {

    public TransactionManager getTransactionManager(Properties props) throws HibernateException {
        try {
            return TransactionManagerLocator.getInstance().getTransactionManager();        } catch (Exception ex) {
            throw new HibernateException("Failed to lookup transaction manager.", ex);
        }
    }

    public String getUserTransactionName() {

        return "java:comp/UserTransaction";
    }

    public Object getTransactionIdentifier(Transaction transaction) {
        return transaction;
    }
}

Activator is just a bundle activator. Its role is to pass a static reference of the bundle context to the TransactionManagerLocator (the bundle context is required by the service tracker).
package org.hibernate;

import org.hibernate.transaction.TransactionManagerLocator;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

/*
 *
 * @author iocanel
 */
public class Activator implements BundleActivator {

    public void start(BundleContext bc) throws Exception {
        TransactionManagerLocator.setContext(bc);
    }

    public void stop(BundleContext bc) throws Exception {
    }
} 
Example use of the bundle & bundle source code.
An example web application that uses the custom hibernate bundles can be found in this  post.

If you feel tired of reading and just want to use the bundles. You can download them from here. All the custom bundles are included in the maven project under the bundles folder (as seen in the picture).



The example application uses Wicket and can be easily deploy in Karaf.
Final Thoughts
I hope you found it useful.

Any feedback is more than welcome.