/
ClientSymmetricEngine.java
578 lines (513 loc) · 23.6 KB
/
ClientSymmetricEngine.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/**
* 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;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.platform.JdbcDatabasePlatformFactory;
import org.jumpmind.db.sql.JdbcSqlTemplate;
import org.jumpmind.db.sql.SqlTemplateSettings;
import org.jumpmind.db.util.BasicDataSourceFactory;
import org.jumpmind.exception.IoException;
import org.jumpmind.properties.TypedProperties;
import org.jumpmind.security.SecurityServiceFactory;
import org.jumpmind.security.SecurityServiceFactory.SecurityServiceType;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.common.SystemConstants;
import org.jumpmind.symmetric.common.TableConstants;
import org.jumpmind.symmetric.db.ISymmetricDialect;
import org.jumpmind.symmetric.db.JdbcSymmetricDialectFactory;
import org.jumpmind.symmetric.ext.ExtensionPointManager;
import org.jumpmind.symmetric.ext.IExtensionPointManager;
import org.jumpmind.symmetric.io.data.DbExport;
import org.jumpmind.symmetric.io.data.DbExport.Format;
import org.jumpmind.symmetric.io.stage.IStagingManager;
import org.jumpmind.symmetric.io.stage.StagingManager;
import org.jumpmind.symmetric.job.IJobManager;
import org.jumpmind.symmetric.job.JobManager;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.jumpmind.util.AppUtils;
import org.jumpmind.util.JarBuilder;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.jndi.JndiObjectFactoryBean;
/**
* Represents the client portion of a SymmetricDS engine. This class can be used
* to embed SymmetricDS into another application.
*/
public class ClientSymmetricEngine extends AbstractSymmetricEngine {
public static final String DEPLOYMENT_TYPE_CLIENT = "client";
protected File propertiesFile;
protected Properties properties;
protected DataSource dataSource;
protected ApplicationContext springContext;
/**
* @param dataSource
* If not null, SymmetricDS will use this provided datasource
* instead of creating it's own.
* @param springContext
* If not null, SymmetricDS will use this provided Spring context
* instead of creating it's own.
* @param properties
* Properties to use for configuration.
* @param registerEngine
* Whether to store a reference to this engine in a local static
* map.
*/
public ClientSymmetricEngine(DataSource dataSource, ApplicationContext springContext,
Properties properties, boolean registerEngine) {
super(registerEngine);
setDeploymentType(DEPLOYMENT_TYPE_CLIENT);
this.dataSource = dataSource;
this.springContext = springContext;
this.properties = properties;
this.init();
}
public ClientSymmetricEngine(DataSource dataSource, Properties properties,
boolean registerEngine) {
super(registerEngine);
setDeploymentType(DEPLOYMENT_TYPE_CLIENT);
this.dataSource = dataSource;
this.properties = properties;
this.init();
}
public ClientSymmetricEngine(File propertiesFile, boolean registerEngine) {
super(registerEngine);
setDeploymentType(DEPLOYMENT_TYPE_CLIENT);
this.propertiesFile = propertiesFile;
this.init();
}
public ClientSymmetricEngine(File propertiesFile) {
this(propertiesFile, true);
}
public ClientSymmetricEngine(Properties properties, boolean registerEngine) {
super(registerEngine);
setDeploymentType(DEPLOYMENT_TYPE_CLIENT);
this.properties = properties;
this.init();
}
public ClientSymmetricEngine(Properties properties) {
this(properties, true);
}
public ClientSymmetricEngine() {
this((Properties) null, true);
}
public ClientSymmetricEngine(boolean registerEngine) {
this((Properties) null, registerEngine);
}
@Override
protected SecurityServiceType getSecurityServiceType() {
return SecurityServiceType.CLIENT;
}
@Override
protected void init() {
try {
super.init();
this.dataSource = platform.getDataSource();
if (springContext == null) {
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
configurer.setProperties(parameterService.getAllParameters());
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext();
ctx.addBeanFactoryPostProcessor(configurer);
if (registerEngine) {
ctx.setConfigLocations(new String[] { "classpath:/symmetric-ext-points.xml",
"classpath:/symmetric-jmx.xml" });
} else {
ctx.setConfigLocations(new String[] { "classpath:/symmetric-ext-points.xml" });
}
ctx.refresh();
this.springContext = ctx;
}
this.extensionPointManger = createExtensionPointManager(springContext);
this.extensionPointManger.register();
} catch (RuntimeException ex) {
destroy();
throw ex;
}
}
@Override
public synchronized void stop() {
if (this.springContext instanceof AbstractApplicationContext) {
AbstractApplicationContext ctx = (AbstractApplicationContext) this.springContext;
try {
if (ctx.isActive()) {
ctx.stop();
}
} catch (Exception ex) {
} finally {
this.springContext = null;
}
}
super.stop();
}
public static BasicDataSource createBasicDataSource(File propsFile) {
TypedProperties properties = createTypedPropertiesFactory(propsFile, null).reload();
return BasicDataSourceFactory.create(properties, SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties));
}
@Override
protected ISymmetricDialect createSymmetricDialect() {
return new JdbcSymmetricDialectFactory(parameterService, platform).create();
}
@Override
protected IDatabasePlatform createDatabasePlatform(TypedProperties properties) {
IDatabasePlatform platform = createDatabasePlatform(properties, dataSource, Boolean.parseBoolean(System.getProperty(SystemConstants.SYSPROP_WAIT_FOR_DATABASE, "true")));
return platform;
}
public static IDatabasePlatform createDatabasePlatform(TypedProperties properties,
DataSource dataSource, boolean waitOnAvailableDatabase) {
if (dataSource == null) {
String jndiName = properties.getProperty(ParameterConstants.DB_JNDI_NAME);
if (StringUtils.isBlank(jndiName)) {
dataSource = BasicDataSourceFactory.create(properties, SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties));
} else {
try {
log.info("Looking up datasource in jndi. The jndi name is {}", jndiName);
JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
jndiFactory.setJndiName(jndiName);
jndiFactory.afterPropertiesSet();
dataSource = (DataSource)jndiFactory.getObject();
if (dataSource == null) {
throw new SymmetricException("Could not locate the configured datasource in jndi. The jndi name is %s", jndiName);
}
} catch (IllegalArgumentException e) {
throw new SymmetricException("Could not locate the configured datasource in jndi. The jndi name is %s", e, jndiName);
} catch (NamingException e) {
throw new SymmetricException("Could not locate the configured datasource in jndi. The jndi name is %s", e, jndiName);
}
}
}
if (waitOnAvailableDatabase) {
waitForAvailableDatabase(dataSource);
}
boolean delimitedIdentifierMode = properties.is(
ParameterConstants.DB_DELIMITED_IDENTIFIER_MODE, true);
return JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource,
createSqlTemplateSettings(properties), delimitedIdentifierMode);
}
protected static SqlTemplateSettings createSqlTemplateSettings(TypedProperties properties) {
SqlTemplateSettings settings = new SqlTemplateSettings();
settings.setFetchSize(properties.getInt(ParameterConstants.DB_FETCH_SIZE, 1000));
settings.setQueryTimeout(properties.getInt(ParameterConstants.DB_QUERY_TIMEOUT_SECS, 300));
settings.setBatchSize(properties.getInt(ParameterConstants.JDBC_EXECUTE_BATCH_SIZE, 100));
return settings;
}
protected IExtensionPointManager createExtensionPointManager(ApplicationContext springContext) {
return new ExtensionPointManager(this, springContext);
}
@Override
protected IJobManager createJobManager() {
return new JobManager(this);
}
@Override
protected IStagingManager createStagingManager() {
long memoryThresholdInBytes = parameterService
.getLong(ParameterConstants.STREAM_TO_FILE_THRESHOLD);
long timeToLiveInMs = parameterService
.getLong(ParameterConstants.STREAM_TO_FILE_TIME_TO_LIVE_MS);
String directory = parameterService.getTempDirectory();
return new StagingManager(memoryThresholdInBytes, timeToLiveInMs, directory);
}
protected static void waitForAvailableDatabase(DataSource dataSource) {
boolean success = false;
while (!success) {
Connection c = null;
try {
synchronized (ClientSymmetricEngine.class) {
c = dataSource.getConnection();
success = true;
}
} catch (Exception ex) {
log.error(
"Could not get a connection to the database: {}. Waiting for 10 seconds before trying to connect to the database again.",
ex.getMessage());
AppUtils.sleep(10000);
} finally {
JdbcSqlTemplate.close(c);
}
}
}
@Override
protected ITypedPropertiesFactory createTypedPropertiesFactory() {
return createTypedPropertiesFactory(propertiesFile, properties);
}
protected static ITypedPropertiesFactory createTypedPropertiesFactory(
final File propertiesFile, final Properties properties) {
return new ITypedPropertiesFactory() {
public TypedProperties reload() {
PropertiesFactoryBean factoryBean = new PropertiesFactoryBean();
factoryBean.setIgnoreResourceNotFound(true);
factoryBean.setLocalOverride(true);
factoryBean.setSingleton(false);
factoryBean.setProperties(properties);
factoryBean.setLocations(buildLocations(propertiesFile));
try {
return new TypedProperties(factoryBean.getObject());
} catch (IOException e) {
throw new IoException(e);
}
}
protected Resource[] buildLocations(File propertiesFile) {
/*
* System properties always override the properties found in
* these files. System properties are merged in the parameter
* service.
*/
List<Resource> resources = new ArrayList<Resource>();
resources.add(new ClassPathResource("/symmetric-default.properties"));
resources.add(new ClassPathResource("/symmetric-console-default.properties"));
resources.add(new FileSystemResource("../conf/symmetric.properties"));
resources.add(new ClassPathResource("/symmetric.properties"));
resources.add(new ClassPathResource("/symmetric-console-default.properties"));
resources.add(new ClassPathResource("/symmetric-override.properties"));
if (propertiesFile != null && propertiesFile.exists()) {
resources.add(new FileSystemResource(propertiesFile.getAbsolutePath()));
}
return resources.toArray(new Resource[resources.size()]);
}
};
}
protected static class PropertiesFactoryBean extends
org.springframework.beans.factory.config.PropertiesFactoryBean {
private static Properties localProperties;
public PropertiesFactoryBean() {
this.setLocalOverride(true);
if (localProperties != null) {
this.setProperties(localProperties);
}
}
public static void setLocalProperties(Properties localProperties) {
PropertiesFactoryBean.localProperties = localProperties;
}
public static void clearLocalProperties() {
PropertiesFactoryBean.localProperties = null;
}
}
@Override
public synchronized void destroy() {
super.destroy();
if (dataSource != null && dataSource instanceof BasicDataSource) {
try {
((BasicDataSource)dataSource).close();
} catch (SQLException e) {
}
}
}
public List<File> listSnapshots() {
File snapshotsDir = getSnapshotDirectory();
List<File> files = new ArrayList<File>(FileUtils.listFiles(snapshotsDir, new String[] {"zip"}, false));
Collections.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
return -o1.compareTo(o2);
}
});
return files;
}
protected File getSnapshotDirectory() {
File snapshotsDir = new File(parameterService.getTempDirectory(), "snapshots");
snapshotsDir.mkdirs();
return snapshotsDir;
}
public File snapshot() {
String dirName = getEngineName().replaceAll(" ", "-") + "-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
File snapshotsDir = getSnapshotDirectory();
File logfile = new File(parameterService.getString(ParameterConstants.SERVER_LOG_FILE));
File tmpDir = new File(parameterService.getTempDirectory(), dirName);
tmpDir.mkdirs();
if (logfile.exists() && logfile.isFile()) {
try {
FileUtils.copyFileToDirectory(logfile, tmpDir);
} catch (IOException e) {
log.warn("Failed to copy the log file to the snapshot directory", e);
}
} else {
log.warn("Could not find {} to copy to the snapshot directory",
logfile.getAbsolutePath());
}
List<TriggerHistory> triggerHistories = triggerRouterService.getActiveTriggerHistories();
List<Table> tables = new ArrayList<Table>();
for (TriggerHistory triggerHistory : triggerHistories) {
Table table = platform.getTableFromCache(triggerHistory.getSourceCatalogName(),
triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(),
false);
if (table != null) {
tables.add(table);
}
}
FileWriter fwriter = null;
try {
fwriter = new FileWriter(new File(tmpDir, "config-export.csv"));
dataExtractorService.extractConfigurationStandalone(nodeService.findIdentity(),
fwriter, TableConstants.SYM_NODE, TableConstants.SYM_NODE_SECURITY,
TableConstants.SYM_NODE_IDENTITY, TableConstants.SYM_NODE_HOST,
TableConstants.SYM_NODE_CHANNEL_CTL);
} catch (IOException e) {
log.warn("Failed to export symmetric configuration", e);
} finally {
IOUtils.closeQuietly(fwriter);
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoData(true);
export.exportTables(fos, tables.toArray(new Table[tables.size()]));
} catch (IOException e) {
log.warn("Failed to export table definitions", e);
} finally {
IOUtils.closeQuietly(fos);
}
fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "runtime-data.xml"));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoCreateInfo(true);
export.exportTables(
fos,
new String[] {
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_NODE),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_NODE_SECURITY),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_NODE_HOST),
TableConstants.getTableName(getTablePrefix(),
TableConstants.SYM_TRIGGER_HIST),
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_LOCK),
TableConstants.getTableName(getTablePrefix(), TableConstants.SYM_NODE_COMMUNICATION)});
} catch (IOException e) {
log.warn("Failed to export table definitions", e);
} finally {
IOUtils.closeQuietly(fos);
}
final int THREAD_INDENT_SPACE = 50;
fwriter = null;
try {
fwriter = new FileWriter(new File(tmpDir, "threads.txt"));
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.getAllThreadIds();
for (long l : threadIds) {
ThreadInfo info = threadBean.getThreadInfo(l, 100);
if (info != null) {
String threadName = info.getThreadName();
fwriter.append(StringUtils.rightPad(threadName, THREAD_INDENT_SPACE));
StackTraceElement[] trace = info.getStackTrace();
boolean first = true;
for (StackTraceElement stackTraceElement : trace) {
if (!first) {
fwriter.append(StringUtils.rightPad("", THREAD_INDENT_SPACE));
} else {
first = false;
}
fwriter.append(stackTraceElement.getClassName());
fwriter.append(".");
fwriter.append(stackTraceElement.getMethodName());
fwriter.append("()");
int lineNumber = stackTraceElement.getLineNumber();
if (lineNumber > 0) {
fwriter.append(": ");
fwriter.append(Integer.toString(stackTraceElement.getLineNumber()));
}
fwriter.append("\n");
}
fwriter.append("\n");
}
}
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fwriter);
}
fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "parameters.properties"));
Properties effectiveParameters = parameterService.getAllParameters();
effectiveParameters.store(fos, "parameters.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}
fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "runtime-stats.properties"));
Properties runtimeProperties = new Properties();
runtimeProperties.setProperty("unrouted.data.count",
Long.toString(routerService.getUnroutedDataCount()));
runtimeProperties.setProperty("outgoing.errors.count",
Long.toString(outgoingBatchService.countOutgoingBatchesInError()));
runtimeProperties.setProperty("outgoing.tosend.count",
Long.toString(outgoingBatchService.countOutgoingBatchesUnsent()));
runtimeProperties.setProperty("incoming.errors.count",
Long.toString(incomingBatchService.countIncomingBatchesInError()));
runtimeProperties.store(fos, "runtime-stats.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}
fos = null;
try {
fos = new FileOutputStream(new File(tmpDir, "system.properties"));
System.getProperties().store(fos, "system.properties");
} catch (IOException e) {
log.warn("Failed to export thread information", e);
} finally {
IOUtils.closeQuietly(fos);
}
try {
File jarFile = new File(snapshotsDir, tmpDir.getName()
+ ".zip");
JarBuilder builder = new JarBuilder(tmpDir, jarFile, new File[] { tmpDir }, Version.version());
builder.build();
FileUtils.deleteDirectory(tmpDir);
return jarFile;
} catch (IOException e) {
throw new IoException("Failed to package snapshot files into archive", e);
}
}
}