909198cdb865c7e2df0672e5fada55bd73809148
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / adapters / AdapterFactory.java
1 package net.sourceforge.phpdt.sql.adapters;
2
3 import java.util.ArrayList;
4
5 public class AdapterFactory {
6         public static final String GENERIC = "GENERIC";
7     public static final String ORACLE = "ORACLE";
8     public static final String POSTGRES = "POSTGRES";
9     public static final String MYSQL = "MYSQL";
10     public static final String DB2 = "DB2";
11     public static final String DB2AS400 = "DB2AS400";
12         public static final String ADABASD = "ADABASD";
13
14     private static AdapterFactory instance;
15     
16     private ArrayList drivers;
17     
18     private AdapterFactory() {
19         loadDrivers();
20     }
21     public static synchronized AdapterFactory getInstance() {
22         if (instance == null) {
23                 instance = new AdapterFactory();
24         }
25         return instance;
26     }
27
28     /**
29      * Master list of supported drivers
30      */
31     private void loadDrivers() {
32         drivers = new ArrayList();
33
34                 DriverInfo generic = new DriverInfo(GENERIC, Messages.getString("adapters.generic"), new GenericAdapter());
35                 DriverInfo oracle = new DriverInfo(ORACLE, Messages.getString("adapters.oracle"), new OracleAdapter());
36                 DriverInfo db2 = new DriverInfo(DB2,  Messages.getString("adapters.db2"), new DB2Adapter());
37                 DriverInfo db2as400 = new DriverInfo(DB2AS400,  Messages.getString("adapters.db2as400"), new DB2AS400Adapter());
38                 DriverInfo postgres = new DriverInfo(POSTGRES,  Messages.getString("adapters.postgres"), new PostgresAdapter());
39                 DriverInfo mysql = new DriverInfo(MYSQL,  Messages.getString("adapters.mysql"), new MySQLAdapter());
40                 DriverInfo adabasd = new DriverInfo(ADABASD, Messages.getString("adapters.adabasd"), new AdabasDAdapter());
41
42         drivers.add(generic);
43         drivers.add(oracle);
44         drivers.add(db2);
45         drivers.add(db2as400);
46         drivers.add(postgres);
47         drivers.add(mysql);
48                 drivers.add(adabasd);
49     }
50     
51     public synchronized DatabaseAdapter getAdapter(String type) throws NoSuchAdapterException {
52         if (drivers == null) {
53                 loadDrivers();
54         }
55         for (int i = 0; i < drivers.size(); i++) {
56                 DriverInfo info = (DriverInfo) drivers.get(i);
57                 if (type.equals(info.getDriverType())) {
58                         return info.getAdapter().getInstance();
59                 }
60         }
61          throw new NoSuchAdapterException(type);
62     }
63     
64     public synchronized DriverInfo[] getDriverList() {
65         DriverInfo[] driverList = new DriverInfo[drivers.size()];
66         for (int i = 0; i < drivers.size(); i++) {
67                 DriverInfo info = (DriverInfo) drivers.get(i);
68                 driverList[i] = info;
69         }
70         return driverList;
71     }
72 }