View Javadoc

1   /* GenerationFileHandler
2   *
3   * $Id: GenerationFileHandler.java 4646 2006-09-22 17:23:04Z paul_jack $
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;
28  import java.io.FileNotFoundException;
29  import java.io.IOException;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.logging.FileHandler;
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 {
44      private LinkedList<String> filenameSeries = new LinkedList<String>();
45      private boolean shouldManifest = false;
46  
47      /***
48       * @return Returns the filenameSeries.
49       */
50      public List 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 pattern, boolean append,
63              boolean shouldManifest)
64      throws IOException, SecurityException {
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<String> filenameSeries,
76              boolean shouldManifest)
77      throws IOException {
78          super((String)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 storeSuffix,
94              String activeSuffix)
95      throws IOException {
96          close();
97          String filename = (String)filenameSeries.getFirst();
98          if (!filename.endsWith(activeSuffix)) {
99              throw new FileNotFoundException("Active file does not have" +
100                 " expected suffix");
101         }
102         String storeFilename = filename.substring(0,
103              filename.length() - activeSuffix.length()) +
104              storeSuffix;
105         File activeFile = new File(filename);
106         File storeFile = new File(storeFilename);
107         if (!activeFile.renameTo(storeFile)) {
108             throw new IOException("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 }