litesoft
@ 811
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.sql; import java.util.*; import org.litesoft.core.typeutils.*; import org.litesoft.db.*; import org.litesoft.logger.*; public class SQLProductSpecificHelperManager { public static final Logger LOGGER = LoggerFactory.getLogger( SQLProductSpecificHelperManager.class ); private SQLProductSpecificHelperManager() { } public static synchronized SQLProductSpecificHelper getVendorSpecificHelperFor( DBinfo pDBinfo ) throws DBException { SQLProductSpecificHelper specificHelper = HELPERS.get( pDBinfo.toString() ); if ( specificHelper == null ) { if ( null == (specificHelper = HELPERS.get( pDBinfo.getProductName() )) ) { String nomName = className( pDBinfo.toString() ); if ( null == (specificHelper = load( nomName )) ) { if ( null == (specificHelper = load( nomName = className( pDBinfo.getProductName() ) )) ) { throw new DisplayableDBException( "UnableLoadSQLProductSpecificHelper", pDBinfo.getProductName(), pDBinfo.getProductVersion() ); } } HELPERS.put( nomName, specificHelper ); } } return specificHelper; } private static final Map<String, SQLProductSpecificHelper> HELPERS = new HashMap<String, SQLProductSpecificHelper>(); private static String className( String pString ) { StringBuilder sb = new StringBuilder( pString ); for ( int i = 0; i < sb.length(); i++ ) { char c = sb.charAt( i ); if ( !Characters.isAlphaNumeric( c ) ) { sb.setCharAt( i, '_' ); } } return sb.toString(); } private static SQLProductSpecificHelper load( String pNomName ) { Class<SQLProductSpecificHelper> zClass = SQLProductSpecificHelper.class; String classNameToLoad = zClass.getName(); String toLoad = classNameToLoad + pNomName; try { Class targetClass = zClass.getClassLoader().loadClass( toLoad ); if ( SQLProductSpecificHelper.class.isAssignableFrom( targetClass ) ) { return (SQLProductSpecificHelper) targetClass.newInstance(); } LOGGER.error.log( toLoad, " does NOT implement ", classNameToLoad ); } catch ( ClassNotFoundException e ) { return null; // Normal Behavior } catch ( IllegalAccessException e ) { LOGGER.error.log( e, "Unable To Load ", toLoad ); } catch ( InstantiationException e ) { LOGGER.error.log( e, "Unable To Load ", toLoad ); } return null; } } |