Distributed-Java Lab#3 design document, db utilities As an interface between the main application and the actual database, I plan to write the following low-level utilties (warning, this is subject to change): In regard to lab3.properties file, in class Lab3Props: mayLoadProperties() Loads the properties if not already loaded. reLoadProperties() Always loads the latest version of properties String getSystem() From loaded properties, get value of 'system' String getProp(String system, String propname) Get system.propname property if it specifically exists, else get generic propname property. openDB() Opens connection to data source, per configured driver&URL closeDB() Closes previously-opened connection ResultSet doQuery(String sql) Do query via already-open DB connection ResultSet doUpdate(String sql) Do update via already-open DB connection Update 2005.May.03-06, added new flexibility to support running under JSP: setProperties(ResourceBundle) Gets contents of properties file from external source, such as JSP, instead of reading directly from disk. setOut(Object) Changes the default output-stream, which must implement the MyPrintableI interface. (setOut throws exception if fails check) Object getOut() Accessor for current default output-stream print(String) Uses current output-stream to print the string println(String) Uses current output-stream to print the string with NL setHtml(boolean) Selects between HTML and plain-text output mode boolean getHtml() Accessor for current HTML/PLAIN mode String nl() Returns "
\n" or just "\n" depending on HTML/PLAIN mode Typical usage is to say print("some text" + nl()); New class invented 2005.May.06, MyPrintableI, which provides runtime enforcement of print/println being provided by the class of an object, and allows runtime building of appropriate calls to such methods: Method[] objectSupportsPrinting(Object o) -- Checks class of object for presence of *both* methods print(String) and println(String), if successful then returns array containing the methods respectively. omsInvoke(Object O, Method m, String s) -- Tries to invoke that method on that object with string given as only argument. If it fails, throws an unchecked exception to make high-level abort easy. In regard to Users table, in class Users: mayMakeTable() If table doesn't exist, make it, and initialize it. add(String username, ? passwd) If no such name already, add record. delete(String username) Delete record. changePW(String username, ? newpasswd) Change password int check(String username, ? passwd) If valid, return Seq, else return -1. String getName(int seq) Return Name field. int findName(String name) Return sequence number of matching name, or -1. String[] getNames() Return vector of all Name fields. In regard to ActiveUsers table, in class Users: MakeActive(int userID, datetime expire, String sessionID) Make this user active by adding record to table. MakeInactive(int userID) Make this user not active by deleting record. SetExpire(int userID, datetime newexpire) Change the when-expire field. datetime GetExpire(int userID) Return the when-expire field. String GetSessionID(int userID) Return the sessionID field. In regard to Msgs table, in class Msgs: MakeMessage(String messageText) Assign a new message number, install this in the table, using the lock to assure proper synchronization. String getMessage(int MsgID) Return text of message. In regard to Cabs table, in class Cabs: Add(String name, boolean onDuty) Assign a new CabID, install this in the table, with OnDuty field set as specified. Delete(String name) Delete record from table. ChangeName(String oldName, String newName) Assuming the new name isn't already present in the table, modify record to change name. SetOnDuty(String name) Change record to show this cab as on duty now. ClearOnDuty(String name) Change record to show this cab as not on duty now. GetName(int cabID) Return name of taxicab. GetOndutyNames() Return enumeration or iteration of names of on-duty cabs. In regard to Locks table, in class Locks: boolean TryLock(String lockname, String username) Try to establish lock by adding row to table, return true if successful. boolean TryLockRepeatedly(String lockname, String username, int ntries) Try to establish lock, if fails then keep trying until ntries exhausted. UnLock(String lockname, String username) Remove the lock previously set. Usage warning: After setting a lock successfully, all further code should be inside a try block, until the matching UnLock, which should be inside the corresponding finally block, to avoid leaving a resource permanently locked. Higher-level utilities performing complex queries against database, in class DBUtil: SendMessage(String messageText, String[] cabNames) Make message, map cabnames to their cabIDs, enter one record for each cabID into MCrelat table. GetLastMessages(int maxcount) Return enumeration or iteration of the most recent messages. GetCabCounts() Return vector of records listing cab name and corresponding number of messages to that cab.