Quantum version 2.4.1
[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         static class DriverInfo {
35                 private final String type;
36                 private final String urlPattern;
37
38                 DriverInfo(String type) {
39                         this(type, null);
40                 }
41                 DriverInfo(String type, String urlPattern) {
42                         this.type = type;
43                         this.urlPattern = urlPattern;
44                         
45                 }
46                 public String getType() {
47                         return this.type;
48                 }
49                 public String getURLPattern() {
50                         return this.urlPattern;
51                 }
52         }
53         
54         public static final String GENERIC = "GENERIC"; //$NON-NLS-1$
55         public static final String HSQLDB = "HSQLDB"; //$NON-NLS-1$
56     public static final String ORACLE = "ORACLE"; //$NON-NLS-1$
57     public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$
58     public static final String MYSQL = "MYSQL"; //$NON-NLS-1$
59     public static final String DB2 = "DB2"; //$NON-NLS-1$
60     public static final String DB2AS400 = "DB2AS400"; //$NON-NLS-1$
61         public static final String ADABASD = "ADABASD"; //$NON-NLS-1$
62     public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$
63         public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$
64         public static final String POINTBASE = "POINTBASE"; //$NON-NLS-1$
65     public static final String SYBASE = "SYBASE"; //$NON-NLS-1$
66     public static final String JDBC_ODBC_BRIDGE = "JDBC_ODBC_BRIDGE"; //$NON-NLS-1$
67     public static final String MS_SQL_SERVER = "MS_SQL_SERVER"; //$NON-NLS-1$
68
69         private static final Map DRIVER_MAP = Collections.synchronizedMap(new HashMap());
70         
71         static {
72                 DRIVER_MAP.put("com.ddtek.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
73                 DRIVER_MAP.put("com.ddtek.jdbc.db2.DB2Driver", new DriverInfo(DB2, "jdbc:datadirect:db2://${hostname}:${port};DatabaseName=${dbname}"));
74                 DRIVER_MAP.put("com.ddtek.jdbc.oracle.OracleDriver", new DriverInfo(ORACLE));
75                 DRIVER_MAP.put("com.ddtek.jdbc.sqlserver.SQLServerDriver", new DriverInfo(GENERIC));
76                 DRIVER_MAP.put("com.ddtek.jdbc.sybase.SybaseDriver", new DriverInfo(SYBASE));
77                 DRIVER_MAP.put("com.ibm.as400.access.AS400JDBCDriver", new DriverInfo(DB2AS400));
78                 DRIVER_MAP.put("COM.ibm.db2.jdbc.app.DB2Driver", new DriverInfo(DB2, "jdbc:db2:{dbname}"));
79                 DRIVER_MAP.put("COM.ibm.db2.jdbc.net.DB2Driver", new DriverInfo(DB2, "jdbc:db2://{hostname}:{port}/{dbname}"));
80                 DRIVER_MAP.put("com.inet.ora.OraDriver", new DriverInfo(ORACLE));
81                 DRIVER_MAP.put("com.inet.drda.DRDADriver", new DriverInfo(DB2, "jdbc:inetdb2:{hostname}:{port}?database={dbname}"));
82                 DRIVER_MAP.put("com.inet.syb.SybDriver", new DriverInfo(SYBASE));
83                 DRIVER_MAP.put("com.inet.tds.TdsDriver", new DriverInfo(MS_SQL_SERVER));
84                 DRIVER_MAP.put("com.informix.jdbc.IfxDriver", new DriverInfo(INFORMIX, "jdbc:informix-sqli://{hostname}:{port}/{dbname}:INFORMIXSERVER={informixserver}"));
85                 DRIVER_MAP.put("com.microsoft.jdbc.sqlserver.SQLServerDriver", new DriverInfo(MS_SQL_SERVER));
86                 DRIVER_MAP.put("com.mysql.jdbc.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
87                 DRIVER_MAP.put("com.pointbase.jdbc.jdbcUniversalDriver", new DriverInfo(POINTBASE, "jdbc:pointbase:server://{hostname}:{port}/{dbname}"));
88                 DRIVER_MAP.put("com.sybase.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
89                 DRIVER_MAP.put("com.sybase.jdbc2.jdbc.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
90                 DRIVER_MAP.put("com.sybase.jdbcx.SybDriver", new DriverInfo(SYBASE, "jdbc:sybase:Tds:{hostname}:{port}/{dbname}"));
91                 DRIVER_MAP.put("net.sourceforge.jtds.jdbc.Driver", new DriverInfo(MS_SQL_SERVER));
92                 DRIVER_MAP.put("oracle.jdbc.driver.OracleDriver", new DriverInfo(ORACLE));
93                 DRIVER_MAP.put("org.gjt.mm.mysql.Driver", new DriverInfo(MYSQL, "jdbc:mysql://{hostname}:{port}/{dbname}"));
94                 DRIVER_MAP.put("org.hsqldb.jdbcDriver", new DriverInfo(HSQLDB));
95                 DRIVER_MAP.put("org.postgresql.Driver", new DriverInfo(POSTGRES, "jdbc:postgresql://{hostname}:{port}/{dbname}"));
96                 DRIVER_MAP.put("sun.jdbc.odbc.JdbcOdbcDriver", new DriverInfo(JDBC_ODBC_BRIDGE, "jdbc:odbc:{datasource}"));
97                 DRIVER_MAP.put("weblogic.jdbc.informix.InformixDriver", new DriverInfo(INFORMIX));
98                 DRIVER_MAP.put("weblogic.jdbc.sqlserver.SybaseDriver", new DriverInfo(SYBASE));
99         }
100         
101     private static AdapterFactory instance;
102     
103     private Map adapters = Collections.synchronizedMap(new HashMap());
104     
105     private AdapterFactory() {
106         addAdapter(new GenericAdapter(GENERIC));
107         addAdapter(new GenericAdapter(HSQLDB));
108         addAdapter(new OracleAdapter());
109         addAdapter(new DB2Adapter());
110         addAdapter(new DB2AS400Adapter());
111         addAdapter(new PostgresAdapter());
112         addAdapter(new MySQLAdapter());
113         addAdapter(new AdabasDAdapter());
114         addAdapter(new InformixAdapter());
115         addAdapter(new RedBrickAdapter());
116         addAdapter(new GenericAdapter(SYBASE));
117         addAdapter(new GenericAdapter(POINTBASE));
118         addAdapter(new GenericAdapter(JDBC_ODBC_BRIDGE));
119         addAdapter(new MSSQLServerAdapter());
120     }
121     private void addAdapter(DatabaseAdapter adapter) {
122         this.adapters.put(adapter.getType(), adapter);
123     }
124     
125     public static synchronized AdapterFactory getInstance() {
126         if (instance == null) {
127                 instance = new AdapterFactory();
128         }
129         return instance;
130     }
131
132     public DatabaseAdapter getAdapter(String type){
133         DatabaseAdapter result = (DatabaseAdapter) this.adapters.get(type);
134         if (result == null) {
135                 result = (DatabaseAdapter) this.adapters.get(GENERIC);
136         }
137         return result == null ? null : result;
138     }
139     
140     public DatabaseAdapter[] getDriverList() {
141         List list = new ArrayList(this.adapters.values());
142         Collections.sort(list, new ComparatorImpl());
143         return (DatabaseAdapter[]) list.toArray(new DatabaseAdapter[list.size()]);
144     }
145     
146     /**
147      * 
148      * @param driverClassName
149      * @return the adapter type, or null if the adapter type is not known
150      */
151     public String getAdapterType(String driverClassName) {
152         DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
153                 return driverInfo == null ? null : driverInfo.getType();
154     }
155         /**
156          * @param type
157          * @return
158          */
159         public String getURLPattern(String driverClassName) {
160         DriverInfo driverInfo = (DriverInfo) DRIVER_MAP.get(driverClassName);
161                 return driverInfo == null ? null : driverInfo.getURLPattern();
162         }
163 }