Changeset 22973

Show
Ignore:
Timestamp:
28.09.2010 10:18:57 (9 years ago)
Author:
davidb
Message:

Code changed to use Class.forName to dynamically load required database wrapper class (e.g. GDBMWrapper or JDBMWrapper). This is so the code can be compiled up with some of these database wrapper classes optionally switched out (e.g. name changed to .java.tmp). Additional routine also added to test is a database is open. In JDBMWrapper, the close routine was modified so it only closes the database one. Our code seems to call 'cleanup' more than once, and letting it close a JDBMWrapper class a second time throws an exception deep within JDBM implementation.

Location:
main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/JDBMWrapper.java

    r21431 r22973  
    4343    static String TNAME = "greenstone"; 
    4444 
    45     RecordManager  recman_; 
     45    RecordManager  recman_ = null; 
    4646    HTree          hashtable_; 
     47 
     48    String db_filename_; 
    4749 
    4850    static private PrintWriter utf8out = null; 
     
    7476      // => assume the database must exist 
    7577      boolean must_exist = true; // default 
    76        
     78 
     79      if (recman_ != null) { 
     80      String message = "openDatabase() called when the class already has a database open\n"; 
     81      message += "  Use closeDatabase before opening the next one.\n"; 
     82      message += "  Existing database file: " + db_filename_ + "\n"; 
     83      message += "  New database file:      " + db_filename + "\n"; 
     84      logger.warn(message); 
     85      // consider closing it automatically? 
     86      } 
     87 
     88 
    7789      try { 
    7890      // create or open a record manager 
     
    91103          if (must_exist) { 
    92104          recman_.close(); 
     105          recman_ = null; 
     106          db_filename_ = null; 
     107 
    93108          System.err.println("Database table '" + TNAME +"' does not exist."); 
    94109          throw new IOException(); 
     
    106121      } 
    107122 
     123      db_filename_ = db_filename; 
    108124 
    109125      return true; 
     
    114130  public void closeDatabase() { 
    115131      try { 
    116       recman_.close(); 
     132      if (recman_ != null) { 
     133          recman_.close(); 
     134          recman_ = null; 
     135          db_filename_ = null; 
     136      } 
    117137      } 
    118138      catch (IOException e) {      
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/SimpleCollectionDatabase.java

    r22319 r22973  
    3333   
    3434  public SimpleCollectionDatabase(String db_type) { 
    35     if (db_type.equalsIgnoreCase("gdbm")) { 
    36       this.coll_db = new GDBMWrapper(); 
    37     } 
    38     else if (db_type.equalsIgnoreCase("jdbm")) { 
    39       this.coll_db = new JDBMWrapper(); 
    40     } 
    41     else { 
    42       logger.error("Couldn't create SimpleCollectionDatabase of type "+db_type); 
    43     }    
    44   } 
    45    
     35 
     36      // Access databaseWrapper through reflection (forName) so code 
     37      // can be more dynamic as to the database backends that are 
     38      // supported for this installation of Greenstone 
     39 
     40      String dbwrap_name = db_type.toUpperCase() + "Wrapper"; 
     41      Class dbwrap_class = null; 
     42 
     43      try { 
     44      String full_dbwrap_name = "org.greenstone.gsdl3.util."+dbwrap_name; 
     45      dbwrap_class = Class.forName(full_dbwrap_name); 
     46      }  
     47      catch(ClassNotFoundException e) { 
     48      try { 
     49          //try the dbwrap_name alone in case the package name is 
     50          //already specified 
     51          dbwrap_class = Class.forName(dbwrap_name); 
     52      }  
     53      catch(ClassNotFoundException ae) { 
     54          logger.error("Couldn't create SimpleCollectionDatabase of type "+db_type); 
     55          logger.info(ae.getMessage()); 
     56      } 
     57      } 
     58 
     59      try { 
     60      this.coll_db = (FlatDatabaseWrapper)dbwrap_class.newInstance(); 
     61      } 
     62      catch(Exception e) { 
     63          logger.error("Failed to call the constructor "+dbwrap_name+"()"); 
     64      } 
     65 
     66 
     67  } 
     68   
     69  public boolean databaseOK() { 
     70      // Previously failed to open database 
     71      // Most likely cause is that this installation of Greenstone3 has not  
     72      // been compiled with support for this database type 
     73      return coll_db != null; 
     74  } 
     75 
    4676  /** open the database filename, with mode mode - uses the FlatDatabaseWrapper modes */ 
    4777  public boolean openDatabase(String filename, int mode){ 
     
    5989      //   logger.warn("All the entries of the db are:"); 
    6090      //   this.coll_db.displayAllEntries(); 
     91 
     92       
     93    if (this.coll_db==null) { 
     94    // Most likely cause is that this installation of Greenstone3 has not  
     95    // been compiled with support for this database type 
     96    return null; 
     97    } 
    6198 
    6299    String key_info = this.coll_db.getValue(main_key);