1 23 24 25 package com.sun.jdo.spi.persistence.utility; 26 27 import com.sun.jdo.spi.persistence.utility.logging.Logger; 28 29 import java.util.Properties ; 30 import java.util.ResourceBundle ; 31 32 import java.io.BufferedInputStream ; 33 import java.io.FileInputStream ; 34 import java.io.InputStream ; 35 import java.io.FileNotFoundException ; 36 import java.io.IOException ; 37 38 import java.security.AccessController ; 39 import java.security.PrivilegedAction ; 40 import java.security.PrivilegedExceptionAction ; 41 import java.security.PrivilegedActionException ; 42 43 44 45 50 public class PropertyHelper { 51 52 55 private static final Logger logger = LogHelperUtility.getLogger(); 56 57 60 private final static ResourceBundle messages = I18NHelper.loadBundle( 61 "com.sun.jdo.spi.persistence.utility.Bundle", PropertyHelper.class.getClassLoader()); 63 64 65 72 public static void loadFromResource(Properties properties, String resourceName, ClassLoader classLoader) 73 throws IOException { 74 load(properties, resourceName, false, classLoader); 75 } 76 77 78 83 public static void loadFromFile(Properties properties, String fileName) 84 throws IOException { 85 load(properties, fileName, true, null); 86 } 87 88 100 private static void load(Properties properties, final String resourceName, 101 final boolean loadFromFile, final ClassLoader classLoader) 102 throws IOException { 103 104 InputStream bin = null; 105 InputStream in = null; 106 boolean debug = logger.isLoggable(); 107 108 if (debug) { 109 Object [] items = new Object [] {resourceName,Boolean.valueOf(loadFromFile)}; 110 logger.fine("utility.PropertyHelper.load",items); } 112 113 in = loadFromFile ? openFileInputStream(resourceName) : 114 openResourceInputStream(resourceName,classLoader); 115 if (in == null) { 116 throw new IOException (I18NHelper.getMessage(messages, 117 "utility.PropertyHelper.failedToLoadResource", resourceName)); } 119 bin = new BufferedInputStream (in); 120 try { 121 properties.load(bin); 122 } finally { 123 try { 124 bin.close(); 125 } catch (Exception e) { 126 } 128 } 129 } 130 131 134 private static InputStream openFileInputStream(final String fileName) throws java.io.FileNotFoundException { 135 try { 136 return (InputStream ) AccessController.doPrivileged( 137 new PrivilegedExceptionAction () { 138 public Object run() throws FileNotFoundException { 139 return new FileInputStream (fileName); 140 } 141 } 142 ); 143 } catch (PrivilegedActionException e) { 144 throw (FileNotFoundException ) e.getException(); 148 } 149 150 } 151 152 155 private static InputStream openResourceInputStream(final String resourceName, final ClassLoader classLoader) 156 throws java.io.FileNotFoundException { 157 return (InputStream ) AccessController.doPrivileged( 158 new PrivilegedAction () { 159 public Object run() { 160 if (classLoader != null) { 161 return classLoader.getResourceAsStream(resourceName); 162 } else { 163 return ClassLoader.getSystemResourceAsStream(resourceName); 164 } 165 } 166 } 167 ); 168 } 169 170 } | Popular Tags |