1 21 22 23 package oracle.toplink.essentials.internal.helper; 24 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.BufferedInputStream ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 import java.util.regex.Pattern ; 33 import java.util.regex.PatternSyntaxException ; 34 import java.security.AccessController ; 35 import java.security.PrivilegedAction ; 36 37 import oracle.toplink.essentials.logging.SessionLog; 38 39 40 44 public class DBPlatformHelper { 45 private static final String DEFAULTPLATFORM = "oracle.toplink.essentials.platform.database.DatabasePlatform"; 47 private final static String PROPERTY_PATH = "oracle/toplink/essentials/internal/helper/"; 49 private final static String VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME = 50 PROPERTY_PATH + "VendorNameToPlatformMapping.properties"; 52 56 private static Properties _nameToVendorPlatform = null; 57 58 66 public static String getDBPlatform(String vendorName, SessionLog logger) { 67 68 initializeNameToVendorPlatform(logger); 69 70 String detectedDbPlatform = null; 71 if(vendorName != null) { 72 detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorPlatform, logger); 73 } 74 if (logger.shouldLog(SessionLog.FINE) ) { 75 logger.log(SessionLog.FINE, "dbPlaformHelper_detectedVendorPlatform", detectedDbPlatform ); } 77 if (detectedDbPlatform == null) { 78 if(logger.shouldLog(SessionLog.INFO)) { 79 logger.log(SessionLog.INFO, "dbPlaformHelper_defaultingPlatform", vendorName, DEFAULTPLATFORM); } 81 detectedDbPlatform = DEFAULTPLATFORM; 82 } 83 return detectedDbPlatform; 84 } 85 86 89 private static Properties initializeNameToVendorPlatform(SessionLog logger) { 90 synchronized(DBPlatformHelper.class) { 91 if(_nameToVendorPlatform == null) { 92 _nameToVendorPlatform = new Properties (); 93 try { 94 loadFromResource(_nameToVendorPlatform, VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME, 95 DBPlatformHelper.class.getClassLoader() ); 96 } catch (IOException e) { 97 logger.log(SessionLog.WARNING, "dbPlaformHelper_noMappingFound", VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME); 98 } 99 } 100 } 101 return _nameToVendorPlatform; 102 } 103 104 107 private static String matchVendorNameInProperties(String vendorName, 108 Properties nameToVendorPlatform, SessionLog logger) { 109 String dbPlatform = null; 110 for( Iterator iterator = nameToVendorPlatform.entrySet().iterator(); 112 dbPlatform == null && iterator.hasNext();) { 113 Map.Entry entry = (Map.Entry ) iterator.next(); 114 String regExpr = (String ) entry.getKey(); 115 String value = (String ) entry.getValue(); 116 if(logger.shouldLog(SessionLog.FINEST)) { 117 logger.log(SessionLog.FINEST, "dbPlaformHelper_regExprDbPlatform", regExpr, value); } 119 if( matchPattern(regExpr, vendorName, logger) ) { 120 dbPlatform = value; 121 } 122 } 123 return dbPlatform; 124 } 125 126 134 private static boolean matchPattern(String regExp, String target, SessionLog logger) { 135 boolean matches = false; 136 try { 137 matches = Pattern.matches(regExp,target); 138 } catch (PatternSyntaxException e){ 139 if(logger.shouldLog(SessionLog.FINE)) { 140 logger.log(SessionLog.FINE, "dbPlaformHelper_patternSyntaxException", e); } 142 } 143 return matches; 144 } 145 146 private static void loadFromResource(Properties properties, String resourceName, ClassLoader classLoader) 148 throws IOException { 149 load(properties, resourceName, classLoader); 150 } 151 152 163 private static void load(Properties properties, final String resourceName, 164 final ClassLoader classLoader) 165 throws IOException { 166 167 InputStream bin = new BufferedInputStream (openResourceInputStream(resourceName,classLoader)); 168 169 try { 170 properties.load(bin); 171 } finally { 172 try { 173 bin.close(); 174 } catch (Exception e) { 175 } 177 } 178 } 179 180 183 private static InputStream openResourceInputStream(final String resourceName, final ClassLoader classLoader) { 184 return (InputStream ) AccessController.doPrivileged( 185 new PrivilegedAction () { 186 public Object run() { 187 if (classLoader != null) { 188 return classLoader.getResourceAsStream(resourceName); 189 } else { 190 return ClassLoader.getSystemResourceAsStream(resourceName); 191 } 192 } 193 } 194 ); 195 } 196 197 } 198 | Popular Tags |