/* -*-Java-*- ******************************************************************************** * * File: MOM.java * Description: Class MOM -- MARS Object Manager.... * Author: Dennis F. Freeze, HP Labs * Created: Sun Jul 14 23:42:34 1996 * Modified: Fri Feb 21 10:57:47 1997 (Dennis F. Freeze) dff@DFFHOME * Language: Java * Status: Experimental (Do Not Distribute) * * (c) Copyright 1996, Hewlett-Packard Company * ******************************************************************************** * * Revisions: * * Wed Jan 29 08:45:01 1997 (Michael L. Creech) creech@HPLMLCPC * Changed base_content_dir & added commented out code for profiling and * debugging. ******************************************************************************** */ package mars.mom; // import mars.mom.*; // import java.util.*; import java.net.*; import java.io.*; // import jgl.HashMap; // import dataramp.sql.*; import dataramp.jdbc.*; // &&& For Debugging and Profiling: // import utils.StatLine; // import utils.Misc; // ************************************************************ // * Class MOM (Mars Object Manager).... // ************************************************************ public final class MOM { // Instance Variables.... private static String Username = "miadmin"; private static String Password = ""; private static String DefaultDB = "Grail"; private static String LockOptions = "(NOLOCK)"; // private static boolean db_connection = false; // private static String DatabaseUrl; private static String ServerURL; private static final String ClassList = "MarsClasses.list"; private static final String VarFileSuffix = ".vars"; private static Connection BaseConnection; private static Statement BaseStatement; private static MarsClass CurrentProcessedClass; private static MVector mars_classes; public static final String RetrieveLoad = "LOAD"; public static final String RetrieveWait = "WAIT"; // // public static final int SkeletonStatus = 0; public static final int CompleteStatus = 1; public static final String no_login_id = ""; // // private static String base_content_dir = "e:/mars-content/"; //private static String base_content_dir = "file:/g:/mars-content/"; private static String base_content_dir = "http://grail.hpl.hp.com/marsweb/mars-content/"; public static String current_author_id = ""; // public static boolean debug_mode; // // Caches for content objects.... // private static HashMap video_streams; private static HashMap scenes; private static HashMap shots; private static HashMap frames; private static HashMap images; // private static HashMap audio_streams; private static HashMap audio_clips; // private static HashMap text_objects; // private static HashMap persons; private static HashMap martians; // private static HashMap mars_objects; // private static MVector attributes; // // public static Runtime rt_env; // // Constructor: establishes a base connection and creates a base statement. // this leads to faster query execution(no need to reconnect). this is the // only non-static member of mom. it can easily be converted to a static // method. right now the assumption is that there is only one mom object in a // given context.(the same will essentially be true if the whole class is // static). // public MOM(String db) { // debug_mode = true; // Cache info about the content classes.... mars_classes = new MVector(10, 5); // setupCacheVectors(); // // Get runtime env for checking memory usage.... rt_env = Runtime.getRuntime (); // if (db == null) { // don't connect yet.... } else { connectToDB(db); } } // // public static void printMemoryUsage() { //rt.gc (); // Optional step to make memory numbers more accurate? long freeMem = rt_env.freeMemory (); // Not always accurate; an estimate$ long totalMem = rt_env.totalMemory (); // print ("$$$ Memory: FREE = " + freeMem + " TOTAL = " + totalMem); } // // public static void print(String txt) { if (debug_mode) { System.out.println(txt); // StatLine.log (txt); } } // // public static void warning(String txt) { System.out.println(txt); } // // public static boolean connectedToDB() { // are we connected? return db_connection; } // // public static boolean connectToDefaultDB() { return connectToDB(DefaultDB); } // // public static boolean connectToDB(String con_db) { // Pick a DB -- and a Web server for class info.... setupDB(con_db); // Read in info about the Mars content classes. RegisterMarsClasses(); //MOM.DumpMarsClasses(); // *DFF* debug // Connect to the DB. try { // register the dataramp driver DriverManager.registerDriver( new DRDriver() ); // Attempt to connect to a driver. Each one // of the registered drivers will be loaded until // one is found that can process this URL. (simpleselect based, kl). // BaseConnection = DriverManager.getConnection(DatabaseUrl, Username, Password); checkForWarning (BaseConnection.getWarnings ()); // Get the DatabaseMetaData object and display // some information about the connection DatabaseMetaData dma = BaseConnection.getMetaData (); print("\nConnected to " + dma.getURL()); print("Driver " + dma.getDriverName()); print("Version " + dma.getDriverVersion()); print(""); BaseStatement = BaseConnection.createStatement (); db_connection = true; // read in the Authors, Users.... initCache(); // return db_connection; } catch (SQLException ex) { HandleSqlException(ex); return false; } catch (java.lang.Exception ex) { // Got some other type of exception. Dump it. ex.printStackTrace (); return false; } } // // public static boolean disconnectDB() { // Disconnect from the DB, and flush the cache.... boolean disc = false; try { BaseConnection.close(); flushCache(); db_connection = false; disc = true; } catch (SQLException ex) { HandleSqlException(ex); } return disc; } // // public static String getLockOptions() { return LockOptions; } // // public static void setupDB(String db) { // Sets up both DB and HTTP server, which are (for now) assumed to // exist on the same server. if (db.equals("Mars")) { // this version does MARS server: MS SQL Server.... //DatabaseUrl = "jdbc:dataramp:AMAROK/dr-ms-sql"; DatabaseUrl = "jdbc:dataramp:AMAROK/LocalServer"; //ServerURL = "http://amarok.hpl.hp.com/ms-dbmgr/"; //ServerURL = "http://grail.hpl.hp.com/marsweb/dbmanager/"; ServerURL = "file:///d:/mars/dbmanager/"; Username = "sa"; Password = ""; print("setupDB('Mars'): DB_URL='" + DatabaseUrl + "' ServerURL='" + ServerURL + "'"); } else if (db.equals("Grail")) { // // this version does GRAIL (Illustra) server.... //DatabaseUrl = "jdbc:dataramp:grail/source3"; DatabaseUrl = "jdbc:dataramp:grail/source4"; ServerURL = "http://grail.hpl.hp.com/marsweb/dbmanager/"; LockOptions = ""; // don't need this for Illustra print("setupDB('Grail'): DB_URL='" + DatabaseUrl + "' ServerURL='" + ServerURL + "'"); } else { // // Unknown DB -- pick GRAIL. DatabaseUrl = "jdbc:dataramp:grail/source3"; ServerURL = "http://grail.hpl.hp.com/marsweb/dbmanager/"; LockOptions = ""; // don't need this for Illustra } } // // public static boolean objectInDB(String classname, String id) { String cname = (String)mars_objects.get(id); if (classname.equals(cname)) { //print("? ObjectInDB (Y): " + classname + "/" + id); return true; } else { //print("? ObjectInDB (N): " + classname + "/" + id); return false; } } // // public static boolean OLDobjectInDB(String classname, String id) { boolean found = false; ResultSet pres = MOM.SendSQL("select marsID from " + classname + " " + LockOptions + " where marsID = '" + id + "';"); // try { while ( pres.next() ) { String mid = pres.getString("marsID"); found = true; } } catch (SQLException ex) { MOM.HandleSqlException(ex); } // return found; } // // ********************************************************************* // * Methods for manipulating the Attributes.... // ********************************************************************* // // // static void loadAttributes() { // Loads default values of various attributes.... // Eventually, this will load the initial values from the DB. // Right now, it just uses hardcoded values in MOM.... // String attr_prog_types = "Lecture,Documentary,Commercial,Video Conference," + "Personal,TV Program,Movie,Music"; String attr_genres = "Action,Animation,Biographical,Business,Children,Comedy," + "Educational,Health,Horror," + "Historical,News,Promotional,Technical,Training," + "Musical,Mystery,Sports,Travel,Western,"; //String attr_genres = "Children,Classics,Comedy,Computers,Drama," + // "Educational,Health,Horror,Film Noir,Foreign," + // "Game show,Instructional,News,Outdoors,Soaps," + // "Sports,Suspense,Talk show,Travel,Western," + // "Business,Management,Products,Training,Technical," + // "Action,Adventure,Animation,Crime,Documentary," + // "Musical,Mystery,Romance,Sci-Fi,Thriller,War"; // // String attr_roles = "Cast,Crew,Character"; String attr_cast_sub_roles = "Actor,Actress,Animal,Other"; String attr_crew_sub_roles = "Producer,Director,Writer,Musician,Camera," + "Art Director,Editor,Photographer,Cinematographer"; String attr_char_sub_roles = "Speaker,Narrator,Host,Guest,Subject"; // String attr_pub_freq = "Hourly,Daily,Weekly,Bi-Weekly,Monthly,Bi-Monthly," + "Annual,Other"; String attr_text_types = "ASCII,HTML,Other"; // // Create and register the Attributes.... // addAttribute(new MarsAttribute("Program Type", new MVector(attr_prog_types))); addAttribute(new MarsAttribute("Genre", new MVector(attr_genres))); addAttribute(new MarsAttribute("Role", new MVector(attr_roles))); addAttribute(new MarsAttribute("Cast Sub_Role", new MVector(attr_cast_sub_roles))); addAttribute(new MarsAttribute("Crew Sub_Role", new MVector(attr_crew_sub_roles))); addAttribute(new MarsAttribute("Char Sub_Role", new MVector(attr_char_sub_roles))); addAttribute(new MarsAttribute("Text Type", new MVector(attr_text_types))); addAttribute(new MarsAttribute("Publication Frequency", new MVector(attr_pub_freq))); } // // public static MarsAttribute getAttribute(String an) { MarsAttribute ma = null; Enumeration menum = attributes.elements(); while (menum.hasMoreElements()) { ma = (MarsAttribute)menum.nextElement(); if (ma.attribute_name.equals(an)) { return ma; } } // return null; } public static void dumpAttributes() { MarsAttribute ma = null; int knt = 0; Enumeration menum = attributes.elements(); while (menum.hasMoreElements()) { ma = (MarsAttribute)menum.nextElement(); MOM.print("Attribute #" + knt + " = '" + ma.attribute_name + "'"); } } // public static void addAttribute(MarsAttribute ma) { attributes.append(ma); } // public static void addAttributeValue(String an, String val) { MarsAttribute ma = getAttribute(an); if (ma != null) { ma.addValue(val); } } // public static void removeAttributeValue(String an, String val) { MarsAttribute ma = getAttribute(an); if (ma != null) { ma.removeValue(val); } } public static void dumpAttributeValues() { MarsAttribute ma = null; int knt = 0; Enumeration menum = attributes.elements(); while (menum.hasMoreElements()) { ma = (MarsAttribute)menum.nextElement(); ma.dumpAttribute(); } } // // ********************************************************************* // * Methods for getting/setting info/pointers for MARS Content.... // ********************************************************************* // // public static String getContentBaseDir() { return base_content_dir; } // public static void setContentBaseDir(String cbd) { if (cbd.endsWith("/")) { base_content_dir = cbd; } else { base_content_dir = cbd + "/"; } } // **************************************************************************** // * Cache management.... // **************************************************************************** // // public static void setupCacheVectors() { // // Init the cache HashMaps for the various content objects.... // Best to use primes for the initial sizes.... // video_streams = new HashMap(); scenes = new HashMap(); shots = new HashMap(); frames = new HashMap(); images = new HashMap(); // audio_streams = new HashMap(); audio_clips = new HashMap(); // text_objects = new HashMap(); // persons = new HashMap(); martians = new HashMap(); // mars_objects = new HashMap(); // for registering all marsIDs // //video_streams = new HashMap( 79); //scenes = new HashMap( 547); //shots = new HashMap(1601); //frames = new HashMap(2357); //images = new HashMap(2357); // //audio_streams = new HashMap( 79); //audio_clips = new HashMap( 127); // //text_objects = new HashMap( 127); // //persons = new HashMap( 79); //martians = new HashMap( 37); // //mars_objects = new HashMap(3571); // for registering all marsIDs // attributes = new MVector(10, 10); } // // public static void initCache() { // loadAttributes(); // if (db_connection) { // registerObjects(); // readMartiansFromDB(); } else { warning("ERROR: attempt to init the cache without a DB connection!"); } } // // public static void initFullCache() { // loadAttributes(); // if (db_connection) { // registerObjects(); // readMartiansFromDB(); readVideoStreamsFromDB(); readScenesFromDB(); readShotsFromDB(); readVideoFramesFromDB(); readMarsImagesFromDB(); readMarsTextsFromDB(); readAudioStreamsFromDB(); readMarsAudioClipsFromDB(); readPersonsFromDB(); } else { warning("ERROR: attempt to init the cache without a DB connection!"); } } // // public static void registerObjects() { ResultSet pres; String mid; // print("*** ENTERING registerObjects...."); // // try { pres = MOM.SendSQL("select marsID from VideoStream " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "VideoStream"); } // pres = MOM.SendSQL("select marsID from Scene " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "Scene"); } // pres = MOM.SendSQL("select marsID from Shot " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "Shot"); } // pres = MOM.SendSQL("select marsID from VideoFrame " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "VideoFrame"); } // pres = MOM.SendSQL("select marsID from MarsImage " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "MarsImage"); } // pres = MOM.SendSQL("select marsID from AudioStream " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "AudioStream"); } // pres = MOM.SendSQL("select marsID from MarsAudioClip " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "MarsAudioClip"); } // pres = MOM.SendSQL("select marsID from MarsText " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "MarsText"); } // pres = MOM.SendSQL("select marsID from Person " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "Person"); } // pres = MOM.SendSQL("select marsID from Martian " + LockOptions + ";"); while ( pres.next() ) { mid = pres.getString("marsID"); mars_objects.put(mid, "Martian"); print("*** Registering existence of Martian '" + mid + "'...."); } // print("*** LEAVING registerObjects...."); } catch (SQLException ex) { MOM.HandleSqlException(ex); } } // // public static void saveCache(boolean authors_flag, boolean attr_flag, boolean content_flag ) { if (content_flag) { // print("*** Saving VideoStream cache.... " + video_streams); Enumeration vsenum = video_streams.elements(); while (vsenum.hasMoreElements()) { VideoStream vs = (VideoStream)vsenum.nextElement(); if (vs != null) { vs.Save(false); } } // print("*** Saving Scene cache.... " + scenes); Enumeration scenum = scenes.elements(); while (scenum.hasMoreElements()) { Scene sc = (Scene)scenum.nextElement(); if (sc != null) { sc.Save(false); } } // print("*** Saving Shot cache.... " + shots); Enumeration shenum = shots.elements(); while (shenum.hasMoreElements()) { Shot sh = (Shot)scenum.nextElement(); if (sh != null) { sh.Save(false); } } // print("*** Saving VideoFrame cache.... " + frames); Enumeration vfenum = frames.elements(); while (vfenum.hasMoreElements()) { VideoFrame vf = (VideoFrame)vfenum.nextElement(); if (vf != null) { vf.Save(false); } } // print("*** Saving MarsImage cache.... " + images); Enumeration mienum = images.elements(); while (mienum.hasMoreElements()) { MarsImage mi = (MarsImage)mienum.nextElement(); if (mi != null) { mi.Save(false); } } // print("*** Saving AudioStream cache.... "+ audio_streams); Enumeration asenum = audio_streams.elements(); while (asenum.hasMoreElements()) { AudioStream as = (AudioStream)asenum.nextElement(); if (as != null) { as.Save(false); } } // print("*** Saving MarsAudioClip cache.... " + audio_clips); Enumeration acenum = audio_clips.elements(); while (acenum.hasMoreElements()) { MarsAudioClip ac = (MarsAudioClip)acenum.nextElement(); if (ac != null) { ac.Save(false); } } // print("*** Saving MarsText cache.... " + text_objects); Enumeration toenum = text_objects.elements(); while (toenum.hasMoreElements()) { MarsText to = (MarsText)toenum.nextElement(); if (to != null) { to.Save(false); } } // print("*** Saving Person cache.... " + persons); Enumeration mpenum = persons.elements(); while (mpenum.hasMoreElements()) { Person mp = (Person)mpenum.nextElement(); if (mp != null) { mp.Save(false); } } } // if (attr_flag) { // Save the Attributes.... } // if (authors_flag) { // print("*** Saving Martians cache.... " + martians); Enumeration auenum = martians.elements(); while (auenum.hasMoreElements()) { Martian au = (Martian)auenum.nextElement(); if (au != null) { au.Save(false); } } } } // // public static void flushCache() { // simply reset the vectors, allowing old version to be GC'ed.... setupCacheVectors(); } // // ********************************************************************* // * // * Methods for using the MOM Object Cache: // * // * 0. Methods for loading objects into the cache. // * 1. Access methods for instances of MARS Objects of various classes. // * 2. Get the cache vector for a given class. // * 3. Register new objects in the cache. // * // ********************************************************************* // // // public static void readVideoStreamsFromDB() { // Read VStreams from the DB.... print("! MOM.readVStreams...."); Enumeration mkeys = mars_objects.keys("VideoStream"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readVStreams is looking for: " + mid); VideoStream new_vs = getVideoStream(mid, false, true); } } // // // // // public static void readVideoStreamsFromDB() // { // // Read VStreams from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from VideoStream;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readVStreams is restoring: " + mid); // // // VideoStream new_vstream = getVideoStream(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getVideoStreamCache() { return video_streams; } // public static void registerVideoStream(VideoStream vs) { // String mid = vs.GET_marsID(); if (video_streams.get(mid) == null) { video_streams.put(mid, vs); } } // // public static VideoStream getVideoStream(String id, boolean recursive, boolean flesh) { print("%%% MOM.getVStream: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); VideoStream new_object = (VideoStream)video_streams.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("VideoStream", id)) { new_object = new VideoStream(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerVideoStream(new_object); } return new_object; } // // public static VideoStream LocateVideoStreamByTitle(String ptitle) { // Checks against the title variable in MarsObject.... Enumeration enum = video_streams.elements(); //print("GET: target = " + s + " elements = " + enum); while (enum.hasMoreElements()) { VideoStream vstream = (VideoStream)enum.nextElement(); if ((vstream != null) && (vstream.title.equals(ptitle))) { return vstream; } else { //print(" NOT a MarsObject : " + foo); } } // return null; } // **************************************************************************** // // public static void readScenesFromDB() { // Read Scenes from the DB.... Enumeration mkeys = mars_objects.keys("Scene"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readScenes is looking for: " + mid); Scene new_sc = getScene(mid, false, true); } } // // // // // public static void readScenesFromDB() // { // // Read Scenes from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from Scene;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readScenes is restoring: " + mid); // // // Scene new_scene = getScene(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getSceneCache() { return scenes; } // public static void registerScene(Scene sc) { // String mid = sc.GET_marsID(); if (scenes.get(mid) == null) { scenes.put(mid, sc); } } // public static Scene getScene(String id, boolean recursive, boolean flesh) { // Misc.startTime ("MOM.getScene"); print("%%% MOM.getScene: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); Scene new_object = (Scene)scenes.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("Scene", id)) { new_object = new Scene(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerScene(new_object); } // Misc.stopTime ("MOM.getScene"); return new_object; } // **************************************************************************** // // public static void readShotsFromDB() { // Read Shots from the DB.... Enumeration mkeys = mars_objects.keys("Shot"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readShots is looking for: " + mid); Shot new_sh = getShot(mid, false, true); } } // // // // // public static void readShotsFromDB() // { // // Read Shots from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from Shot;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readShots is restoring: " + mid); // // // Shot new_shot = getShot(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getShotCache() { return shots; } // public static void registerShot(Shot sh) { // String mid = sh.GET_marsID(); if (shots.get(mid) == null) { shots.put(mid, sh); } } // public static Shot getShot(String id, boolean recursive, boolean flesh) { print("%%% MOM.getShot: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); Shot new_object = (Shot)shots.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("Shot", id)) { new_object = new Shot(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerShot(new_object); } return new_object; } // **************************************************************************** // // public static void readVideoFramesFromDB() { // Read Frames from the DB.... Enumeration mkeys = mars_objects.keys("VideoFrame"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readVideoFrames is looking for: " + mid); VideoFrame new_vf = getVideoFrame(mid, false, true); } } // // // // // public static void readVideoFramesFromDB() // { // // Read Frames from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from VideoFrame;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readVFrames is restoring: " + mid); // // // VideoFrame new_frame = getVideoFrame(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getVideoFrameCache() { return frames; } // public static void registerVideoFrame(VideoFrame vf) { // String mid = vf.GET_marsID(); if (frames.get(mid) == null) { frames.put(mid, vf); } } // public static VideoFrame getVideoFrame(String id, boolean recursive, boolean flesh) { print("%%% MOM.getVFrame: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); VideoFrame new_object = (VideoFrame)frames.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("VideoFrame", id)) { new_object = new VideoFrame(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerVideoFrame(new_object); } return new_object; } // **************************************************************************** // // public static void readMarsImagesFromDB() { // Read Images from the DB.... Enumeration mkeys = mars_objects.keys("MarsImage"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readMarsImages is looking for: " + mid); MarsImage new_mi = getMarsImage(mid, false, true); } } // // // // // public static void readMarsImagesFromDB() // { // // Read Images from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from MarsImage;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readImages is restoring: " + mid); // // // MarsImage new_image = getMarsImage(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getMarsImageCache() { return images; } // public static void registerMarsImage(MarsImage mi) { // String mid = mi.GET_marsID(); if (images.get(mid) == null) { images.put(mid, mi); } } // public static MarsImage getMarsImage(String id, boolean recursive, boolean flesh) { print("%%% MOM.getImage: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); MarsImage new_object = (MarsImage)images.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("MarsImage", id)) { new_object = new MarsImage(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerMarsImage(new_object); } return new_object; } // // **************************************************************************** // * Audio classes: AudioStream, MarsAudioClip // **************************************************************************** // // // public static void readAudioStreamsFromDB() { // Read AudioStreams from the DB.... Enumeration mkeys = mars_objects.keys("AudioStream"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readAudioStreams is looking for: " + mid); AudioStream new_as = getAudioStream(mid, false, true); } } // // // // // public static void readAudioStreamsFromDB() // { // // Read AudioStreams from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from AudioStream;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readAStreams is restoring: " + mid); // // // AudioStream new_astream = getAudioStream(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getAudioStreamCache() { return audio_streams; } // public static void registerAudioStream(AudioStream as) { // String mid = as.GET_marsID(); if (audio_streams.get(mid) == null) { audio_streams.put(mid, as); } } // public static AudioStream getAudioStream(String id, boolean recursive, boolean flesh) { print("%%% MOM.getAStream: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); AudioStream new_object = (AudioStream)audio_streams.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("AudioStream", id)) { new_object = new AudioStream(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerAudioStream(new_object); } return new_object; } // **************************************************************************** // // public static void readMarsAudioClipsFromDB() { // Read MarsAudioClips from the DB.... Enumeration mkeys = mars_objects.keys("MarsAudioClip"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readAClips is looking for: " + mid); MarsAudioClip new_ac = getMarsAudioClip(mid, false, true); } } // // // // // public static void readMarsAudioClipsFromDB() // { // // Read MarsAudioClips from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from MarsAudioClip;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readAClips is restoring: " + mid); // // // MarsAudioClip new_aclip = getMarsAudioClip(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getMarsAudioClipCache() { return audio_clips; } // public static void registerMarsAudioClip(MarsAudioClip ac) { // String mid = ac.GET_marsID(); if (audio_clips.get(mid) == null) { audio_clips.put(mid, ac); } } // public static MarsAudioClip getMarsAudioClip(String id, boolean recursive, boolean flesh) { print("%%% MOM.getAClip: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); MarsAudioClip new_object = (MarsAudioClip)audio_clips.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("MarsAudioClip", id)) { new_object = new MarsAudioClip(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerMarsAudioClip(new_object); } return new_object; } // // **************************************************************************** // * People classes: Person, Author, User // **************************************************************************** // // // public static void readPersonsFromDB() { // Read Persons from the DB.... Enumeration mkeys = mars_objects.keys("Person"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readPersons is looking for: " + mid); Person new_mp = getPerson(mid, false, true); } } // // // // // public static void readPersonsFromDB() // { // // Read Persons from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from Person;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.readPersons is restoring: " + mid); // // // Person new_mp = getPerson(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getPersonCache() { return persons; } // public static void registerPerson(Person mp) { // String mid = mp.GET_marsID(); if (persons.get(mid) == null) { persons.put(mid, mp); } } // public static Person getPerson(String id, boolean recursive, boolean flesh) { print("%%% MOM.getPerson: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); Person new_object = (Person)persons.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("Person", id)) { new_object = new Person(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerPerson(new_object); } return new_object; } // **************************************************************************** // // public static void readMartiansFromDB() { // Read Martians (Authors and Users) from the DB.... Enumeration mkeys = mars_objects.keys("Martian"); // while (mkeys.hasMoreElements()) { String mid = (String)mkeys.nextElement(); print("! MOM.readMartians is looking for: " + mid); Martian new_auth = getMartian(mid, false, true); } } // // // // // public static void readMartiansFromDB() // { // // Read Martians (Authors and Users) from the DB.... // ResultSet pres = MOM.SendSQL("select marsID from Martian;"); // // // try // { // while ( pres.next() ) // { // String mid = pres.getString("marsID"); // print("! MOM.readMartians is looking for: " + mid); // // $$$ // //mars_objects.put(mid, "Martian"); // Martian new_auth = getMartian(mid, false, true); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // } // // public static HashMap getMartianCache() { return martians; } // public static void registerMartian(Martian ma) { // String mid = ma.GET_marsID(); if (martians.get(mid) == null) { martians.put(mid, ma); } } // public static Martian getMartian(String id, boolean recursive, boolean flesh) { print("%%% MOM.getMartian: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); Martian new_object = (Martian)martians.get(id); if (new_object != null) { print("%%% getMartian '" + id + "': found " + new_object.GET_last_name()); new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("Martian", id)) { print("%%% getMartian '" + id + "': in DB...."); new_object = new Martian(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerMartian(new_object); } else { print("%%% getMartian '" + id + "': NOT IN DB!!!"); } // return new_object; } // // public static Martian LocateMartianByUserID(String user_id) { // checks to see if a Martian exists with the given user_id.... Martian val_martian = null; String mid = null; // String sql = "select marsID from Martian " + LockOptions + " where user_id = '" + user_id + "';"; ResultSet pres = MOM.SendSQL(sql); try { while ( pres.next() ) { mid = pres.getString("marsID"); //print("! MOM.LocateMartian: found user_id '" + user_id + "'"); } } catch (SQLException ex) { MOM.HandleSqlException(ex); } // if (mid != null) { val_martian = MOM.getMartian(mid, false, true); } else { print("! MOM.LocateMartian: UNKNOWN user_id '" + user_id + "'"); } // return val_martian; } // // // // // public static Martian LocateMartianByUserID(String user_id) // { // // checks to see if a Martian exists with the given user_id.... // Martian val_martian = null; // // // String sql = "select marsID from Martian where user_id = '" + user_id + "';"; // ResultSet pres = MOM.SendSQL(sql); // // try // { // if ( pres.next() ) // { // String mid = pres.getString("marsID"); // //print("! MOM.LocateMartian: found user_id '" + user_id + "'"); // val_martian = MOM.getMartian(mid, false, true); // } // else // { // print("! MOM.LocateMartian: UNKNOWN user_id '" + user_id + "'"); // } // } // catch (SQLException ex) // { // MOM.HandleSqlException(ex); // } // // // return val_martian; // } // // **************************************************************************** // * Misc classes: MarsText // **************************************************************************** // // // public static void readMarsTextsFromDB() { // Read Text objects from the DB.... ResultSet pres = MOM.SendSQL("select marsID from MarsText " + LockOptions + ";"); // try { while ( pres.next() ) { String mid = pres.getString("marsID"); //print("! MOM.readTexts is restoring: " + mid); // MarsText new_text = getMarsText(mid, false, true); } } catch (SQLException ex) { MOM.HandleSqlException(ex); } } // // public static HashMap getMarsTextCache() { return text_objects; } // public static void registerMarsText(MarsText mt) { // String mid = mt.GET_marsID(); if (text_objects.get(mid) == null) { text_objects.put(mid, mt); } } // public static MarsText getMarsText(String id, boolean recursive, boolean flesh) { print("%%% MOM.getMarsText: ID = " + id + " recurse = " + recursive + " flesh = " + flesh); MarsText new_object = (MarsText)text_objects.get(id); if (new_object != null) { new_object.Restore(recursive, flesh); } else if (MOM.objectInDB("MarsText", id)) { new_object = new MarsText(); new_object.marsID = id; new_object.Restore(recursive, flesh); new_object.saved_in_db = true; // isn't set if (false, false).... registerMarsText(new_object); } return new_object; } // **************************************************************************** // * // * Send SQL statements to the DB, and check results/warnings.... // * // **************************************************************************** // // Send a query to the database, and receive and return the response. // public static synchronized ResultSet SendSQL(String query) { // Misc.startTime ("SendSQL"); ResultSet MarsResultSet = null; try { print("\n? MOM.SendSQL: " + query + "\n"); //printMemoryUsage(); MarsResultSet = BaseStatement.executeQuery(query); } catch (SQLException ex) { HandleSqlException(ex); } catch (java.lang.Exception ex) { // Got some other type of exception. Dump it. ex.printStackTrace (); } // // Misc.stopTime ("SendSQL"); return MarsResultSet; } // // Print info about the SQL exception. // public static void HandleSqlException(SQLException ex) { // A SQLException was generated. Catch it and display the error info. Note // that there could be multiple error objects chained together. warning ("\n*** SQLException caught ***\n"); while (ex != null) { warning ("SQLState: " + ex.getSQLState ()); warning ("Message: " + ex.getMessage ()); warning ("Vendor: " + ex.getErrorCode ()); ex = ex.getNextException (); warning (""); } } // **************************************************************************** // * checkForWarning: Checks for and displays warnings. Returns true if a // * warning existed.... // **************************************************************************** // private static boolean checkForWarning (SQLWarning warn) throws SQLException { boolean rc = false; // If an SQLWarning object was given, display the warning messages. Note // that there could be multiple warnings chained together.... if (warn != null) { warning ("\n *** SQL Warning ***\n"); rc = true; while (warn != null) { warning ("SQLState: " + warn.getSQLState ()); warning ("Message: " + warn.getMessage ()); warning ("Vendor: " + warn.getErrorCode ()); warning (""); warn = warn.getNextWarning (); } } // return rc; } // **************************************************************************** // Debugging methods -- erase when not needed. // **************************************************************************** // public static void DumpMarsClasses() { int length = mars_classes.size(); MarsClass mclass; for(int i = 0; i< length; i++) { mclass = (MarsClass)(mars_classes.get(i)); print("--------------------------------------"); print(mclass.class_name + " [Parent = " + mclass.parent_class_name + "]"); DumpPVars(mclass.instance_variables); print("--------------------------------------"); } } // public static void DumpPVars(MVector persistent_vars) { int length = persistent_vars.size(); MarsIVar mars_var; for (int i = 0; i< length; i++) { mars_var = (MarsIVar)(persistent_vars.get(i)); DumpMarsIVar(mars_var); } } // // public static void DumpMarsIVar(MarsIVar mars_var) { print(mars_var.var_name + " " + mars_var.var_type + " " + mars_var.var_sub_type + " " + mars_var.var_length + " " + mars_var.scope + " " + mars_var.retrieval_flag + " " + mars_var.reference_type); } // // public static void DumpVector(MVector vector) { // this will cause an error if MVector doesn't hold MarsObjects....*DFF* int length = vector.size(); print("Dumping MVector....:"); for (int i = 0; i< length; i++) { MarsObject mo = (MarsObject)(vector.get(i)); print(mo.marsID); } print("End of MVector...."); } // // **************************************************************************** // * Reading Class info.... // **************************************************************************** // // Register all classes for which configuration files are available. // public static void RegisterMarsClasses() { URL MarsClassesUrl; InputStream ServerConnection; DataInputStream DataConnection; String ClassName; MarsClass mclass; try { print("*** MOM.registerClasses()...."); MarsClassesUrl = new URL(ServerURL + ClassList); print("*** MOM.registerClasses(): URL = '" + MarsClassesUrl + "'...."); ServerConnection = MarsClassesUrl.openStream(); DataConnection = new DataInputStream(new BufferedInputStream(ServerConnection)); while ( (ClassName = DataConnection.readLine()) != null ) { CurrentProcessedClass = new MarsClass(); CurrentProcessedClass.parent_class_name = ProcessClass(ClassName); CurrentProcessedClass.class_name = ClassName; RegisterClassInfo(CurrentProcessedClass); } } catch(MalformedURLException e) { warning("*** Bad URL in MOM.RegisterMarsClasses()."); } catch (IOException e) { warning("*** I/O error in MOM.RegisterMarsClasses(): msg = " + e.getMessage()); } } // // Read the description file of a class and fill in the instance_variables // vector with useful info about the class' persistent instance variables. // public static String ProcessClass(String ClassName) { URL ClassVarsUrl; InputStream ServerConnection; DataInputStream DataConnection; String PVariable; String HeaderLine; String ParentClass = null; MarsIVar VarInfo; //print("MOM.ProcClass(" + ClassName + ")"); try { ClassVarsUrl = new URL(ServerURL + ClassName + VarFileSuffix); ServerConnection = ClassVarsUrl.openStream(); DataConnection = new DataInputStream(new BufferedInputStream(ServerConnection)); // HeaderLine = DataConnection.readLine(); // skip the first line ParentClass = DataConnection.readLine(); // read the parent class // while ( (PVariable = DataConnection.readLine()) != null ) { VarInfo = ProcessVariable(PVariable); CurrentProcessedClass.instance_variables.append(VarInfo); } } catch(MalformedURLException e) { warning("*** Bad URL in MOM.ProcessClass()."); } catch (IOException e) { warning("*** IO error in MOM.ProcessClass()."); } return ParentClass; } // // Process a variable description line from the class description file // extracting the info and returning it packaged in a MarsIVar object. // private static MarsIVar ProcessVariable(String PVariable) { MarsIVar VarInfo = new MarsIVar(); StringTokenizer VarTokenizer = new StringTokenizer(PVariable); //print("MOM.ProcVar: '" + PVariable + "'"); try { VarInfo.var_name = VarTokenizer.nextToken(); VarInfo.var_type = VarTokenizer.nextToken(); VarInfo.var_sub_type = VarTokenizer.nextToken(); // String tmp_len = VarTokenizer.nextToken(); VarInfo.var_length = Integer.parseInt(tmp_len); // VarInfo.scope = VarTokenizer.nextToken(); VarInfo.retrieval_flag = VarTokenizer.nextToken(); VarInfo.reference_type = VarTokenizer.nextToken(); } catch (NoSuchElementException e) { warning("MOM.ProcessVariable: Wrong description_file format!"); } return VarInfo; } // // ********************************************************************* // * Methods for handling persistence info about MARS classes.... // ********************************************************************* // // // Add the info about a Mars class to the vector of MarsClass objects // describing each class registered with MOM. // public static void RegisterClassInfo(MarsClass mclass) { mars_classes.append(mclass); } // // Return a mars class object containing info about the class with // the specified name. // public static MarsClass FetchClassInfo(String class_name) { int Length = mars_classes.size(); MarsClass mclass; for (int i=0; i< Length; i++) { mclass = (MarsClass)(mars_classes.get(i)); if (mclass.class_name.equals(class_name)) { return mclass; } } return null; } // // Determine whether the specified persistent variable in the specified // class should be loaded immediately or left skeletal. // public static boolean LoadOk(String ClassName, String PVarName) { MarsClass CurrentMarsClass = MOM.FetchClassInfo(ClassName); MarsIVar MarsVarInfo = CurrentMarsClass.FetchIVar(PVarName); // if (MarsVarInfo.retrieval_flag.equals(RetrieveLoad)) { return true; } else { return false; } } } // *** end of class MOM *** // ************************************************************ // * // * Class MarsClass: descriptions of Mars Content Classes.... // * // ************************************************************ class MarsClass { // Description of a MARS class.... String class_name; String parent_class_name; MVector instance_variables; // // public MarsClass() { instance_variables = new MVector(); } // // MarsClass(String cname, String parent_name) { class_name = cname; parent_class_name = parent_name; instance_variables = new MVector(); } // // Return info about the specified persistent variable in this object. // MarsIVar FetchIVar(String vname) { int length = instance_variables.size(); MarsIVar mars_ivar; for (int i = 0; i < length ; i++) { mars_ivar = (MarsIVar)(instance_variables.get(i)); if (mars_ivar.var_name.equals(vname)) { return mars_ivar; } } return null; } } // ************************************************************* // * // * Class MarsIVar: info about a single persistent variable.... // * // ************************************************************* class MarsIVar { // Info about a single instance variable in some MARS class.... String var_name; // the name of the instance variable String var_type; // String or Int or Date or Object String var_sub_type; // the class name when var_type is an Object int var_length; // length of the String or array String scope; // SIMPLE or COMPLEX String retrieval_flag; // LOAD or WAIT String reference_type; // SELF or COMPONENT or REFERENCE // public MarsIVar() { } }