KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > datamodel > Checkpoint


1 /* Checkpoint
2 *
3 * $Id: Checkpoint.java,v 1.2 2005/12/17 02:31:51 stack-sf Exp $
4 *
5 * Created on Apr 25, 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.crawler.datamodel;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.Serializable JavaDoc;
31
32 import org.archive.crawler.util.CheckpointUtils;
33 import org.archive.util.FileUtils;
34
35 /**
36  * Record of a specific checkpoint on disk.
37  * Used recovering from a checkpoint or displaying list of checkpoints done
38  * so far.
39  * @author gojomo
40  */

41 public class Checkpoint implements Serializable JavaDoc {
42     /**
43      * Generated by eclipse.
44      */

45     private static final long serialVersionUID = 5121498771788002844L;
46
47     /**
48      * Flag label for invalid Checkpoints
49      */

50     private static final String JavaDoc INVALID = "INVALID";
51     
52     /**
53      * Name of file written with timestamp into valid checkpoints.
54      */

55     public static final String JavaDoc VALIDITY_STAMP_FILENAME = "valid";
56     
57     
58     private transient String JavaDoc timestamp;
59     private File JavaDoc directory;
60     
61     /**
62      * Publically inaccessible default constructor.
63      */

64     protected Checkpoint() {
65         super();
66     }
67
68     /**
69      * Create a Checkpoint instance based on the given prexisting
70      * checkpoint directory
71      *
72      * @param checkpointDir Directory that holds checkpoint.
73      */

74     public Checkpoint(File JavaDoc checkpointDir) {
75         this.directory = checkpointDir;
76         readValid();
77     }
78     
79     private void readObject(ObjectInputStream JavaDoc s)
80     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
81         s.defaultReadObject();
82         readValid();
83     }
84     
85     protected void readValid() {
86         File JavaDoc validityStamp = new File JavaDoc(this.directory,
87             VALIDITY_STAMP_FILENAME);
88         if (validityStamp.exists() == false) {
89             this.timestamp = INVALID;
90         } else {
91             try {
92                 this.timestamp = FileUtils.readFileAsString(validityStamp).
93                     trim();
94             } catch (IOException JavaDoc e) {
95                 e.printStackTrace();
96                 this.timestamp = INVALID;
97             }
98         }
99     }
100
101     /**
102      * @return Return true if this checkpoint appears complete/resumable
103      * (has 'valid' stamp file).
104      */

105     public boolean isValid() {
106         return timestamp != INVALID;
107     }
108
109     /**
110      * @return Returns name of this Checkpoint
111      */

112     public String JavaDoc getName() {
113         return this.directory.getName();
114     }
115
116     /**
117      * @return Return the combination of given name and timestamp most commonly
118      * used in administrative interface.
119      */

120     public String JavaDoc getDisplayName() {
121         return getName() + " [" + getTimestamp() + "]";
122     }
123
124     /**
125      * @return Returns the timestamp.
126      */

127     public String JavaDoc getTimestamp() {
128         return timestamp;
129     }
130
131     /**
132      * @return Returns the checkpoint directory.
133      */

134     public File JavaDoc getDirectory() {
135         return this.directory;
136     }
137     
138     /**
139      * @return True if this checkpoint contains bdb logs (It won't if we're
140      * doing 'fast' checkpoints).
141      */

142     public boolean hasBdbjeLogs() {
143         boolean decision = false;
144         File JavaDoc bdbjeDir = CheckpointUtils.getBdbSubDirectory(this.directory);
145         if (bdbjeDir.exists()) {
146             String JavaDoc [] files =
147                 bdbjeDir.list(CheckpointUtils.getJeLogsFilter());
148             decision = (files != null && files.length > 0);
149         }
150         return decision;
151     }
152 }
Popular Tags