
java.fedora.server.journal.readerwriter.multicast.TransportOutputFile Maven / Gradle / Ivy
Show all versions of fcrepo-client Show documentation
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.server.journal.readerwriter.multicast;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import org.apache.log4j.Logger;
/**
*
* Title: TransportOutputFile.java
*
*
* Description: A "renaming" journal file, for use by Transports.
*
*
* When this file is created, a prefix is put on the filename to show that
* output is in progress. When the file is closed, it is renamed to remove the
* prefix.
*
*
* @author jblake
* @version $Id: TransportOutputFile.java,v 1.3 2007/06/01 17:21:31 jblake Exp $
*/
public class TransportOutputFile {
private static final Logger LOG =
Logger.getLogger(TransportOutputFile.class);
private enum State {
READY, OPEN, CLOSED
};
private final File file;
private final File tempFile;
private State state = State.READY;
private FileWriter fileWriter;
/**
* Sture the filename, and the "in process" filename.
*
* @throws IOException
* if either file already exists.
*/
public TransportOutputFile(File directory, String name)
throws IOException {
LOG.debug("creating TransportOutputFile: '" + directory + "', '" + name
+ "'");
file = new File(directory, name);
if (file.exists()) {
throw new IOException("File " + file + " already exists.");
}
tempFile = new File(directory, ("_" + name));
if (tempFile.exists()) {
throw new IOException("File " + tempFile + " already exists.");
}
}
/**
* Create the file with its "in progress" filename.
*
* @return a Writer on the new file.
*/
public Writer open() throws IOException {
switch (state) {
case OPEN:
throw new IllegalStateException("File " + tempFile
+ " is already open.");
case CLOSED:
throw new IllegalStateException("File " + tempFile
+ " has been closed already.");
default: // READY
state = State.OPEN;
tempFile.createNewFile();
fileWriter = new FileWriter(tempFile);
return fileWriter;
}
}
/**
* Close the writer and rename the file.
*
* @throws IOException
*/
public void close() throws IOException {
switch (state) {
case READY:
throw new IllegalStateException("File " + tempFile
+ " hasn't been opened yet.");
case CLOSED:
throw new IllegalStateException("File " + tempFile
+ " has been closed already.");
default: // OPEN
fileWriter.close();
tempFile.renameTo(file);
state = State.CLOSED;
}
}
/**
* Did somebody ask who we are?
*/
public String getName() {
return file.getName();
}
}