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 Object containing additional information for database
047                 * @throws ClassNotFoundException if class not found
048                 * @throws InstantiationException if unable to create object
049                 * @throws IllegalAccessException if access not allowed
050                 * @throws NotSupportedException if not supported
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                 * 
072                 * @return DatabaseExtras object
073                 */
074                public DatabaseExtras getDatabaseExtras()
075                {
076                        return object;
077                }
078                /**
079                 * Return the name of the DatabaseExtras subclass that handles the
080                 * specific type of database.
081                 * @return Name of subclass
082                 */
083                public String getClassName()
084                {
085                        return extrasName;
086                }
087        }
088        /**
089         * This table contains the registration information for the various DatabaseExtras objects.
090         */
091        protected static Hashtable<String, Item> registrationList = new Hashtable<String, Item>();
092        /**
093         * This method is called by the DatabaseExtras objects when they are instantiated.
094         * @see DatabaseExtras
095         * @param object DatabaseExtras object to be registered
096         * @throws ClassNotFoundException if class not found
097         * @throws InstantiationException if unable to create object
098         * @throws IllegalAccessException if access not allowed
099         * @throws NotSupportedException if not supported
100         */
101        public static void register(DatabaseExtras object) 
102        throws ClassNotFoundException, InstantiationException, IllegalAccessException, NotSupportedException
103        {
104                Item item = new ExtrasRegistration.Item(object);
105                registrationList.put(item.getDbms(), item);
106        }
107        /**
108         * Return the entire contents of the registration list.
109         * 
110         * <p>Primarily for diagnostics.
111         * 
112         * @return Collection of registration information
113         */
114        public static Collection<Item> getContents()
115        {
116                return registrationList.values();
117        }
118        /**
119         * Returns an array containing the codes for the registered databases.
120         * @return Array of code values
121         */
122        public static String[] getNames()
123        {
124                String test[] = new String[1];
125                Enumeration<String> keys = registrationList.keys();
126                Vector<String> list = new Vector<String>();
127                while (keys.hasMoreElements())
128                { list.add(keys.nextElement()); }
129                list.trimToSize();
130                return list.toArray(test);
131        }
132        /**
133         * Return the DatabaseExtras object that is appropriate for a specific type
134         * of database.
135         * 
136         * @param code The code identifying the DBMS
137         * @return The appropriate DatabaseExtras object
138         */
139        public static DatabaseExtras getDatabaseExtras(String code)
140        {
141                Item temp = registrationList.get(code);
142                return temp.getDatabaseExtras();
143        }
144        /**
145         * Test driver
146         * @param args Not used in this case
147         */
148        public static void main(String[] args) 
149        {
150                try
151                {
152                        Class.forName("bradleyross.library.database.MySQL").newInstance();
153                        Class.forName("bradleyross.library.database.SQLServer").newInstance();
154                        Class.forName("bradleyross.library.database.DB2").newInstance();
155                        Class.forName("bradleyross.library.database.Postgres").newInstance();
156                        Class<?> thisclass = Class.forName("bradleyross.library.database.Oracle");
157                        thisclass.newInstance();
158                        Collection<Item> list = getContents();
159                        Iterator<Item> iter = list.iterator();
160                        while (iter.hasNext())
161                        {
162                                System.out.println();
163                                Item next = (Item) iter.next();
164                                System.out.println(next.getDbms() + " " + next.getDatabaseExtras().getClass().getName());
165                                System.out.println(next.getDatabaseExtras().listAllTerms());
166                        }
167                }
168                catch (Exception e)
169                {
170                        System.out.println(e.getClass().getName() + " " + e.getMessage());
171                        e.printStackTrace();
172                }
173                System.out.println();
174                System.out.println("Registered databases");
175                String names[] = getNames();
176                for (int i = 0; i < names.length; i++)
177                {
178                        String item = names[i];
179                        
180                        DatabaseExtras instance = ExtrasRegistration.getDatabaseExtras(item);
181                        System.out.println(item + " " + instance.getDbms());
182                }
183        }
184}