How to access JSF from an EJB with JBoss Enterprise Application Platform 6 (aka JBoss AS7 / Wildfly)

The good old JBoss Seam framework introduced the usage of stateful session beans (SFSB) as backing beans for JSF applications. The trick was to bind the lifecycle of a SFSB to a web context, such as the session or the request context. Meanwhile this concept was integrated into the Java EE by the Context and Dependency Injection (CDI) specification. We really like to use SFSB in JSF because it provides a comfortable way to access the logic and persistence layer with an automatic and painless transaction management.

We also like to modularize our applications by separating its different layers into different Maven modules. Thus, usually the web and application logic are bundled as EJB archives, whereas the web pages are stored in a WAR archive. All modules are combined to an application as an EAR archive. In our opinion this approach is more maintainable than to mix everything into one big WAR archive.

Sometimes the web logic has to access JSF classes, i.e. to query the locale used in the current request. To do this with the JBoss EAP 6, a particularity must be taken into account. By default in the EAP6 only WAR archives containing a JSF descriptor have access to the JSF classes, EJB jars do not.

This is due to rules for implicit class loading dependencies which are added automatically by the application server at deployment time. To access JSF classes from an EJB archive, the EJB jar has to state an explicit dependency to the faces module. This is pretty simple, if you know how to do it.

First you need to know, which module you want to access. In the EAP 6.1 the module containing the JSF API is stored in:

jboss-eap-6.1/modules/system/layers/base/javax/faces/api/main

In the module.xml file you can see the name of the module:

<module name="javax.faces.api">

Now you need to add the module name as dependency manifest entry in the MANIFEST.MF file of your EJB-archive. Using Maven the best way to do this is to adapt the configuration of the EJB-jar plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-ejb-plugin</artifactId>
  <configuration>
    <ejbVersion>3.1</ejbVersion>
    <archive>
      <manifestEntries>
         <Dependencies>javax.faces.api</Dependencies>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Voila, that is it. If the application is now deployed, the JBoss EAP 6 will allow the EJBs to access the JSF classes.

As alternative to define module dependencies, you can also use the JBoss-specific jboss-deployment-structure.xml descriptor file. Using this descriptor, you can specify the dependencies for all modules of an application. But, in our opinion a dependency as the one above should be specified in the component that needs it. Thus, we prefer the alternative we presented.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s