Tutorials : Adding Spice to Struts Part 2 :

Listing 2 represent a pseudo implementation of the CustomMessageResources class and full source code is downloadable from here.

                public CustomMessageResources (List configFiles) {
                                super(null, null);
                                this.bundles = configFiles;
                }
                /** (non-Javadoc)
                 
* @see org.apache.struts.util.MessageResources#getMessage
(java.util.Locale, java.lang.String)
                 */
                public String getMessage(Locale locale, String key) {
                                
                                String message = null;
                                //initialise the variables we need
                                String localeKey = "";
                                if(!super.defaultLocale.equals(locale)){
                                                this.localeKey(locale);
                                }                             
                                //go through all the resource bundle files and load them
                                for (int i = 0; i < bundles.size(); i++) {
                                String config = (String)bundles.get(i);
this.loadBundle(localeKey, config);
//now look if the message can be found
message = (String)resourceMap.get(key);
//message found, so get out of the loop
if(message !=null){
break;
}else{

System.out.println("COULD NOT FIND RESOURCE FILE VALUE FOR KEY: "+key);
}
}
return message;
}   

    /**
     * Loads the resource bundle based on the locale.
     * @param localeKey The locale
     * @param config The resource file.
     */
protected void loadBundle(String localeKey, String config) {

        String fileName = null;
        InputStream inStream = null;
        Properties props = new Properties();
        //first check if this bundle is alreay loaded previously
        if (bundlesLoaded.get(config) != null) {
return;
        }
        //otherwise mark this bundle being loaded
        this.bundlesLoaded.put(config, localeKey);
        // Set up to load the property resource for this locale key, if we can
        fileName = config.replace('.', '/');
        if (localeKey.length() > 0) {
            fileName += "_" + localeKey;
    }
    fileName += ".properties";

    //obtain a class loader
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    //if this classloader is null, use the class loader used to load this class
    if(classLoader ==null){
        classLoader = this.getClass().getClassLoader();
    }    
    //load the file as Stream
    inStream = classLoader.getResourceAsStream(fileName);      
    //check if the Stream in not null
    if(inStream!=null){
        //populate the properties from this input Stream
    try {
        props.load(inStream);
    } catch (IOException e) {
       // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
    try {
        inStream.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    }
        
        //now retrieve the key value pairs from the resource file and 
        //put it in the map
        synchronized(this.resourceMap){
Iterator iterator = props.keySet().iterator();

        while(iterator.hasNext()){
          String key = (String)iterator.next();
          //String messageKey = this.messageKey(localeKey,key);
          String value = props.getProperty(key);  
            this.resourceMap.put(key,value);
}
}          

                }else{
                                  
 System.out.println("COULD NOT LOAD RESOURCE FILE: "+fileName);
                                }
                   
                             
                }
Listing 2: Pseudo implementation of the CustomMessageResources class

Once we have this new CustomMessageResources class, the only thing left to do is to make use of it. We have overriden the Struts ActionServlet class to make use of the new CustomMessageResources class. We have decided that we will specify the message resource files in a comma separated manner within the struts-config.xml file. Here is how they should look:

<message-resources null="false" parameter="page-resources,error-resources"/>

Here is all we need to get us going. In a nutshell, we have a new CustomMessageResources class, which parses through a list of resource bundle files and loads them. The overridden ActionServlet class stores this CustomMessageResources class instance against any config key specified in the configuration or else stores against a default key.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.