Apache MyFaces in the cloud

The Apache Software Foundation has a lot of offerings for cloud based infrastructure. There are a lot of projects in that directions. A few examples are: Hadoop, Cassandra, HBase or some that are currently undergoing incubation: Whirr / Libcloud.

Two days ago, Amazon launched it’s AWS Elastic Beanstalk offer. A “Platform as a Service” (Paas) based on Apache Tomcat, Version 6.

Time to play!

Yesterday I tried to deploy my ‘kickstart’ project to the Amazon offer, and it worked. Within a few minutes my account was setup and I could upload the WAR file.

The project itself is quite simple, but it contains an interesting set of modern JavaEE 6 technologies:

-CDI (Apache OpenWebBeans)
-JPA (Apache OpenJPA)
-JSF2 (Apache MyFaces)

and a set of CDI extensions (Apache MyFaces CODI)!

The entire project had no problems when deploying it to the cloud. Why should it have a problem at all? The stack is running fine on a vanilla Tomcat 6. But getting it working, in the cloud, was still pretty exciting :-)

 

The project itself can be found on github. Give it (and perhaps Beanstalk) a try!

Enterprise Java without EJBs (but with CDI and MyFaces CODI)

At the W-JAX 2010 Peter Rossbach (Apache Tomcat Committer and freelancer) and I spoke about JavaEE 6 and particular the new ‘lightweight’ possibilities that are available with the WebProfile from JavaEE 6. We covered the following technologies in our talk:

  • JavaServer Faces 2.0
  • CDI
  • EJB 3.1 lite
  • JPA 2.0

We showed a demo of the integration of these tools with Apache Tomcat 7. Was good, so far….

..but why not doing it without EJB… ?

One of the above technologies is really really powerful: CDI (Contexts and Dependency Injection for the Java(tm) EE platform). With CDI it is even possible to replace EJB. All you need is an annotation (something like Spring’s @Transactional) and an Interceptor that kicks in the transaction handling for annotated methods. But… do I have to write all that stuff? The answer is NO!

The Apache MyFaces CODI project offers this as part of it’s 0.9.0 release (and way more cool CDI extensions, like new scopes etc). Don’t worry about the ‘low’ release number. It’s pretty stable but there maybe the change that there are a few changes on the API and this would be a PITA for a 1.x release…

The stack for an application like that would basically contain: JSF2, CDI, JPA. That’s it. All components are managed (including their scoping) via CDI. The JSF ‘Controller’ Bean (behind a page) uses @Inject to get access to a Service implementation:

@Named
@RequestScoped
public class SomeFacesController
{
@Inject private SomeService service;
...
}

Inside of the service implementation the @Inject is used in a similar way: The CDI Container injects our DAO object:

public class SomeServerImpl implements SomeService
{
@Inject
private SomeDao dao;
...
}

The interesting part is happening here, inside of the DAO. That’s where we use the @Transactional annotation from CODI:

...
import org.apache.myfaces.extensions.cdi.jpa.api.Transactional;

public class SomeDao
{
@PersistenceContext
protected EntityManager em;

@Transactional
public void delete(SomeObject entity)
{
em.remove(em.merge(entity));
}
...

That’s all! The stack is really really cool and there is no need for an EJB container. The Service uses a JPA-based DAO, which is uses the CODI Interceptor for the transaction handling.

I wrote two demos to use CODI + CDI with JSF (2.0 and 1.2). All you need to do to get them running is the execution of Maven (via ‘mvn’).

Have you!

Example on accessing Apache HBase with JPA

A few month ago I started to play with Apache HBase, Apache’s Bigtable implementation. Since the Bigtable impl. from the Google AppEngine (->Datastore) can be accessed with JPA, I searched for a way to use HBase with JPA as well.

Fortunately there is an easy: The DataNuculeus plugin (used by Google AppEngine) does have support for Apache HBase as well.

Over the time I noticed that folks are searching for “JPA HBase” are coming to my blog. As of now there is a SIMPLE demo: A (JSF2-based) web application that uses JPA to talk to an existing HBase installation.

I did commit to the code to github.

On
http://github.com/matzew/hbase-jpa-jsf
I also added a little README that gives some more details on how to run the example on your machine.

Note: It is just a quick and simple example that mainly should act as a playground for those that are interested in this topic.

Enjoy!

Trinidad: JSF state-saving per page

In a JSF/Trinidad application the “state-saving” is configured via some parameters in the web.xml file. Trinidad adds a bit more to it with its TOKEN approach, check the documentation for the “org.apache.myfaces.trinidad.CLIENT_STATE_METHOD” to read more. The result of the configuration via WEB.XML file is that the entire configuration for the state-staving behavior is GLOBAL. In Trinidad most of the time we use a hybrid approach (server+token on client), but sometimes you want a specifc page to be just client-side, to avoid nasty “session expired errors”. A good example would be a login page, to have (client-only) state – of course a totally stateless page would make sense too, but this means a login page is created outside of the (Trinidad/JSF) framework.

We recently introduced a new feature to enable the configuration of the state-saving on a per-page base! For that we introduce a new attribute on the <tr:document> component (stateSaving). The “stateSaving” attribute takes three different values:

