Joshua Java

Posts Tagged ‘Seam

2009 is coming to your way and it’s time to get ready on what technology lies ahead. Refactor, refactor and refactor is the theme for 2009. A whole new technology is coming your way and it’s good to take a sneak peek on what technology is worth evaluating to be used for our projects in 2009. The first half technology are the technologies that you should keep your eyes at while the next half are the technologies that you might want to start trying for your next project in 2009. So here are the lists for 2009:
Read the rest of this entry »

Advertisements

Wow, I just realized that I haven’t wrote anything in this blog for more than a month. Shame on me. Ok here’s another code that hopefully be beneficial. Recently I had a project where I need to display the enum values in local language. Thanks God for Seam I can easily do this.
In your model class (e.g Person.java)

public class Person {

	public enum Gender {
 	MALE,
 	FEMALE;

		@Override
 	public String toString() {
 		switch (this){
 			case MALE: return ResourceBundle.instance().getString("male");
 			case FEMALE: return ResourceBundle.instance().getString("female");
 			default: return super.toString();
 		}
 	}

	private Gender gender;
 public Gender getGender() {
 	return gender;
 }

	public void setGender(Gender gender) {
 	this.gender = gender;
 }
 };

I had this inner enum inside a Person class and in there I just override toString() method. Inside the method I just call the Seam’s ResourceBundle component and call the appropriate message from my resource bundles which is located in messages.properties.Now when I call the gender property from my view, it will be automatically localized.

        <h:panelGrid columns="2" columnClasses="form-column"
            rowClasses="form-row" styleClass="form-table">

            <h:outputLabel for="gender" value="#{messages&#91;'gender'&#93;}" />
            <h:outputText id="gender" value="#{client.gender}" />

        </h:panelGrid>

I’ve tried running my Seam apps on Glassfish 2.1 and it’s running very smooth and faster than before. This is a major improvement to Glassfish 2.0. Thanks for the hardwork done by the Glassfish team.

Alot of people wondered whether Seam can be deployed on other appserver other than JBoss AS. Well indeed it can. Now I’m going to show how to deploy Seam on Glassfish in a very simple way without you having to modify your project stub that is created with seam-gen. So in the future you only have to modify your persistence.xml, other than that you can deploy the same seam apps you deploy on JBossAS. Note that this only works with Seam POJO apps and doesn’t work with Seam EJB3 apps mainly because Glassfish requires you to write the EJB3 reference on your web.xml. So it’s not Seam fault! As this blog entry is written I am using Seam 2.x and Glassfish 2.

Here’s what you need to do:

  1. Copy these libraries into $GLASSFISH_HOME/domains/$DOMAIN_NAME/lib/ext:
    • javassist
    • dom4j
    • hibernate-validator
    • commons-logging
    • cglib-nodep
    • eclipse-jdt-core. In seam distribution this is packaged as core.jar đŸ™‚
    • antlr
    • log4j
    • Your database JDBC driver. I use mysql, so I would copy mysql-connector-java here
  2. Copy these libraries into $GLASSFISH_HOME/lib: Here we will use Hibernate as the persistence layer and will override toplink which is the default persistence layer from Glassfish because Seam does not yet support Toplink as the persistence layer for Seam POJO apps.
    • jboss-archive-browsing
    • jboss-common-core
    • hibernate core. The core library
    • hibernate-annotations
    • hibernate-commons-annotations
    • hibernate-entitymanager
    • hibernate-validator
  3. Note: These libraries is bundled with seam distribution so you don’t have download it separately.
    FAQ: We copied these libraries into two different folders because Glassfish has a very unique classloader and becaus we want to override Toplink which is located inside $GLASSFISH_HOME/lib

