001package bradleyross.library.database; 002import java.util.Hashtable; 003import java.util.Enumeration; 004import java.util.Collection; 005import java.util.Vector; 006import java.util.Iterator; 007import bradleyross.library.NotSupportedException; 008import bradleyross.library.database.DatabaseExtras; 009/** 010 * Provide for registration of DatabaseExtras 011 * subclasses. 012 * @see DatabaseExtras 013 * @author Bradley Ross 014 * 015 */ 016public class ExtrasRegistration 017{ 018 /** 019 * Contains registration information for a subclass of DatabaseExtras 020 * @author Bradley Ross 021 * 022 */ 023 public static class Item 024 { 025 /** 026 * DatabaseExtras object for handling the database specific properties. 027 */ 028 protected DatabaseExtras object = null; 029 /** 030 * Name of the DatabaseExtras subclass that handles the database 031 * specific properties. 032 */ 033 protected String extrasName = null; 034 /** 035 * Character string that identifies the type of database. 036 * 037 * <p>Typical values are <code>SQLSERVER</code>, <code>ORACLE</code>, 038 * and <code>MYSQL</code>.</p> 039 */ 040 protected String databaseCode = null; 041 /** 042 * This object is instantiated when the DatabaseExtras object 043 * calls the register method below. 044 * 045 * @see #register(DatabaseExtras) 046 * @param extrasObject 047 * @throws ClassNotFoundException 048 * @throws InstantiationException 049 * @throws IllegalAccessException 050 * @throws NotSupportedException 051 */ 052 public Item(DatabaseExtras extrasObject) 053 throws ClassNotFoundException, InstantiationException, IllegalAccessException, NotSupportedException 054 { 055 extrasName = extrasObject.getClass().getName(); 056 057 object = extrasObject; 058 databaseCode = object.getDbms(); 059 } 060 /** 061 * Return the code that identifies the type of database. 062 * @return Code for specific type of database 063 */ 064 public String getDbms() 065 { 066 return databaseCode; 067 } 068 /** 069 * Return the DatabaseExtras object that handles the 070 * specific type of database. 071 * @return DatabaseExtras object 072 */ 073 public DatabaseExtras getDatabaseExtras() 074 { 075 return object; 076 } 077 /** 078 * Return the name of the DatabaseExtras subclass that handles the 079 * specific type of database. 080 * @return Name of subclass 081 */ 082 public String getClassName() 083 { 084 return extrasName; 085 } 086 } 087 /** 088 * This table contains the registration information for the various DatabaseExtras objects. 089 */ 090 protected static Hashtable<String, Item> registrationList = new Hashtable<String, Item>(); 091 /** 092 * This method is called by the DatabaseExtras objects when they are instantiated. 093 * @see DatabaseExtras 094 * @param object DatabaseExtras object to be registered 095 * @throws ClassNotFoundException 096 * @throws InstantiationException 097 * @throws IllegalAccessException 098 * @throws NotSupportedException 099 */ 100 public static void register(DatabaseExtras object) 101 throws ClassNotFoundException, InstantiationException, IllegalAccessException, NotSupportedException 102 { 103 Item item = new ExtrasRegistration.Item(object); 104 registrationList.put(item.getDbms(), item); 105 } 106 /** 107 * Return the entire contents of the registration list. 108 * 109 * <p>Primarily for diagnostics. 110 * 111 * @return Collection of registration information 112 */ 113 public static Collection<Item> getContents() 114 { 115 return registrationList.values(); 116 } 117 /** 118 * Returns an array containing the codes for the registered databases. 119 * @return Array of code values 120 */ 121 public static String[] getNames() 122 { 123 String test[] = new String[1]; 124 Enumeration<String> keys = registrationList.keys(); 125 Vector<String> list = new Vector<String>(); 126 while (keys.hasMoreElements()) 127 { list.add(keys.nextElement()); } 128 list.trimToSize(); 129 return list.toArray(test); 130 } 131 /** 132 * Return the DatabaseExtras object that is appropriate for a specific type 133 * of database. 134 * 135 * @param code The code identifying the DBMS 136 * @return The appropriate DatabaseExtras object 137 */ 138 public static DatabaseExtras getDatabaseExtras(String code) 139 { 140 Item temp = registrationList.get(code); 141 return temp.getDatabaseExtras(); 142 } 143 /** 144 * Test driver 145 * @param args Not used in this case 146 */ 147 public static void main(String[] args) 148 { 149 try 150 { 151 Class.forName("bradleyross.library.database.MySQL").newInstance(); 152 Class.forName("bradleyross.library.database.SQLServer").newInstance(); 153 Class.forName("bradleyross.library.database.DB2").newInstance(); 154 Class.forName("bradleyross.library.database.Postgres").newInstance(); 155 Class<?> thisclass = Class.forName("bradleyross.library.database.Oracle"); 156 thisclass.newInstance(); 157 Collection<Item> list = getContents(); 158 Iterator<Item> iter = list.iterator(); 159 while (iter.hasNext()) 160 { 161 System.out.println(); 162 Item next = (Item) iter.next(); 163 System.out.println(next.getDbms() + " " + next.getDatabaseExtras().getClass().getName()); 164 System.out.println(next.getDatabaseExtras().listAllTerms()); 165 } 166 } 167 catch (Exception e) 168 { 169 System.out.println(e.getClass().getName() + " " + e.getMessage()); 170 e.printStackTrace(); 171 } 172 System.out.println(); 173 System.out.println("Registered databases"); 174 String names[] = getNames(); 175 for (int i = 0; i < names.length; i++) 176 { 177 String item = names[i]; 178 179 DatabaseExtras instance = ExtrasRegistration.getDatabaseExtras(item); 180 System.out.println(item + " " + instance.getDbms()); 181 } 182 } 183}