-
Notifications
You must be signed in to change notification settings - Fork 221
/
SymmetricEngineHolder.java
533 lines (451 loc) · 20.6 KB
/
SymmetricEngineHolder.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.web;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.util.BasicDataSourcePropertyConstants;
import org.jumpmind.properties.TypedProperties;
import org.jumpmind.security.ISecurityService;
import org.jumpmind.security.SecurityConstants;
import org.jumpmind.security.SecurityServiceFactory;
import org.jumpmind.security.SecurityServiceFactory.SecurityServiceType;
import org.jumpmind.symmetric.AbstractCommandLauncher;
import org.jumpmind.symmetric.ISymmetricEngine;
import org.jumpmind.symmetric.SymmetricAdmin;
import org.jumpmind.symmetric.SymmetricException;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.NodeGroup;
import org.jumpmind.symmetric.model.NodeGroupLink;
import org.jumpmind.symmetric.model.NodeGroupLinkAction;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.IRegistrationService;
import org.jumpmind.symmetric.service.ITriggerRouterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
public class SymmetricEngineHolder {
final Logger log = LoggerFactory.getLogger(getClass());
private static Map<String, ServerSymmetricEngine> staticEngines = new HashMap<String, ServerSymmetricEngine>();
private static Set<EngineStarter> staticEnginesStarting = new HashSet<SymmetricEngineHolder.EngineStarter>();
private Map<String, ServerSymmetricEngine> engines = new HashMap<String, ServerSymmetricEngine>();
private Set<EngineStarter> enginesStarting = new HashSet<SymmetricEngineHolder.EngineStarter>();
private boolean staticEnginesMode = false;
private boolean multiServerMode = false;
private boolean autoStart = true;
private boolean autoCreate = true;
private ApplicationContext springContext;
private String singleServerPropertiesFile;
private static Date createTime = new Date();
private int engineCount;
private String deploymentType = "server";
private boolean holderHasBeenStarted = false;
public Map<String, ServerSymmetricEngine> getEngines() {
return engines;
}
public void setDeploymentType(String deploymentType) {
this.deploymentType = deploymentType;
}
public void setMultiServerMode(boolean multiServerMode) {
this.multiServerMode = multiServerMode;
}
public boolean isMultiServerMode() {
return multiServerMode;
}
public void setAutoCreate(boolean autoCreate) {
this.autoCreate = autoCreate;
}
public boolean isAutoCreate() {
return autoCreate;
}
public void setStaticEnginesMode(boolean staticEnginesMode) {
this.staticEnginesMode = staticEnginesMode;
}
public boolean isStaticEnginesMode() {
return staticEnginesMode;
}
public void setSingleServerPropertiesFile(String singleServerPropertiesFile) {
this.singleServerPropertiesFile = singleServerPropertiesFile;
}
public String getSingleServerPropertiesFile() {
return singleServerPropertiesFile;
}
public boolean areEnginesConfigured() {
return enginesStarting.size() > 0 || engines.size() > 0;
}
public int getNumerOfEnginesStarting() {
return enginesStarting.size();
}
public void setAutoStart(boolean autoStart) {
this.autoStart = autoStart;
}
public boolean isAutoStart() {
return autoStart;
}
public synchronized void stop() {
Set<String> engineNames = engines.keySet();
for (String engineName : engineNames) {
engines.get(engineName).destroy();
}
engines.clear();
}
public void start() {
try {
if (staticEnginesMode) {
log.info("In static engine mode");
engines = staticEngines;
enginesStarting = staticEnginesStarting;
}
if (autoCreate) {
if (isMultiServerMode()) {
File enginesDir = new File(AbstractCommandLauncher.getEnginesDir());
File[] files = null;
if (enginesDir != null) {
files = enginesDir.listFiles();
}
if (files == null) {
String firstAttempt = enginesDir.getAbsolutePath();
enginesDir = new File(".");
log.warn(
"Unable to retrieve engine properties files from {}. Trying current working directory {}",
firstAttempt, enginesDir.getAbsolutePath());
if (enginesDir != null) {
files = enginesDir.listFiles();
}
}
validateEngineFiles(files);
if (files != null) {
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.getName().endsWith(".properties")) {
engineCount++;
enginesStarting.add(new EngineStarter(file.getAbsolutePath()));
}
}
} else {
log.error("Unable to retrieve engine properties files from default location or from current working directory. No engines to start.");
}
} else {
engineCount++;
enginesStarting.add(new EngineStarter(singleServerPropertiesFile));
}
for (EngineStarter starter : enginesStarting) {
starter.start();
}
}
} finally {
holderHasBeenStarted = true;
}
}
public void uninstallEngine(ISymmetricEngine engine) {
Node node = engine.getNodeService().getCachedIdentity();
String engineName = engine.getEngineName();
File file = new SymmetricAdmin("uninstall", "", "")
.findPropertiesFileForEngineWithName(engineName);
engine.uninstall();
engine.destroy();
if (file != null) {
file.delete();
}
getEngines().remove(engineName);
for (ISymmetricEngine existingEngine : this.getEngines().values()) {
existingEngine.removeAndCleanupNode(node.getNodeId());
}
}
public void setSpringContext(ApplicationContext applicationContext) {
this.springContext = applicationContext;
}
public int getEngineCount() {
return engineCount;
}
protected ISymmetricEngine create(String propertiesFile) {
ServerSymmetricEngine engine = null;
try {
engine = new ServerSymmetricEngine(propertiesFile != null ? new File(propertiesFile)
: null, springContext, this);
engine.setDeploymentType(deploymentType);
synchronized (this) {
if (!engines.containsKey(engine.getEngineName())) {
engines.put(engine.getEngineName(), engine);
} else {
log.error(
"An engine with the name of {} was not started because an engine of the same name has already been started. Please set the engine.name property in the properties file to a unique name.",
engine.getEngineName());
}
}
return engine;
} catch (Exception e) {
log.error("", e);
return null;
}
}
public ISymmetricEngine install(Properties passedInProperties) throws Exception {
TypedProperties properties = new TypedProperties(passedInProperties);
String password = properties.getProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD);
if (StringUtils.isNotBlank(password) && !password.startsWith(SecurityConstants.PREFIX_ENC)) {
try {
ISecurityService service = SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties);
properties.setProperty(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD,
SecurityConstants.PREFIX_ENC + service.encrypt(password));
} catch (Exception ex) {
log.warn("Could not encrypt password", ex);
}
}
String engineName = validateRequiredProperties(properties);
passedInProperties.setProperty(ParameterConstants.ENGINE_NAME, engineName);
if (engines.get(engineName) != null) {
try {
engines.get(engineName).stop();
} catch (Exception e) {
log.error("", e);
}
engines.remove(engineName);
}
File enginesDir = new File(AbstractCommandLauncher.getEnginesDir());
File symmetricProperties = new File(enginesDir, engineName + ".properties");
FileOutputStream fileOs = null;
try {
fileOs = new FileOutputStream(symmetricProperties);
properties.store(fileOs, "Updated by SymmetricDS Pro");
} catch (IOException ex) {
throw new RuntimeException("Failed to write symmetric.properties to engine directory",
ex);
} finally {
IOUtils.closeQuietly(fileOs);
}
ISymmetricEngine engine = null;
try {
String registrationUrl = properties.getProperty(ParameterConstants.REGISTRATION_URL);
if (StringUtils.isNotBlank(registrationUrl)) {
Collection<ServerSymmetricEngine> all = getEngines().values();
for (ISymmetricEngine currentEngine : all) {
if (currentEngine.getParameterService().getSyncUrl()
.equals(registrationUrl)) {
String serverNodeGroupId = currentEngine.getParameterService()
.getNodeGroupId();
String clientNodeGroupId = properties
.getProperty(ParameterConstants.NODE_GROUP_ID);
String externalId = properties.getProperty(ParameterConstants.EXTERNAL_ID);
IConfigurationService configurationService = currentEngine
.getConfigurationService();
ITriggerRouterService triggerRouterService = currentEngine.
getTriggerRouterService();
List<NodeGroup> groups = configurationService.getNodeGroups();
boolean foundGroup = false;
for (NodeGroup nodeGroup : groups) {
if (nodeGroup.getNodeGroupId().equals(clientNodeGroupId)) {
foundGroup = true;
}
}
if (!foundGroup) {
configurationService.saveNodeGroup(new NodeGroup(clientNodeGroupId));
}
boolean foundLink = false;
List<NodeGroupLink> links = configurationService
.getNodeGroupLinksFor(serverNodeGroupId, false);
for (NodeGroupLink nodeGroupLink : links) {
if (nodeGroupLink.getTargetNodeGroupId().equals(clientNodeGroupId)) {
foundLink = true;
}
}
if (!foundLink) {
configurationService.saveNodeGroupLink(new NodeGroupLink(
serverNodeGroupId, clientNodeGroupId, NodeGroupLinkAction.W));
triggerRouterService.syncTriggers();
}
IRegistrationService registrationService = currentEngine
.getRegistrationService();
if (!registrationService.isAutoRegistration()
&& !registrationService.isRegistrationOpen(clientNodeGroupId,
externalId)) {
Node node = new Node(properties);
registrationService.openRegistration(node);
}
}
}
}
engine = create(symmetricProperties.getAbsolutePath());
if (engine != null) {
engineCount++;
engine.start();
} else {
FileUtils.deleteQuietly(symmetricProperties);
log.warn("The engine could not be created. It will not be started");
}
return engine;
} catch (RuntimeException ex) {
if (engine != null) {
engine.destroy();
}
FileUtils.deleteQuietly(symmetricProperties);
throw ex;
}
}
public boolean areEnginesStarting() {
return !holderHasBeenStarted || enginesStarting.size() > 0;
}
public boolean hasAnyEngineInitialized() {
if (enginesStarting.size() < engines.size()) {
return true;
}
for (EngineStarter starter : enginesStarting) {
ISymmetricEngine engine = starter.getEngine();
if (engine != null && engine.isInitialized()) {
return true;
}
}
return false;
}
protected void validateEngineFiles(File[] files) {
Map<String, String> dbToPropertyFiles = new LinkedHashMap<String, String>();
for (File file : files) {
if (file.getName().endsWith(".properties")) {
// external.id
Properties properties = new Properties();
InputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file.getAbsolutePath());
properties.load(fileInputStream);
String dbUrl = properties.getProperty(BasicDataSourcePropertyConstants.DB_POOL_URL, "");
checkDuplicate(dbUrl, BasicDataSourcePropertyConstants.DB_POOL_URL, dbToPropertyFiles, file);
} catch (Exception ex) {
if (ex instanceof SymmetricException) {
log.error("**** FATAL **** error " + ex.toString()); // Jetty logs the stack at WARN level.
throw (SymmetricException)ex;
} else {
log.warn("Failed to validate engine properties file " + file, ex);
}
} finally {
if (fileInputStream != null) {
IOUtils.closeQuietly(fileInputStream);
}
}
}
}
}
protected void checkDuplicate(String value, String key, Map<String, String> values, File propertiesFile) {
if (values.containsKey(value)) {
throw new SymmetricException(String.format("Invalid configuration detected. 2 properties files reference "
+ "the same %s: '%s'. Maybe an engines file was copied and needs to be moved. See: %s and %s.",
key, value, values.get(value), propertiesFile.getAbsolutePath()));
} else {
values.put(value, propertiesFile.getAbsolutePath());
}
}
public String getEngineName(Properties properties) {
String engineName = properties.getProperty(ParameterConstants.ENGINE_NAME);
if (StringUtils.isBlank(engineName)) {
String externalId = properties.getProperty(ParameterConstants.EXTERNAL_ID, "");
String groupId = properties.getProperty(ParameterConstants.NODE_GROUP_ID, "");
if (externalId.equals(groupId)) {
engineName = groupId;
} else {
engineName = groupId + "-" + externalId;
}
engineName = properties.getProperty(ParameterConstants.ENGINE_NAME, engineName);
String engineExt = "";
int engineNumber = 0;
while (new File(AbstractCommandLauncher.getEnginesDir(), engineName + engineExt
+ ".properties").exists()) {
engineNumber++;
engineExt = "-" + engineNumber;
}
engineName = engineName + engineExt;
}
return engineName;
}
public String validateRequiredProperties(Properties properties) {
String externalId = properties.getProperty(ParameterConstants.EXTERNAL_ID);
if (StringUtils.isBlank(externalId)) {
throw new IllegalStateException("Missing property " + ParameterConstants.EXTERNAL_ID);
}
String groupId = properties.getProperty(ParameterConstants.NODE_GROUP_ID);
if (StringUtils.isBlank(groupId)) {
throw new IllegalStateException("Missing property " + ParameterConstants.NODE_GROUP_ID);
}
String engineName = getEngineName(properties);
properties.setProperty(ParameterConstants.ENGINE_NAME, engineName);
if (StringUtils.isBlank(properties.getProperty(ParameterConstants.SYNC_URL))) {
throw new IllegalStateException("Missing property " + ParameterConstants.SYNC_URL);
}
if (StringUtils.isBlank(properties
.getProperty(BasicDataSourcePropertyConstants.DB_POOL_DRIVER))) {
throw new IllegalStateException("Missing property "
+ BasicDataSourcePropertyConstants.DB_POOL_DRIVER);
}
if (StringUtils.isBlank(properties
.getProperty(BasicDataSourcePropertyConstants.DB_POOL_URL))) {
throw new IllegalStateException("Missing property "
+ BasicDataSourcePropertyConstants.DB_POOL_URL);
}
if (!properties.containsKey(BasicDataSourcePropertyConstants.DB_POOL_USER)) {
throw new IllegalStateException("Missing property "
+ BasicDataSourcePropertyConstants.DB_POOL_USER);
}
if (!properties.containsKey(BasicDataSourcePropertyConstants.DB_POOL_PASSWORD)) {
throw new IllegalStateException("Missing property "
+ BasicDataSourcePropertyConstants.DB_POOL_PASSWORD);
}
if (!properties.containsKey(ParameterConstants.REGISTRATION_URL)) {
properties.setProperty(ParameterConstants.REGISTRATION_URL, "");
}
return engineName;
}
public static Date getCreateTime() {
return createTime;
}
static int threadNumber = 0;
class EngineStarter extends Thread {
String propertiesFile;
ISymmetricEngine engine;
public EngineStarter(String propertiesFile) {
super("symmetric-engine-startup-"+threadNumber++);
this.propertiesFile = propertiesFile;
}
@Override
public void run() {
engine = create(propertiesFile);
if (engine != null && autoStart &&
engine.getParameterService().is(ParameterConstants.AUTO_START_ENGINE)) {
engine.start();
}
enginesStarting.remove(this);
}
public ISymmetricEngine getEngine() {
return engine;
}
}
}