-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSzCoreConfig.java
235 lines (198 loc) · 7.26 KB
/
SzCoreConfig.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
package com.senzing.g2.engine;
import static com.senzing.g2.engine.SzException.*;
import static com.senzing.g2.engine.Utilities.*;
/**
* The package-protected implementation of {@link SzConfig} that works with the {@link
* SzCoreEnvironment} class.
*/
class SzCoreConfig implements SzConfig {
/** The {@link SzCoreEnvironment} that constructed this instance. */
private SzCoreEnvironment env = null;
/** The underlying {@link G2ConfigJNI} instance. */
G2ConfigJNI nativeApi = null;
/**
* Constructs with the specified {@link SzCoreEnvironment}.
*
* @param environment The {@link SzCoreEnvironment} with which to construct.
* @throws IllegalStateException If the underlying {@link SzCoreEnvironment} instance has already
* been destroyed.
* @throws SzException If a Senzing failure occurs during initialization.
*/
SzCoreConfig(SzCoreEnvironment environment) throws IllegalStateException, SzException {
this.env = environment;
this.env.execute(
() -> {
this.nativeApi = new G2ConfigJNI();
int returnCode =
this.nativeApi.init(
this.env.getInstanceName(), this.env.getSettings(), this.env.isVerboseLogging());
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig()",
paramsOf(
"instanceName", this.env.getInstanceName(),
"settings", redact(this.env.getSettings()),
"verboseLogging", this.env.isVerboseLogging()));
}
return null;
});
}
/** The package-protected function to destroy the Senzing Config SDK. */
void destroy() {
synchronized (this) {
if (this.nativeApi == null) return;
this.nativeApi.destroy();
this.nativeApi = null;
}
}
/**
* Checks if this instance has been destroyed by the associated {@link SzEnvironment}.
*
* @return <code>true</code> if this instance has been destroyed, otherwise <code>false</code>.
*/
protected boolean isDestroyed() {
synchronized (this) {
return (this.nativeApi == null);
}
}
@Override
public long createConfig() throws SzException {
return this.env.execute(
() -> {
// create the result object
Result<Long> result = new Result<>();
// call the underlying C function
int returnCode = this.nativeApi.create(result);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(returnCode, this.nativeApi, "SzCoreConfig.createConfig()");
}
// return the config handle
return result.getValue();
});
}
@Override
public long importConfig(String configDefinition) throws SzException {
return this.env.execute(
() -> {
// create the result object
Result<Long> result = new Result<>();
// call the underlying C function
int returnCode = this.nativeApi.load(configDefinition, result);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.importConfig(String)",
paramsOf("configDefintion", configDefinition));
}
// return the config handle
return result.getValue();
});
}
@Override
public String exportConfig(long configHandle) throws SzException {
return this.env.execute(
() -> {
// create the response buffer
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.save(configHandle, sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.exportConfig(long)",
paramsOf("configHandle", hexFormat(configHandle)));
}
// return the contents of the buffer
return sb.toString();
});
}
@Override
public void closeConfig(long configHandle) throws SzException {
this.env.execute(
() -> {
// call the underlying C function
int returnCode = this.nativeApi.close(configHandle);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.closeConfig(long)",
paramsOf("configHandle", hexFormat(configHandle)));
}
// return null
return null;
});
}
@Override
public String getDataSources(long configHandle) throws SzException {
return this.env.execute(
() -> {
// create the response buffer
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.listDataSources(configHandle, sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.getDataSources(long)",
paramsOf("configHandle", hexFormat(configHandle)));
}
// return the contents of the buffer
return sb.toString();
});
}
@Override
public void addDataSource(long configHandle, String dataSourceCode) throws SzException {
this.env.execute(
() -> {
// format the JSON for the JNI call
String inputJson = "{\"DSRC_CODE\":" + jsonEscape(dataSourceCode) + "}";
// create a StringBuffer for calling the JNI call
StringBuffer sb = new StringBuffer();
// call the underlying C function
int returnCode = this.nativeApi.addDataSource(configHandle, inputJson, sb);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.addDataSource(long,String)",
paramsOf(
"configHandle", hexFormat(configHandle), "dataSourceCOde", dataSourceCode));
}
// return null
return null;
});
}
@Override
public void deleteDataSource(long configHandle, String dataSourceCode) throws SzException {
this.env.execute(
() -> {
// format the JSON for the JNI call
String inputJson = "{\"DSRC_CODE\":" + jsonEscape(dataSourceCode) + "}";
// call the underlying C function
int returnCode = this.nativeApi.deleteDataSource(configHandle, inputJson);
// handle any error code if there is one
if (returnCode != 0) {
this.env.handleReturnCode(
returnCode,
this.nativeApi,
"SzCoreConfig.deleteDataSource(long,String)",
paramsOf(
"configHandle", hexFormat(configHandle), "dataSourceCode", dataSourceCode));
}
// return null
return null;
});
}
}