You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Erik Worth <ew...@altoweb.com> on 2001/10/31 20:49:31 UTC

[Patch] for org.apache.commons.resources.message

Hi Gang,
I submitted this patch to the wrong list earlier (the struts developers --
oops), so nothing happened with it.  Please let me know if there is some
reason that prevents these changes from being adopted.

I would like to offer the enclosed patch for the classes in the
org.apache.commons.resources.message package.  This is a very fine package
that provides a nice mechanism for localizing messages maintained in a
properties or XML file.  I noticed that the properties file and XML file is
loaded by the class loader for the PropertyMessageResources or
XMLMessageResources classes or a derived instance of these classes.  Ideally
one should be able to specify the class loader without having to derive
these classes.

I propose the addition of a new factory method in the
MessagesResorucesFactory (and the concrete derivatives) that takes a
ClassLoader as an argument:

    public abstract MessageResources createResources(String config,
ClassLoader classLoader);

I further propose the addition a new constructor for MessageResources and
its concrete derivatives:

    public MessageResources(MessageResourcesFactory factory, String config,
                         boolean returnNull, ClassLoader classLoader)

The MessageResources class is augmented to hold a protected instance
variable with the ClassLoader.  If a class loader is not provided (through
one of the existing constructors), it is obtained from
this.getClass().getClassLoader().

Then, the file input stream in the PropertyMessageResources and
XMLMessageResources is loaded like this:

            is = this.classLoader.getResourceAsStream(name);

where the classLoader instance variable is set from the constructor (and
factory method) or obtained from the class instance (if a class loader was
not explicitly specified).

If we do this, we can rest assured that the properties (or XML) file is
retrieved using the proper class loader.  This is an issue in a J2EE 2.x
application server where the class loader associated with these utilities
(perhaps loaded by the EAR class loader) may not be able to see the resource
bundle property files in (let's say) a Web application.

cvs diff XMLMessageResourcesFactory.java XMLMessageResources.java
PropertyMessageResourcesFactory.java PropertyMessageResources.java
MessageResourcesFactory.java MessageResources.java (in directory
D:\downloads\Apache\Commons\jakarta-commons-sandbox\resources\src\java\org\a
pache\commons\resources\message\)
Index: XMLMessageResourcesFactory.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/XMLMessageResourcesFactory.java,v
retrieving revision 1.1
diff -r1.1 XMLMessageResourcesFactory.java
93a94,105
>     /**
>      * Create and return a newly instantiated
<code>MessageResources</code>.
>      *
>      * @param config Configuration parameter(s) for the requested bundle
>      * @param classLoader the class loader to used when loading resources
>      */
>     public MessageResources createResources(String config, ClassLoader
classLoader) {
> 
>         return new XMLMessageResources(this, config, this.returnNull,
classLoader);
> 
>     }
> 
Index: XMLMessageResources.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/XMLMessageResources.java,v
retrieving revision 1.1
diff -r1.1 XMLMessageResources.java
118a119,136
>     /**
>      * Construct a new <code>XMLMessageResources</code> object 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
>      * @param classLoader the class loader to used when loading resources
>      */
>     public XMLMessageResources(MessageResourcesFactory factory,
>                                     String config, boolean returnNull,
>                                     ClassLoader classLoader) {
> 
>         super(factory, config, returnNull, classLoader);
> 
>     }
> 
> 
153c171
<             is =
this.getClass().getClassLoader().getResourceAsStream(name);
---
>             is = this.classLoader.getResourceAsStream(name);
Index: PropertyMessageResourcesFactory.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/PropertyMessageResourcesFactory.java,v
retrieving revision 1.1
diff -r1.1 PropertyMessageResourcesFactory.java
93a94,104
>     /**
>      * Create and return a newly instansiated
<code>MessageResources</code>.
>      * This method must be implemented by concrete subclasses.
>      *
>      * @param config Configuration parameter(s) for the requested bundle
>      * @param classLoader the class loader to used when loading resources
>      */
>     public MessageResources createResources(String config, ClassLoader
classLoader) {
>         return new PropertyMessageResources(this, config, this.returnNull,
classLoader);
>     }
>     
Index: PropertyMessageResources.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/PropertyMessageResources.java,v
retrieving revision 1.1
diff -r1.1 PropertyMessageResources.java
133a134,150
>     /**
>      * 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
>      * @param classLoader the class loader to use when loading resources
>      */
>     public PropertyMessageResources(MessageResourcesFactory factory,
>                                     String config, boolean returnNull, 
>                                     ClassLoader classLoader) {
> 
>         super(factory, config, returnNull, classLoader);
> 
>     }
> 
274c291
<             is =
this.getClass().getClassLoader().getResourceAsStream(name);
---
>             is = this.classLoader.getResourceAsStream(name);
Index: MessageResourcesFactory.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/MessageResourcesFactory.java,v
retrieving revision 1.2
diff -r1.2 MessageResourcesFactory.java
122a123,131
>      * Create and return a newly instansiated
<code>MessageResources</code>.
>      * This method must be implemented by concrete subclasses.
>      *
>      * @param config Configuration parameter(s) for the requested bundle
>      * @param classLoader the class loader to used when loading resources
>      */
>     public abstract MessageResources createResources(String config,
ClassLoader classLoader);
>     
>     /**
Index: MessageResources.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons-sandbox/resources/src/java/org/apache/common
s/resources/message/MessageResources.java,v
retrieving revision 1.2
diff -r1.2 MessageResources.java
148a149,153
>     
>     /**
>      * The class loader to use when loading resources.
>      */
>     protected ClassLoader classLoader = null;
162c167
<         this(factory, config, false);
---
>         this(factory, config, false, null);
176a182,197
>         this(factory, config, false, null);
>         
>     }
> 
> 
>     /**
>      * Construct a new MessageResources 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
>      * @param classLoader the class loader to use when loading resources
>      */
>     public MessageResources(MessageResourcesFactory factory, String
config,
>                          boolean returnNull, ClassLoader classLoader) {
> 
181c202,204
< 
---
>         this.classLoader = (null == classLoader) ? 
>             this.getClass().getClassLoader() : classLoader;
>         






--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>