Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extend the JDK Classes with Jakarta Commons, Part II : Page 5

This second installment of a three-part series further explores components in Jakarta Commons and presents real world examples to demonstrate how you can use them in your projects.


advertisement

i18n

Jakarta has not released Commons i18n yet. However, you can either obtain and build the source or download a snapshot build. In order to download the source, you need a Subversion client. I used SmartSVN to get it. Click here for a quick start guide.

You use Commons i18n in any Java application that requires the exceptions thrown and the error or success messages displayed to be localized for a particular region. The messages can be stored in XML or properties file format. In the case of XML, the advantage is that messages pertaining to all locales can be stored together.

The sample application in this section demonstrates how a locale-specific error can be thrown and handled for an application that accesses the database. I used XML for storing the messages. The Commons i18n library commons-i18n-0.3.jar needs to be in the classpath, and you need to run the TestI18n JUnit test case class to launch the application. The source code can be found in the package in.co.narayanan.commons.i18n in the src folder of the source code.



The first step is to make all the application exceptions in the system extend the org.apache.commons.i18n.LocalizedException class instead of java.lang.Exception. Listing 14 shows the application exception class used in the sample application.

Listing 14. Application Exception Extending LocalizedException Instead of java.lang.Exception
public class DAOException extends LocalizedException { public DAOException(ErrorBundle errorMessage, Throwable throwable) { super(errorMessage, throwable); } public DAOException(ErrorBundle errorMessage) { super(errorMessage); } }

The next step is to create the locale-specific error messages. Listing 15 shows the XML file used in this example.

Listing 15. Locale-Specific Error Messages
<?xml version="1.0" encoding="UTF-8" ?> <messages> <message id="insertionfailed"> <locale language="en"> <entry key="title">Database insertion failed</entry> <entry key="text">Unable to insert row value {0} to the database</entry> <entry key="summary">Summary: Database insertion error</entry> <entry key="details">The given value cannot be inserted since the database has
reported an error</entry> </locale> <locale language="fr"> <entry key="title">French - Database insertion failed</entry> <entry key="text">French - Unable to insert row value {0} to the database</entry> <entry key="summary">French - Summary: Database insertion error</entry> <entry key="details">French - The given value cannot be inserted since the
database has reported an error</entry> </locale> </message> </messages>

Note that all locale messages are grouped in the same XML file. The messages for the French locale are prefixed with 'French' so that it will be easy to decipher the output during runtime.

The next step is to create a class that will load the messages. The code snippet in Listing 16 illustrates this. It adds a new message provider and stores it against the key "dao-errors".

Listing 16. Helper Class Loading the Messages from the XML File
public class ErrorHelper { static { // Load the message bundle from the XML file MessageManager.addMessageProvider("dao-errors", new XMLMessageProvider(ErrorHelper.class.getResourceAsStream("exceptions.xml"))); } public static ErrorBundle getErrorBundle(String bundleKey, String arg0) { return new ErrorBundle("dao-errors", bundleKey, new Object[] {arg0}); } }

The final step is to use the created application exception class to indicate a failure in the system. In Listing 17, an instance of DAOException is created and passed with a reference to an ErrorBundle instance. The Commons i18n framework fetches messages from MessageManager and sends them to the ErrorBundle class for the given message key. In this case, the message pertaining to key insertionfailed is set.

Listing 17. DAO Class Using Commons i18n for Throwing a Locale-Specific Error
public void createCustomer(String name) throws DAOException { Connection con = null; Statement createStmt = null; try { // Create a customer } catch (SQLException e) { throw new DAOException(ErrorHelper.getErrorBundle("insertionfailed", name), e); } }

To get the actual message in the presentation layer of the application or to write it to the logs, you can access the ErrorBundle set in the exception object.

Using Commons i18n improves the application's design and simplifies globalizing it.

So What Have You Learned?

Now that you've completed this second installment of the Jakarta Commons series, you know the following:
  • The rich features provided by various Commons components
  • Where you can use a particular component or method from a utility class in a real-world Java project
  • A high-level understanding of the packages and classes in various APIs

Now, when you design or develop a Java application, you'll be in a position to pick a useful class and use it appropriately. Part III will discuss some of the notable remaining components.

In Case You Missed It
Extend the JDK Classes with Jakarta Commons, Part I



Narayanan A.R. is a passionate advocate of test-driven development, agile methodologies, Java technology, and design patterns. He has several years of experience in software design and development using Java technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap