Michael Yuan

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

February 28th, 2007

Glassfish and Seam Tips

The Seam forum regularly gets questions about Glassfish -- it is probably the third most popular platform for Seam apps behind JBoss AS and Tomcat. Here are some tips on how to make Seam apps work best on Glassfish.

1. Enable Seam logging in Glassfish: Seam uses log4j to log. Glassfish does not support log4j by default. You need to include a log4j.xml file in the JAR file for EJB3 beans (the myapp.jar file inside myapp.ear). For an example, check out the new examples/glassfish example in Seam 1.2 GA.

2. Must include the SeamELResolver: In order for Glassfish to resolve the Seam EL expressions correctly, you need to add an SeamELResolver to faces-config.xml.

XML:
  1. <faces-config version="1.2"
  2.    xmlns="http://java.sun.com/xml/ns/javaee"
  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.       http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
  6.  
  7.     <application>
  8.         <el-resolver>org.jboss.seam.jsf.SeamELResolver</el-resolver>
  9.     </application>
  10.  
  11.     <!-- Select one of the standard transaction models for the Seam application -->^M
  12.     <lifecycle>
  13.         <phase-listener>org.jboss.seam.jsf.SeamPhaseListener</phase-listener>
  14.     </lifecycle>
  15. </faces-config>

If not, you will not be able to reference Seam built-in component using their shorthand names (e.g., #{conversationList} and #{identity} etc.). This is the cause of the issues discussed here and here in the forum.

3. No need for the el-*.jar: Since Glassfish already bundles JSF 1.2, there is no need to include the el-*.jar files in your EAR file and reference them in application.xml.

Let me know if you have more tips on how to get Glassfish and Seam work more smoothly together. :)

February 21st, 2007

The return of mobile browser (Nokia N80 screenshots)

As a Java (J2ME) guy, I have always been bullish on the "rich/smart" client when it comes to mobile applications. I have seen the rise and fall of WAP. The poor usability of WAP browsers and the lack of WAP content has nearly killed the platform. I have used Opera browser on my Nokia 6600 to access "regular" web sites. But it works unreliablly with T-Mobile's $5/mo all-you-can-eat data plan (t-zones), and frankly, the web pages do not look very good when they are "reformatted" by Opera to fit the small screen. In real life, I do not know anyone who uses a phone-based browser on a regular basis. People use voice, messaging, audio/video players, and specialized rich apps such as Google Local Mobile and Yahoo! Go, but not the browser. Given the vast amount of content available on the web, it seems like a huge waste ...

However, just as we are about to give up mobile browsers, two new phones changed my mind. First, of course, is the Apple iphone. It has a really impressive Safari-based browser. We all saw the demo and I will not bore you with the details. But then, I got a new Nokia N80 and tried out its browser. I have to say that it completely blown me away. I have been getting my usual news update (digg / slashdot / cnn / nytimes / dzones / theserverside / infoq / javablogs etc.) almost exclusively on this phone in the past week and the user experience has been supurb. So, what makes the web browser on Nokia N80 so great? Well, here are some of things I liked ...

* Renders all web sites with complex CSS and JavaScript. Well, a browser would not be very useful if it cannot render most of the web pages out there. This browser renders nearly all web sites I visited including this blog and most of the CSS/JavaScript heavy web 2.0 sites. The visual elements and layout of the site is rendered almost exactly as how they would appear on a desktop browser. It does not, however, work with heavy AJAX sites like the Google Map (but that is what Google Local Mobile is for :) ), or flash heavy sites like YouTube.

Digg home page (notice the visual elements and JavaScript-based ads are rendered)
Screenshot0007.jpg

New York Times home page (notice the layout is rendered correctly)
Screenshot0016.jpg

* Provides a thumbnail view of the entire page with one keystroke or while you are scrolling. Since most web sites are not designed for small mobile phone screens. A fair amount of scrolling with the joystick is typically needed to view the entire page. The browser has a very nice feature to display the whole page in thumbnail and show the region that is currently visible on the screen. This is especially useful if you are already familiar with the site and know what information you are looking for.

