Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically load files on classpath using ReloadableResourceBundleMessageSource

I'm new to Spring and am attempting to use it's ReloadableResourceBundleMessageSource class.

I'm attempting to use it so that we no longer have to restart our web app for properties files changes/updates.

I have a web app (Primarily using JSF) and a separate tar component which contains all my properties files.

The structure of the properties tar is as follows:

 - CompanyOneMessages.properties
 - CompanyOneMessages_fr_FR.properties
 - CompanyTwoMessages.properties
 - CompanyTwoMessages_fr_FR.properties
 - CompanyThreeMessages.properties
 - CompanyThreeMessages_fr_FR.properties
 - ...

This tar is unzipped and deployed to a location on the server which is specified as been on the classpath within the websphere configurations.

I added the following to my applicationContext-config.xml:

<!-- Enable reloading of resource bundles without requiring web-app restart -->
    <bean id="messages"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>  
                <value>classpath:com/resource/dynamic/CompanyOneMessages</value>
                <value>classpath:com/resource/dynamic/CompanyTwoMessages</value>
                <value>classpath:com/resource/dynamic/CompanyThreeMessages</value>              
            </list>
        </property>     
        <property name="cacheSeconds" value="1" />
    </bean>

    <!-- Declare location of bean to handle messages and define property reference 
         we will use to reference message properties throughout the presentation layer -->
    <bean id="myappMessages" class="com.myapp.resource.MyAPPMessages">
        <property name="messages" ref="messages" />
    </bean>

This all works fine.

BUT, it does not fully fix the original problem. Any time I want to add a new company to our application, I will have to add a new line to the applicationContext-config.xml file and redeploy/restart the web app.

I would like to be able to simply drop the new company properties file into the properties tar and for it to be dynamically picked up.

Is it possible to extend the ReloadableResourceBundleMessageSource class in such a way that it will search the class-path for properties files on application start-up and dynamically load all of them?

Update

This is what I have so far:

applicationContext-config.xml:

<bean id="messages" class="com.resource.MyAPPReloadableResourceBundleMessageSource">
</bean>

MyAPPReloadableResourceBundleMessageSource:

package com.myapp.resource;

import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class MyAPPReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource
{   
    public MyAPPReloadableResourceBundleMessageSource()
    {   
        getResourceBundlesMessages();

        // Simply single basename test
        setBasename("classpath:/resource/dynamic/companyOneMessages");      
    }

    @Override
    public void setBasename(String baesname)
    {
        System.out.println("In setBasename");
        super.setBasename(baesname);
    }

    @Override
    public void setBasenames(String[] baesnames)
    {
        System.out.println("In setBasenames");
        super.setBasenames(baesnames);
    }

    private String[] getResourceBundlesMessages()
    {
        String[] propertiesFiles = null;

        // How do I get all filenames with .properties under com.resources.dynamic? (location is under classpath)

        return propertiesFiles;
    }
}

So all I need is how to get a list of all files under the classpath with .properties extension?

Thanks

Thomas

like image 954
Thomas Buckley Avatar asked Feb 21 '23 17:02

Thomas Buckley


1 Answers

ReloadableResourceBundleMessageSource requires the files to be under the webapp/ directory, not in your classpath (it can't reload those).

like image 85
JamesC Avatar answered Apr 07 '23 17:04

JamesC