How to use JSF 2.2 with JBoss 7.1

As the new JSF 2.2 is nearly finished, we all want to play with the new features of it.

Markus Eisele has shown in his blog post Testdriving Mojarra 2.2.0-m08 how to do this with Glassfish 3. He’s German too, I begin to wonder if only we guys are that curious or no one else is using JSF any more 😉

Unfortunately with JBoss 7 we face the same problem he had: We can not just add the new libraries to our WAR archive as they will clash with the JSF 2.1 version of Mojarra (the reference implementation of JSF) already provided by JBoss 7.
So basically we have to disable this old version and activate the new version directly in the application server.

I will show you how to do it with JBoss 7.1.1 and the M08 release of Mojarra 2.2, but it should work for other versions the very same way:

JSF is divided into two parts: The implementation and the API.

We firstly exchange the former one by starting to download jsf-impl-2.2.0-m08.jar, which is the JAR archive containing the implementation.
After downloading, copy this archive to the folder of the old JSF implementation of your JBoss installation, which will be %JBOSS_HOME%/modules/com/sun/jsf-impl/main in that case.
In the same directory you will find a file called module.xml. Edit it and change the contents of the resources tag like this:

This is important as it tells JBoss which of the provided JAR archives (now there are two of them) to use. If you want to know more about this: Take a look at JBoss Modules, a module class loading approach JBoss 7 is using.
After that you have finished with exchanging the implementation and basically you have to do the same for the API part:

Firstly download the jsf-api-2.2.0-m08.jar archive and copy it to %JBOSS_HOME%/modules/javax/faces/api/main. Secondly edit the module.xml in the same directory by exchanging the resources tag:

Basically you should be finished now, but there is a bug in the milestone 8 of the API that it has a dependency to the implementation. For that reason you have to declare it by adding the following contents to the dependencies tag of the same module.xml file:

After that you can restart your modified JBoss instance. As JSF is lazily loaded you have to deploy a WAR referencing JSF to check whether our changes work. If so, you should see a log message like this:

[javax.enterprise.resource.webcontainer.jsf.config] (MSC service thread 1-2) Mojarra 2.2.0-m08

Finally, if you want to use the new features in application, you have to add the following dependency to your pom.xml:

And now have fun using JSF 2.2!

Doing EJB-like transactions with Google App Engine

Using Google App Engine you can’t define a method as transactional with a simple annotation as you can in EJB.
You always need to call some boilerplate code which gets quite annoying.
Therefore the following utility class comes handy, which takes care of the transaction handling for you:

To make use of it, you have to set the same persistence unit as in your persistence.xml configuration file first. Do this by changing the parameter of the method createEntityManagerFactory.

After that you can easily define transcation boundaries like this:

In this example the transaction method has a return type of String – change this if you need a different one.
The transaction is always starting a new transaction, similar to REQUIRES_NEW in EJB (more about that in the Java EE 6 tutorial). If you want to use another transaction strategy, you will have to modify the code.
It is still not as easy as setting an annotation in EJB, but way better than without. If you want to define transactions for Google App Engine via annotations, you will need some DI engine like Guice or CDI, but then you add a lot of other code that you probably don’t need.

Using the Facebook Graph API from your GWT application

It might happen that you have a GWT application and you want to use the Graph API from Facebook.
As the Graph API requires an authentication with OAuth2 this task can be easily accomplished using the GWT-OAuth2 library.
To simplify things even more I build the following utility class for you:

After setting the unique ID of your facebook app in the constant FACEBOOK_CLIENT_ID, you can easily use the Graph API by calling the doGraph method.
Here is an example that posts a message on the feed of the app user by sending some data to the me/feed path:

As you notice the variables message and url need to be defined with values of your choice before. Now have fun trying other methods of the Graph API.
If you need further permissions, you have to add them as parameters to the call of withScopes inside of the doAuth method.

Storing files RESTful in the cloud using Google App Engine

Do you want to store files RESTful in the cloud? Why not use the Google App Engine for it?

Firstly you will need a entity class that is storing the file in the data store:

Beside the actual content of the file, this entity also stores the media type (you never know, it might be good to know…).
As I just like standards, you also might mention that I used JPA as much as possible (Blob is a Google specific thing, as unfortunately byte[] can not store large amounts. BTW: Blob is also limited by 1MB, so you can not store larger files than 1MB using this approach).

Secondly you will need a JAX-RS resource class that is handling the GET and PUT requests from clients:

At last you just need a small helper class (EMF.java) that is handling the transactions and of course an JAX-RS implementation (I used Jersey 1.6 – works perfectly with App Engine).

To test the file store, just call:
curl --upload-file mypic.jpg -H "Content-Type: image/jpeg" http://localhost:8888/rest/file/store

This request returns the URL of the file that is stored. Just request this URL to retrieve your file back from the storage.

Have fun and let me know, if you are using this code for some of your projects.

Using real POJOs (without JAXB Annotations) as transfer objects with JAX-RS

Are you annoyed that you have to annotate your POJOs with @XmlRootElement, so they can be used with JAX-RS? If your using Jersey as JAX-RS implementation your lucky: Just add to the <servlet> tag in your web.xml the following snippet:

After restarting your servlet, your POJOs are marshalled to JSON as a charme. Enjoy!