Scrolling view of this blog
Screenshot0006.jpg

New York Times scrolling view
Screenshot0014.jpg

* High fidelity screen. The screen resolution on the Nokia N80 is 352x418. It is incrediblly bright and crisp -- you can see tiny letters on it. Just checkout the above screen shots. That really helps viewing text intensive web pages. Yet, the screen size is just the regular mobile phone screen size. Actually, the phone is one of the smallest I have owned.

* Reformat text columns. Even the high resolution screen cannot accomodate large text columns on some web sites. Instead of requiring the user to scroll left and right to finish long lines of text, the browser reformats large text columns to fit inside the screen. That is very useful when you read news articles or blogs.

Read a story on New York Times
Screenshot0015.jpg

Read a discussion thread on digg
Screenshot0010.jpg

* Quick zoom in and zoom out. If you think the text letters are too small to see, you can quick zoom in and zoom out on any part of the page with few keystrokes.

Zoom out and move around on the digg home page
Screenshot0041.jpg

* Supports WiFi as well as GPRS/EDGE/3G. The browser works reliablly with my home WiFi and my t-zones GPRS service. To be able to use the fast WiFi network on my coach while I am watching TV is an important feature to me.

Select my home WiFi as the access point
Screenshot0013.jpg

* Deep integration with the phone. A big advantage of the built-in browser on the phone is that it integrates directly with other software components on the phone. For instance, if you open a picture in the browser, it would provide an option to save it as use as the phone's wallpaperl; if you open a MP3 file, the phone's built in MP3 player would kick in to play it and offer you a choice to use it as ringtone etc. I really miss this level of native integration when developing apps in the J2ME sandbox!

Download and play a MP3 file
Screenshot0017.jpg

Anyway, I just think that the Nokia NSeries phones have done some great innovation when it comes to mobile browsers. It is rumored that this browser is also based Safari (like iphone) and will be open sourced soon. I think that would be really exicting times ahead of us. :)

February 15th, 2007

Updated Source Code Download for the Book

NOTE: The final version of the book is updated to Seam 1.2 to include the security framework etc.

Okay guys, sorry for the delay. I finally completed the book -- it is updated to include cool Seam 1.1.5 features, and is now 400+ pages. The book is now in production and should be in book stores everywhere in the US in April. I think Safari Rough Cut will probably update its PDF to the "final" version of the book a couple of weeks before the physical book appears. So, buy it on Safari if you cannot wait. :)

In the meanwhile, I will release the latest sample applications. Download it here. Please let me know if you have any comments. Thanks!

February 1st, 2007

New Features in Seam 1.1.5

One and half month after Seam 1.1 release and 14,000 downloads later (yes, Seam is breaking 10k download per month), we are now releasing the latest and newest Seam 1.1.5. It has some very nice features not found in *any* web application framework out there. Here are some highlights.

The rule-based finely-grained security framework

Managed security is one of those "half measure solutions" in J2EE. The standard J2EE security works okay for the simplest authentication cases (i.e., to put part of the site complete behind a password protected wall). But more often than not, developer struggle hard to fight against the standard security schemes than using it.

In Seam 1.1.5, we provide a much better security model based on the JBoss Rules engine. You can have a very detailed set of rules to specify who is permitted to access which page, which UI element, which bean methods. Users are managed in groups and the permission meta data are easily expressed in annotations and JSF tags. It is a security solution for almost every use case a web application encounters. Here are some examples to show how to restrict access to methods, pages, and components based on the user's role.

XML:
  1. <!-- only users with "admin" role can view this page-->
  2. <page view-id="/reports.xhtml">
  3.   <restrict>#{s:hasRole('admin')}</restrict>
  4. </page>

