KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
20 import java.net.Socket JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 public class BackupManager {
25     private static BackupManager instance;
26
27     private static JMXRepositoryLocker locker;
28
29     private static File JavaDoc backupLocation;
30
31     private List JavaDoc backupInstances;
32
33     private List JavaDoc entryLoaders;
34
35     private boolean isRepositoryLocked;
36
37     private BackupManager(File JavaDoc backupLocation, JMXRepositoryLocker locker) throws Exception JavaDoc {
38         if (backupLocation != null && backupLocation.exists())
39             BackupManager.backupLocation = backupLocation;
40         else
41             throw new Exception JavaDoc("Backuplocation is not set or does not exist");
42
43         if (locker != null)
44             BackupManager.locker = locker;
45         else
46             throw new Exception JavaDoc("Repository locker is not set");
47
48         backupInstances = new ArrayList JavaDoc();
49         entryLoaders = new ArrayList JavaDoc();
50     }
51
52     public static BackupManager getInstance() throws Exception JavaDoc {
53         if (instance == null) {
54             instance = new BackupManager(BackupManager.backupLocation, BackupManager.locker);
55         }
56         return instance;
57     }
58
59     public BackupInstance createBackupInstance() throws Exception JavaDoc {
60         BackupInstance buInstance = new BackupInstance(backupLocation);
61         for (int i = 0; i < entryLoaders.size(); i++) {
62             ((EntryLoader) entryLoaders.get(i)).createEntries(buInstance);
63         }
64         backupInstances.add(buInstance);
65         return buInstance;
66     }
67
68     public BackupInstance loadBackupInstance(String JavaDoc backupName) throws Exception JavaDoc {
69         BackupInstance buInstance = new BackupInstance(backupLocation, backupName);
70
71         for (int i = 0; i < entryLoaders.size(); i++) {
72             ((EntryLoader) entryLoaders.get(i)).reloadEntries(buInstance);
73         }
74
75         backupInstances.add(buInstance);
76         return buInstance;
77     }
78     
79     public void rehash(BackupInstance buInstance) throws Exception JavaDoc {
80         buInstance.invalidate();
81         BackupHelper.saveDocument(buInstance.getInstanceFile(), buInstance.getInstanceDocument());
82     }
83
84     public void registerEntryLoader(EntryLoader el) {
85         entryLoaders.add(el);
86     }
87
88     public static File JavaDoc getBackupLocation() {
89         return backupLocation;
90     }
91
92     public static void setBackupLocation(File JavaDoc backupLocation) {
93         BackupManager.backupLocation = backupLocation;
94     }
95
96     public static JMXRepositoryLocker getLocker() {
97         return locker;
98     }
99
100     public static void setLocker(JMXRepositoryLocker locker) {
101         BackupManager.locker = locker;
102     }
103
104     public void backup(BackupInstance buInstance) throws Exception JavaDoc {
105         for (int i = 0; i < buInstance.entryCount(); i++) {
106             buInstance.getEntry(i).backupInit();
107         }
108         try {
109             if (isRepositoryOn()) {
110                 System.out.println("Locking repository ...");
111                 locker.lock();
112                 isRepositoryLocked = locker.isLocked();
113             } else {
114                 throw new Exception JavaDoc("Could not connect to repository. Please turn on the repository server and try backing up again");
115             }
116
117             System.out.println("Copying data to backup destination...");
118             for (int i = 0; i < buInstance.entryCount(); i++) {
119                 buInstance.getEntry(i).backup();
120             }
121
122         } finally {
123             if (isRepositoryLocked) {
124                 System.out.println("Unlocking repository ...");
125                 locker.unlock();
126             }
127         }
128
129         System.out.println("Post-processing backup...");
130         for (int i = 0; i < buInstance.entryCount(); i++) {
131             buInstance.getEntry(i).backupCleanup();
132         }
133
134         BackupHelper.saveDocument(buInstance.getInstanceFile(), buInstance.getInstanceDocument());
135         System.out.println("Done.");
136     }
137
138     public void restore(BackupInstance buInstance) throws Exception JavaDoc {
139         if (!isRepositoryOn()) {
140             System.out.println("Restoring backup-" + buInstance.getName() + " ...");
141             for (int i = 0; i < buInstance.entryCount(); i++) {
142                 buInstance.getEntry(i).restore();
143             }
144             System.out.println("Restored backup-" + buInstance.getName() + ".");
145         } else {
146             throw new Exception JavaDoc("Repository server is running. Please shut it down before restoring a backup.");
147         }
148     }
149
150     private boolean isRepositoryOn() throws Exception JavaDoc {
151         boolean isOn;
152         try {
153             Socket JavaDoc socket = new Socket JavaDoc(InetAddress.getByName(locker.getRepositoryHost()), locker.getRepositoryPort());
154             socket.close();
155             isOn = true;
156         } catch (Exception JavaDoc e) {
157             isOn = false;
158         }
159         return isOn;
160     }
161 }
162
Popular Tags