Michael Yuan

“Science is a wonderful thing if one does not have to earn one’s living at it” — Albert Einstein

November 29th, 2006

Official Seam demo app for Glassfish

As an application framework for Java EE 5.0, JBoss Seam works on both JBoss and Glassfish application servers (see Riger Kitain and Brian Leonard's blogs). From Seam 1.1, we will officially maintain the Hotel Booking example application for Glassfish. To use it, just go to the examples/glassfish directory, run ant, and then deploy the build/jboss-seam-glassfish.ear application into Glassfish. The application is available at http://localhost:8080/jboss-seam-glassfish. The example is in CVS now and it should be available in distributions from Seam 1.1 CR2.

But of course, the Hotel Booking is just one example. How do you port other Seam apps to Glassfish? Here are how.

Choose the Hibernate JPA

First, let's be clear that we highly recommend you use Hibernate as the JPA (Java Persistence API) provider in Glassfish. By default, Glassfish uses "TopLink Essentials" (a.k.a the watered-down "lesser TopLink") for JPA implementation. It might be fine for basic JPA needs but Seam makes good use of Hibernate specific features such as Hibernate validators and filters etc. In fact, it would be foolish not to use the Hibernate JPA with Seam considerring how easy it is to install Hibernate JPA in Glassfish. :)

By including Hibernate JARs in your EAR, you can enable Hibernate JPA for a single application. Or you can simply copy Hibernate JARs to Glassfish's lib directory and enable Hibernate JPA for all applications. To use the Hibernate JPA, just choose the proper persistence provider in your persistence.xml as we do below.

If you have to use the "lesser TopLink" JPA, we also have a toplink build target in the examples/glassfish project. But be aware that you need to load the database manually for the hotel data since TopLink does not read the import.sql file.

Diff JBoss Glassfish

All the changes from a JBoss deployment to a Glassfish deployment concern the configuration files and library JARs only.

Since Glassfish uses the JSF RI not MyFaces, you should first make sure that the MyFaces phrase listener is turned off. Just comment the following lines out in web.xml

XML:
  1. <!-- MyFaces -->   
  2. <!--   
  3. <listener>       
  4.   <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>   
  5. </listener>   
  6. -->

Glassfish requires you to declare all EJB3 session bean reference names in the web.xml file for the web application to access the beans. This is a rather tedious process. But you have to add the following lines in web.xml for each session bean in your application.

XML:
  1. <ejb-local-ref>       
  2.   <ejb-ref-name>jboss-seam-glassfish/BookingListAction/local</ejb-ref-name>       
  3.   <ejb-ref-type>Session</ejb-ref-type>       
  4.   <local>org.jboss.seam.example.booking.BookingList</local>       
  5.   <ejb-link>BookingListAction</ejb-link>   
  6. </ejb-local-ref>

You also need to tell Seam the session bean naming pattern you just used in web.xml, so that Seam can locate those beans. So, make sure you have the following in components.xml file.

XML:
  1. <core:init jndi-pattern="java:comp/env/jboss-seam-glassfish/#{ejbName}/local" debug="true"/>

In order to use the Hibernate JPA with Glassfish's buid-in JavaDB (Derby database), you need a persistence.xml file like the following.

XML:
  1. <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  3.    
  4.    <persistence-unit name="bookingDatabase">
  5.      
  6.       <provider>org.hibernate.ejb.HibernatePersistence</provider>
  7.       <jta-data-source>jdbc/__default</jta-data-source>
  8.       <properties>
  9.        
  10.          <property name="hibernate.dialect"
  11.               value="org.hibernate.dialect.DerbyDialect"/>
  12.          <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
  13.          <property name="hibernate.show_sql" value="true"/>
  14.          
  15.          <property name="hibernate.cache.provider_class"
  16.               value="org.hibernate.cache.HashtableCacheProvider"/>
  17.       </properties>
  18.    </persistence-unit>
  19. </persistence>

Finally, you need to bundle the following JAR files in your EAR in addition to any libary files you already need for JBoss deploment (e.g., jboss-seam-*.jar, facelets JARs, and Ajax4jsf JARs etc.)

CODE:
  1. hibernate*.jar:              Hibernate3, Annotation, EntityManager JARs
  2. thirdparty-all.jar:          Third party JARs for Hibernate JPA outside of JBoss AS
  3. jboss-archive-browsing.jar:  Required for Hibernate EntityManager
  4. commons-beanutils-1.7.0.jar: Required by Seam outside of JBoss AS
  5. commons-digester-1.6.jar:    Required by Seam outside of JBoss AS

That's it. Now enjoy!

November 27th, 2006

Why did Nuxeo Switch from Python/Zope to Java/Seam?

Nuxeo (formerly CPS) is a major open source enterprise content management system. After all, it already has multi-million dollar deployments in places ranging from French Nuclear Agency to Fortune 500 companies. Recently, Nuxeo announced that they would re-write the next version of their product in Java, and build it around the JBoss Seam framework (together with other Java EE and Eclipse frameworks).

Normally, it takes a lot for such a mature and successful project to switch platform and "start from scratch". The new platform must be substantially "better" in order to justify the switch. So, what are the reasons for Nuxeo? Well, it is the best to hear it from the Nuxeo guys themselves! Checkout the Nuxeo presentations at the recent JBoss World conference in Berlin. For the impatient, Nuxeo seems to be able to take advantage of all key Seam features:

* Improve usability of JSF (e.g., much improved validation, bookmarkable URL etc.)

* Make ORM (e.g., Hibernate) work better with web applications (e.g., no lazy ladoing errors)

* Automatically manage cached content in finely grained contexts

* Manage the application state better than just jamming everything into the HTTP session (e.g., nested conversation, multiple browser windows support etc.)

* Integrate business processes

* Add AJAX features to complex stateful applications

* Take advantage loosely coupled components and Seam dependency bi-jection

* Take advantage of the built-in asynchronous event and method invocation model in Seam

Checkout the presentation and see all those for yourself! I think it is interesting because I keep hearing how dynamic language like Python or Ruby would "kill Java" -- guess not so fast!

November 23rd, 2006

Articles: Seam POJO apps without the EJB3 container

As promised, here are the two articles on how to build Seam POJO applications and deploy them in J2EE or Tomcat (no EJB3) servers.

I think the important news here is that you can deploy Seam applications on a commercially supported application server now!

November 16th, 2006

Rapid Seam Development with NetBeans

NetBeans is my choice of IDE and JBoss Seam is my choice of application framework. It is a shame that those two do not integrate well. Well, that is, UNTIL NOW! I have just added NetBeans support to Seam Gen -- the RoR like command line application skeleton generation tool for Seam (yes, it also generates entity objects from existing DB tables). By answering a few questions on the command line, you can generate a skeleton Seam project with all the necessarily configuration files, source directories, build script, and best of all, IDE integration. The generated Seam project can be opened in both Eclipse and NetBeans, with support for the rapid "edit/save/refresh browser" workflow people seem to crave for ... I will cover Seam Gen in detail in the book. But here is what to expect in the NetBeans environment:

Below is what the generated project looks like in NetBeans. The left is the overall structure. You can click on any file to edit -- with syntax highlighting, compiler/schema checking, and other NetBeans editor goodies. The right is an expenaded view of the classes in the project.

Picture 11.png

Right click on the project, you can build it and deploy it (Seam Gen already asked for your JBoss AS home when setting up the project). Now, here is the cool part: When the application is running, you can make changes to any file (Java classes, XHTML pages, or configuration files) and click on the "restart" menu. The changed files are automatically pushed to the server and you can simply refresh your browser to see the change. Is niiiice!

Picture 16.png

PS. Sun's Brian Leonard has a series of blog posts on NetBeans and Seam. His setup is more complex but it does not depend on Seam Gen and provides tighter App Server integration.

November 14th, 2006

Seam without EJB3

When I tell people that you can now run Seam apps outside of the EJB3 container, I got two typical responses: "You must be joking" or "This is not news. We have been running Seam in Tomcat from day one".

Well, both responses have their points. Seam is developed and marketed as a "framework for Java EE 5.0", and to "support the EJB3 component model in the web tier". It has always been tied closely with EJB3. It is true that Seam applications have always been able to deploy to Tomcat with the help of JBoss Embeddable EJB3. But the keyword here is "Embeddable EJB3" -- the application bootstraps its own EJB3 container. With Seam 1.1, you can finally deploy Seam application in plain J2EE server or in Tomcat without embedded EJB3 runtime!

Seam has always supported POJO components in addition to EJB3 components. You can use Seam POJOs to replace EJB3 session beans and Hibernate POJOs to replace EJB3 entity beans. Instead of writing

JAVA:
  1. @Stateful
  2. @Name(myBizComp)
  3. public class ManagerAction implements Manager {
  4.   @PersistenceContext (type=EXTENDED)
  5.   private EntityManager em;
  6.  
  7.   ... ...
  8.  
  9.   @Destroy @Remove
  10.   public void destroy () { }
  11. }

You can simply write

JAVA:
  1. @Name(myBizComp)
  2. public class ManagerPOJO {
  3.   @In (create=true)
  4.   private Session session;
  5.  
  6.   ... ...
  7. }

However, in the past, even if your application is entirely written in Seam POJOs, you still need EJB3 container to run it. With Seam 1.1, we finally get rid of this dependency. As a result, you can deploy Seam applications in any J2EE app servers (JBoss, WebLogic, WebSphere, Oracle etc.) as well as Tomcat (without the need to bootstrap an EJB3 container). For JBoss customers, that also means you can get official support for your Seam applications now (the EJB3 stuff is not supported until JBoss AS 5).

Of course, there is no free lunch. Running Seam POJOs outside of EJB3 container means that you do not get container services such as async mesaging, deployment management etc. But the Seam runtime does provide injection of the managed EntityManager (or Hibernate Session) object as well as transaction demarcation. So, for basic database driven web applications, you will proably do just fine.

Anyway, I am writing a white paper on this subject as we speak and it will be adequately covered in the book. But for the impatient, checkout the "jpa" and "hibernate" example apps in the Seam 1.1 distribution. They both run out of the EJB3 container -- the "jpa" example uses the Hibernate JPA persistence provider (same as EJB3 session beans) and the "hibernate" example simply uses Hibernate POJOs.

Update: Checkout the Seam POJO app articles here.

November 13th, 2006

What’s real in Borat

Borat is one of the most outragous, offensive, and yet hilarious movie I have seen. But I always thought that scenes in the movies were "staged" by actors -- very much like the fake reality show "The Office".

borat.jpg

Well, I was wrong. According to this Salon story (you need to watch a short ads to get in), most of the Borat scenes are shot with *real people* who did not know it was a prank. Even the scene in the rodeo was shot with a real rodeo! You can read people's reactions from the article. It is even more hilarious than I thought! :)

November 13th, 2006

Confusion over open source Java ME

Sun has finally announced their open source plans for Java ME. As it turns out, Java ME will be licensed under strict GPLv2 -- without the exception made for Java SE. That means the Java ME license is viral to all software built on top of it. All those MIDlets or Xlets extend classes from Java ME implementation after all!

Device manufacturers still have to license a non-GPL version of Java ME. Or, all Java software bundled in the device must be open source as well.

That is understandable since Sun certainly does not want to lose the large Java ME revenue it collects from Nokia, Motorola and alike. But what does it mean for developers in the OSS community? If I take the Java ME code and port it to, say, the latest Motorola Linux phone, does that make all MIDlets running on top it GPL too? Does that mean it is illegal to run non-GPL licensed MIDlets on such Java ME VMs?

I am not a lawyer. So, it would be niiiiice if someone from Sun can clarify. :)

