001/**
002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC
003 * and GZip files. (http://jwat.org/)
004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/)
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.jwat.arc;
019
020import java.io.File;
021import java.io.IOException;
022import java.io.RandomAccessFile;
023
024import org.jwat.common.RandomAccessFileOutputStream;
025
026/**
027 * Simple ARC file writer wrapping some of the trivial code related to writing records.
028 * Handles automatic max file size closing and renaming of old file and opening of new file.
029 * The preferred workflow is to class nextWriter() and close(). Using open() does not expose a clean internal state.
030 *
031 * @author nicl
032 */
033public class ArcFileWriter {
034
035    /** Suffix used for open files. */
036    protected static final String ACTIVE_SUFFIX = ".open";
037
038    /** Overall ARC file writer configuration. */
039    protected ArcFileWriterConfig arcFileConfig;
040
041    /** ARC file naming Configuration. */
042    protected ArcFileNaming arcFileNaming;
043
044    /** Current sequence number. */
045    protected int sequenceNr = -1;
046
047    /** Current ARC file. */
048    protected File writerFile;
049
050    /** Current random access file. */
051    protected RandomAccessFile writer_raf;
052
053    /** Current random access output stream. */
054    protected RandomAccessFileOutputStream writer_rafout;
055
056    /** Current ARC writer. */
057    public ArcWriter writer;
058
059    /**
060     * Constructor for internal and unit test use.
061     */
062    protected ArcFileWriter() {
063    }
064
065    /**
066     * Returns a configured ARC file writer.
067     * @param arcFileNaming ARC file naming configuration
068     * @param arcFileConfig overall ARC writer configuration
069     * @return ARC file writer instance using the supplied configuration
070     */
071    public static ArcFileWriter getArcWriterInstance(ArcFileNaming arcFileNaming, ArcFileWriterConfig arcFileConfig) {
072        if (arcFileNaming == null) {
073                throw new IllegalArgumentException("'arcFileNaming' is null!");
074        }
075        if (arcFileConfig == null) {
076                throw new IllegalArgumentException("'arcFileConfig' is null!");
077        }
078        ArcFileWriter wfw = new ArcFileWriter();
079        wfw.arcFileNaming = arcFileNaming;
080        wfw.arcFileConfig = arcFileConfig;
081        return wfw;
082    }
083
084    /**
085     * Returns the current sequence number.
086     * @return the current sequence number
087     */
088    public int getSequenceNr() {
089        return sequenceNr;
090    }
091
092    /**
093     * Returns the current ARC file object.
094     * @return current ARC file object
095     */
096    public File getFile() {
097        return writerFile;
098    }
099
100    /**
101     * Returns the current ARC writer object.
102     * @return current ARC writer object
103     */
104    public ArcWriter getWriter() {
105        return writer;
106    }
107
108    /**
109     * Open new file with active prefix and prepare for writing.
110     * @throws IOException if an I/O exception occurs while opening file
111     */
112    public void open() throws IOException {
113        if (writer == null) {
114            ++sequenceNr;
115            String finishedFilename = arcFileNaming.getFilename(sequenceNr, arcFileConfig.bCompression);
116            String activeFilename = finishedFilename + ACTIVE_SUFFIX;
117            File finishedFile = new File(arcFileConfig.targetDir, finishedFilename);
118            writerFile = new File(arcFileConfig.targetDir, activeFilename);
119            if (writerFile.exists()) {
120                if (arcFileConfig.bOverwrite) {
121                    writerFile.delete();
122                } else {
123                    throw new IOException("'" + writerFile + "' already exists, will not overwrite");
124                }
125            }
126            if (finishedFile.exists()) {
127                if (arcFileConfig.bOverwrite) {
128                    finishedFile.delete();
129                } else {
130                    throw new IOException("'" + finishedFile + "' already exists, will not overwrite");
131                }
132            }
133            writer_raf = new RandomAccessFile(writerFile, "rw");
134            writer_raf.seek(0L);
135            writer_raf.setLength(0L);
136            writer_rafout = new RandomAccessFileOutputStream(writer_raf);
137            writer = ArcWriterFactory.getWriter(writer_rafout, 8192, arcFileConfig.bCompression);
138        }
139    }
140
141    /**
142     * Checks to see whether a new file needs to be created. Depending on the configuration this also checks if the max file size has been reached and closes/renames the old file and opens a new one.
143     * @return boolean indicating whether new writer/file was created
144     * @throws Exception if an exception occurs
145     */
146    public boolean nextWriter() throws Exception {
147        boolean bNewWriter = false;
148        if (writer_raf == null) {
149            bNewWriter = true;
150        } else if (arcFileNaming.supportMultipleFiles() && writer_raf.length() > arcFileConfig.maxFileSize) {
151            close();
152            bNewWriter = true;
153        }
154        if (bNewWriter) {
155            open();
156        }
157        return bNewWriter;
158    }
159
160    /**
161     * Close writer and release all resources.
162     * @throws IOException in an I/O exception occurs while closing resources
163     */
164    public void close() throws IOException {
165        if (writer != null) {
166            writer.close();
167            writer = null;
168        }
169        if (writer_rafout != null) {
170            writer_rafout.close();
171            writer_rafout = null;
172        }
173        if (writer_raf != null) {
174            writer_raf.close();
175            writer_raf = null;
176        }
177        if (writerFile != null && writerFile.getName().endsWith(ACTIVE_SUFFIX)) {
178            String finishedName = writerFile.getName().substring(0, writerFile.getName().length() - ACTIVE_SUFFIX.length());
179            File finishedFile = new File(writerFile.getParent(), finishedName);
180            if (finishedFile.exists()) {
181                throw new IOException("Unable to rename '" + writerFile + "' to '" + finishedFile + "' - destination file already exists");
182            }
183            boolean success = writerFile.renameTo(finishedFile);
184            if (!success) {
185                throw new IOException("Unable to rename '" + writerFile + "' to '" + finishedFile + "' - unknown problem");
186            }
187        }
188        writerFile = null;
189    }
190
191}