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

DevResourceManagerFactory

public class DevResourceManagerFactory extends AppResourceManagerFactory
This class represents a resource manager factory for creating device resource managers.

Fields Summary
private static final String
BASENAME
Default resource file name for device resources.
private static final String
classname
Class name.
private static com.sun.midp.security.SecurityToken
classSecurityToken
Security token to allow access to implementation APIs
Constructors Summary
public DevResourceManagerFactory()
Default constructor for SecurityToken initialization.


              
      
        super(null);
    
public DevResourceManagerFactory(ResourceCache cache)
Creates a new resource manager factory for creating device resource managers.

param
cache resource cache used

        super(cache);
    
Methods Summary
protected MetaFilegetMetafileForBaseName(java.lang.String baseName)
Read metafile {@link com.sun.j2me.global.AppResourceManagerFactory.MetaFile} for given 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;
        if (baseName != null && baseName.length() == 0) {
            baseName = BASENAME;
        }

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": Get metafile for \"" +
                           baseName + "\"");
        }
        int hcode = resourceCache.getHashCodeForResource("_" + baseName, "", 0);
        Resource r = resourceCache.lookup(hcode);
        if (r == null) {
            String mfileName = "_" + baseName;
            try {
                if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
                    Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                                   classname  + ": " +
                                   "Not found in cache. Reading from store:" +
                                   "\"" + mfileName + "\"");
                }
                RandomAccessStream storage =
                            new RandomAccessStream(classSecurityToken);

                if (storage == null) {
                    throw new IOException("Storage is null.");
                }

                storage.connect(File.getConfigRoot(Constants.INTERNAL_STORAGE_ID)
                        + Configuration.getProperty("microedition.global.root")
                        + mfileName, Connector.READ);

                InputStream is = storage.openInputStream();
                mf = new MetaFile(is);
                is.close();

            } 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)
Create new {@link ResourceBundleReader} for reading device resources.

param
name name of resource
return
resource bundle reader

        return DevResourceBundleReader.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(String name)}.

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

        String resourceUrl;
        if (baseName == null ||
                baseName.length() == 0) {
            baseName = "common.res";
        }
        resourceUrl = (locale != null && locale.length() > 0)
                 ? locale + "/" + baseName
                 : baseName;
        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": url=" + resourceUrl);
        }
        return resourceUrl;
    
protected ResourceManagernewResourceManagerInstance(java.lang.String baseName, java.lang.String[] locales, ResourceBundleReader[] readers)
Creates a new instance of DevResourceManager. The new instance can be used to get device resources of the given locale. The resource files will be accessed through the given resource bundle reader.

param
baseName baseName should be always "common.res". Because this constructor is called by the {@link DevResourceManagerFactory} it is always ok.
param
locales array of supported locales
param
readers array of opened readers for supported locales
return
new DevResourceManager

        if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
            Logging.report(Logging.INFORMATION, LogChannels.LC_JSR238,
                           classname + ": Creating new DevResourceManager " +
                           "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 DevResourceManager(baseName,
                                      locales,
                                      readers,
                                      this.resourceCache);