001: /* Checkpoint
002: *
003: * $Id: Checkpoint.java 4047 2005-12-17 02:31:52Z stack-sf $
004: *
005: * Created on Apr 25, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.crawler.datamodel;
026:
027: import java.io.File;
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.Serializable;
031:
032: import org.archive.crawler.util.CheckpointUtils;
033: import org.archive.util.FileUtils;
034:
035: /**
036: * Record of a specific checkpoint on disk.
037: * Used recovering from a checkpoint or displaying list of checkpoints done
038: * so far.
039: * @author gojomo
040: */
041: public class Checkpoint implements Serializable {
042: /**
043: * Generated by eclipse.
044: */
045: private static final long serialVersionUID = 5121498771788002844L;
046:
047: /**
048: * Flag label for invalid Checkpoints
049: */
050: private static final String INVALID = "INVALID";
051:
052: /**
053: * Name of file written with timestamp into valid checkpoints.
054: */
055: public static final String VALIDITY_STAMP_FILENAME = "valid";
056:
057: private transient String timestamp;
058: private File directory;
059:
060: /**
061: * Publically inaccessible default constructor.
062: */
063: protected Checkpoint() {
064: super ();
065: }
066:
067: /**
068: * Create a Checkpoint instance based on the given prexisting
069: * checkpoint directory
070: *
071: * @param checkpointDir Directory that holds checkpoint.
072: */
073: public Checkpoint(File checkpointDir) {
074: this .directory = checkpointDir;
075: readValid();
076: }
077:
078: private void readObject(ObjectInputStream s) throws IOException,
079: ClassNotFoundException {
080: s.defaultReadObject();
081: readValid();
082: }
083:
084: protected void readValid() {
085: File validityStamp = new File(this .directory,
086: VALIDITY_STAMP_FILENAME);
087: if (validityStamp.exists() == false) {
088: this .timestamp = INVALID;
089: } else {
090: try {
091: this .timestamp = FileUtils.readFileAsString(
092: validityStamp).trim();
093: } catch (IOException e) {
094: e.printStackTrace();
095: this .timestamp = INVALID;
096: }
097: }
098: }
099:
100: /**
101: * @return Return true if this checkpoint appears complete/resumable
102: * (has 'valid' stamp file).
103: */
104: public boolean isValid() {
105: return timestamp != INVALID;
106: }
107:
108: /**
109: * @return Returns name of this Checkpoint
110: */
111: public String getName() {
112: return this .directory.getName();
113: }
114:
115: /**
116: * @return Return the combination of given name and timestamp most commonly
117: * used in administrative interface.
118: */
119: public String getDisplayName() {
120: return getName() + " [" + getTimestamp() + "]";
121: }
122:
123: /**
124: * @return Returns the timestamp.
125: */
126: public String getTimestamp() {
127: return timestamp;
128: }
129:
130: /**
131: * @return Returns the checkpoint directory.
132: */
133: public File getDirectory() {
134: return this .directory;
135: }
136:
137: /**
138: * @return True if this checkpoint contains bdb logs (It won't if we're
139: * doing 'fast' checkpoints).
140: */
141: public boolean hasBdbjeLogs() {
142: boolean decision = false;
143: File bdbjeDir = CheckpointUtils
144: .getBdbSubDirectory(this .directory);
145: if (bdbjeDir.exists()) {
146: String[] files = bdbjeDir.list(CheckpointUtils
147: .getJeLogsFilter());
148: decision = (files != null && files.length > 0);
149: }
150: return decision;
151: }
152: }
|