You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Caroline Lauferon <ca...@cgey.com> on 2003/09/26 15:58:34 UTC

change dynamically ApplicationResources path

Hello,

I would like to know if it is possible to set the path of the
ApplicationResources.properties file without hard-coding it in
struts-config.xml.
Indeed, I would like to read a property file (whose path is in an
environment property) which would contain the path of the
ApplicationResources to use.
This would allow me to deploy my EAR, and then, without having to
re-generate it and re-deploy it, change the ApplicationResources file.

Thanks,

Caroline

Re: change dynamically ApplicationResources path

Posted by Caroline Lauferon <ca...@cgey.com>.
Thanks a lot!! I used the second method (see code below).

Caroline

in struts-config.xml :
<message-resources parameter="do.not.care.about.this"
factory="fr.capgemini.webihm.struts.ExternPropertyMessageResourcesFactory"
/>


public class ExternPropertyMessageResourcesFactory extends
PropertyMessageResourcesFactory {
    public MessageResources createResources(String config) {
        return new ExternPropertyMessageResources(this, config,
this.returnNull);
    }
}


public class ExternPropertyMessageResources extends PropertyMessageResources
{
// ----------------------------------------------------------- Constructors
/**
* Construct a new PropertyMessageResources according to the
* specified parameters.
*
* @param factory The MessageResourcesFactory that created us
* @param config The configuration parameter for this MessageResources
*/
public ExternPropertyMessageResources(MessageResourcesFactory factory,String
config) {
super(factory, config);
log.info("Initializing, config='" + config + "'");
}
/**
* Construct a new PropertyMessageResources according to the
* specified parameters.
*
* @param factory The MessageResourcesFactory that created us
* @param config The configuration parameter for this MessageResources
* @param returnNull The returnNull property we should initialize with
*/
public ExternPropertyMessageResources(MessageResourcesFactory factory,String
config, boolean returnNull) {
    super(factory, config, returnNull);
    log.info("Initializing, config='" + config +"', returnNull=" +
returnNull);
}

// ------------------------------------------------------------- Properties
/**
* The set of locale keys for which we have already loaded messages, keyed
* by the value calculated in <code>localeKey()</code>.
*/
protected HashMap locales = new HashMap();

/**
* The <code>Log</code> instance for this class.
*/
protected static final Log log
=LogFactory.getLog(ExternPropertyMessageResources.class);

/**
* Load the messages associated with the specified Locale key. For this
* implementation, the <code>config</code> property should contain a fully
* qualified package and resource name, separated by periods, of a series
* of property resources to be loaded from the class loader that created
* this PropertyMessageResources instance. This is exactly the same name
* format you would use when utilizing the
* <code>java.util.PropertyResourceBundle</code> class.
*
* @param localeKey Locale key for the messages to be retrieved
*/
protected synchronized void loadLocale(String localeKey) {
    log.info("Recuperation des messages loadLocale("+ localeKey +")");
    if (log.isDebugEnabled()) {
        log.debug("loadLocale(" + localeKey + ")");
    }
// Have we already attempted to load messages for this locale?
    if (locales.get(localeKey) != null) {
        return;
    }
    locales.put(localeKey, localeKey);
    InputStream is = null;
    Properties props = new Properties();
    try{
    String fileURL =
System.getProperty("URL_UTIL_PROPERTIES")+"ApplicationResources.properties";
    // System.out.println("on essaie de lire le fichier"+fileURL);
    URL url=null;
    try {
        url = new URL(fileURL);
    } catch (Exception e){
        log.error("probleme a la recuperation de l'URL");
    }
    String fileName = url.getFile();
    is = new FileInputStream(fileName);
    } catch (FileNotFoundException fnfe) {
        log.error("Fichier inexistant");
        // System.out.println("Fichier pas la!");
    }
    if (is != null) {
    try {
        props.load(is);
        // System.out.println("props="+props);
    } catch (IOException e) {
        log.error("loadLocale()", e);
    } finally {
    try {
        is.close();
    } catch (IOException e) {
        log.error("loadLocale()", e);
    }
    }
    }
    if (log.isTraceEnabled()) {
        log.trace(" Loading resource completed");
    }
    // Copy the corresponding values into our cache
    if (props.size() < 1) {
        return;
    }
    synchronized (messages) {
        Iterator names = props.keySet().iterator();
        while (names.hasNext()) {
        String key = (String) names.next();
        if (log.isTraceEnabled()) {
            log.trace(" Saving message key '" + messageKey(localeKey, key));
        }
        messages.put(messageKey(localeKey, key), props.getProperty(key));
    }
}
}

}


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: change dynamically ApplicationResources path

Posted by Manish Singla <Ma...@Sun.COM>.
You may use following to set bundleName defined in struts-config.xml...
request.setAttribute(Globals.MESSAGES_KEY, bundleName);
but bundleName is MessageResources object ..... and bundleName is  key
attribute of message-resources in struts-config.xml...

OR
If above do not meet your requirements...
than you have write factory of message-resources and overload loadLocale
method of  PropertyMessageResources...


Caroline Lauferon wrote:

> Hello,
>
> I would like to know if it is possible to set the path of the
> ApplicationResources.properties file without hard-coding it in
> struts-config.xml.
> Indeed, I would like to read a property file (whose path is in an
> environment property) which would contain the path of the
> ApplicationResources to use.
> This would allow me to deploy my EAR, and then, without having to
> re-generate it and re-deploy it, change the ApplicationResources file.
>
> Thanks,
>
> Caroline


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org