001: /* GenerationFileHandler
002: *
003: * $Id: GenerationFileHandler.java 4646 2006-09-22 17:23:04Z paul_jack $
004: *
005: * Created on May 18, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.io;
026:
027: import java.io.File;
028: import java.io.FileNotFoundException;
029: import java.io.IOException;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.logging.FileHandler;
033:
034: /**
035: * FileHandler with support for rotating the current file to
036: * an archival name with a specified integer suffix, and
037: * provision of a new replacement FileHandler with the current
038: * filename.
039: *
040: * @author gojomo
041: */
042: public class GenerationFileHandler extends FileHandler {
043: private LinkedList<String> filenameSeries = new LinkedList<String>();
044: private boolean shouldManifest = false;
045:
046: /**
047: * @return Returns the filenameSeries.
048: */
049: public List getFilenameSeries() {
050: return filenameSeries;
051: }
052:
053: /**
054: * Constructor.
055: * @param pattern
056: * @param append
057: * @param shouldManifest
058: * @throws IOException
059: * @throws SecurityException
060: */
061: public GenerationFileHandler(String pattern, boolean append,
062: boolean shouldManifest) throws IOException,
063: SecurityException {
064: super (pattern, append);
065: filenameSeries.addFirst(pattern);
066: this .shouldManifest = shouldManifest;
067: }
068:
069: /**
070: * @param filenameSeries
071: * @param shouldManifest
072: * @throws IOException
073: */
074: public GenerationFileHandler(LinkedList<String> filenameSeries,
075: boolean shouldManifest) throws IOException {
076: super ((String) filenameSeries.getFirst(), false); // Never append in this case
077: this .filenameSeries = filenameSeries;
078: this .shouldManifest = shouldManifest;
079: }
080:
081: /**
082: * Move the current file to a new filename with the storeSuffix in place
083: * of the activeSuffix; continuing logging to a new file under the
084: * original filename.
085: *
086: * @param storeSuffix Suffix to put in place of <code>activeSuffix</code>
087: * @param activeSuffix Suffix to replace with <code>storeSuffix</code>.
088: * @return GenerationFileHandler instance.
089: * @throws IOException
090: */
091: public GenerationFileHandler rotate(String storeSuffix,
092: String activeSuffix) throws IOException {
093: close();
094: String filename = (String) filenameSeries.getFirst();
095: if (!filename.endsWith(activeSuffix)) {
096: throw new FileNotFoundException("Active file does not have"
097: + " expected suffix");
098: }
099: String storeFilename = filename.substring(0, filename.length()
100: - activeSuffix.length())
101: + storeSuffix;
102: File activeFile = new File(filename);
103: File storeFile = new File(storeFilename);
104: if (!activeFile.renameTo(storeFile)) {
105: throw new IOException("Unable to move " + filename + " to "
106: + storeFilename);
107: }
108: filenameSeries.add(1, storeFilename);
109: GenerationFileHandler newGfh = new GenerationFileHandler(
110: filenameSeries, shouldManifest);
111: newGfh.setFormatter(this .getFormatter());
112: return newGfh;
113: }
114:
115: /**
116: * @return True if should manifest.
117: */
118: public boolean shouldManifest() {
119: return this.shouldManifest;
120: }
121: }
|