KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.DateFormat JavaDoc;
20 import java.text.DecimalFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Locale JavaDoc;
26
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
32
33 public class BackupInstance {
34     private final DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("EEE MMM dd hh:mm:ss z yyyy", Locale.US);
35     private String JavaDoc name;
36     private List JavaDoc entries;
37     private File JavaDoc directory;
38     private Date JavaDoc createDate;
39     private String JavaDoc serverVersion;
40     private File JavaDoc instanceFile;
41     private Document JavaDoc instanceDocument;
42     private boolean changed;
43     
44     public BackupInstance(File JavaDoc backupBaseDir) throws Exception JavaDoc{
45         SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc("yyyyMMdd");
46         String JavaDoc dirName = dateFormat.format(new Date JavaDoc());
47         
48         long seq = 0;
49         DecimalFormat JavaDoc df = new DecimalFormat JavaDoc("000");
50         do {
51             String JavaDoc dirNameSeq = dirName + "_" + df.format(seq);
52             directory = new File JavaDoc(backupBaseDir, dirNameSeq);
53             seq++;
54         } while (!directory.mkdir() && seq < 1000);
55         if (seq > 999)
56             throw new Exception JavaDoc("Could not create directory with prefix "
57                     + dirName);
58         
59         entries = new ArrayList JavaDoc();
60         name = directory.getName();
61         createDate = new Date JavaDoc();
62         serverVersion = BackupManager.getLocker().getServerVersion();
63     }
64     
65     public BackupInstance(File JavaDoc backupBaseDir, String JavaDoc backupName) throws Exception JavaDoc {
66         this.name = backupName;
67         directory = new File JavaDoc(backupBaseDir, this.name);
68         entries = new ArrayList JavaDoc();
69         if (!directory.exists())
70             throw new Exception JavaDoc ("Backup " + name + " does not exist at " + directory.getPath());
71
72         instanceDocument = BackupHelper.parseFile(getInstanceFile());
73         
74         createDate = dateFormat.parse(BackupHelper.getStringFromDom(instanceDocument, "/backupInstance/@createDate"));
75         serverVersion = BackupHelper.getStringFromDom(instanceDocument, "/backupInstance/@serverVersion");
76     }
77     
78     public void addEntry(BackupEntry entry) {
79         entries.add(entry);
80     }
81     
82     public BackupEntry getEntry(int index ) {
83         return (BackupEntry) entries.get(index);
84     }
85     
86     public int entryCount() {
87         return entries.size();
88     }
89     
90     public void backup() {
91         
92     }
93     public void restore() {
94         
95     }
96     public File JavaDoc getDirectory() {
97         return directory;
98     }
99     public void setDirectory(File JavaDoc directory) {
100         changed = true;
101         this.directory = directory;
102     }
103
104     public Date JavaDoc getCreateDate() {
105         return createDate;
106     }
107
108     public String JavaDoc getName() {
109         return name;
110     }
111
112     public void setName(String JavaDoc name) {
113         changed = true;
114         this.name = name;
115     }
116
117     public String JavaDoc getServerVersion() {
118         return serverVersion;
119     }
120     
121     public Document JavaDoc getInstanceDocument() throws Exception JavaDoc {
122         if (changed || instanceDocument == null)
123             instanceDocument = backupToDocument();
124         return instanceDocument;
125     }
126
127     public File JavaDoc getInstanceFile() {
128         if (instanceFile == null || instanceFile.getParentFile().equals(getDirectory()))
129             instanceFile = new File JavaDoc(getDirectory(), "instance.xml");
130         return instanceFile;
131     }
132     
133     public void invalidate() {
134         changed = true;
135     }
136
137     private Document JavaDoc backupToDocument() throws Exception JavaDoc {
138         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
139         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
140         Document JavaDoc document = db.newDocument();
141
142         Element JavaDoc backupElement = document.createElement("backupInstance");
143         document.appendChild(backupElement);
144         backupElement.setAttribute("name", this.getName());
145         backupElement.setAttribute("createDate", dateFormat.format(this.getCreateDate()));
146         backupElement.setAttribute("serverVersion", serverVersion);
147
148         for (int i = 0; i < this.entryCount(); i++) {
149             AbstractBackupEntry buEntry = (AbstractBackupEntry) this.getEntry(i);
150             if (buEntry.backupFile.exists()) {
151                 Element JavaDoc entry = document.createElement("entry");
152                 backupElement.appendChild(entry);
153                 entry.setAttribute("checksum", buEntry.generateDigest());
154                 entry.setAttribute("filename", buEntry.backupFile.getName());
155             }
156         }
157
158         return document;
159     }
160 }
161
Popular Tags