Update: Please see Terrence Barr's explanation on java.net. In short, you are affected by GPL only if you *distribute the JVM* with your MIDP application. So, the only people affected are device vendors and integrators.

November 11th, 2006

The Prestige

We went to watch the move "The Prestige" today (Pre-order DVD). Very good movie. It is part scifi, part mystery, and part drama. The fast switching between threads and the back-and-forth timeline keep the audience on the edge all the time. (Remember, this is from the director who brought us Momento). This is simply excellent story telling. And there is a surprise ending where the magic is revealed and the real life begins -- "the prestige" -- so to speak.

ThePrestige.jpg

The real world rivalry between Nikola Tesla and Thomas Edison presented in the SciFi context is just hilarious.

Anyway, I like it a lot. If you are geek, you probably would too. :) Go watch it.

November 11th, 2006

Tennis

We took a 5-week beginner's tennis class from the local community college. It was a lot of fun. But what's impressed us most was our instructor. He is 89 years old and still very much active in the tennis court! He started playing in the 30's. I wonder what would we be like in the 2060's? What would the world be like then?

IMGP0678.jpg

IMGP0676.jpg

November 8th, 2006

My wishlist for open source Java ME

Motorola has recently announced that it will develop an open source Java ME implementation (MIDP 3) under the Apache license. This is the second open source Java ME announcement since Sun announced that it will open source Java ME with the rest of the Java platform!

