1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 }