views:

207

answers:

1

I'm trying to use a reloadable spring resource bundle but spring cannot find the file. I've tried tons of different paths, but can't get it to work anywhere. In the code below you'll see that i load both the spring bundle and the regular one from the same path variable but only one works.

I've been banging my head against this for far too long. Anybody have any ideas?

logfile


INFO  2010-04-28 11:38:31,805 [main] org.myorg.test.TestMessages: C:\www\htdocs\messages.properties
INFO  2010-04-28 11:38:31,805 [main] org.myorg.data.Messages: initializing Spring Message Source to C:\www\htdocs\messages.properties
INFO  2010-04-28 11:38:31,821 [main] org.myorg.data.Messages: Attempting to load properties from C:\www\htdocs\messages.properties
DEBUG 2010-04-28 11:38:31,836 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en_US] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,842 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,848 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties] - neither plain properties nor XML
INFO  2010-04-28 11:38:31,848 [main] org.myorg.test.TestMessages: I am C:\www\htdocs\messages.properties

Messages.java


package org.myorg.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class Messages {
    protected static final Log logger = LogFactory.getLog(Messages.class);
    private static ReloadableResourceBundleMessageSource msgSource = null;
    private static ResourceBundle RESOURCE_BUNDLE;

    public static final String PATH = "C:" + File.separator + "www" 
            + File.separator + "htdocs" + File.separator + "messages.properties";

    private Messages() {

    }

    public static String getString(String key) {
        initBundle();

        return msgSource.getMessage(key, null, 
                    RESOURCE_BUNDLE.getString(key), null); 
    }

    private static void initBundle(){
        if(null == msgSource || null == RESOURCE_BUNDLE){
            logger.info("initializing Spring Message Source to " + PATH);
            msgSource = new ReloadableResourceBundleMessageSource();
            msgSource.setBasename(PATH);
            msgSource.setCacheSeconds(1);

            FileInputStream fis = null;
            try {
                logger.info("Attempting to load properties from " + PATH);
                fis = new FileInputStream(PATH);
                RESOURCE_BUNDLE = new PropertyResourceBundle(fis);
            } catch (Exception e) {
                logger.info("couldn't find " + PATH);
            } finally {
                try {
                    if(null != fis)
                        fis.close();
                } catch (IOException e) {

                }
            }
        }
    }
}

TestMessages.java


package org.myorg.test;

import org.myorg.data.Messages;

public class TestMessages extends AbstractTest {
        public void testMessage(){
            logger.info(Messages.PATH);
            logger.info(Messages.getString("OpenKey.TEST"));
        }
}

A: 

rather than reading from a file, you should probably read from the classpath using

Messages.class.getResourceAsStream("/path/from/classpathroot/messages.properties")
MeBigFatGuy
My goal here is to have the file readable outside of the jar file so that we can make changes without redeploying. The FileInputStream method that you see above does that, but still requires the server to be restarted so isn't optimal. This should work... the whole reason for ReloadableResourceBundleMessageSource's existence is to have a reloadable properties file, and the api specifically states it is made to read them from the filesystem as well.
samspot