Results 1 to 1 of 1

Thread: Dynamically load files on classpath using ReloadableResourceBundleMessageSource

  1. #1
    Join Date
    Jan 2012
    Location
    Dublin, Ireland
    Posts
    4

    Default Dynamically load files on classpath using ReloadableResourceBundleMessageSource

    Hi

    I'm new to Spring and have encountered an issue with it's ReloadableResourceBundleMessageSource.

    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:

    Code:
    <!-- 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.PCUIMessages">
    		<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:

    Code:
    <bean id="messages" class="com.resource.PCUIReloadableResourceBundleMessageSource">
    	</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="pcuiMessages" class="com.resource.PCUIMessages">
    		<property name="messages" ref="messages" />
    	</bean>
    PCUIReloadableResourceBundleMessageSource:
    Code:
        package com.myapp.resource;
        
        import org.springframework.context.support.ReloadableResourceBundleMessageSource;
        
        public class PCUIReloadableResourceBundleMessageSource extends ReloadableResourceBundleMessageSource
        {	
        	public PCUIReloadableResourceBundleMessageSource()
        	{	
        		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
    Last edited by langer123; Jan 27th, 2012 at 11:45 AM. Reason: Made some progress

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •