use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class FileSyncZipDataWriter method end.
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);
}
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class FileSyncZipDataWriter method finish.
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();
}
}
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class FileSyncService method sendFiles.
public List<OutgoingBatch> sendFiles(ProcessInfo processInfo, Node targetNode, IOutgoingTransport outgoingTransport) {
List<OutgoingBatch> batchesToProcess = getBatchesToProcess(targetNode);
if (batchesToProcess.isEmpty()) {
return batchesToProcess;
}
IStagingManager stagingManager = engine.getStagingManager();
long maxBytesToSync = parameterService.getLong(ParameterConstants.TRANSPORT_MAX_BYTES_TO_SYNC);
List<OutgoingBatch> processedBatches = new ArrayList<OutgoingBatch>();
OutgoingBatch currentBatch = null;
IStagedResource stagedResource = null;
IStagedResource previouslyStagedResource = null;
FileSyncZipDataWriter dataWriter = null;
try {
long syncedBytes = 0;
try {
for (int i = 0; i < batchesToProcess.size(); i++) {
currentBatch = batchesToProcess.get(i);
previouslyStagedResource = getStagedResource(currentBatch);
if (isWaitForExtractionRequired(currentBatch, previouslyStagedResource) || isFlushBatchesRequired(currentBatch, processedBatches, previouslyStagedResource)) {
// previously staged batch will have to wait for the next push/pull.
break;
}
if (previouslyStagedResource != null) {
log.debug("Using existing extraction for file sync batch {}", currentBatch.getNodeBatchId());
stagedResource = previouslyStagedResource;
} else {
if (dataWriter == null) {
stagedResource = stagingManager.create(Constants.STAGING_CATEGORY_OUTGOING, processInfo.getSourceNodeId(), targetNode.getNodeId(), "filesync.zip");
dataWriter = new FileSyncZipDataWriter(maxBytesToSync, this, engine.getNodeService(), stagedResource);
}
log.debug("Extracting batch {} for filesync.", currentBatch.getNodeBatchId());
((DataExtractorService) engine.getDataExtractorService()).extractOutgoingBatch(processInfo, targetNode, dataWriter, currentBatch, false, true, DataExtractorService.ExtractMode.FOR_SYM_CLIENT);
}
processedBatches.add(currentBatch);
syncedBytes += stagedResource.getSize();
processInfo.incrementBatchCount();
processInfo.setCurrentBatchId(currentBatch.getBatchId());
log.debug("Processed file sync batch {}. syncedBytes={}, maxBytesToSync={}", currentBatch, syncedBytes, maxBytesToSync);
/*
* check to see if max bytes to sync has been reached and
* stop processing batches
*/
if (previouslyStagedResource != null || dataWriter.readyToSend()) {
break;
}
}
} finally {
if (dataWriter != null) {
dataWriter.finish();
}
}
processInfo.setStatus(ProcessInfo.Status.TRANSFERRING);
for (OutgoingBatch outgoingBatch : processedBatches) {
outgoingBatch.setStatus(Status.SE);
}
engine.getOutgoingBatchService().updateOutgoingBatches(processedBatches);
try {
if (stagedResource != null && stagedResource.exists()) {
InputStream is = stagedResource.getInputStream();
try {
OutputStream os = outgoingTransport.openStream();
IOUtils.copy(is, os);
os.flush();
} catch (IOException e) {
throw new IoException(e);
}
}
for (int i = 0; i < batchesToProcess.size(); i++) {
batchesToProcess.get(i).setStatus(Status.LD);
}
engine.getOutgoingBatchService().updateOutgoingBatches(batchesToProcess);
} finally {
if (stagedResource != null) {
stagedResource.close();
}
}
} catch (RuntimeException e) {
if (stagedResource == previouslyStagedResource) {
// on error, don't let the load extract be deleted.
stagedResource = null;
}
if (currentBatch != null) {
engine.getStatisticManager().incrementDataExtractedErrors(currentBatch.getChannelId(), 1);
currentBatch.setSqlMessage(getRootMessage(e));
currentBatch.revertStatsOnError();
if (currentBatch.getStatus() != Status.IG) {
currentBatch.setStatus(Status.ER);
}
currentBatch.setErrorFlag(true);
engine.getOutgoingBatchService().updateOutgoingBatch(currentBatch);
if (isStreamClosedByClient(e)) {
log.warn("Failed to extract file sync batch {}. The stream was closed by the client. The error was: {}", currentBatch, getRootMessage(e));
} else {
log.error("Failed to extract file sync batch " + currentBatch, e);
}
} else {
log.error("Could not log the outgoing batch status because the batch was null", e);
}
throw e;
} finally {
if (stagedResource != null) {
stagedResource.delete();
}
}
return processedBatches;
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class AbstractTransportManager method append.
protected static void append(StringBuilder builder, String name, Object value) {
try {
int len = builder.length();
if (len > 0 && builder.charAt(len - 1) != '?') {
builder.append("&");
}
if (value == null) {
value = "";
}
builder.append(name).append("=").append(URLEncoder.encode(value.toString(), IoConstants.ENCODING));
} catch (IOException ex) {
throw new IoException(ex);
}
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class HttpOutgoingTransport method openStream.
public OutputStream openStream() {
try {
connection = HttpTransportManager.openConnection(url, basicAuthUsername, basicAuthPassword);
if (streamOutputEnabled) {
connection.setChunkedStreamingMode(streamOutputChunkSize);
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(httpTimeout);
connection.setReadTimeout(httpTimeout);
if (this.requestProperties != null) {
for (Map.Entry<String, String> requestProperty : this.requestProperties.entrySet()) {
connection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
}
}
boundary = Long.toHexString(System.currentTimeMillis());
if (!fileUpload) {
connection.setRequestMethod("PUT");
connection.setRequestProperty("Accept-Encoding", "gzip");
if (useCompression) {
// application/x-gzip?
connection.addRequestProperty("Content-Type", "gzip");
}
} else {
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
}
os = connection.getOutputStream();
if (!fileUpload && useCompression) {
os = new GZIPOutputStream(os) {
{
this.def.setLevel(compressionLevel);
this.def.setStrategy(compressionStrategy);
}
};
}
if (fileUpload) {
final String fileName = "file.zip";
IOUtils.write("--" + boundary + CRLF, os);
IOUtils.write("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + fileName + "\"" + CRLF, os);
IOUtils.write("Content-Type: " + URLConnection.guessContentTypeFromName(fileName) + CRLF, os);
IOUtils.write("Content-Transfer-Encoding: binary" + CRLF + CRLF, os);
os.flush();
}
return os;
} catch (IOException ex) {
throw new IoException(ex);
}
}
Aggregations