-
Notifications
You must be signed in to change notification settings - Fork 1
/
JooqClusterStorage.java
351 lines (284 loc) · 13.5 KB
/
JooqClusterStorage.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* This file is part of the CRISTAL-iSE jOOQ Cluster Storage Module.
* Copyright (c) 2001-2017 The CRISTAL Consortium. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
* http://www.fsf.org/licensing/licenses/lgpl.html
*/
package org.cristalise.storage.jooqdb;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_AUTOCOMMIT;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_DISABLE_DOMAIN_CREATE;
import static org.cristalise.storage.jooqdb.JooqHandler.JOOQ_DOMAIN_HANDLERS;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang3.StringUtils;
import org.cristalise.kernel.common.PersistencyException;
import org.cristalise.kernel.entity.C2KLocalObject;
import org.cristalise.kernel.lookup.ItemPath;
import org.cristalise.kernel.persistency.ClusterType;
import org.cristalise.kernel.persistency.TransactionalClusterStorage;
import org.cristalise.kernel.process.Gateway;
import org.cristalise.kernel.process.auth.Authenticator;
import org.cristalise.kernel.querying.Query;
import org.cristalise.kernel.utils.Logger;
import org.cristalise.storage.jooqdb.clusterStore.JooqCollectionHadler;
import org.cristalise.storage.jooqdb.clusterStore.JooqHistoryHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqItemPropertyHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqJobHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqLifecycleHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqOutcomeAttachmentHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqOutcomeHandler;
import org.cristalise.storage.jooqdb.clusterStore.JooqViewpointHandler;
import org.jooq.DSLContext;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConnectionProvider;
/**
* Implementation of the {@link TransactionalClusterStorage} based on <a>http://www.jooq.org/</a>}
*/
public class JooqClusterStorage extends TransactionalClusterStorage {
protected Boolean autoCommit = Gateway.getProperties().getBoolean(JOOQ_AUTOCOMMIT, false);
protected HashMap<ClusterType, JooqHandler> jooqHandlers = new HashMap<ClusterType, JooqHandler>();
protected List<JooqDomainHandler> domainHandlers = new ArrayList<JooqDomainHandler>();
@Override
public void open(Authenticator auth) throws PersistencyException {
initialiseHandlers();
}
/**
* Initialise internal handlers for all ClusterTypes and all the DomainHandlers-
*
* @throws PersistencyException Error during initialise ...
*/
public void initialiseHandlers() throws PersistencyException {
Logger.msg(1, "JooqClusterStorage.initialiseHandlers() - Starting with standard hadlers.");
jooqHandlers.put(ClusterType.PROPERTY, new JooqItemPropertyHandler());
jooqHandlers.put(ClusterType.OUTCOME, new JooqOutcomeHandler());
jooqHandlers.put(ClusterType.VIEWPOINT, new JooqViewpointHandler());
jooqHandlers.put(ClusterType.LIFECYCLE, new JooqLifecycleHandler());
jooqHandlers.put(ClusterType.COLLECTION, new JooqCollectionHadler());
jooqHandlers.put(ClusterType.HISTORY, new JooqHistoryHandler());
jooqHandlers.put(ClusterType.JOB, new JooqJobHandler());
jooqHandlers.put(ClusterType.ATTACHMENT, new JooqOutcomeAttachmentHandler());
DSLContext context = JooqHandler.connect();
for (JooqHandler handler: jooqHandlers.values()) handler.createTables(context);
try {
String handlers = Gateway.getProperties().getString(JOOQ_DOMAIN_HANDLERS, "");
for(String handlerClass: StringUtils.split(handlers, ",")) {
if (!handlerClass.contains(".")) handlerClass = "org.cristalise.storage."+handlerClass;
Logger.msg(1, "JooqClusterStorage.initialiseHandlers() - Instantiate domain handler:"+handlerClass);
domainHandlers.add( (JooqDomainHandler) Class.forName(handlerClass).newInstance());
}
}
catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {
Logger.error("JooqClusterStorage could not instantiate domain handler");
Logger.error(ex);
throw new PersistencyException("JooqClusterStorage could not instantiate domain handler:"+ex.getMessage());
}
if (! Gateway.getProperties().getBoolean(JOOQ_DISABLE_DOMAIN_CREATE, false)) {
for (JooqDomainHandler handler: domainHandlers) handler.createTables(context);
}
}
public void dropHandlers() throws PersistencyException {
DSLContext context = JooqHandler.connect();
for (JooqHandler handler: jooqHandlers.values()) handler.dropTables(context);
}
@Override
public void close() throws PersistencyException {
Logger.msg(1, "JooqClusterStorage.close()");
try {
JooqHandler.connect().close();
}
catch (Exception e) {
Logger.error(e);
throw new PersistencyException(e.getMessage());
}
}
@Override
public void postBoostrap() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postBoostrap(JooqHandler.connect());
}
@Override
public void postStartServer() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postStartServer(JooqHandler.connect());
}
@Override
public void postConnect() throws PersistencyException {
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.postConnect(JooqHandler.connect());
}
@Override
public void begin(Object locker) {
DSLContext context =null;
try {
context = JooqHandler.connect();
}
catch (PersistencyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logger.msg(8, "JooqClusterStorage.begin() - Nothing DONE.");
if (Logger.doLog(5)) JooqHandler.logConnectionCount("JooqClusterStorage.begin()", context);
}
@Override
public void commit(Object locker) throws PersistencyException {
DSLContext context = JooqHandler.connect();
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.commit(context, locker);
if (autoCommit) {
Logger.msg(1, "JooqClusterStorage.commit(DISABLED) - autoCommit:"+autoCommit);
return;
}
Logger.msg(1, "JooqClusterStorage.commit()");
try {
((DataSourceConnectionProvider)context.configuration().connectionProvider()).dataSource().getConnection().commit();
if (Logger.doLog(5)) JooqHandler.logConnectionCount("JooqClusterStorage.commit()", context);
}
catch (Exception e) {
Logger.error(e);
throw new PersistencyException(e.getMessage());
}
}
@Override
public void abort(Object locker) throws PersistencyException {
DSLContext context = JooqHandler.connect();
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.abort(context, locker);
if (autoCommit) {
Logger.msg(1, "JooqClusterStorage.abort(DISABLED) - autoCommit:"+autoCommit);
return;
}
Logger.msg(1, "JooqClusterStorage.abort()");
try {
((DefaultConnectionProvider)context.configuration().connectionProvider()).rollback();
}
catch (Exception e) {
Logger.error(e);
}
}
@Override
public short queryClusterSupport(String type) {
return queryClusterSupport(ClusterType.getValue(type));
}
@Override
public short queryClusterSupport(ClusterType type) {
if (type == ClusterType.PATH) {
return NONE;
}
return READWRITE;
}
@Override
public boolean checkQuerySupport(String language) {
String lang = language.trim().toUpperCase();
return "SQL".equals(lang) || ("SQL:"+JooqHandler.dialect).equals(lang);
}
@Override
public String getName() {
return "JOOQ:"+JooqHandler.dialect+" ClusterStorage";
}
@Override
public String getId() {
return "JOOQ:"+JooqHandler.dialect;
}
@Override
public String executeQuery(Query query) throws PersistencyException {
throw new PersistencyException("UnImplemented");
}
@Override
public ClusterType[] getClusters(ItemPath itemPath) throws PersistencyException {
ArrayList<ClusterType> result = new ArrayList<ClusterType>();
for (ClusterType type:jooqHandlers.keySet()) {
if (jooqHandlers.get(type).exists(JooqHandler.connect(), itemPath.getUUID())) result.add(type);
}
return result.toArray(new ClusterType[0]);
}
@Override
public String[] getClusterContents(ItemPath itemPath, String path) throws PersistencyException {
if (StringUtils.isBlank(path)) {
ArrayList<String> result = new ArrayList<String>();
for (ClusterType k: getClusters(itemPath)) { result.add(k.toString()); }
return result.toArray(new String[0]);
}
UUID uuid = itemPath.getUUID();
String[] pathArray = path.split("/");
String[] primaryKeys = Arrays.copyOfRange(pathArray, 1, pathArray.length);
ClusterType cluster = ClusterType.getValue(pathArray[0]);
JooqHandler handler = jooqHandlers.get(cluster);
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.getClusterContents() - uuid:"+uuid+" cluster:"+cluster+" primaryKeys"+Arrays.toString(primaryKeys));
return handler.getNextPrimaryKeys(JooqHandler.connect(), uuid, primaryKeys);
}
else
throw new PersistencyException("No handler found for cluster:'"+cluster+"'");
}
@Override
public C2KLocalObject get(ItemPath itemPath, String path) throws PersistencyException {
UUID uuid = itemPath.getUUID();
String[] pathArray = path.split("/");
String[] primaryKeys = Arrays.copyOfRange(pathArray, 1, pathArray.length);
ClusterType cluster = ClusterType.getValue(pathArray[0]);
JooqHandler handler = jooqHandlers.get(cluster);
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.get() - uuid:"+uuid+" cluster:"+cluster+" primaryKeys:"+Arrays.toString(primaryKeys));
C2KLocalObject obj = handler.fetch(JooqHandler.connect(), uuid, primaryKeys);
if (obj == null && Logger.doLog(8)) {
Logger.warning(("JooqClusterStorage.get() - Could NOT fetch '"+itemPath+"/"+path+"'"));
}
return obj;
}
else
throw new PersistencyException("No handler found for cluster:'"+cluster+"'");
}
@Override
public void put(ItemPath itemPath, C2KLocalObject obj) throws PersistencyException {
put(itemPath, obj, null);
}
@Override
public void put(ItemPath itemPath, C2KLocalObject obj, Object locker) throws PersistencyException {
UUID uuid = itemPath.getUUID();
ClusterType cluster = obj.getClusterType();
JooqHandler handler = jooqHandlers.get(cluster);
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.put() - uuid:"+uuid+" cluster:"+cluster+" path:"+obj.getClusterPath());
handler.put(JooqHandler.connect(), uuid, obj);
}
else {
throw new PersistencyException("Write is not supported for cluster:'"+cluster+"'");
}
// Trigger all registered handlers to update domain specific tables
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.put(JooqHandler.connect(), uuid, obj, locker);
}
@Override
public void delete(ItemPath itemPath, String path) throws PersistencyException {
delete(itemPath, path, null);
}
@Override
public void delete(ItemPath itemPath, String path, Object locker) throws PersistencyException {
UUID uuid = itemPath.getUUID();
String[] pathArray = path.split("/");
String[] primaryKeys = Arrays.copyOfRange(pathArray, 1, pathArray.length);
ClusterType cluster = ClusterType.getValue(pathArray[0]);
DSLContext context = JooqHandler.connect();
JooqHandler handler = jooqHandlers.get(cluster);
if (handler != null) {
Logger.msg(5, "JooqClusterStorage.delete() - uuid:"+uuid+" cluster:"+cluster+" primaryKeys"+Arrays.toString(primaryKeys));
handler.delete(context, uuid, primaryKeys);
}
else {
throw new PersistencyException("No handler found for cluster:'"+cluster+"'");
}
// Trigger all registered handlers to update domain specific tables
for (JooqDomainHandler domainHandler : domainHandlers) domainHandler.delete(context, uuid, locker, primaryKeys);
}
}