Now, the big question is which hardware / OS platforms will those open source Java ME implementation run on. In theory, they could release an implementation that only runs in a software emulator on desktop Windows or Linux OSes -- pretty much like what Sun did with Java ME CDC reference implementation before. IMO, that was part of the reason why CDC failed to take hold in the PDA market. Without a variety of real devices to run those open source Java ME implementations, they would be utterly useless for most users and developers. I hope the Motorola implementation will run on the new Motorola Linux-based smartphones, and the Sun implementation will run on Windows Mobile 5.0 devices!

The next question is whether Motorola / Sun will provide implementations for important Java ME optional packages. As we all know, MIDP by itself is not very useful. The open source implementation needs at least the MMAPI and the PIM API to be useful. The Bluetooth API, the Mobile 3D API, the Web Services API, and even the Location API are also much needed. It would be ideal if the open source implementation is structured so that the user can decide what optional APIs to install on his/her own Linux/Windows Smartphone.

Well, we can hope ...

November 8th, 2006

Dallas

Some pictures from our weekend trip to Dallas:

Sculpture with the Dallas Federal Reserve Bank in the background
Sculpture with the Dallas Federal Reserve Bank in the background

Window washers
Window washers

Dallas Symphony Orchestra
Dallas Symphony Orchestra

Ancient and Modern
Ancient and Modern

