package com.quantum.adapters; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Basically this Factory is a Singleton that is used to return the proper adapter * @author root */ public class AdapterFactory { static class ComparatorImpl implements Comparator { public int compare(Object arg0, Object arg1) { DatabaseAdapter adapter0 = (DatabaseAdapter) arg0; DatabaseAdapter adapter1 = (DatabaseAdapter) arg1; if (adapter0 == null && adapter0 != null) { return -1; } else if (adapter0 != null && adapter1 == null) { return 1; } else if (adapter0 == null && adapter1 == null) { return 0; } else { return adapter0.getDisplayName().compareTo(adapter1.getDisplayName()); } } } public static final String GENERIC = "GENERIC"; //$NON-NLS-1$ public static final String HSQLDB = "HSQLDB"; //$NON-NLS-1$ public static final String ORACLE = "ORACLE"; //$NON-NLS-1$ public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$ public static final String MYSQL = "MYSQL"; //$NON-NLS-1$ public static final String DB2 = "DB2"; //$NON-NLS-1$ public static final String DB2AS400 = "DB2AS400"; //$NON-NLS-1$ public static final String ADABASD = "ADABASD"; //$NON-NLS-1$ public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$ public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$ public static final String POINTBASE = "POINTBASE"; //$NON-NLS-1$ public static final String SYBASE = "SYBASE"; //$NON-NLS-1$ private static final Map DRIVER_MAP = Collections.synchronizedMap(new HashMap()); static { DRIVER_MAP.put("com.ddtek.jdbc.informix.InformixDriver", AdapterFactory.INFORMIX); DRIVER_MAP.put("com.ddtek.jdbc.db2.DB2Driver", AdapterFactory.DB2); DRIVER_MAP.put("com.ddtek.jdbc.oracle.OracleDriver", AdapterFactory.ORACLE); DRIVER_MAP.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", AdapterFactory.GENERIC); DRIVER_MAP.put("com.ddtek.jdbc.sybase.SybaseDriver", AdapterFactory.SYBASE); DRIVER_MAP.put("com.ibm.as400.access.AS400JDBCDriver", AdapterFactory.DB2AS400); DRIVER_MAP.put("COM.ibm.db2.jdbc.app.DB2Driver", AdapterFactory.DB2); DRIVER_MAP.put("COM.ibm.db2.jdbc.net.DB2Driver", AdapterFactory.DB2); DRIVER_MAP.put("com.inet.ora.OraDriver", AdapterFactory.ORACLE); DRIVER_MAP.put("com.inet.drda.DRDADriver", AdapterFactory.DB2); DRIVER_MAP.put("com.inet.syb.SybDriver", AdapterFactory.SYBASE); DRIVER_MAP.put("com.inet.tds.TdsDriver", AdapterFactory.GENERIC); DRIVER_MAP.put("com.informix.jdbc.IfxDriver", AdapterFactory.INFORMIX); DRIVER_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", AdapterFactory.GENERIC); DRIVER_MAP.put("com.mysql.jdbc.Driver", AdapterFactory.MYSQL); DRIVER_MAP.put("com.pointbase.jdbc.jdbcUniversalDriver", AdapterFactory.POINTBASE); DRIVER_MAP.put("com.sybase.jdbcx.SybDriver", AdapterFactory.SYBASE); DRIVER_MAP.put("oracle.jdbc.driver.OracleDriver", AdapterFactory.ORACLE); DRIVER_MAP.put("org.gjt.mm.mysql.Driver", AdapterFactory.MYSQL); DRIVER_MAP.put("org.hsqldb.jdbcDriver", AdapterFactory.HSQLDB); DRIVER_MAP.put("org.postgresql.Driver", AdapterFactory.POSTGRES); DRIVER_MAP.put("weblogic.jdbc.informix.InformixDriver", AdapterFactory.INFORMIX); DRIVER_MAP.put("weblogic.jdbc.sqlserver.SybaseDriver", AdapterFactory.SYBASE); } private static AdapterFactory instance; private Map adapters = Collections.synchronizedMap(new HashMap()); private AdapterFactory() { addAdapter(new GenericAdapter(GENERIC)); addAdapter(new GenericAdapter(HSQLDB)); addAdapter(new OracleAdapter()); addAdapter(new DB2Adapter()); addAdapter(new DB2AS400Adapter()); addAdapter(new PostgresAdapter()); addAdapter(new GenericAdapter(MYSQL)); addAdapter(new AdabasDAdapter()); addAdapter(new GenericAdapter(INFORMIX)); addAdapter(new RedBrickAdapter()); addAdapter(new GenericAdapter(SYBASE)); addAdapter(new GenericAdapter(POINTBASE)); } private void addAdapter(DatabaseAdapter adapter) { this.adapters.put(adapter.getType(), adapter); } public static synchronized AdapterFactory getInstance() { if (instance == null) { instance = new AdapterFactory(); } return instance; } public DatabaseAdapter getAdapter(String type){ DatabaseAdapter result = (DatabaseAdapter) this.adapters.get(type); if (result == null) { result = (DatabaseAdapter) this.adapters.get(GENERIC); } return result == null ? null : result; } public DatabaseAdapter[] getDriverList() { List list = new ArrayList(this.adapters.values()); Collections.sort(list, new ComparatorImpl()); return (DatabaseAdapter[]) list.toArray(new DatabaseAdapter[list.size()]); } /** * * @param driverClassName * @return the adapter type, or null if the adapter type is not known */ public String getAdapterType(String driverClassName) { return (String) DRIVER_MAP.get(driverClassName); } }