    1   /* Checkpoint
    2   *
    3   * $Id: Checkpoint.java 4047 2005-12-17 02:31:52Z stack-sf $
    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;
    28  import java.io.IOException;
    29  import java.io.ObjectInputStream;
    30  import java.io.Serializable;
    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 {
    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 INVALID = "INVALID";
    51
    52      /***
    53       * Name of file written with timestamp into valid checkpoints.
    54       */
    55      public static final String VALIDITY_STAMP_FILENAME = "valid";
    56
    57
    58      private transient String timestamp;
    59      private File 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 checkpointDir) {
    75          this.directory = checkpointDir;
    76          readValid();
    77      }
    78
    79      private void readObject(ObjectInputStream s)
    80      throws IOException, ClassNotFoundException {
    81          s.defaultReadObject();
    82          readValid();
    83      }
    84
    85      protected void readValid() {
    86          File validityStamp = new File(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 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 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 getDisplayName() {
    121         return getName() + " [" + getTimestamp() + "]";
    122     }
    123
    124     /***
    125      * @return Returns the timestamp.
    126      */
    127     public String getTimestamp() {
    128         return timestamp;
    129     }
    130
    131     /***
    132      * @return Returns the checkpoint directory.
    133      */
    134     public File 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 bdbjeDir = CheckpointUtils.getBdbSubDirectory(this.directory);
    145         if (bdbjeDir.exists()) {
    146             String [] files =
    147                 bdbjeDir.list(CheckpointUtils.getJeLogsFilter());
    148             decision = (files != null && files.length > 0);
    149         }
    150         return decision;
    151     }
    152 }