/
FileSyncZipDataWriter.java
290 lines (253 loc) · 12.3 KB
/
FileSyncZipDataWriter.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
/**
* 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.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Table;
import org.jumpmind.exception.IoException;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.io.data.Batch;
import org.jumpmind.symmetric.io.data.CsvData;
import org.jumpmind.symmetric.io.data.DataContext;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.io.data.IDataWriter;
import org.jumpmind.symmetric.io.stage.IStagedResource;
import org.jumpmind.symmetric.model.FileSnapshot;
import org.jumpmind.symmetric.model.FileSnapshot.LastEventType;
import org.jumpmind.symmetric.model.FileTrigger;
import org.jumpmind.symmetric.model.FileTriggerRouter;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.service.IFileSyncService;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.util.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileSyncZipDataWriter implements IDataWriter {
static final Logger log = LoggerFactory.getLogger(FileSyncZipDataWriter.class);
protected long byteCount;
protected long maxBytesToSync;
protected IFileSyncService fileSyncService;
protected IStagedResource stagedResource;
protected ZipOutputStream zos;
protected Table snapshotTable;
protected Batch batch;
protected Map<Batch, Statistics> statistics = new HashMap<Batch, Statistics>();
protected List<FileSnapshot> snapshotEvents;
protected DataContext context;
protected INodeService nodeService;
public FileSyncZipDataWriter(long maxBytesToSync, IFileSyncService fileSyncService,
INodeService nodeService, IStagedResource stagedResource) {
this.maxBytesToSync = maxBytesToSync;
this.fileSyncService = fileSyncService;
this.stagedResource = stagedResource;
this.nodeService = nodeService;
}
public void open(DataContext context) {
this.context = context;
}
@Override
public void close() {
// no-op as this is called at batch boundaries, but this writer can handle multiple batches.
}
public Map<Batch, Statistics> getStatistics() {
return statistics;
}
public void start(Batch batch) {
this.batch = batch;
this.statistics.put(batch, new Statistics());
this.snapshotEvents = new ArrayList<FileSnapshot>();
}
public boolean start(Table table) {
this.snapshotTable = table;
return true;
}
public void write(CsvData data) {
DataEventType eventType = data.getDataEventType();
if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE) {
Map<String, String> columnData = data.toColumnNameValuePairs(
snapshotTable.getColumnNames(), CsvData.ROW_DATA);
Map<String, String> oldColumnData = data.toColumnNameValuePairs(
snapshotTable.getColumnNames(), CsvData.OLD_DATA);
FileSnapshot snapshot = new FileSnapshot();
snapshot.setTriggerId(columnData.get("TRIGGER_ID"));
snapshot.setRouterId(columnData.get("ROUTER_ID"));
snapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
snapshot.setCrc32Checksum(Long.parseLong(columnData.get("CRC32_CHECKSUM")));
String oldChecksum = oldColumnData.get("CRC32_CHECKSUM");
if (StringUtils.isNotBlank(oldChecksum)) {
snapshot.setOldCrc32Checksum(Long.parseLong(oldChecksum));
}
snapshot.setFileSize(Long.parseLong(columnData.get("FILE_SIZE")));
snapshot.setLastUpdateBy(columnData.get("LAST_UPDATE_BY"));
snapshot.setFileName(columnData.get("FILE_NAME"));
snapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
snapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
snapshotEvents.add(snapshot);
} else if (eventType == DataEventType.RELOAD) {
String targetNodeId = context.getBatch().getTargetNodeId();
Node targetNode = nodeService.findNode(targetNodeId);
List<FileTriggerRouter> fileTriggerRouters = fileSyncService
.getFileTriggerRoutersForCurrentNode();
for (FileTriggerRouter fileTriggerRouter : fileTriggerRouters) {
if (fileTriggerRouter.isEnabled()
&& fileTriggerRouter.isInitialLoadEnabled()
&& fileTriggerRouter.getRouter().getNodeGroupLink().getTargetNodeGroupId()
.equals(targetNode.getNodeGroupId())) {
DirectorySnapshot directorySnapshot = fileSyncService
.getDirectorySnapshot(fileTriggerRouter);
snapshotEvents.addAll(directorySnapshot);
}
}
}
}
public void end(Table table) {
}
public void end(Batch batch, boolean inError) {
try {
if (!inError) {
if (zos == null) {
zos = new ZipOutputStream(stagedResource.getOutputStream());
}
FileSyncZipScript script = createFileSyncZipScript(batch.getTargetNodeId());
script.buildScriptStart(batch);
Map<String, LastEventType> entries = new HashMap<String, LastEventType>();
for (FileSnapshot snapshot : snapshotEvents) {
FileTriggerRouter triggerRouter = fileSyncService.getFileTriggerRouter(
snapshot.getTriggerId(), snapshot.getRouterId());
if (triggerRouter != null) {
LastEventType eventType = snapshot.getLastEventType();
FileTrigger fileTrigger = triggerRouter.getFileTrigger();
String targetBaseDir = ((triggerRouter.getTargetBaseDir()==null)?null:triggerRouter.getTargetBaseDir().replace('\\', '/'));
if (StringUtils.isBlank(targetBaseDir)) {
targetBaseDir = ((fileTrigger.getBaseDir()==null)?null:fileTrigger.getBaseDir().replace('\\', '/'));
}
targetBaseDir = StringEscapeUtils.escapeJava(targetBaseDir);
StringBuilder entryName = new StringBuilder(Long.toString(batch
.getBatchId()));
entryName.append("/");
if (!snapshot.getRelativeDir().equals(".")) {
entryName.append(snapshot.getRelativeDir()).append("/");
}
entryName.append(snapshot.getFileName());
File file = fileTrigger.createSourceFile(snapshot);
if (file.isDirectory()) {
entryName.append("/");
}
String targetFile = "targetBaseDir + \"/\" + targetRelativeDir + \"/\" + targetFileName";
LastEventType previousEventForEntry = entries.get(entryName.toString());
boolean process = true;
if (previousEventForEntry != null) {
if ((previousEventForEntry == eventType)
|| (previousEventForEntry == LastEventType.CREATE && eventType == LastEventType.MODIFY)) {
process = false;
}
}
if (process) {
if (eventType != LastEventType.DELETE) {
if (file.exists()) {
byteCount += file.length();
ZipEntry entry = new ZipEntry(entryName.toString());
entry.setSize(file.length());
entry.setTime(file.lastModified());
zos.putNextEntry(entry);
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
try {
IOUtils.copy(fis, zos);
} finally {
IOUtils.closeQuietly(fis);
}
}
zos.closeEntry();
entries.put(entryName.toString(), eventType);
} else {
log.warn(
"Could not find the {} file to package for synchronization. Skipping it.",
file.getAbsolutePath());
}
}
script.buildScriptFileSnapshot(batch, snapshot, triggerRouter, fileTrigger,
file, targetBaseDir, targetFile);
}
} else {
log.error(
"Could not locate the file trigger ({}) router ({}) to process a snapshot event. The event will be ignored",
snapshot.getTriggerId(), snapshot.getRouterId());
}
}
script.buildScriptEnd(batch);
ZipEntry entry = new ZipEntry(batch.getBatchId() + "/" + script.getScriptFileName(batch));
zos.putNextEntry(entry);
IOUtils.write(script.getScript().toString(), zos);
zos.closeEntry();
entry = new ZipEntry(batch.getBatchId() + "/batch-info.txt");
zos.putNextEntry(entry);
IOUtils.write(batch.getChannelId(), zos);
zos.closeEntry();
}
} catch (IOException e) {
throw new IoException(e);
}
}
public void finish() {
try {
if (zos != null) {
zos.finish();
IOUtils.closeQuietly(zos);
}
} catch (IOException e) {
throw new IoException(e);
} finally {
if (stagedResource != null) {
stagedResource.setState(IStagedResource.State.DONE);
stagedResource.close();
}
}
}
public boolean readyToSend() {
return byteCount > maxBytesToSync;
}
protected FileSyncZipScript createFileSyncZipScript(String targetNodeId) {
if (isCClient(targetNodeId)) {
return new BashFileSyncZipScript();
} else {
return new BeanShellFileSyncZipScript();
}
}
protected boolean isCClient(String nodeId) {
boolean cclient = false;
Node node = nodeService.findNode(nodeId, true);
if (node != null) {
cclient = StringUtils.equals(node.getDeploymentType(), Constants.DEPLOYMENT_TYPE_CCLIENT);
}
return cclient;
}
}