View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package mnemosyne.archiver;
17  
18  import mnemosyne.core.PersistentRoot;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import java.io.*;
23  
24  /***
25   * @version $Id: SnapshotDirectory.java,v 1.1.1.1 2004/08/07 06:40:54 charlesblaxland Exp $
26   */
27  public class SnapshotDirectory extends ArchiveDirectory
28  {
29      private static final Log log = LogFactory.getLog(SnapshotDirectory.class);
30  
31      private static final String SNAPSHOT_FILE_PREFIX    = "";
32      private static final String SNAPSHOT_FILE_POSTFIX   = "_snapshot";
33      private static final String SNAPSHOT_FILE_EXTENSION = "snp";
34  
35      public SnapshotDirectory(String directoryName, ArchiveSequence sequence)
36      {
37          super(directoryName, sequence);
38      }
39  
40      protected String prefix()
41      {
42          return SNAPSHOT_FILE_PREFIX;
43      }
44  
45      protected String postfix()
46      {
47          return SNAPSHOT_FILE_POSTFIX;
48      }
49  
50      protected String extension()
51      {
52          return SNAPSHOT_FILE_EXTENSION;
53      }
54  
55      public long getLatestSnapshotSequenceNumber()
56      {
57          if (archiveFiles.isEmpty())
58          {
59              return -1;
60          }
61          else
62          {
63              return extractFileSequenceNumber((String)archiveFiles.last());
64          }
65      }
66  
67      public PersistentRoot readLatestSnapshot() throws ArchiverException
68      {
69          if (archiveFiles.isEmpty())
70          {
71              log.info("No snapshots found");
72              return null;
73          }
74  
75          String latestSnapshotName = (String)archiveFiles.last();
76          File latestSnapshot = constructArchiveFile(latestSnapshotName);
77          return (PersistentRoot) readArchiveFile(latestSnapshot);
78      }
79  }