37f05c46e32c061aee914d8c61d4502f75b693c3
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / adapters / AdapterFactory.java
1 package com.quantum.adapters;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.Comparator;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 /**
11  * Basically this Factory is a Singleton that is used to return the proper adapter
12  * @author root
13  */
14 public class AdapterFactory {
15         
16         static class ComparatorImpl implements Comparator {
17
18                 public int compare(Object arg0, Object arg1) {
19                         DatabaseAdapter adapter0 = (DatabaseAdapter) arg0;
20                         DatabaseAdapter adapter1 = (DatabaseAdapter) arg1;
21                         
22                         if (adapter0 == null && adapter0 != null) {
23                                 return -1;
24                         } else if (adapter0 != null && adapter1 == null) {
25                                 return 1;
26                         } else if (adapter0 == null && adapter1 == null) {
27                                 return 0;
28                         } else {
29                                 return adapter0.getDisplayName().compareTo(adapter1.getDisplayName());
30                         }
31                 }
32         }
33         
34         public static final String GENERIC = "GENERIC"; //$NON-NLS-1$
35         public static final String HSQLDB = "HSQLDB"; //$NON-NLS-1$
36     public static final String ORACLE = "ORACLE"; //$NON-NLS-1$
37     public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$
38     public static final String MYSQL = "MYSQL"; //$NON-NLS-1$
39     public static final String DB2 = "DB2"; //$NON-NLS-1$
40     public static final String DB2AS400 = "DB2AS400"; //$NON-NLS-1$
41         public static final String ADABASD = "ADABASD"; //$NON-NLS-1$
42     public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$
43         public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$
44         public static final String POINTBASE = "POINTBASE"; //$NON-NLS-1$
45     public static final String SYBASE = "SYBASE"; //$NON-NLS-1$
46
47         private static final Map DRIVER_MAP = Collections.synchronizedMap(new HashMap());
48         
49         static {
50                 DRIVER_MAP.put("com.ddtek.jdbc.informix.InformixDriver", AdapterFactory.INFORMIX);
51                 DRIVER_MAP.put("com.ddtek.jdbc.db2.DB2Driver", AdapterFactory.DB2);
52                 DRIVER_MAP.put("com.ddtek.jdbc.oracle.OracleDriver", AdapterFactory.ORACLE);
53                 DRIVER_MAP.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", AdapterFactory.GENERIC);
54                 DRIVER_MAP.put("com.ddtek.jdbc.sybase.SybaseDriver", AdapterFactory.SYBASE);
55                 DRIVER_MAP.put("com.ibm.as400.access.AS400JDBCDriver", AdapterFactory.DB2AS400);
56                 DRIVER_MAP.put("COM.ibm.db2.jdbc.app.DB2Driver", AdapterFactory.DB2);
57                 DRIVER_MAP.put("COM.ibm.db2.jdbc.net.DB2Driver", AdapterFactory.DB2);
58                 DRIVER_MAP.put("com.inet.ora.OraDriver", AdapterFactory.ORACLE);
59                 DRIVER_MAP.put("com.inet.drda.DRDADriver", AdapterFactory.DB2);
60                 DRIVER_MAP.put("com.inet.syb.SybDriver", AdapterFactory.SYBASE);
61                 DRIVER_MAP.put("com.inet.tds.TdsDriver", AdapterFactory.GENERIC);
62                 DRIVER_MAP.put("com.informix.jdbc.IfxDriver", AdapterFactory.INFORMIX);
63                 DRIVER_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", AdapterFactory.GENERIC);
64                 DRIVER_MAP.put("com.mysql.jdbc.Driver", AdapterFactory.MYSQL);
65                 DRIVER_MAP.put("com.pointbase.jdbc.jdbcUniversalDriver", AdapterFactory.POINTBASE);
66                 DRIVER_MAP.put("com.sybase.jdbcx.SybDriver", AdapterFactory.SYBASE);
67                 DRIVER_MAP.put("oracle.jdbc.driver.OracleDriver", AdapterFactory.ORACLE);
68                 DRIVER_MAP.put("org.gjt.mm.mysql.Driver", AdapterFactory.MYSQL);
69                 DRIVER_MAP.put("org.hsqldb.jdbcDriver", AdapterFactory.HSQLDB);
70                 DRIVER_MAP.put("org.postgresql.Driver", AdapterFactory.POSTGRES);
71                 DRIVER_MAP.put("weblogic.jdbc.informix.InformixDriver", AdapterFactory.INFORMIX);
72                 DRIVER_MAP.put("weblogic.jdbc.sqlserver.SybaseDriver", AdapterFactory.SYBASE);
73         }
74         
75     private static AdapterFactory instance;
76     
77     private Map adapters = Collections.synchronizedMap(new HashMap());
78     
79     private AdapterFactory() {
80         addAdapter(new GenericAdapter(GENERIC));
81         addAdapter(new GenericAdapter(HSQLDB));
82         addAdapter(new OracleAdapter());
83         addAdapter(new DB2Adapter());
84         addAdapter(new DB2AS400Adapter());
85         addAdapter(new PostgresAdapter());
86         addAdapter(new GenericAdapter(MYSQL));
87         addAdapter(new AdabasDAdapter());
88         addAdapter(new GenericAdapter(INFORMIX));
89         addAdapter(new RedBrickAdapter());
90         addAdapter(new GenericAdapter(SYBASE));
91         addAdapter(new GenericAdapter(POINTBASE));
92     }
93     private void addAdapter(DatabaseAdapter adapter) {
94         this.adapters.put(adapter.getType(), adapter);
95     }
96     
97     public static synchronized AdapterFactory getInstance() {
98         if (instance == null) {
99                 instance = new AdapterFactory();
100         }
101         return instance;
102     }
103
104     public DatabaseAdapter getAdapter(String type){
105         DatabaseAdapter result = (DatabaseAdapter) this.adapters.get(type);
106         if (result == null) {
107                 result = (DatabaseAdapter) this.adapters.get(GENERIC);
108         }
109         return result == null ? null : result;
110     }
111     
112     public DatabaseAdapter[] getDriverList() {
113         List list = new ArrayList(this.adapters.values());
114         Collections.sort(list, new ComparatorImpl());
115         return (DatabaseAdapter[]) list.toArray(new DatabaseAdapter[list.size()]);
116     }
117     
118     /**
119      * 
120      * @param driverClassName
121      * @return the adapter type, or null if the adapter type is not known
122      */
123     public String getAdapterType(String driverClassName) {
124         return (String) DRIVER_MAP.get(driverClassName);
125     }
126 }