  4. Create your connection pool and datasource from Glassfish admin console. Create it from these menu from inside of your admin console:
    • Resources > JDBC > Connection Pools
    • Resources > JDBC > JDBC Resources
  5. Open up your persistence.xml. If your project is created with seam-gen this will be located inside $PROJECT_HOME/resources/META-INF/. And then edit these lines:
    • element into the datasource you’ve created on previous step
    • hibernate.dialect property into the database you use. You can find these dialects for various databases inside hibernate docs.
    • hibernate.transaction.manager_lookup_class into org.hibernate.transaction.SunONETransactionManagerLookup

Now the next time you create and deploy another project to the same Glassfish server, you only need to do step 3 and 4. Good luck đŸ™‚

I think Seam is really great for handling conversational context, but recently I faced a problems when I wanted to unit test my action method that is annotated with @Conversational. Now if you annotate your action method with @Conversational, this means Seam assume you are in a conversation to access that method, which means you have to have a conversation id to pass to Seam. Now the problem is how do you provide these conversation id for testing in Seam? Luckily everything is very simple in Seam and I can pass a dummy id and Seam don’t really care if it is as real as in real apps or not. But I wanted to make it as real as it is in real apps, and here it goes:

Assuming you have created your action with @Conversational method as such:

@Name("applicationForm")
public class ApplicationForm implements Serializable{

	@Logger Log log;

	@Conversational
	public void save(){
		log.info("Saving...");
	}
}

Here is the in testing code which I assume you’re already familiar with, if you don’t you can take a look Seam docs regarding testing. đŸ™‚

public class ApplicationTest extends SeamTest {
	private String cid;
	private List<String> cidStack;

	private static LogProvider log = Logging
			.getLogProvider(ApplicationTest.class);

	@Test
	public void testApplicationForm() throws Exception {
		new ComponentTest() {
			protected void testComponents() throws Exception {
//				cid = (String) invokeMethod("#{ConversationIdGenerator.getNextId}");
//				log.info("cid= " + cid);

				cid = ConversationIdGenerator.instance().getNextId();
				log.info("cid= " + cid);

				cidStack = new ArrayList<String>();
				cidStack.add(cid);

				Manager.instance().setCurrentConversationId(cid);
				Manager.instance().setCurrentConversationIdStack(cidStack);

				Conversation.instance().begin();

				invokeMethod("#{applicationForm.save}");

				Conversation.instance().end();
			}
		}.run();
	}
}
  1. So first you need to create a conversation id with the ConversationIdGenerator component. Now there are two ways to create it, the remarked line is the first way but I like the second way better. If you take a look at the code, that’s how Seam create the conversation id as in real apps, though as I said before you can just create your own id which Seam don’t care about.
  2. You then put the id that is generated on previous line in a List of String.
  3. Now you set the conversation id and the conversation id stack and pass it to Seam conversation manager component as it will be used for the conversation.
  4. Now you can start the conversation with the conversation component instance and invoke your conversational method. đŸ™‚
  5. Don’t forget to end the conversation đŸ˜‰

IMHO Seam is really cool indeed.

Tags:

I know that Seam is by default use ant for building the project and the seam-gen generates the ant projec structure, but there are several reasons I found quite worthy to use maven with Seam.

  1. Seam requires alot of library and they are often comes in large size such as the embeddable-jboss library. So what is important about it? It is important if you are doing a project where the developers spans across several countries. You still don’t get it? It will be a problem if you are going to check out from the source repository with all those libraries.
    For people that comes from a country where bandwith is cheap, this might not be a problem but for people like me where bandwith here is expensive this is a problem.
  2. Also it will be a problem if new libraries comes/has been released and you must update it to the source repository back and forth. Again, it’s only a problem for people that comes from a country where bandwith is expensive.
    With maven you only required to change a small property inside pom.xml and then just commit back those pom.xml to the source repository instead of deleting the library from the source repo, then commit it back again. Get the idea?

Read the rest of this entry »

Tags: ,

Dear JUGgers,

Java Meet Up 07.08 will be held on August 25th 2007. I’ll be presenting my opensource project Hireway that is built using JBoss Seam. There are lots of interesting case study in it that really shows how Seam could boost development phase productivity and ease the code maintenance during maintenance phase.

Come to the event if you are nearby.

Here is the detail about the event.
Read the rest of this entry »

Tags: