FileDocCategorySizeDatePackage
AppResourceManagerFactory.javaAPI DocphoneME MR2 API (J2ME)19268Wed May 02 18:00:46 BST 2007com.sun.j2me.global

AppResourceManagerFactory

public class AppResourceManagerFactory extends ResourceManagerFactory
This class represents a resource manager factory for creating application resource managers.

Fields Summary
private static final String
classname
Class name.
protected ResourceCache
resourceCache
Implementation of resource cache.
Constructors Summary
public AppResourceManagerFactory(ResourceCache cache)
Creates a new resource manager factory for creating application resource managers.

param
cache implementation of {@link ResourceCache} to cache resources.



                                    
       
        resourceCache = cache;
    
Methods Summary
public ResourceManagergetManager(java.lang.String baseName)
Get manager for system locale. It's never used. {@link AppResourceManagerFactory#getManager(String)} replaces this call.

param
baseName the base name
return
null
throws
javax.microedition.global.ResourceException if manager cannot be created

        return null;
    
private ResourceManagergetManager(java.lang.String baseName, java.lang.String locale, com.sun.j2me.global.AppResourceManagerFactory$MetaFile mf, ResourceBundleReader rbr)
Instantiates ResourceManager for a given locale, going through all parent locales and creating respective {@link ResourceBundleReader}s for them.

param
baseName the base name
param
locale the locale
param
mf the metafile
param
rbr the bundle reader
return
created {@link AppResourceManager} object

        // locales
        Vector l = new Vector(5);
        // readers
        Vector r = new Vector(5);

        l.addElement(locale);
        r.addElement(rbr);
        locale = LocaleHelpers.getParentLocale(locale);

        /*
         * Go through all parent locales,
         * e.g. "en-US-var", "en-US", "en", "", null
         */
        while (locale != null) {
            if (mf.containsLocale(locale)) {
                try {
                    ResourceBundleReader reader =
                            getResourceBundleReaderForName(
                                    getResourceUrl(baseName, locale));
                    l.addElement(locale);
                    r.addElement(reader);
                } catch (ResourceException re_ignore) {
                /* intentionally ignored */
                /* For parent locales it does not throw exception in case any */
                /* problems. The exception is thrown for the current locale */
                /* only in the public getManager methods. */
                }
            }
            locale = LocaleHelpers.getParentLocale(locale);
        }

        String[] locales = new String[l.size()];
        ResourceBundleReader[] readers = new ResourceBundleReader[r.size()];
        l.copyInto(locales);
        r.copyInto(readers);
        // instantiate ResourceManager with supported locales

        return newResourceManagerInstance(baseName, locales, readers);
    
public ResourceManagergetManager(java.lang.String baseName, java.lang.String locale)
Creates ResourceManager for given base name and locale. Locale inheritance is resolved here. All parent locales are found and ResourceManager is instantiated for the first locale that is supported and contains any resources. Metafile is read for base name and parent locales are matched with locales from the metafile.

param
baseName the base name
param
locale the locale code
return
created {@link AppResourceManager} object
throws
ResourceException if resources are not found for any of the matched locales or resource file has invalid format.
throws
UnsupportedLocaleException if given base name supports neither requested locale nor any of its parent locales.

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": " + "getManager (" +
                           baseName + "," + locale + ")");
        }
        Vector l = new Vector(4);

        while (locale != null) {
            l.addElement(locale);
            locale = LocaleHelpers.getParentLocale(locale);
        }

        String[] locales = new String[l.size()];
        l.copyInto(locales);

        return getManager(baseName, locales);
    
public ResourceManagergetManager(java.lang.String baseName, java.lang.String[] locales)
Create ResourceManager for given base name and list of locales. No locale inheritance used here. Metafile is read for base name and locales in array are matched with locales from metafile. Only those that match are used.

param
baseName the base name
param
locales array of locales
return
created {@link AppResourceManager} object
throws
ResourceException if no resources for the base name and any of the locales were found.
throws
UnsupportedLocaleException if no locales from the array are listed in metafile.


        // read metafile
        MetaFile mf = getMetafileForBaseName(baseName);

        // match locales with those read from metafile
        for (int i = 0; i < locales.length; i++) {
            if (mf.containsLocale(locales[i])) {
                ResourceBundleReader reader = getResourceBundleReaderForName(
                        getResourceUrl(baseName, locales[i]));
                return getManager(baseName, locales[i], mf, reader);
            }
        }
        throw new UnsupportedLocaleException();
    
protected com.sun.j2me.global.AppResourceManagerFactory$MetaFilegetMetafileForBaseName(java.lang.String baseName)
Read metafile {@link MetaFile} for givane base name. Metafiles are stored under global directory in form _<base name>.

param
baseName the base name
return
metafile for the base name

        MetaFile mf;
        int hcode = resourceCache.getHashCodeForResource("_" + baseName, "", 0);
        Resource r = resourceCache.lookup(hcode);
        if (r == null) {
            try {
                mf = new MetaFile(GLOBAL_PREFIX + "_" + baseName);

            } catch (IOException ioe) {
                throw new ResourceException(
                                     ResourceException.METAFILE_NOT_FOUND,
                                     "Not found metafile for base name \""
                                     + baseName + "\"");
            }
            resourceCache.addResource(new Resource(hcode, mf.getSize(), mf));
        } else {
            mf = (MetaFile) r.getValue();
        }
        return (MetaFile)mf.clone();
    
protected ResourceBundleReadergetResourceBundleReaderForName(java.lang.String name)
Creates initialized instance of ResourceBundleReader for given name. It opens resource file and reads header.

param
name name of resource file
return
reader for this resource file

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": getResourceBundleReaderForName (" +
                           name + ")");
        }
        return AppResourceBundleReader.getInstance(name);
    
protected java.lang.StringgetResourceUrl(java.lang.String baseName, java.lang.String locale)
Gives resource URL that can be used for method {@link Class#getResourceAsStream}.

param
baseName the base name
param
locale the locale
return
resource URL

        String resourceUrl;
        resourceUrl = (locale != null && locale.length() > 0)
                 ? GLOBAL_PREFIX + locale + "/" + baseName
                 : GLOBAL_PREFIX + baseName;

        String url = resourceUrl + RESOURCE_FILE_EXTENSION;
        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": url=" + url);
        }
        return url;

    
public java.lang.String[]getSupportedLocales(java.lang.String baseName)
Get array of supported locales for given base name. It reads metafile for the base name.

param
baseName the base name
return
array of locales

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": getSupportedLocales");
        }
        if (baseName == null) 
            throw new NullPointerException("Basename is null.");
        
        MetaFile mf = getMetafileForBaseName(baseName);
        String[] locales = mf.toArray();
        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            for (int i = 0; i < locales.length; i++) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                               locales[i]);
            }
        }
        return locales;
    
protected ResourceManagernewResourceManagerInstance(java.lang.String baseName, java.lang.String[] locales, ResourceBundleReader[] readers)
Creates a new instance of AppResourceManager. The new instance can be used to get application resources of the given base name and locale. The resource files will be accessed through the given resource bundle reader.

param
baseName the base name
param
locales array of locales to try
param
readers array of readers corresponding to locales
return
new AppResourceManager object

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": " +
                           "Creating new AppResourceManager for \"" +
                           baseName + "\" with cache: " + this.resourceCache +
                           "\nwith readers:");
            for (int i = 0; i < readers.length; i++) {
                Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                               readers[i].toString());
            }
        }

        return new AppResourceManager(baseName,
                                      locales,
                                      readers,
                                      this.resourceCache);