1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
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 | |
|
61 | |
|
62 | |
public final class LogicalDatabase { |
63 | |
|
64 | 0 | private LogicalDatabase() {} |
65 | |
|
66 | |
|
67 | |
|
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 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public static Vector<Database> initialisedDatabases() { |
90 | 2 | return PoemDatabaseFactory.initialisedDatabases(); |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public static Vector<String> getInitialisedDatabaseNames() { |
103 | 63 | return PoemDatabaseFactory.getInitialisedDatabaseNames(); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
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 | |
|
153 | |
|
154 | |
public static void setDatabaseDefs(Properties databaseDefsIn) { |
155 | 4 | databaseDefs = databaseDefsIn; |
156 | 4 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public static String getPropertiesName() { |
165 | 7 | return "org.melati.LogicalDatabase.properties"; |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|