You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Johan Compagner <jc...@j-com.nl> on 2001/01/10 11:14:38 UTC

Why is MessageResources.formats (hashmap) private??

I am making a reloadable PropertyMessageResources (checking the filestamp if possible)
But the problem is that <bean:message> tag and so call the MessageResources.getMessaga(locale,string,object[])
And there you don't try to load the resouces but check if there are cached MessageFormat object
for that given key.

If so then you don't look anyfurther.

Now can i override that method and can call my method: mustReload(Locale)
But when that is true i want to empty the formats hashmap.
So that that method calls my getMessage(locale,string) method so that i reload the properties
because the format isn't there anymore.

A reloadable PropertyMessageResources is great in development and even at runtime/deployment.
(change something, and only that property resouces get's reloaded, don't have to do anything else)

I think this also would be a nice addition to struts, if the property's got auto reloaded, i have seen
many messages asking for this (how to reload)

Johan C.





Re: Why is MessageResources.formats (hashmap) private??

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Johan Compagner wrote:

> > > So i could override the getMessage(locale,key,args[]) and don't call super at all.
> > > But that is not a nice way.
> > >
> >
> > Or override this in a similar fashion -- do the check and then call super.getMessage(locale, key, args[]) of no reload
> > was necessary.
>
> But if a reload is necessary in that method, what must i do?
> Then i want to clear the formats hashmap!
> But this isn't possible because it is private.
>

Oops ... that's where I was being dense and not understanding you.  Sorry 'bout that -- I just checked in a patch to make
this protected instead.

>
> Johan

Craig



Re: Why is MessageResources.formats (hashmap) private??

Posted by Johan Compagner <jc...@j-com.nl>.
> > So i could override the getMessage(locale,key,args[]) and don't call super at all.
> > But that is not a nice way.
> >
> 
> Or override this in a similar fashion -- do the check and then call super.getMessage(locale, key, args[]) of no reload
> was necessary.

But if a reload is necessary in that method, what must i do?
Then i want to clear the formats hashmap!
But this isn't possible because it is private.

Because i can try to reload everything but if the formats isn't cleared and therefore all MessageFormat objects
are still there. There won't be new MessageFormat objects created for that new loaded resources the oldd ones
will still be used in every call to getMessage(locale,key,args[]).

By the way what does that MessageFormat object exactly do (especially with the arg[] array)?
And Why is it possible that the locale in the MessageFormat objects is different that the current locale inside struts?

I thought at first that the args[] meant that i could include a message that would be appended, example:

I get an exception and i want do this:
ActionError error = new ActionError("exception.text", exception.getMessage());

I thought this would be the output of that error:

[localized text] There is an exception thrown: [/localized text] and here the exception message.

So is it possible to show a more dynamic message to the screen, not only based on the resouce string
but also another runtime string?

Johan C.






Johan



Re: Why is MessageResources.formats (hashmap) private??

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Johan Compagner wrote:

