Search in sources :

Example 31 with IoException

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);
    }
}
Also used : HashMap(java.util.HashMap) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) LastEventType(org.jumpmind.symmetric.model.FileSnapshot.LastEventType) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) FileTrigger(org.jumpmind.symmetric.model.FileTrigger) ZipOutputStream(java.util.zip.ZipOutputStream) IoException(org.jumpmind.exception.IoException) File(java.io.File)

Example 32 with IoException

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();
        }
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 33 with IoException

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;
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileSyncZipDataWriter(org.jumpmind.symmetric.file.FileSyncZipDataWriter) IStagingManager(org.jumpmind.symmetric.io.stage.IStagingManager) IoException(org.jumpmind.exception.IoException) IStagedResource(org.jumpmind.symmetric.io.stage.IStagedResource) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch)

Example 34 with IoException

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);
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 35 with IoException

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);
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) ChannelMap(org.jumpmind.symmetric.model.ChannelMap) Map(java.util.Map)

Aggregations

IoException (org.jumpmind.exception.IoException)48 IOException (java.io.IOException)41 File (java.io.File)8 Table (org.jumpmind.db.model.Table)8 BufferedReader (java.io.BufferedReader)6 Column (org.jumpmind.db.model.Column)6 BufferedWriter (java.io.BufferedWriter)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SymmetricException (org.jumpmind.symmetric.SymmetricException)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 BufferedInputStream (java.io.BufferedInputStream)2