001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.backupTool;
017:
018: import java.io.File;
019: import java.text.DateFormat;
020: import java.text.DecimalFormat;
021: import java.text.SimpleDateFormat;
022: import java.util.ArrayList;
023: import java.util.Date;
024: import java.util.List;
025: import java.util.Locale;
026:
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029:
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032:
033: public class BackupInstance {
034: private final DateFormat dateFormat = new SimpleDateFormat(
035: "EEE MMM dd hh:mm:ss z yyyy", Locale.US);
036: private String name;
037: private List<BackupEntry> entries;
038: private File directory;
039: private Date createDate;
040: private String serverVersion;
041: private File instanceFile;
042: private Document instanceDocument;
043: private boolean changed;
044:
045: public BackupInstance(File backupBaseDir) throws Exception {
046: SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
047: String dirName = dateFormat.format(new Date());
048:
049: long seq = 0;
050: DecimalFormat df = new DecimalFormat("000");
051: do {
052: String dirNameSeq = dirName + "_" + df.format(seq);
053: directory = new File(backupBaseDir, dirNameSeq);
054: seq++;
055: } while (!directory.mkdir() && seq < 1000);
056: if (seq > 999)
057: throw new Exception(
058: "Could not create directory with prefix " + dirName);
059:
060: entries = new ArrayList<BackupEntry>();
061: name = directory.getName();
062: createDate = new Date();
063: serverVersion = BackupManager.getLocker().getServerVersion();
064: }
065:
066: public BackupInstance(File backupBaseDir, String backupName)
067: throws Exception {
068: this .name = backupName;
069: directory = new File(backupBaseDir, this .name);
070: entries = new ArrayList<BackupEntry>();
071: if (!directory.exists())
072: throw new Exception("Backup " + name
073: + " does not exist at " + directory.getPath());
074:
075: instanceDocument = BackupHelper.parseFile(getInstanceFile());
076:
077: createDate = dateFormat.parse(BackupHelper.getStringFromDom(
078: instanceDocument, "/backupInstance/@createDate"));
079: serverVersion = BackupHelper.getStringFromDom(instanceDocument,
080: "/backupInstance/@serverVersion");
081: }
082:
083: public void addEntry(BackupEntry entry) {
084: entries.add(entry);
085: }
086:
087: public BackupEntry getEntry(int index) {
088: return entries.get(index);
089: }
090:
091: public int entryCount() {
092: return entries.size();
093: }
094:
095: public void backup() {
096:
097: }
098:
099: public void restore() {
100:
101: }
102:
103: public File getDirectory() {
104: return directory;
105: }
106:
107: public void setDirectory(File directory) {
108: changed = true;
109: this .directory = directory;
110: }
111:
112: public Date getCreateDate() {
113: return createDate;
114: }
115:
116: public String getName() {
117: return name;
118: }
119:
120: public void setName(String name) {
121: changed = true;
122: this .name = name;
123: }
124:
125: public String getServerVersion() {
126: return serverVersion;
127: }
128:
129: public Document getInstanceDocument() throws Exception {
130: if (changed || instanceDocument == null)
131: instanceDocument = backupToDocument();
132: return instanceDocument;
133: }
134:
135: public File getInstanceFile() {
136: if (instanceFile == null
137: || instanceFile.getParentFile().equals(getDirectory()))
138: instanceFile = new File(getDirectory(), "instance.xml");
139: return instanceFile;
140: }
141:
142: public void invalidate() {
143: changed = true;
144: }
145:
146: private Document backupToDocument() throws Exception {
147: DocumentBuilderFactory dbf = DocumentBuilderFactory
148: .newInstance();
149: DocumentBuilder db = dbf.newDocumentBuilder();
150: Document document = db.newDocument();
151:
152: Element backupElement = document
153: .createElement("backupInstance");
154: document.appendChild(backupElement);
155: backupElement.setAttribute("name", this .getName());
156: backupElement.setAttribute("createDate", dateFormat.format(this
157: .getCreateDate()));
158: backupElement.setAttribute("serverVersion", serverVersion);
159:
160: for (int i = 0; i < this .entryCount(); i++) {
161: AbstractBackupEntry buEntry = (AbstractBackupEntry) this
162: .getEntry(i);
163: if (buEntry.backupFile.exists()) {
164: Element entry = document.createElement("entry");
165: backupElement.appendChild(entry);
166: entry
167: .setAttribute("checksum", buEntry
168: .generateDigest());
169: entry.setAttribute("filename", buEntry.backupFile
170: .getName());
171: }
172: }
173:
174: return document;
175: }
176: }
|