avalon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l...@apache.org
Subject cvs commit: avalon-excalibur/i18n/src/java/org/apache/avalon/excalibur/i18n ResourceManager.java
Date Sun, 18 Apr 2004 16:35:28 GMT
leif        2004/04/18 09:35:28

  Modified:    i18n/src/java/org/apache/avalon/excalibur/i18n
                        ResourceManager.java
  Log:
  Make it possible to request a Resources object for a specific locale.  This was
  added to support the use of the ResourceManager in cases where the Locale
  depends on the calling thread.  Specifically in Servlet environments where each
  client may have a different locale.
  
  Revision  Changes    Path
  1.26      +100 -14   avalon-excalibur/i18n/src/java/org/apache/avalon/excalibur/i18n/ResourceManager.java
  
  Index: ResourceManager.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/i18n/src/java/org/apache/avalon/excalibur/i18n/ResourceManager.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- ResourceManager.java	28 Feb 2004 11:47:30 -0000	1.25
  +++ ResourceManager.java	18 Apr 2004 16:35:28 -0000	1.26
  @@ -18,6 +18,7 @@
   
   import java.lang.ref.WeakReference;
   import java.util.HashMap;
  +import java.util.Locale;
   
   /**
    * Manager for resources.
  @@ -48,23 +49,58 @@
        * Retrieve resource with specified basename.
        *
        * @param baseName the basename
  +     * @param locale Locale of the requested Resources, null implies default locale
        * @param classLoader the classLoader to load resources from
  +     *
        * @return the Resources
        */
       public synchronized static final Resources getBaseResources( final String baseName,
  +                                                                 final Locale locale,
                                                                    final ClassLoader classLoader
)
       {
  -        Resources resources = getCachedResource( baseName );
  +        String cacheKey;
  +        if ( locale == null )
  +        {
  +            cacheKey = baseName;
  +        }
  +        else
  +        {
  +            cacheKey = baseName + "-" + locale.toString();
  +        }
  +        
  +        Resources resources = getCachedResource( cacheKey );
           if( null == resources )
           {
  -            resources = new Resources( baseName, classLoader );
  -            putCachedResource( baseName, resources );
  +            if ( locale == null )
  +            {
  +                resources = new Resources( baseName, classLoader );
  +            }
  +            else
  +            {
  +                resources = new Resources( baseName, locale, classLoader );
  +            }
  +            
  +            putCachedResource( cacheKey, resources );
           }
   
           return resources;
       }
   
       /**
  +     * Retrieve resource with specified basename.
  +     *
  +     * @param baseName the basename
  +     * @param classLoader the classLoader to load resources from
  +     *
  +     * @return the Resources
  +     */
  +    public synchronized static final Resources getBaseResources( final String baseName,
  +                                                                 final ClassLoader classLoader
)
  +    {
  +        return getBaseResources( baseName, null, classLoader );
  +    }
  +
  +    /**
        * Clear the cache of all resources currently loaded into the
        * system. This method is useful if you need to dump the complete
        * cache and because part of the application is reloading and
  @@ -92,26 +128,25 @@
       /**
        * Cache specified resource in weak reference.
        *
  -     * @param baseName the resource key
  +     * @param cacheKey the key used to reference the resource in the cache
        * @param resources the resources object
        */
  -    private synchronized static final void putCachedResource( final String baseName,
  +    private synchronized static final void putCachedResource( final String cacheKey,
                                                                 final Resources resources
)
       {
  -        c_resources.put( baseName,
  -                         new WeakReference( resources ) );
  +        c_resources.put( cacheKey, new WeakReference( resources ) );
       }
   
       /**
        * Retrieve cached resource.
        *
  -     * @param baseName the resource key
  +     * @param cacheKey the key used to reference the resource in the cache
  +     *
        * @return resources the resources object
        */
  -    private synchronized static final Resources getCachedResource( final String baseName
)
  +    private synchronized static final Resources getCachedResource( final String cacheKey
)
       {
  -        final WeakReference weakReference =
  -            (WeakReference)c_resources.get( baseName );
  +        final WeakReference weakReference = (WeakReference)c_resources.get( cacheKey );
           if( null == weakReference )
           {
               return null;
  @@ -127,11 +162,42 @@
        * The basename is determined by name postfixed with ".Resources".
        *
        * @param name the name to use when looking up resources
  +     * @param locale Locale of the requested Resources, null implies default locale
  +     *
  +     * @return the Resources
  +     */
  +    public static final Resources getResources( final String name, final Locale locale
)
  +    {
  +        return getBaseResources( name + ".Resources", locale, null );
  +    }
  +
  +    /**
  +     * Retrieve resource for specified name.
  +     * The basename is determined by name postfixed with ".Resources".
  +     *
  +     * @param name the name to use when looking up resources
  +     *
        * @return the Resources
        */
       public static final Resources getResources( final String name )
       {
  -        return getBaseResources( name + ".Resources" );
  +        return getResources( name, null );
  +    }
  +
  +    /**
  +     * Retrieve resource for specified Classes package.
  +     * The basename is determined by name of classes package
  +     * postfixed with ".Resources".
  +     *
  +     * @param clazz the Class
  +     * @param locale Locale of the requested Resources, null implies default locale
  +     *
  +     * @return the Resources
  +     */
  +    public static final Resources getPackageResources( final Class clazz, final Locale
locale )
  +    {
  +        return getBaseResources(
  +            getPackageResourcesBaseName( clazz ), locale, clazz.getClassLoader() );
       }
   
       /**
  @@ -140,11 +206,28 @@
        * postfixed with ".Resources".
        *
        * @param clazz the Class
  +     *
        * @return the Resources
        */
       public static final Resources getPackageResources( final Class clazz )
       {
  -        return getBaseResources( getPackageResourcesBaseName( clazz ), clazz.getClassLoader()
);
  +        return getPackageResources( clazz, null );
  +    }
  +
  +    /**
  +     * Retrieve resource for specified Class.
  +     * The basename is determined by name of Class
  +     * postfixed with "Resources".
  +     *
  +     * @param clazz the Class
  +     * @param locale Locale of the requested Resources, null implies default locale
  +     *
  +     * @return the Resources
  +     */
  +    public static final Resources getClassResources( final Class clazz, final Locale locale
)
  +    {
  +        return getBaseResources(
  +            getClassResourcesBaseName( clazz ), locale, clazz.getClassLoader() );
       }
   
       /**
  @@ -153,11 +236,12 @@
        * postfixed with "Resources".
        *
        * @param clazz the Class
  +     *
        * @return the Resources
        */
       public static final Resources getClassResources( final Class clazz )
       {
  -        return getBaseResources( getClassResourcesBaseName( clazz ), clazz.getClassLoader()
);
  +        return getClassResources( clazz, null );
       }
   
       /**
  @@ -166,6 +250,7 @@
        * postfixed with ".Resources".
        *
        * @param clazz the Class
  +     *
        * @return the resource basename
        */
       public static final String getPackageResourcesBaseName( final Class clazz )
  @@ -199,6 +284,7 @@
        * postfixed with "Resources".
        *
        * @param clazz the Class
  +     *
        * @return the resource basename
        */
       public static final String getClassResourcesBaseName( final Class clazz )
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org


Mime
View raw message