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.

</tbody> </table>

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

@In
@Name</p>

@Scope(ScopeType.SESSION)</td>

@Inject@Named</p>

@SessioScoped</td> </tr>

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.</p>

 </td> </tr>

Hibernate validator@NotNull</p>

@Pattern

@Length</td>

Bean validation@NotNull</p>

@Pattern

@Size</td> </tr>

They are minor modifications and easy to understand.</p>

 </td> </tr>

@Logger JUL Logger
Uses the JUL Logger class, very simple to use.</p>

 </td> </tr>

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

 </td> </tr>

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.</p>

 </td> </tr>

@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.</p>

 </td> </tr>

@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.</p>

 

 </td> </tr>

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.</td> </tr>

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

 </td> </tr>

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.</p>

 </td> </tr>

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</p>

private Events events;

events.raiseTransactionSuccessEvent(“bookingConfirmed”);

 

Receive an event

@Observer(“bookingConfirmed”)

public void getBookings() {</td>

Send an event

@Inject

private Event bookingevent;</span> </p>

bookingevent.fire(new BookingEvent());

Receive an event

public void updateBookingList(@Observes BookingEvent bookingEvent)</td> </tr>

</tr> </tbody> </table>

 

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.

JBoss Seam

Java EE 7

Removal of JBoss Seam taglib definition</p>

 

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

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

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

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.</p>

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

 

 </td> </tr>

<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”/>

Categorias:

Atualizado em:

Deixe um comentário