KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > io > GenerationFileHandler


1 /* GenerationFileHandler
2 *
3 * $Id: GenerationFileHandler.java,v 1.6.14.1 2007/01/13 01:31:32 stack-sf Exp $
4 *
5 * Created on May 18, 2004
6 *
7 * Copyright (C) 2004 Internet Archive.
8 *
9 * This file is part of the Heritrix web crawler (crawler.archive.org).
10 *
11 * Heritrix is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * any later version.
15 *
16 * Heritrix is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser Public License
22 * along with Heritrix; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */

25 package org.archive.io;
26
27 import java.io.File JavaDoc;
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.logging.FileHandler JavaDoc;
33
34
35 /**
36  * FileHandler with support for rotating the current file to
37  * an archival name with a specified integer suffix, and
38  * provision of a new replacement FileHandler with the current
39  * filename.
40  *
41  * @author gojomo
42  */

43 public class GenerationFileHandler extends FileHandler JavaDoc {
44     private LinkedList JavaDoc<String JavaDoc> filenameSeries = new LinkedList JavaDoc<String JavaDoc>();
45     private boolean shouldManifest = false;
46
47     /**
48      * @return Returns the filenameSeries.
49      */

50     public List JavaDoc getFilenameSeries() {
51         return filenameSeries;
52     }
53
54     /**
55      * Constructor.
56      * @param pattern
57      * @param append
58      * @param shouldManifest
59      * @throws IOException
60      * @throws SecurityException
61      */

62     public GenerationFileHandler(String JavaDoc pattern, boolean append,
63             boolean shouldManifest)
64     throws IOException JavaDoc, SecurityException JavaDoc {
65         super(pattern, append);
66         filenameSeries.addFirst(pattern);
67         this.shouldManifest = shouldManifest;
68     }
69
70     /**
71      * @param filenameSeries
72      * @param shouldManifest
73      * @throws IOException
74      */

75     public GenerationFileHandler(LinkedList JavaDoc<String JavaDoc> filenameSeries,
76             boolean shouldManifest)
77     throws IOException JavaDoc {
78         super((String JavaDoc)filenameSeries.getFirst(), false); // Never append in this case
79
this.filenameSeries = filenameSeries;
80         this.shouldManifest = shouldManifest;
81     }
82
83     /**
84      * Move the current file to a new filename with the storeSuffix in place
85      * of the activeSuffix; continuing logging to a new file under the
86      * original filename.
87      *
88      * @param storeSuffix Suffix to put in place of <code>activeSuffix</code>
89      * @param activeSuffix Suffix to replace with <code>storeSuffix</code>.
90      * @return GenerationFileHandler instance.
91      * @throws IOException
92      */

93     public GenerationFileHandler rotate(String JavaDoc storeSuffix,
94             String JavaDoc activeSuffix)
95     throws IOException JavaDoc {
96         close();
97         String JavaDoc filename = (String JavaDoc)filenameSeries.getFirst();
98         if (!filename.endsWith(activeSuffix)) {
99             throw new FileNotFoundException JavaDoc("Active file does not have" +
100                 " expected suffix");
101         }
102         String JavaDoc storeFilename = filename.substring(0,
103              filename.length() - activeSuffix.length()) +
104              storeSuffix;
105         File JavaDoc activeFile = new File JavaDoc(filename);
106         File JavaDoc storeFile = new File JavaDoc(storeFilename);
107         if (!activeFile.renameTo(storeFile)) {
108             throw new IOException JavaDoc("Unable to move " + filename + " to " +
109                 storeFilename);
110         }
111         filenameSeries.add(1, storeFilename);
112         GenerationFileHandler newGfh =
113             new GenerationFileHandler(filenameSeries, shouldManifest);
114         newGfh.setFormatter(this.getFormatter());
115         return newGfh;
116     }
117     
118     /**
119      * @return True if should manifest.
120      */

121     public boolean shouldManifest() {
122         return this.shouldManifest;
123     }
124 }
Popular Tags