  • client (force to be (full) client)
  • server (force to be server-side)
  • default (do what ever the user said in web.xml)

So with this it is possible to tweak the state-saving on a per-page base, instead of all done globally!

Mobile Development with Apache MyFaces Trinidad

Since smartphones, like iPhone or Android phones, are getting more and more popular, more companies are getting serious about a “mobile strategy”. JavaServer Faces is a natural fit, since it’s rich component model allows you to simply switch the renderer… Yes you could introduce new tags/components for that, but’s that for sure an anti-pattern.

Apache MyFaces Trinidad is offering “mobile” support for a bunch of interesting devices, including Android, Microsoft Windows Mobile 5 and 6, or Apple’s iPhone. When companies want to offer a mobile version, they don’t just want “modern” phones… Consumer phones are still common and therefore used by potential customers. However supporting the wide range of different phones is not trivial: Some phones support JavaScript with Ajax, some without… Some have no JavaScript, some have a bad performance etc. All not to easy to deal with, when you have to do it from scratch!

The Apache MyFaces Trinidad renderkit covers that for your convenience, since it handles JavaScript free fallback during mobile rendering! Basically Trinidad assigns the JavaScript capability for a few mobile user-agents to ‘none’ in their capability files (Trinidad maintains a capability file for each/several user-agents). Fellow committer Mamallan is describing technical details here, on a thread to discuss “Apache MyFaces – JS free JSF” for Google Summer of Code.

Not only the capability of JavaScript is different, the size of the screen and the (native) look-and-feel differ as well. To archive a nice and almost native way of rendering ONE PAGE on different phones, (mobile) web developers use the the Skinning facility of Trinidad. Users/Developers have to create phone specific CSS files (every SDK has a tutorial for that, like Apple for its iPhone). Trinidad is able to switch the Skin during runtime. So you could use an EL and some code to attach the right skin, via user-agent detection.

Besides the device-specific CSS creating (and detaching) a custom skin is fairly easy. In the trinidad-config.xml you have the following:

<skin-family>#{agentUtil.phoneFamily}</skin-family>

The trinidad-skins.xml file, which contains would look like:

...
<skin>
<id>iphone</id>
<family>iphoneFamily</family>
<render-kit-id>org.apache.myfaces.trinidad.desktop</render-kit-id>
<style-sheet-name> iPhone/iPhone.css </style-sheet-name>
</skin>
...

While the “phoneFamily()” method in the underlying JavaBean would do something like:

public String getPhoneFamily()
{
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest req =
(HttpServletRequest)fc.getExternalContext().getRequest();
//Get the User Agent string from the browser
String agent = req.getHeader("User-Agent");
// Prints out user agent.  Comment out the line below for production system
if (agent != null && agent.indexOf("iPhone") > -1)
{
// If iPhone is returned.
// Comment out the followling line for production system
return IPHONE_SKIN;
 }
...

Tipp: You can ship your skins in a JAR, so that multiple (mobile) applications could benefit from the work!

Once that all is done, you are good to go! The source code of the JSPX/XHTML pages look like regular Trinidad pages! No new tags/components are required! That’s a good news, since you can leverage the knowledge you gained.

So to give an idea how rich your page could look on different phones, below are some screenshots:


This screenshot has also some information about the Trinidad TAGs that are used.

There my iPod touch and my (old ;-) ) Nokia phone:

The both show the same page, just with a different style sheet. The JSPX page is not different. All the magic is done by the Trinidad Skinning-Engine.

The ADF mobile team at Oracle put together a nice tutorial, which contains a decent demo application. It includes already some Skinnings for different mobile phones. So the getting started is fairly simple with that demo! The Trinidad developers guide has also some resources on mobile JSF development.

Generally the Trinidad library offers a pretty decent and stable framework-core (besides the 100+ components). Without its rock-solid architecture doing mobile applications would be way more odd (or complicated)!

Enjoy!

Demo of Apache MyFaces 2 and OpenWebBeans

Recently the Apache MyFaces project released its second beta release and yesterday the Apache OpenWebBeans project released its M4 release.

These are great milestones in the direction of JavaEE at Apache!

A few month ago Bernd Bohmann and I were giving a JSF2 + X presentation in Muenster, at the JUG. The presentation was great and we showed a lot of cool features of JSF2 and CDI. The big plus was that we were Apache projects (MyFaces and OWB), which we build from the trunk. Now since there are these important milestones, I was able to actually make the demo project available under my “facesgoodies” demo/kickstart project.

Get the source of the Maven project from here.

Once you extracted the source, just run “mvn” and Maven downloads all you need!

Enjoy!

combining CDI producer methods with EL

CDI’s Producer methods are nice to expose any sort of class (e.g. legacy or 3rd party (JDK)). Combining the @Produces with the @Named you can reuse the result in your XHTML JSF page.

Some Java code:

...
@Produces @Named public MyBean getMyBeanImpl()
{
....
return someBean;
}

Now the XHTML can have something like:

...
<h:outputText value="#{myBeanImpl.someOfItsProperties}" />
...

If you want, you can use the @Named with a named value (e.g. @Named(“fooBar”)). Now the fooBar also is required by the EL.

Pretty nice!

Apache MyFaces goes in strong direction to JSF 2.0

The Apache MyFaces project is very active on the JSF 2.0 front.

Recently the first alpha of the MyFaces 2.0 package has been released. Download the alpha here. Today, the Apache MyFaces Trinidad community also released the first alpha release of its JSF 2.0 efforts. Get the download here.

In the middle of January we expect to see the next alpha (or beta) of Apache MyFaces Core. The 2.0 package is pretty stable so far..

Enjoy!

JSF 2 and CDI – a nice combo!

In JSF 2.0 there is (optional) support for annotating JSF Managed beans, via the Faces Managed Bean Annotation Specification. Both Apache MyFaces and the SUN RI implement this specification.

With the advent of these two JSRs:

There is an ongoing discussion about the right annotation for the “JSF beans”.

I agree that a JSF 2.0 application should avoid using the @ManagedBean annotation. I prefer using the stuff that 299/330 are offering. The good news is that even Spring (in Version 3.0) does actually support the @inject specification (e.g. @Named).

Using the two specifications, a simple bean could look like:

...
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class HelloWorldBean
{
...
}
...

For CDI there is a neat convenience annotation (@Model) that combines the two above (@Named and @RequestScoped)!

Too bad that Spring does not support CDI (JSR 299), so the @Model is not usable there… However, making something on your own (e.g. @SpringModelBean) is not hard, as you just have to combine the @Named and the @Scope(“request”) annotation, from Spring…

UPDATE: Enabling the CDI/299 Scopes in Spring is pretty simple.

One concern maybe that 299 is only usable inside of a fullblown Java EE 6 container, like Glassfish3. But that is not the case. The implementations of the 299 specification from Apache(OpenWebBeans) and JBoss (Weld) are offering support for Servlet Containers, like Tomcat.

I added a new (very) simple HelloWorld that combines CDI(299/330) and JSF 2.0 to my FacesGoodies project. The source is located here. For the implementations I picked Apache MyFaces 2.0 (the first alpha release is out; a beta is coming soon) and Apache OpenWebBean (which recently left the Apache Incubator). The combination of these two standards really matches very well. I will add more complex demos to FacesGoodies soon, but this (trivial) HelloWorld demo is just a starting point.
Note: Currently you have to build the OpenWebBeans stuff, to have the latest greatest fixes from the trunk (or, use the M3 release from the incubator repository).

Another benefit is, as my demo uses the JettyMavenPlugin, with CDI you can use the jetty:run goal. The annotations for the JSF managed bean facility do require jetty:run-exploded, which is kinda odd… See… another reason to use CDI with JSF 2.0 :-)

Apache offers great support for these with its MyFaces and OpenWebBeans project! Check em out!

HINT: You need to build Apache OpenWebBeans in order to run this example. Do the SVN checkout from this location. And run mvn install in its root-folder!