> > It would certainly be possible to do this.  You could override the PropertyMessageResources.getMessage() method
> > like this (pseudo-code):
> >
> >     public String getMessage(Locale locale, String key) {
> >
> >         if (needToReload(locale)) {
> >             ... clean out old messages for this locale ...
> >             loadLocale(localeKey(locale);
> >         }
> >         super.getMessage(locale, key);
> >
> >     }
> >
> > and not have to change anything else -- just add a needToReload() method that does the appropriate check.
>
> No this doesn't work:
> because of this method in the MessageResources class.
>
> public String getMessage(Locale locale, String key, Object args[]){
>  // Cache MessageFormat instances as they are accessed
>  if (locale == null)
>   locale = defaultLocale;
>  MessageFormat format = null;
>  String formatKey = messageKey(locale, key);
>  synchronized (formats)
> {
>   format = (MessageFormat) formats.get(formatKey);
>   if (format == null) // AS YOU CAN SEE HERE. IF FORMAT FOUND THEN getMessage(locale,key) ISN'T CALLED
>   {
>    String formatString = getMessage(locale, key);
>    if (formatString == null)
>    {
>     if (returnNull)
>      return (null);
>     else
>      return ("???" + formatKey + "???");
>    }
>    format = new MessageFormat(formatString);
>    formats.put(formatKey, format);
>   }
>  }
>  return (format.format(args));
> }
>
> So i could override the getMessage(locale,key,args[]) and don't call super at all.
> But that is not a nice way.
>

Or override this in a similar fashion -- do the check and then call super.getMessage(locale, key, args[]) of no reload
was necessary.

>
> > One feature of Struts that is not documented well yet is a set of standard Action classes in the
> > org.apache.struts.actions package.  One of them will trigger a reload of all the application resources, along with
> > your struts-config.xml file.  In the example app, this is mapped to the "/admin/reload" path.
>
> That's nice, but i would love to have a auto reload for the features.
> (it's a very simple check so why not?)
>

Well, maybe ... If your servlet container is running out of a file directory, then you can use the File.lastModified()
method to check for this.  But that is *not* the case for all servlet containers -- some run directly out of WAR files,
some run out of databases (all your resource files become BLOB objects), and some run out of other internal
architectures.

Unfortunately, there is no ServletContext.getResourceLastModified() call :-(.

It also has a performance impact, so you would want it configurable anyway.

>
> > The only thing this reload cannot do is load updated Java classes -- Struts relies on your servlet container to
> > perform that service for you.
>
> Yes this must be left alone to Tomcat or whatever.
> (Does tomcat look at the classes or at the war file?)
>

Tomcat looks at classes in WEB-INF/classes, and only does this if you configure it to (<Context ... reloadable="true"
...> in server.xml).

>
> Because most of the time a WAR file will be updated not the classes.
>
> Johan C.

Craig



Re: Why is MessageResources.formats (hashmap) private??

Posted by Johan Compagner <jc...@j-com.nl>.
> It would certainly be possible to do this.  You could override the PropertyMessageResources.getMessage() method
> like this (pseudo-code):
> 
>     public String getMessage(Locale locale, String key) {
> 
>         if (needToReload(locale)) {
>             ... clean out old messages for this locale ...
>             loadLocale(localeKey(locale);
>         }
>         super.getMessage(locale, key);
> 
>     }
> 
> and not have to change anything else -- just add a needToReload() method that does the appropriate check.

No this doesn't work:
because of this method in the MessageResources class.

public String getMessage(Locale locale, String key, Object args[]){
 // Cache MessageFormat instances as they are accessed
 if (locale == null)
  locale = defaultLocale;
 MessageFormat format = null;
 String formatKey = messageKey(locale, key);
 synchronized (formats)
{
  format = (MessageFormat) formats.get(formatKey);
  if (format == null) // AS YOU CAN SEE HERE. IF FORMAT FOUND THEN getMessage(locale,key) ISN'T CALLED
  {
   String formatString = getMessage(locale, key);
   if (formatString == null)
   {
    if (returnNull)
     return (null);
    else
     return ("???" + formatKey + "???");
   }
   format = new MessageFormat(formatString);
   formats.put(formatKey, format);
  }
 }
 return (format.format(args));
}

So i could override the getMessage(locale,key,args[]) and don't call super at all.
But that is not a nice way.


> One feature of Struts that is not documented well yet is a set of standard Action classes in the
> org.apache.struts.actions package.  One of them will trigger a reload of all the application resources, along with
> your struts-config.xml file.  In the example app, this is mapped to the "/admin/reload" path.

That's nice, but i would love to have a auto reload for the features.
(it's a very simple check so why not?)

> The only thing this reload cannot do is load updated Java classes -- Struts relies on your servlet container to
> perform that service for you.

Yes this must be left alone to Tomcat or whatever.
(Does tomcat look at the classes or at the war file?)

Because most of the time a WAR file will be updated not the classes.

Johan C.



Re: Why is MessageResources.formats (hashmap) private??

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.

Johan Compagner wrote:

> I am making a reloadable PropertyMessageResources (checking the filestamp if possible)
> But the problem is that <bean:message> tag and so call the MessageResources.getMessaga(locale,string,object[])
> And there you don't try to load the resouces but check if there are cached MessageFormat object
> for that given key.
>
> If so then you don't look anyfurther.
>
> Now can i override that method and can call my method: mustReload(Locale)
> But when that is true i want to empty the formats hashmap.
> So that that method calls my getMessage(locale,string) method so that i reload the properties
> because the format isn't there anymore.
>

It would certainly be possible to do this.  You could override the PropertyMessageResources.getMessage() method
like this (pseudo-code):

    public String getMessage(Locale locale, String key) {

        if (needToReload(locale)) {
            ... clean out old messages for this locale ...
            loadLocale(localeKey(locale);
        }
        super.getMessage(locale, key);

    }

and not have to change anything else -- just add a needToReload() method that does the appropriate check.

>
> A reloadable PropertyMessageResources is great in development and even at runtime/deployment.
> (change something, and only that property resouces get's reloaded, don't have to do anything else)
>
> I think this also would be a nice addition to struts, if the property's got auto reloaded, i have seen
> many messages asking for this (how to reload)
>

One feature of Struts that is not documented well yet is a set of standard Action classes in the
org.apache.struts.actions package.  One of them will trigger a reload of all the application resources, along with
your struts-config.xml file.  In the example app, this is mapped to the "/admin/reload" path.

The only thing this reload cannot do is load updated Java classes -- Struts relies on your servlet container to
perform that service for you.

>
> Johan C.

Craig