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 private LogicalDatabase() {}
65
66
67 /** Properties, named for this class. */
68 private static Properties databaseDefs = null;
69
70 private static final int MAX_TRANSACTIONS_DEFAULT = 8;
71
72 private static synchronized Properties databaseDefs() {
73 if (databaseDefs == null)
74 try {
75 databaseDefs =
76 PropertiesUtils.fromResource(LogicalDatabase.class,
77 getPropertiesName());
78 } catch (IOException e) {
79 throw new MelatiBugMelatiException("Cannot open database properties file " + getPropertiesName(), e);
80 }
81 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 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 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 Database db = PoemDatabaseFactory.getDatabase(name);
116 if (db == null) {
117 Properties defs = databaseDefs();
118 String pref = LogicalDatabase.class.getName() + "." + name + ".";
119 String url = null;
120 String user = null;
121 String pass = null;
122 String clazz = null;
123 String dbmsClass = null;
124 String addConstraints = null;
125 String logSQL = null;
126 String logCommits = null;
127 int maxTrans;
128 try {
129 url = PropertiesUtils.getOrDie(defs, pref + "url");
130 user = PropertiesUtils.getOrDie(defs, pref + "user");
131 pass = PropertiesUtils.getOrDie(defs, pref + "pass");
132 clazz = PropertiesUtils.getOrDie(defs, pref + "class");
133 dbmsClass = PropertiesUtils.getOrDie(defs, pref + "dbmsclass");
134 addConstraints = PropertiesUtils.getOrDefault(defs, pref + "addconstraints", "false");
135 logSQL = PropertiesUtils.getOrDefault(defs, pref + "logsql", "false");
136 logCommits = PropertiesUtils.getOrDefault(defs, pref + "logcommits", "false");
137 maxTrans = PropertiesUtils.
138 getOrDefault_int(defs, pref + "maxtransactions", MAX_TRANSACTIONS_DEFAULT);
139 } catch (PropertyException e) {
140 throw new DatabaseInitException(getPropertiesName(),name, e);
141 }
142 db = PoemDatabaseFactory.getDatabase(name, url, user, pass,
143 clazz, dbmsClass,
144 new Boolean(addConstraints).booleanValue(),
145 new Boolean(logSQL).booleanValue(),
146 new Boolean(logCommits).booleanValue(),
147 maxTrans);
148 }
149 return db;
150 }
151 /**
152 * Set the databaseDefs.
153 */
154 public static void setDatabaseDefs(Properties databaseDefsIn) {
155 databaseDefs = databaseDefsIn;
156 }
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 return "org.melati.LogicalDatabase.properties";
166 }
167 }
168
169
170
171
172
173
174