KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > backupTool > AbstractEntryLoader


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
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 org.outerj.daisy.backupTool;
17
18 import java.io.File JavaDoc;
19 import java.io.FileNotFoundException JavaDoc;
20 import java.security.DigestException JavaDoc;
21
22 import org.outerj.daisy.backupTool.dbDump.DbDumper;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25
26 public abstract class AbstractEntryLoader implements EntryLoader {
27     protected File JavaDoc configFile;
28
29     protected Document JavaDoc configDocument;
30
31     public AbstractEntryLoader(File JavaDoc configFile) throws Exception JavaDoc {
32         this.configFile = configFile;
33         configDocument = BackupHelper.parseFile(configFile);
34     }
35
36     protected boolean isFileBackedUp(BackupInstance buInstance, String JavaDoc backupFileName) throws Exception JavaDoc {
37         boolean entryOk = false;
38         Element JavaDoc entryElement = BackupHelper.getElementFromDom(buInstance.getInstanceDocument(), "/backupInstance/entry[@filename = '" + backupFileName + "']", false);
39         if (entryElement != null) {
40             File JavaDoc backupFile = new File JavaDoc(buInstance.getDirectory(), entryElement.getAttribute("filename"));
41             String JavaDoc hash = entryElement.getAttribute("checksum");
42             if (!backupFile.exists()) {
43                 throw new FileNotFoundException JavaDoc("Backupfile " + backupFileName + " was not found");
44             } else if (!hash.equals(BackupHelper.generateMD5Hash(backupFile)))
45                 throw new DigestException JavaDoc(backupFileName + " has been tampered with");
46             else
47                 entryOk = true;
48         } else {
49             // File was not backed up
50
entryOk = false;
51         }
52
53         return entryOk;
54     }
55
56     protected boolean areFilesBackedUp(BackupInstance buInstance, String JavaDoc[] backupFiles) throws Exception JavaDoc {
57         boolean entriesOk = true;
58         for (int i = 0; i < backupFiles.length; i++)
59             entriesOk = entriesOk & isFileBackedUp(buInstance, backupFiles[i]);
60
61         return entriesOk;
62     }
63
64     protected DbBackupEntry createDbEntry(BackupInstance buInstance, DbDumper dumper, String JavaDoc backupFileName) {
65         return new DbBackupEntry(new File JavaDoc(buInstance.getDirectory(), backupFileName), dumper, buInstance);
66     }
67
68     protected FileBackupEntry createFileEntry(BackupInstance buInstance, File JavaDoc toBackup, File JavaDoc baseDir, String JavaDoc backupFileName) throws Exception JavaDoc {
69         return new FileBackupEntry(toBackup, new File JavaDoc(buInstance.getDirectory(), backupFileName), baseDir, buInstance);
70     }
71 }
72
Popular Tags