/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License"). You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * glassfish/bootstrap/legal/CDDLv1.0.txt or * https://glassfish.dev.java.net/public/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable, * add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your * own identifying information: Portions Copyright [yyyy] * [name of copyright owner] */ package oracle.toplink.essentials.platform.database; import java.io.IOException; import java.io.InputStream; import java.io.BufferedInputStream; import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import java.security.AccessController; import java.security.PrivilegedAction; /** * @author Mitesh Meswani * This class defines string constants representing database names. * This class is responsible to translate given database name to internal name. */ public class DBPlatformHelper { private static final String DERBYPLATFORM = "oracle.toplink.essentials.platform.database.DerbyPlatform"; private static final String DEFAULTPLATFORM = DERBYPLATFORM; private final static String PROPERTY_PATH = "oracle/toplink/essentials/platform/database/"; // NOI18N private final static String VENDOR_NAME_TO_TYPE_PROPERTY = "oracle.toplink.essentials.internal.ejb.cmp3.VENDOR_NAME_TO_TYPE"; //NOI18N private final static String VENDOR_NAME_TO_TYPE_RESOURCE_PROPERTY = "oracle.toplink.essentials.internal.ejb.cmp3.VENDOR_NAME_TO_TYPE_RESOURCE"; //NOI18N private final static String VENDOR_NAME_TO_TYPE_RESOURCE_DEFAULT_NAME = PROPERTY_PATH + "VendorNameToPlatformMapping.properties"; //NOI18N /** * The logger. */ //private static final Logger logger = LogHelperUtility.getLogger(); /** * Holds mapping between possible vendor names to internal types defined above. * vendor names are treated as regular expressions. */ private static Properties _nameToVendorType = initializeNameToVendorType(); /** Get Database Platform from vendor name. * @param vendorName Input vendor name. Typically this is obtained by querying * DatabaseMetaData. * @return Database type that corresponds to vendorName. * If vendorName does not match any of predefined vendor names, * DEFAULTPLATFORM is returned. */ public static String getDBPlatform(String vendorName) { String detectedDbPlatform = DEFAULTPLATFORM; if(vendorName != null) { detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorType); } // if(debug) // logger.fine("utility.database.DBPlatformHelper.detectedVendorType",detectedDbPlatform); //NOI18N return detectedDbPlatform; } /** * Allocate and initialize nameToVendorType if not already done. */ private static Properties initializeNameToVendorType() { synchronized(DBPlatformHelper.class) { if(_nameToVendorType == null) { _nameToVendorType = new Properties(); String resourceName = System.getProperty(VENDOR_NAME_TO_TYPE_RESOURCE_PROPERTY, VENDOR_NAME_TO_TYPE_RESOURCE_DEFAULT_NAME); try { loadFromResource(_nameToVendorType,resourceName, DBPlatformHelper.class.getClassLoader() ); } catch (IOException e) { // if(logger.isLoggable() ) { // logger.fine("utility.database.DBPlatformHelper.couldNotLoadResource", // NOI18N // resourceName,e); // } } overrideWithSystemProperties(_nameToVendorType); } } return _nameToVendorType; } /** * Match vendorName in properties specifieid by nameToVendorType. */ private static String matchVendorNameInProperties(String vendorName, Properties nameToVendorType) { String dbType = null; //Iterate over all properties till we find match. for( Iterator iterator = nameToVendorType.entrySet().iterator(); dbType == null && iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String regExpr = (String) entry.getKey(); String value = (String) entry.getValue(); // if(logger.isLoggable(Logger.FINEST) ) // logger.finest("utility.database.DBPlatformHelper.regExprDbType",regExpr,value); // NOI18N if( matchPattern(regExpr,vendorName) ) { dbType = value; } } return dbType; } /** Matches target to pattern specified regExp. Returns false if there is * any error compiling regExp. * @param regExp The regular expression. * @param target The target against which we are trying to match regExp. * @return false if there is error compiling regExp or target does not * match regExp. true if regExp matches pattern. */ private static boolean matchPattern(String regExp, String target) { boolean matches = false; try { matches = Pattern.matches(regExp,target); } catch (PatternSyntaxException e){ // logger.fine("utility.database.DBPlatformHelper.patternSyntaxException",e); // NOI18N } return matches; } /** * Overrides nameToVendorType with any system properties defined. */ private static void overrideWithSystemProperties(Properties nameToVendorType) { String vendorNameToType = null; // boolean debug = logger.isLoggable(); int counter = 1; do { String vendorNameToTypeProperty = VENDOR_NAME_TO_TYPE_PROPERTY + counter++; vendorNameToType = System.getProperty(vendorNameToTypeProperty); if(vendorNameToType != null) { //Split the vendorNameToType into two at char '=' String[] parsedProperty = vendorNameToType.split("=",2); //NOI18N if( parsedProperty.length >= 2) { String suggestedDbType = parsedProperty[0]; String regExp = parsedProperty[1]; // if(debug) { // logger.fine("utility.database.DBPlatformHelper.traceVendorNameToTypeProperty", //NOI18N // vendorNameToTypeProperty,regExp,suggestedDbType); // } nameToVendorType.put(regExp,suggestedDbType); } else { // if(debug) // logger.fine("utility.database.DBPlatformHelper.errorParsingVendorNameToTypeProperty", //NOI18N // vendorNameToTypeProperty,vendorNameToType); } } } while (vendorNameToType != null); } //-----Property Loading helper methods ----/ private static void loadFromResource(Properties properties, String resourceName, ClassLoader classLoader) throws IOException { load(properties, resourceName, classLoader); } /** * Loads properties list from the specified resource * into specified Properties object. * @param properties Properties object to load * @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. */ private static void load(Properties properties, final String resourceName, final ClassLoader classLoader) throws IOException { InputStream bin = new BufferedInputStream(openResourceInputStream(resourceName,classLoader)); try { properties.load(bin); } finally { try { bin.close(); } catch (Exception e) { // no action } } } /** * Open resourcenName as input stream inside doPriviledged block */ private static InputStream openResourceInputStream(final String resourceName, final ClassLoader classLoader) { return (InputStream) AccessController.doPrivileged( new PrivilegedAction() { public Object run() { if (classLoader != null) { return classLoader.getResourceAsStream(resourceName); } else { return ClassLoader.getSystemResourceAsStream(resourceName); } } } ); } }