protected void processInOnly()

in bindings/servicemix-file/src/main/java/org/apache/servicemix/file/FileSenderEndpoint.java [81:165]


    protected void processInOnly(MessageExchange exchange, NormalizedMessage in) throws Exception {
        OutputStream out = null;
        File newFile = null;
        String name = null;
        String writeTempName = null;
        boolean success = false;
        try {
            name = marshaler.getOutputName(exchange, in);
            if (name == null) {
                newFile = File.createTempFile("" + System.currentTimeMillis(), "tmp", directory);
            } else {
                newFile = new File(directory, name);
                if (newFile.exists()) {
                	if (isOverwrite()) {
                		// overwrite active
                		newFile.delete();
                	} else if (isAppend()) {
                		// all fine, we append
                	} else {
                		// no overwrite and no append
                		newFile = null;
                		throw new IOException("Can not write " + name
                                + " : file already exists and overwrite has not been enabled");
                	}
                }
                writeTempName = marshaler.getTempOutputName(exchange, in) != null ? marshaler.getTempOutputName(exchange, in) : name;
                newFile = new File(directory, writeTempName);
            }
            
            if (!newFile.getParentFile().exists() && isAutoCreateDirectory()) {
                newFile.getParentFile().mkdirs();
            }
            logger.debug("Writing to file: {}", newFile.getCanonicalPath());
            out = new BufferedOutputStream(new FileOutputStream(newFile, append));
            marshaler.writeMessage(exchange, in, out, name);
            success = true;
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    logger.error("Caught exception while closing stream on error: {}", e, e);
                }
            }
            if (success) {
                if (name != null && !name.equals(newFile.getName())) {
                    if (isAppend()) {
                        // append mode...now we need to transfer the file content into the original file
                        File targetFile = new File(directory, name);
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(newFile));
                        out = new BufferedOutputStream(new FileOutputStream(targetFile, append));
                        try {
                            FileUtil.copyInputStream(bis, out);
                        } catch (IOException ioex) {
                            logger.error("Unable to append to file {}", targetFile.getName(), ioex);
                        } finally {
                            try {
                                out.close();
                            } catch (IOException e) {
                                logger.error("Caught exception while closing stream on error: {}", e, e);
                            }
                            if (!newFile.delete()) {
                                throw new IOException("File " + newFile.getName() + " could not be deleted...");          
                            }
                        }            			
                    } else {
                        // no append mode, so just rename it
                        if (!newFile.renameTo(new File(directory, name))) {
                            throw new IOException("File " + newFile.getName() + " could not be renamed to " + name);            				
                        }           				
                    }
                }
            } else {
                // cleaning up incomplete files after things went wrong
                if (newFile != null) {
                    logger.error("An error occured while writing file {}, deleting the invalid file", newFile.getCanonicalPath());
                    if (!newFile.delete()) {
                        logger.warn("Unable to delete file {} after an error had occured", newFile.getCanonicalPath());
                    }
                } else {
                    logger.error("An error occured while creating file or creating name of this file");
                }
            }
        }
    }