I’d never been a fun of xml files. Never. I like to put my money where my mouth is, and that is not in xml files. Then came EJB3. And just when I was getting settled down with the new craze (Annotations), along came Seam. Now I could happily run away from my EJB 2.1 days of verbose xml and never go
down the trail of “xml hell” again, editing xml just when i
really, really need to. Alas, twas just a dream. When you are faced
with an application that needs to be flexible and extensible,
sometimes the best (and probably only) way is through xml. And that
is exactly what I was faced with recently. However, JBoss reminded me
of one of the reasons why Seam is a compelling choice of framework –
tools support through JBoss Tools. Now I’ve fallen in love with xml
editing – simply because I don’t have to do any.
Here’s the scenario. Let’s say I were
developing an application for playing games, allowing users to play
different games which obey a consistent API. This means a separation
into interfaces and implementations. Here is how I tried to create
Plugnplay-api – consistent
interfaces that are to be coded to.
Plugnplay-impl – implementations
of the above api. Contains Seam components (i.e. Entities, Actions,
UI components etc).
Plugnplay-web – the basic Seam
application (here’s where the facelets as well as the aforementioned
components finally end up).
This meant that I’d have to use the
components.xml file to specify the names (instead of @Name) as well
as other properties of my Seam entities and actions. No hustle. I
immediately realized how big such a file will get. Thankfully Seam
allows you to define one components.xml file for each component like
Hibernate (not my idea of fun) or one components.xml file per
package. I chose the second option and decided to put JBoss Tools to
work. The result was pleasing. Let’s look at mapping different
implementations of the Game interface – DominoGameImpl and
Select File -> New -> Other.
Scroll to XML subcontent and
Enter the name “components.xml”
as file name and click Next.
In the “Create XML File From”
screen, choose “Create an XML file from an XML schema file”.
This forces our xml file to conform to a schema, in this case the
components-2.0.xsd from Seam. Click Next.
At XML Schema File, select “Select
XML Catalog entry”. This should show a list of all schema files
currently registered in your Eclipse installation.
Scroll through the XML Catalog and
find the key “http://jboss.com/products/seam/components-2.0.xsd”.
At the Root Element dialog box,
change the default Root element of “action” to “components”.
This is the root element in all components.xml files, and allow
other child elements such as pdf, mail etc. You may leave the other
checkboxes as their default. Click Finish to end the wizard.
Finally our components.xml file is
created and displayed in the Seam Components Editor. At this top
level we can add components, define factories, events and imports.
Without much ado we’ll add our Game components. This is the part I
love the most.
Click “Add” to add a seam
Enter the name of the Seam
To add the particular class, click
Browse. You have a window with 3 tabs – “Search”, “Browse”
In “Search” begin typing the
name “DominoImpl” as our Seam component. A dynamic list of
matching classes are displayed as you type to select the particular
one you want.
Select the appropriate class and
In the Seam Components Editor, you
may now specify the scope as well as other properties. I’ll choose
CONVERSATION from the Scope drop down list. Press Ctrl+S to save.
Repeat these steps to add the
MonopolyGameImpl as a Seam component.
Now that we have our games, we want to
associate them with a particular user. Our User API defines a list of
Games as a field. We repeat the above process in the “admin”
package, selecting UserImpl as with the component name “user”.
Set the scope of the component as you wish.
To add properties to the “user”
component do the following.
Click on it in Component Editor.
In the Properties sub-section, click on Add… . You immediate get
three options to select from; “Add Simple Property”, “Add List
property” and “Add Map property”.
Choose the 2nd option.
In the dialog box that appears give our list the name “games”,
mapping to the getGames and setGames of the UserImpl. We can now add
values to the games list. Add the name “monopoly”.
Repeat the steps above to add
“domino” as another game to the “games” list.
Your Editor should look like this
This XML support in JBoss Tools allows me to easily wire my components together (like Spring, though Spring IDE can’t compare) and still get all the ooze of Seam. So why wouldn’t I fall in love with XML editing with JBoss Tools. And note, JBoss Tools 3.0-alpha is out with experimental pages.xml editor, visual view of navigations, portlet and a host of other stuff that you are better of finding out for yourself. And I’ll definitely be blogging about my experiences with it very soon.