Migration from JBoss Seam 2.2 to Java EE 7

This article is about a migration of application seam-booking from JBoss Seam to Java EE 7, removing JBoss Seam dependency.

This is important because there lots of legacy JBoss Seam application that must run in newest containers, like Wildfly 8.

Seam-booking is an example application from JBoss Seam 2 distribution. It uses the following technologies:

* JBoss Seam 2.2
* Java Server Faces 1.2
* JPA 1.0
* Hibernate 3.3.2.GA
* Richfaces 3.3.1
* Hibernate Validator

The migration is to remove JBoss Seam dependency, update it to Java EE 7, to the following technologies, present in Wildfly 8.1

* CDI 1.1
* JPA 2.1
* JSF 2.1
* Hibernate 4.3
* Richfaces 4.3.5
* Picketlink 2.5.2
* Bean Validation

Some general notes about the migrated application:

booking is the name I will refer, when talking about the application, seam-booking is the original application.

There were some decisions I did, for example remove EJB and let only CDI Beans, but this move would change too much, and the intent of this article is not to change the architecture of this application, but only change the technologies.

The automated tests were removed from booking, as I am not interested to migrate it.

As a side note, a lighter migration is possible, make the seam-booking works with minimal change, running with JBoss Seam 2.2, hibernate 3.3.2, etc. But that is a subject for other article.

There are many references to the word “seam” (package names, explication texts, etc.), I didn’t remove it because I didn’t want to change too much of the application, to facilitate the diff operation and to focus in the technology side.

Picketlink is used as the security authenticator, as it integrates with CDI.

Differences

The differences will be outlined as “before” and “after” when applicable, the code “before” is contained in a box with white background, the “after” code is contained in a grey background to facilitate the reading process. Also I will provide some justification why I have implemented that way.

The original seam-booking and booking are available for download, if you want to run a diff and navigate yourself into it.

This is a list of the overall changes, easier to understand, that any migration from JBoss Seam to Java EE 7 must perform.

Also, some EJB interfaces return method names changed, as it uses the JSF 2 implicit navigation to return the page names for each action.

JBoss Seam

Java EE 7

@In
@Name

@Scope(ScopeType.SESSION)

@Inject@Named

@SessioScoped

Only preserves the value if is appropriate. The @Named is only necessary if the bean is used in facelets pages, the #{my_name} instruction. As CDI uses injection by class type,
all classes are eligible to injection between classes of the same deployable artifact.

 

Hibernate validator@NotNull

@Pattern

@Length

Bean validation@NotNull

@Pattern

@Size

They are minor modifications and easy to understand.

 

@Logger JUL Logger
Uses the JUL Logger class, very simple to use.

 

@Restrict
No replacement in Java EE 7. As it only checks for user authentication, I added a servlet filter (SecurityFilter) to check user authentication.

 

pages.xml
seam-booking uses pages.xml to handle navigation, but booking uses JSF implicit navigation and a servlet filter to redirect user if he try to access a protected page.

 

@PersistentContext @Inject
The entity manager is produced in Resources class, it injects a @PersistenceUnit (EntityManagerFactory) and fabricates a entity manager at the time of the first request, and closes it when disposing.

 

@DataModel@DataModelSelection javax.faces.model.ListDataModel;
The annotations @DataModel and @DataModelSelection facilitates the use of JPA results into facelets pages.This is facilitated in JSF 2, to wrap the JPA result into a javax.faces.model.ListDataModel object.

 

 

The seam-booking uses a mixture of maven and ant to build.The resultant file is an EAR file.

booking uses only maven to build it, it uses the BOM org.jboss.bom:jboss-javaee-6.0-with-hibernate

The resultant file is a WAR.

WAR file are simpler to use and allows for EJB lite to be deployed in it.

 

FacesMessages.instance().add(“Booking cancelled for confirmation number #0”, booking.getId()); FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
JBoss Seam allows to send messages to end user after a redirect, however for that to work, flash scope must be used, as show above.

 

Stateless EJB, uses JBoss Seam authenticator infrastructure defined in components.xml. Uses @Picketlink annotation for authentication, also stores the user bean into
http session, to be validate at the SecurityFilter. Also produces a bean named “loggedUser”,
to export the authenticated
Send an event@In

private Events events;

events.raiseTransactionSuccessEvent(“bookingConfirmed”);

 

Receive an event

@Observer(“bookingConfirmed”)

public void getBookings() {

Send an event

@Inject

private Event<BookingEvent> bookingevent;

bookingevent.fire(new BookingEvent());

Receive an event

public void updateBookingList(@Observes BookingEvent bookingEvent)

New classes were added

  • Resources.java: Produces and close the EntityManager.
  • SecurityAction.java: Control the login and logout process.
  • SecurityFilter.java: Control the access to the protected pages. This could be implemented using DeltaSpike Security api

Now the changes to the facelets pages and descriptors.

There are lots of changes only to replace JBoss Seam taglibs to JSF 2 taglibs, the most commons are show below. Most of them are very intuitive to follow, so I will not explain.

JBoss Seam

Java EE 7

Removal of JBoss Seam taglib definition

 

xmlns:s=”http://jboss.com/products/seam/taglib”

<s:decorate id=”checkinDateDecorate” template=”edit.xhtml” > <ui:decorate template=”edit.xhtml”>

<ui:param name=”id” value=”checkoutDate” />

On the template fragment edit.xhtml, needed to add the id attribute as #{id} as its value, as the template rendered leads to error of an already used id value.

Also, use the ui:param to send parameters to the template page.

 

 

<a:support id=”onblur” event=”onblur” reRender=”creditCardNameDecorate”/> <f:ajax render=”message_creditCardName” event=”blur” />
<s:button id=”cancel” value=”Cancel” action=”#{hotelBooking.cancel}”/> <h:commandButton id=”cancel” value=”Cancel” action=”#{hotelBooking.cancel}” immediate=”true”/>
<link href=”css/screen.css” rel=”stylesheet” type=”text/css” /> <h:outputStylesheet name=”css/screen.css”/>

 

To run, you need to add a datasource named java:/bookingDatasource

You can download the booking migration.

Write a comment if there is something missing or if it helped you.

6 Comentários

  1. Hi, I’ve found your blog by chance and since then I’ve learnt so much.
    A couple of days ago I was trying to set up this project but I get a JPA error “Description Resource Path Location Type
    Console configuration “booking” does not exist. booking line 0 JPA Problem”

    Do you know how to fix it? seems to be a Hibernate configuration error but I can’t get it to work.
    Thanks in advance

    • Hi Opfer, I have seen that error for some JPA eclipse projects, it is related to the Hibernate Console plugin, where you can type JPAQL queries. Just ignore it for the moment, or you can configure it, go to the hibernate perspective and add a configuration, setting the database connection and hibernate version, pretty intuitive.

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *