KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > util > CheckpointUtils


1 /* CheckpointUtils
2  *
3  * $Id: CheckpointUtils.java,v 1.2.10.1 2007/01/13 01:31:29 stack-sf Exp $
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 JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.FileNotFoundException JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.FilenameFilter JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.ObjectInputStream JavaDoc;
34 import java.io.ObjectOutputStream JavaDoc;
35
36 /**
37  * Utilities useful checkpointing.
38  * @author stack
39  * @version $Date: 2007/01/13 01:31:29 $ $Revision: 1.2.10.1 $
40  */

41 public class CheckpointUtils {
42     public static final String JavaDoc SERIALIZED_CLASS_SUFFIX = ".serialized";
43     
44     public static File JavaDoc getBdbSubDirectory(File JavaDoc checkpointDir) {
45         return new File JavaDoc(checkpointDir, "bdbje-logs");
46     }
47     
48     public static File JavaDoc getClassCheckpointFile(File JavaDoc checkpointDir,
49             final String JavaDoc suffix, Class JavaDoc c) {
50         return new File JavaDoc(checkpointDir, getClassCheckpointFilename(c, suffix));
51     }
52     
53     public static File JavaDoc getClassCheckpointFile(File JavaDoc checkpointDir, Class JavaDoc c) {
54         return new File JavaDoc(checkpointDir, getClassCheckpointFilename(c, null));
55     }
56     
57     public static String JavaDoc getClassCheckpointFilename(final Class JavaDoc c) {
58         return getClassCheckpointFilename(c, null);
59     }
60     
61     public static String JavaDoc getClassCheckpointFilename(final Class JavaDoc c,
62             final String JavaDoc 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 JavaDoc o, final File JavaDoc dir)
78     throws IOException JavaDoc {
79         writeObjectToFile(o, null, dir);
80     }
81         
82     public static void writeObjectToFile(final Object JavaDoc o, final String JavaDoc suffix,
83             final File JavaDoc dir)
84     throws IOException JavaDoc {
85         dir.mkdirs();
86         ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(
87             new FileOutputStream JavaDoc(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 JavaDoc<T> c, final File JavaDoc dir)
97     throws FileNotFoundException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc {
98         return readObjectFromFile(c, null, dir);
99     }
100     
101     public static <T> T readObjectFromFile(final Class JavaDoc<T> c, final String JavaDoc suffix,
102             final File JavaDoc dir)
103     throws FileNotFoundException JavaDoc, IOException JavaDoc, ClassNotFoundException JavaDoc {
104         ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(
105             new FileInputStream JavaDoc(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 JavaDoc getJeLogsFilter() {
120         return new FilenameFilter JavaDoc() {
121             public boolean accept(File JavaDoc dir, String JavaDoc name) {
122                 return name != null && name.toLowerCase().endsWith(".jdb");
123             }
124         };
125     }
126 }
127
Popular Tags