FileDocCategorySizeDatePackage
DBPlatformHelper.javaAPI DocGlassfish v2 API8920Tue May 22 16:54:34 BST 2007oracle.toplink.essentials.internal.helper

DBPlatformHelper

public class DBPlatformHelper extends Object
author
Mitesh Meswani This class is responsible to translate given database name to a DatabasePlatform.

Fields Summary
private static final String
DEFAULTPLATFORM
private static final String
PROPERTY_PATH
private static final String
VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME
private static Properties
_nameToVendorPlatform
Holds mapping between possible vendor names to internal platforms defined above. vendor names are treated as regular expressions.
Constructors Summary
Methods Summary
public static java.lang.StringgetDBPlatform(java.lang.String vendorName, oracle.toplink.essentials.logging.SessionLog logger)
Get Database Platform from vendor name.

param
vendorName Input vendor name. Typically this is obtained by querying DatabaseMetaData.
param
logger The logger.
return
Database platform that corresponds to vendorName. If vendorName does not match any of predefined vendor names, DEFAULTPLATFORM is returned.


                                                     
           

        initializeNameToVendorPlatform(logger);

        String detectedDbPlatform = null;
        if(vendorName != null) {
            detectedDbPlatform = matchVendorNameInProperties(vendorName, _nameToVendorPlatform, logger);
        }
        if (logger.shouldLog(SessionLog.FINE) ) {
            logger.log(SessionLog.FINE, "dbPlaformHelper_detectedVendorPlatform", detectedDbPlatform ); // NOI18N
        }
        if (detectedDbPlatform == null) {
            if(logger.shouldLog(SessionLog.INFO)) {
                logger.log(SessionLog.INFO, "dbPlaformHelper_defaultingPlatform",  vendorName, DEFAULTPLATFORM); // NOI18N
            }
            detectedDbPlatform = DEFAULTPLATFORM;
        }
        return detectedDbPlatform;
    
private static java.util.PropertiesinitializeNameToVendorPlatform(oracle.toplink.essentials.logging.SessionLog logger)
Allocate and initialize nameToVendorPlatform if not already done.

        synchronized(DBPlatformHelper.class) {
            if(_nameToVendorPlatform == null) {
                _nameToVendorPlatform = new Properties();
                try {
                    loadFromResource(_nameToVendorPlatform, VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME,
                                            DBPlatformHelper.class.getClassLoader() );
                } catch (IOException e) {
                    logger.log(SessionLog.WARNING, "dbPlaformHelper_noMappingFound", VENDOR_NAME_TO_PLATFORM_RESOURCE_NAME);
                }
            }
        }
        return _nameToVendorPlatform;
    
private static voidload(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. 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.


        InputStream bin = new BufferedInputStream(openResourceInputStream(resourceName,classLoader));

        try {
            properties.load(bin);
        } finally {
            try {
                bin.close();
            } catch (Exception e) {
                // no action
            }
        }
    
private static voidloadFromResource(java.util.Properties properties, java.lang.String resourceName, java.lang.ClassLoader classLoader)

        load(properties, resourceName, classLoader);
    
private static booleanmatchPattern(java.lang.String regExp, java.lang.String target, oracle.toplink.essentials.logging.SessionLog logger)
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.
param
logger
return
false if there is error compiling regExp or target does not match regExp. true if regExp matches pattern.

        boolean matches = false;
        try {
            matches = Pattern.matches(regExp,target);
        } catch (PatternSyntaxException e){
            if(logger.shouldLog(SessionLog.FINE)) {
                logger.log(SessionLog.FINE, "dbPlaformHelper_patternSyntaxException", e); // NOI18N
            }
        }
        return matches;
    
private static java.lang.StringmatchVendorNameInProperties(java.lang.String vendorName, java.util.Properties nameToVendorPlatform, oracle.toplink.essentials.logging.SessionLog logger)
Match vendorName in properties specifieid by _nameToVendorPlatform.

        String dbPlatform = null;
        //Iterate over all properties till we find match.
        for( Iterator iterator = nameToVendorPlatform.entrySet().iterator();
                dbPlatform == null && iterator.hasNext();) {
            Map.Entry entry = (Map.Entry) iterator.next();
            String regExpr = (String) entry.getKey();
            String value = (String) entry.getValue();
            if(logger.shouldLog(SessionLog.FINEST)) {
                logger.log(SessionLog.FINEST, "dbPlaformHelper_regExprDbPlatform", regExpr, value); // NOI18N
            }
            if( matchPattern(regExpr, vendorName, logger) ) {
                dbPlatform = value;
            }
        }
        return dbPlatform;
    
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);
                    }
                }
            }
        );