FileDocCategorySizeDatePackage
PropertyHelper.javaAPI DocGlassfish v2 API7296Fri May 04 22:35:20 BST 2007com.sun.jdo.spi.persistence.utility

PropertyHelper

public class PropertyHelper extends Object
author
Mitesh Meswani This class provides helper to load reources into property object.

Fields Summary
private static final com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger.
private static final ResourceBundle
messages
I18N message handler.
Constructors Summary
Methods Summary
private static voidload(java.util.Properties properties, java.lang.String resourceName, boolean loadFromFile, java.lang.ClassLoader classLoader)
Loads properties list from the specified resource into specified Properties object.

param
resourceName Name of resource. If loadFromFile is true, this is fully qualified path name to a file. param classLoader is ignored. If loadFromFile is false,this is resource name.
param
classLoader The class loader that should be used to load the resource. If null,primordial class loader is used.
param
properties Properties object to load
param
loadFromFile true if resourcename is to be treated as file name.


        InputStream bin = null;
        InputStream in = null;
        boolean debug = logger.isLoggable();

        if (debug) {
            Object[] items = new Object[] {resourceName,Boolean.valueOf(loadFromFile)};
            logger.fine("utility.PropertyHelper.load",items); // NOI18N
        }

        in =  loadFromFile ? openFileInputStream(resourceName) : 
                                openResourceInputStream(resourceName,classLoader);
        if (in == null) {
            throw new IOException(I18NHelper.getMessage(messages,
                    "utility.PropertyHelper.failedToLoadResource", resourceName));// NOI18N
        }
        bin = new BufferedInputStream(in);
        try {
            properties.load(bin);
        } finally {
            try {
                bin.close();
            } catch (Exception e) {
                // no action
            }
        }
    
public static voidloadFromFile(java.util.Properties properties, java.lang.String fileName)
Loads properties list from the specified file into specified Properties object.

param
properties Properties object to load
param
fileName Fully qualified path name to the file.

        load(properties, fileName, true, null);
    
public static voidloadFromResource(java.util.Properties properties, java.lang.String resourceName, java.lang.ClassLoader classLoader)
Loads properties list from the specified resource into specified Properties object.

param
properties Properties object to load
param
resourceName Name of resource.
param
classLoader The class loader that should be used to load the resource. If null,primordial class loader is used.

    

                                                                             
            
              
        load(properties, resourceName, false, classLoader);
    
private static java.io.InputStreamopenFileInputStream(java.lang.String fileName)
Open fileName as input stream inside doPriviledged block

        try {
            return (InputStream) AccessController.doPrivileged(
                new PrivilegedExceptionAction() {
                    public Object run() throws FileNotFoundException {
                            return new FileInputStream(fileName);
                    }
                }
           );
        } catch (PrivilegedActionException e) {
            // e.getException() should be an instance of FileNotFoundException,
            // as only "checked" exceptions will be "wrapped" in a
            // PrivilegedActionException.
            throw (FileNotFoundException) e.getException();
        }
    
    
private static java.io.InputStreamopenResourceInputStream(java.lang.String resourceName, java.lang.ClassLoader classLoader)
Open resourcenName as input stream inside doPriviledged block

        return (InputStream) AccessController.doPrivileged(
            new PrivilegedAction() {
                public Object run() {
                    if (classLoader != null) {
                        return classLoader.getResourceAsStream(resourceName);
                    } else {
                        return ClassLoader.getSystemResourceAsStream(resourceName);
                    }
                }
            }
        );