fixed ID bug
[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.List;
6
7 import com.quantum.Messages;
8
9 /**
10  * @author root
11  * Basically this Factory is a Singleton that is used to return the proper adapter
12  */
13 public class AdapterFactory {
14         public static final String GENERIC = "GENERIC"; //$NON-NLS-1$
15     public static final String ORACLE = "ORACLE"; //$NON-NLS-1$
16     public static final String POSTGRES = "POSTGRES"; //$NON-NLS-1$
17     public static final String MYSQL = "MYSQL"; //$NON-NLS-1$
18     public static final String DB2 = "DB2"; //$NON-NLS-1$
19     public static final String DB2AS400 = "DB2AS400"; //$NON-NLS-1$
20         public static final String ADABASD = "ADABASD"; //$NON-NLS-1$
21     public static final String INFORMIX = "INFORMIX"; //$NON-NLS-1$
22         public static final String REDBRICK = "REDBRICK"; //$NON-NLS-1$
23     public static final String SYBASE = "SYBASE"; //$NON-NLS-1$
24
25     private static AdapterFactory instance;
26     
27     private ArrayList drivers;
28     
29     private AdapterFactory() {
30         loadDrivers();
31     }
32     public static synchronized AdapterFactory getInstance() {
33         if (instance == null) {
34                 instance = new AdapterFactory();
35         }
36         return instance;
37     }
38
39     /**
40      * Master list of supported drivers
41      */
42     private void loadDrivers() {
43         drivers = new ArrayList();
44
45                 DriverInfo generic = new DriverInfo(GENERIC, Messages.getString("adapters.generic"), new GenericAdapter()); //$NON-NLS-1$
46                 DriverInfo oracle = new DriverInfo(ORACLE, Messages.getString("adapters.oracle"), new OracleAdapter()); //$NON-NLS-1$
47                 DriverInfo db2 = new DriverInfo(DB2,  Messages.getString("adapters.db2"), new DB2Adapter()); //$NON-NLS-1$
48                 DriverInfo db2as400 = new DriverInfo(DB2AS400,  Messages.getString("adapters.db2as400"), new DB2AS400Adapter()); //$NON-NLS-1$
49                 DriverInfo postgres = new DriverInfo(POSTGRES,  Messages.getString("adapters.postgres"), new PostgresAdapter()); //$NON-NLS-1$
50                 DriverInfo mysql = new DriverInfo(MYSQL,  Messages.getString("adapters.mysql"), new GenericAdapter()); //$NON-NLS-1$
51                 DriverInfo adabasd = new DriverInfo(ADABASD, Messages.getString("adapters.adabasd"), new AdabasDAdapter()); //$NON-NLS-1$
52         DriverInfo informix = new DriverInfo(INFORMIX, Messages.getString("adapters.informix"), new GenericAdapter()); //$NON-NLS-1$
53                 DriverInfo redbrick = new DriverInfo(REDBRICK, Messages.getString("adapters.redbrick"), new RedBrickAdapter()); //$NON-NLS-1$
54         DriverInfo sybase = new DriverInfo(SYBASE, Messages.getString("adapters.sybase"), new GenericAdapter()); //$NON-NLS-1$
55
56         drivers.add(generic);
57         drivers.add(oracle);
58         drivers.add(db2);
59         drivers.add(db2as400);
60         drivers.add(postgres);
61         drivers.add(mysql);
62                 drivers.add(adabasd);
63         drivers.add(informix);
64                 drivers.add(redbrick);
65         drivers.add(sybase);
66     }
67     
68     public synchronized DatabaseAdapter getAdapter(String type){
69         if (drivers == null) {
70                 loadDrivers();
71         }
72         for (int i = 0; i < drivers.size(); i++) {
73                 DriverInfo info = (DriverInfo) drivers.get(i);
74                 if (type.equals(info.getDriverType())) {
75                         return info.getAdapter();
76                 }
77         }
78         // If its not a recognized driver, we return the generic one
79                 for (int i = 0; i < drivers.size(); i++) {
80                         DriverInfo info = (DriverInfo) drivers.get(i);
81                         if (type.equals(GENERIC)) {
82                                 return info.getAdapter();
83                         }
84                 }
85                 return null;
86     }
87     
88     public synchronized DriverInfo[] getDriverList() {
89         List list = new ArrayList(this.drivers);
90         Collections.sort(list);
91         return (DriverInfo[]) list.toArray(new DriverInfo[list.size()]);
92     }
93 }