Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

BackupManager.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): Emmanuel Cecchet.
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  * This class defines a BackupManager
00045  * 
00046  * @author <a href="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk </a>
00047  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
00048  * @version 1.0
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    * Creates a new <code>BackupManager</code> object
00063    * 
00064    * @param backupDir the directory used for backup
00065    * @param cleanBackupFiles should we clean temp backup files
00066    * @param zipBackupFiles should we zip backup files
00067    * @param numberOfBackups number of backups before deleting others, not used
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    * Creates a new <code>BackupManager</code> object
00082    */
00083   public BackupManager()
00084   {
00085     threads = new Hashtable();
00086   }
00087 
00088   /**
00089    * Create a backup from the content of a backend
00090    * 
00091    * @param backend the target backend to backup from
00092    * @param checkpoint the checkpoint name of the backup to create
00093    * @param tables the list of tables to consider for backup
00094    * @param listener handback object to notify
00095    * @throws BackupException if backup fails for unknown reasons
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    * Get the result of the backup/recovery process for the given backend
00115    * 
00116    * @param backend the backend we started a backup recovery process on
00117    * @param waitTime the time to wait to join the octopus thread
00118    * @throws BackupException if backup fails for unknown reasons
00119    * @throws OctopusException if backup fails because of Octopus
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    * Restore the content of a backup onto a specific backend
00149    * 
00150    * @param backend the target backend to restore to
00151    * @param checkpoint the checkpoint name of the backup to restore
00152    * @param tables the list of tables to consider for restore
00153    * @param listener handback object to notify
00154    * @throws BackupException if backup fails for unknown reasons
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    * List all the files this backup manager has access to.
00174    * 
00175    * @return a <code>File[]</code> object that can be empty, but cannot be
00176    *         null
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          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
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    * Returns the backupDir value.
00203    * 
00204    * @return Returns the backupDir.
00205    */
00206   public String getBackupDir()
00207   {
00208     return backupDir;
00209   }
00210 
00211   /**
00212    * Sets the backupDir value.
00213    * 
00214    * @param backupDir The backupDir to set.
00215    */
00216   public void setBackupDir(String backupDir)
00217   {
00218     this.backupDir = backupDir;
00219   }
00220 
00221   /**
00222    * Returns the cleanBackupFiles value.
00223    * 
00224    * @return Returns the cleanBackupFiles.
00225    */
00226   public boolean isCleanBackupFiles()
00227   {
00228     return cleanBackupFiles;
00229   }
00230 
00231   /**
00232    * Sets the cleanBackupFiles value.
00233    * 
00234    * @param cleanBackupFiles The cleanBackupFiles to set.
00235    */
00236   public void setCleanBackupFiles(boolean cleanBackupFiles)
00237   {
00238     this.cleanBackupFiles = cleanBackupFiles;
00239   }
00240 
00241   /**
00242    * Returns the numberOfBackups value.
00243    * 
00244    * @return Returns the numberOfBackups.
00245    */
00246   public int getNumberOfBackups()
00247   {
00248     return numberOfBackups;
00249   }
00250 
00251   /**
00252    * Sets the numberOfBackups value.
00253    * 
00254    * @param numberOfBackups The numberOfBackups to set.
00255    */
00256   public void setNumberOfBackups(int numberOfBackups)
00257   {
00258     this.numberOfBackups = numberOfBackups;
00259   }
00260 
00261   /**
00262    * Returns the zipBackupFiles value.
00263    * 
00264    * @return Returns the zipBackupFiles.
00265    */
00266   public boolean isZipBackupFiles()
00267   {
00268     return zipBackupFiles;
00269   }
00270 
00271   /**
00272    * Sets the zipBackupFiles value.
00273    * 
00274    * @param zipBackupFiles The zipBackupFiles to set.
00275    */
00276   public void setZipBackupFiles(boolean zipBackupFiles)
00277   {
00278     this.zipBackupFiles = zipBackupFiles;
00279   }
00280 
00281   /**
00282    * @see org.objectweb.cjdbc.common.xml.XmlComponent#getXml()
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 }

Generated on Mon Apr 11 22:01:29 2005 for C-JDBC by  doxygen 1.3.9.1