1 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 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 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 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 } 127 | Popular Tags |