00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.controller.backup;
00026
00027 import java.io.File;
00028 import java.io.FilenameFilter;
00029 import java.util.ArrayList;
00030 import java.util.Hashtable;
00031
00032 import org.objectweb.cjdbc.common.exceptions.BackupException;
00033 import org.objectweb.cjdbc.common.exceptions.OctopusException;
00034 import org.objectweb.cjdbc.common.i18n.Translate;
00035 import org.objectweb.cjdbc.common.log.Trace;
00036 import org.objectweb.cjdbc.common.shared.BackupListener;
00037 import org.objectweb.cjdbc.common.util.Constants;
00038 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00039 import org.objectweb.cjdbc.common.xml.XmlComponent;
00040 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
00041 import org.objectweb.cjdbc.controller.core.ControllerConstants;
00042
00043
00044
00045
00046
00047
00048
00049
00050 public class BackupManager implements XmlComponent
00051 {
00052 static Trace logger = Trace.getLogger(BackupManager.class
00053 .getName());
00054 private String backupDir = ControllerConstants.DEFAULT_BACKUP_DIR;
00055 private boolean cleanBackupFiles = true;
00056 private boolean zipBackupFiles = true;
00057 private int numberOfBackups = -1;
00058
00059 private Hashtable threads;
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 public BackupManager(String backupDir, boolean cleanBackupFiles,
00070 boolean zipBackupFiles, int numberOfBackups)
00071 {
00072 this();
00073 this.backupDir = backupDir;
00074 this.cleanBackupFiles = cleanBackupFiles;
00075 this.zipBackupFiles = zipBackupFiles;
00076 this.numberOfBackups = numberOfBackups;
00077
00078 }
00079
00080
00081
00082
00083 public BackupManager()
00084 {
00085 threads = new Hashtable();
00086 }
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 public void backup(DatabaseBackend backend, String checkpoint,
00098 ArrayList tables, BackupListener listener) throws BackupException
00099 {
00100 logger.info(Translate.get("backup.manager.backuping.backend", new String[]{
00101 backend.getName(), checkpoint}));
00102 Octopus octopus = new Octopus(backend, checkpoint, tables);
00103 octopus.setZipOctopus(zipBackupFiles);
00104 octopus.setCleanOctopus(cleanBackupFiles);
00105 octopus.setOctopusDirectory(backupDir);
00106 octopus.setOctopusMode(Octopus.MODE_BACKUP);
00107 if (listener != null)
00108 octopus.setListener(listener);
00109 threads.put(backend, octopus);
00110 octopus.start();
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 public synchronized void getResult(DatabaseBackend backend, long waitTime)
00122 throws BackupException, OctopusException
00123 {
00124 logger.info(Translate.get("backup.manager.waiting.backend.result",
00125 new String[]{backend.getName(), String.valueOf((waitTime / 1000))}));
00126 Octopus backup = (Octopus) threads.get(backend);
00127 try
00128 {
00129 backup.join(waitTime);
00130 }
00131 catch (InterruptedException e)
00132 {
00133 logger.error("Interrupted while joining on backend:" + backend.getName(),
00134 e);
00135 }
00136 Exception e = backup.getRunException();
00137 if (e != null)
00138 {
00139 if (e instanceof BackupException)
00140 throw (BackupException) e;
00141 else if (e instanceof OctopusException)
00142 throw (OctopusException) e;
00143 }
00144
00145 }
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156 public void restore(DatabaseBackend backend, String checkpoint,
00157 ArrayList tables, BackupListener listener) throws BackupException
00158 {
00159 logger.info(Translate.get("backup.manager.restoring.backend", new String[]{
00160 backend.getName(), checkpoint}));
00161 Octopus octopus = new Octopus(backend, checkpoint, tables);
00162 octopus.setZipOctopus(zipBackupFiles);
00163 octopus.setCleanOctopus(cleanBackupFiles);
00164 octopus.setOctopusDirectory(backupDir);
00165 if (listener != null)
00166 octopus.setListener(listener);
00167 octopus.setOctopusMode(Octopus.MODE_RECOVERY);
00168 threads.put(backend, octopus);
00169 octopus.start();
00170 }
00171
00172
00173
00174
00175
00176
00177
00178 public File[] listAvailableDumpFiles()
00179 {
00180 File f = new File(backupDir);
00181 if (f.exists())
00182 {
00183 return f.listFiles(new FilenameFilter()
00184 {
00185
00186
00187
00188 public boolean accept(File dir, String name)
00189 {
00190 if (name.endsWith(Constants.ZIP_EXT))
00191 return true;
00192 else
00193 return false;
00194 }
00195 });
00196 }
00197 else
00198 return new File[0];
00199 }
00200
00201
00202
00203
00204
00205
00206 public String getBackupDir()
00207 {
00208 return backupDir;
00209 }
00210
00211
00212
00213
00214
00215
00216 public void setBackupDir(String backupDir)
00217 {
00218 this.backupDir = backupDir;
00219 }
00220
00221
00222
00223
00224
00225
00226 public boolean isCleanBackupFiles()
00227 {
00228 return cleanBackupFiles;
00229 }
00230
00231
00232
00233
00234
00235
00236 public void setCleanBackupFiles(boolean cleanBackupFiles)
00237 {
00238 this.cleanBackupFiles = cleanBackupFiles;
00239 }
00240
00241
00242
00243
00244
00245
00246 public int getNumberOfBackups()
00247 {
00248 return numberOfBackups;
00249 }
00250
00251
00252
00253
00254
00255
00256 public void setNumberOfBackups(int numberOfBackups)
00257 {
00258 this.numberOfBackups = numberOfBackups;
00259 }
00260
00261
00262
00263
00264
00265
00266 public boolean isZipBackupFiles()
00267 {
00268 return zipBackupFiles;
00269 }
00270
00271
00272
00273
00274
00275
00276 public void setZipBackupFiles(boolean zipBackupFiles)
00277 {
00278 this.zipBackupFiles = zipBackupFiles;
00279 }
00280
00281
00282
00283
00284 public String getXml()
00285 {
00286 return "<" + DatabasesXmlTags.ELT_Backup + " " + DatabasesXmlTags.ATT_dir
00287 + "=\"" + backupDir + "\" " + DatabasesXmlTags.ATT_clean + "=\""
00288 + cleanBackupFiles + "\" " + DatabasesXmlTags.ATT_zip + "=\""
00289 + zipBackupFiles + "\" " + "/>";
00290 }
00291 }