Nasher Sculpture Garden
Nasher Sculpture Garden

Inside the Cathedral
Inside the Cathedral

November 2nd, 2006

Scaling Enterprise Java on 64-bit Multi-Core X86-Based Servers

Onjava just published our article on Java performance tuning on highend X86 servers. It is the result of a collaboration effort between JBoss/RedHat and Dell. Most of the advices given in the article are already available elsewhere. But we put them together and hopefully give you some more reasons to upgrade to Java 5.0. :)

multicore.png

We stress tested a number of JBoss applications, including EJB3 and Seam applications on a cluster of PoweerEdge 1955 Blade servers. The 10-server cluster has 40 CPU cores and 80GB of combined memory. I am really impressed by the performance of those servers. A single blade can handle several millions of page views per day for a small Seam application. I wish I have one of those! :)

November 1st, 2006

Hello VPS (Virtual Private Server)

As I mentioned eariler, my blog site has out-grown my cheap "shared" hosting account. WP-Cache (suggested by Johnny) helped a little but did not solve my problems. So, I decided to switch to a Vitrual Private Server at Tektonic and run my own server on Fedora. It is only slightly more expensive than mass hosting ($14/mo for 256MB dedicated RAM and $28/mo for 512MB). The service seems good so far. Let's hope it stays this way! :)

With VPS, I can finally run JBoss in my hosting account! The only caveat is that JBoss with all the EJB3 / Seam goodies is a real memory hog when starting up -- you do need 512 MB RAM. But once it starts up, the memory use is stable (no memory leak in Seam applications thanks to better session state management!) and the CPU load is in lower single digit even I hit the app hard.

With VPS, I also get to run my own mail servers and MySQL database server. It has been a couple of years since I last ran a Linux server. So, it takes some time to get everything working properly. But once it is set up, it runs a lot faster and smoother than the mass hosting provider.

Of course, that also means I now have to secure my server and keep software updated. Yum automatic update is great for that. However, the VPS host cannot install "Red Hat "Unfakeable" Linux" on my VPS even if I have a license from Red Hat. Only if I can get a Red Hat Network subscription with the VPS ...