View Javadoc

1   /* CheckpointUtils
2    *
3    * $Id: CheckpointUtils.java 4658 2006-09-25 22:40:19Z paul_jack $
4    *
5    * Created on December 16, 2005.
6    *
7    * Copyright (C) 2005 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.crawler.util;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileNotFoundException;
30  import java.io.FileOutputStream;
31  import java.io.FilenameFilter;
32  import java.io.IOException;
33  import java.io.ObjectInputStream;
34  import java.io.ObjectOutputStream;
35  
36  /***
37   * Utilities useful checkpointing.
38   * @author stack
39   * @version $Date: 2006-09-25 22:40:19 +0000 (Mon, 25 Sep 2006) $ $Revision: 4658 $
40   */
41  public class CheckpointUtils {
42      public static final String SERIALIZED_CLASS_SUFFIX = ".serialized";
43      
44      public static File getBdbSubDirectory(File checkpointDir) {
45          return new File(checkpointDir, "bdbje-logs");
46      }
47      
48      public static File getClassCheckpointFile(File checkpointDir,
49              final String suffix, Class c) {
50          return new File(checkpointDir, getClassCheckpointFilename(c, suffix));
51      }
52      
53      public static File getClassCheckpointFile(File checkpointDir, Class c) {
54          return new File(checkpointDir, getClassCheckpointFilename(c, null));
55      }
56      
57      public static String getClassCheckpointFilename(final Class c) {
58          return getClassCheckpointFilename(c, null);
59      }
60      
61      public static String getClassCheckpointFilename(final Class c,
62              final String suffix) {
63          return c.getName() + ((suffix == null)? "": "." + suffix) +
64              SERIALIZED_CLASS_SUFFIX;
65      }
66      
67      /***
68       * Utility function to serialize an object to a file in current checkpoint
69       * dir. Facilities
70       * to store related files alongside the serialized object in a directory
71       * named with a <code>.auxillary</code> suffix.
72       *
73       * @param o Object to serialize.
74       * @param dir Directory to serialize into.
75       * @throws IOException
76       */
77      public static void writeObjectToFile(final Object o, final File dir)
78      throws IOException {
79          writeObjectToFile(o, null, dir);
80      }
81          
82      public static void writeObjectToFile(final Object o, final String suffix,
83              final File dir)
84      throws IOException {
85          dir.mkdirs();
86          ObjectOutputStream out = new ObjectOutputStream(
87              new FileOutputStream(getClassCheckpointFile(dir, suffix,
88                  o.getClass())));
89          try {
90              out.writeObject(o);
91          } finally {
92              out.close();
93          }
94      }
95      
96      public static <T> T readObjectFromFile(final Class<T> c, final File dir)
97      throws FileNotFoundException, IOException, ClassNotFoundException {
98          return readObjectFromFile(c, null, dir);
99      }
100     
101     public static <T> T readObjectFromFile(final Class<T> c, final String suffix,
102             final File dir)
103     throws FileNotFoundException, IOException, ClassNotFoundException {
104         ObjectInputStream in = new ObjectInputStream(
105             new FileInputStream(getClassCheckpointFile(dir, suffix, c)));
106         T o = null;
107         try {
108             o = c.cast(in.readObject());
109         } finally {
110             in.close();
111         }
112         return o;
113     }
114 
115     /***
116      * @return Instance of filename filter that will let through files ending
117      * in '.jdb' (i.e. bdb je log files).
118      */
119     public static FilenameFilter getJeLogsFilter() {
120         return new FilenameFilter() {
121             public boolean accept(File dir, String name) {
122                 return name != null && name.toLowerCase().endsWith(".jdb");
123             }
124         };
125     }
126 }