Coverage Report - org.melati.LogicalDatabase
 
Classes in this File Line Coverage Branch Coverage Complexity
LogicalDatabase
93%
42/45
100%
4/4
1.857
 
 1  
 /*
 2  
  * $Source$
 3  
  * $Revision$
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc At paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati;
 47  
 
 48  
 import java.io.IOException;
 49  
 import java.util.Vector;
 50  
 import java.util.Properties;
 51  
 
 52  
 import org.melati.poem.Database;
 53  
 import org.melati.poem.PoemDatabaseFactory;
 54  
 import org.melati.util.DatabaseInitException;
 55  
 import org.melati.util.MelatiBugMelatiException;
 56  
 import org.melati.util.PropertiesUtils;
 57  
 import org.melati.util.PropertyException;
 58  
 
 59  
 /**
 60  
  * An object which knows how to connect to a database.
 61  
  */
 62  
 public final class LogicalDatabase {
 63  
   
 64  0
   private LogicalDatabase() {}
 65  
 
 66  
 
 67  
   /** Properties, named for this class. */
 68  1
   private static Properties databaseDefs = null;
 69  
   
 70  
   private static final int MAX_TRANSACTIONS_DEFAULT = 8;
 71  
 
 72  
   private static synchronized Properties databaseDefs() {
 73  32
     if (databaseDefs == null)
 74  
       try { 
 75  3
         databaseDefs =
 76  3
             PropertiesUtils.fromResource(LogicalDatabase.class,
 77  3
                 getPropertiesName());
 78  0
       } catch (IOException e) {
 79  0
         throw new MelatiBugMelatiException("Cannot open database properties file " + getPropertiesName(), e);
 80  3
       }        
 81  32
     return databaseDefs;
 82  
   }
 83  
 
 84  
   /** 
 85  
    * Retrieve the databases which have completed initialisation.
 86  
    *
 87  
    * @return a <code>Vector</code> of the initialised databases
 88  
    */
 89  
   public static Vector<Database> initialisedDatabases() {
 90  2
     return PoemDatabaseFactory.initialisedDatabases();
 91  
   }
 92  
 
 93  
   
 94  
   /** 
 95  
    * Retrieve the names of the databases which have 
 96  
    * completed initialisation.
 97  
    * Note that a database which has not been used 
 98  
    * will not have been initialised.
 99  
    *
 100  
    * @return a <code>Vector</code> of the initialised database names
 101  
    */
 102  
   public static Vector<String> getInitialisedDatabaseNames() {
 103  63
     return PoemDatabaseFactory.getInitialisedDatabaseNames();
 104  
   }
 105  
   
 106  
   /** 
 107  
    * Retrieve a database by name.
 108  
    *
 109  
    * @param name the name of the database
 110  
    * @throws DatabaseInitException if any Exception is trapped
 111  
    * @return a <code>Database</code> with the name specified
 112  
    */
 113  
   public static Database getDatabase(String name) 
 114  
       throws DatabaseInitException {
 115  618
     Database db = PoemDatabaseFactory.getDatabase(name);
 116  617
     if (db == null) {
 117  32
       Properties defs = databaseDefs();
 118  32
       String pref =  LogicalDatabase.class.getName() + "." + name + ".";
 119  32
       String url = null;
 120  32
       String user = null;
 121  32
       String pass = null;
 122  32
       String clazz = null;
 123  32
       String dbmsClass = null;
 124  32
       String addConstraints = null;
 125  32
       String logSQL  = null;
 126  32
       String logCommits = null;
 127  
       int maxTrans;
 128  
       try {
 129  32
         url = PropertiesUtils.getOrDie(defs, pref + "url");
 130  29
         user = PropertiesUtils.getOrDie(defs, pref + "user");
 131  29
         pass = PropertiesUtils.getOrDie(defs, pref + "pass");
 132  29
         clazz = PropertiesUtils.getOrDie(defs, pref + "class");
 133  29
         dbmsClass = PropertiesUtils.getOrDie(defs, pref + "dbmsclass");
 134  29
         addConstraints = PropertiesUtils.getOrDefault(defs, pref + "addconstraints", "false");
 135  29
         logSQL = PropertiesUtils.getOrDefault(defs, pref + "logsql", "false");
 136  29
         logCommits = PropertiesUtils.getOrDefault(defs, pref + "logcommits", "false");
 137  29
         maxTrans = PropertiesUtils.
 138  29
                          getOrDefault_int(defs, pref + "maxtransactions", MAX_TRANSACTIONS_DEFAULT);
 139  3
       } catch (PropertyException e) {
 140  3
         throw new DatabaseInitException(getPropertiesName(),name, e);
 141  29
       }
 142  58
       db = PoemDatabaseFactory.getDatabase(name, url, user, pass, 
 143  
              clazz, dbmsClass, 
 144  29
              new Boolean(addConstraints).booleanValue(), 
 145  29
              new Boolean(logSQL).booleanValue(), 
 146  29
              new Boolean(logCommits).booleanValue(),
 147  
              maxTrans);
 148  
     }
 149  613
     return db;
 150  
   }  
 151  
   /**
 152  
    * Set the databaseDefs.
 153  
    */
 154  
   public static void setDatabaseDefs(Properties databaseDefsIn) {
 155  4
     databaseDefs = databaseDefsIn;
 156  4
   }
 157  
 
 158  
   /**
 159  
    * The name used with <code>getResourceAsStream</code> to 
 160  
    * get the file and the properties within it. 
 161  
    * 
 162  
    * @return Returns the defaultPropertiesName.
 163  
    */
 164  
   public static String getPropertiesName() {
 165  7
     return "org.melati.LogicalDatabase.properties";
 166  
   }
 167  
 }
 168  
 
 169  
 
 170  
 
 171  
 
 172  
 
 173  
 
 174