JAVA:
  1. // Only logged in users with role "admin" can execute this method
  2. public class AccountAction {
  3.   @Restrict("#{s:hasRole('admin')}")
  4.    public void delete() {
  5.     // code
  6.   }
  7. }

XML:
  1. <!-- only users with "admin" role can see this UI component -->
  2. <h:outputLink action="#{reports.listManagerReports}"
  3.                     rendered="#{s:hasRole('admin')}">
  4. Manager Reports
  5. </h:outputLink>

PDF output

With the new PDF support, you can generate PDF documents, instead of XHTML pages, from Facelets view files. You can use the Seam stateful component model and JSF EL in the PDF. Just replace the XHTML component tags in regular web page with PDF tags, and you have a PDF output instead of a web page output. Extremely useful feature for sites that need to generate reports. Here is an example JSF page that outputs a PDF document with two chapters ...

XML:
  1. <p:document xmlns:p="http://jboss.com/products/seam/pdf"
  2.             title="Hello">
  3.  
  4.    <p:chapter number="1">
  5.       <p:title><p:paragraph>Hello</p:paragraph></p:title>
  6.       <p:paragraph>Hello #{user.name}!</p:paragraph>
  7.    </p:chapter>
  8.  
  9.    <p:chapter number="2">
  10.       <p:title><p:paragraph>Goodbye</p:paragraph></p:title>
  11.       <p:paragraph>Goodbye #{user.name}.</p:paragraph>
  12.    </p:chapter>
  13.  
  14. </p:document>

Template-based email support

Sending email from your web application is not hard but it can be a messy task. The developer has to embed email text in Java code -- a major source of poorly worded email messages from applications. In Seam 1.1.5, we provide a template based approach to handle email. You can get your page designer to write email as if it is a web page. Then from Seam, you can execute a method to render the email and send it out. Here is an example email as a JSF view page:

XML:
  1. <m:message xmlns="http://www.w3.org/1999/xhtml"
  2.            xmlns:m="http://jboss.com/products/seam/mail"
  3.            xmlns:h="http://java.sun.com/jsf/html">
  4.  
  5.   <m:from name="Peter" address="peter@example.com" />
  6.   <m:to name="#{person.firstname} #{person.lastname}">#{person.address}</m:to>
  7.   <m:subject>Try out Seam!</m:subject>
  8.  
  9.   <m:body>
  10.     <p><h:outputText value="Dear #{person.firstname}" />,</p>
  11.     <p>You can try out Seam by visiting
  12.     <a href="http://labs.jboss.com/jbossseam">http://labs.jboss.com/jbossseam</a>.</p>
  13.     <p>Regards,</p>
  14.     <p>Peter</p>
  15.   </m:body>
  16. </m:message>

And here is the code to actually send out the email to #{person.address}

JAVA:
  1. @In(create=true)
  2. private Renderer renderer;
  3.  
  4. public void send() {
  5.   try {
  6.   renderer.render("/simple.xhtml");
  7.   facesMessages.add("Email sent successfully");
  8.   } catch (Exception e) {
  9.   facesMessages.add("Email sending failed: " + e.getMessage());
  10.   }
  11. }

Rich text support

Rich text support is a much needed feature in many web applications. But how do you trust rich text input from users? Obviously, you cannot allow them to enter arbitary HTML text as it opens up many security issues. Wiki text, on the other hand, is an excellent alternative. Seam 1.1.5 provide a UI component to convert wiki text from your database to HTML displays on a page on the fly. Nice feature if you are building a community oriented (a.k.a web 2.0) web site.

New JSF controls

Examples include <s:selectItems>, <s:selectDate>, and <s:fileUpload> etc.

WebSphere 6.1 compatible

From Seam 1.1.5, we will support Seam on the latest WebSphere application server. It may not sound like a big deal since we already support Seam on WebLogic, Tomcat, and Sun AS. But WebSphere is a beast that requires its own proprietary JDK to run -- it is a big deal that Seam works just fine on WebSphere 6.1 :)