1 23 24 package org.objectweb.cjdbc.controller.backup; 25 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 30 import org.objectweb.cjdbc.common.exceptions.BackupException; 31 import org.objectweb.cjdbc.common.log.Trace; 32 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 33 import org.objectweb.cjdbc.common.xml.XmlComponent; 34 35 42 public class BackupManager implements XmlComponent 43 { 44 static Trace logger = Trace.getLogger(BackupManager.class.getName()); 45 46 49 private HashMap backupers; 50 51 54 public BackupManager() 55 { 56 backupers = new HashMap (); 57 } 58 59 66 public synchronized Backuper getBackuperByName(String name) 67 { 68 return (Backuper) backupers.get(name); 69 } 70 71 78 public synchronized String [] getBackuperNames() 79 { 80 Set backuperNames = backupers.keySet(); 81 return (String []) backuperNames.toArray(new String [backuperNames.size()]); 82 } 83 84 91 public synchronized Backuper getBackuperByFormat(String format) 92 { 93 if (format == null) 94 return null; 95 for (Iterator iter = backupers.values().iterator(); iter.hasNext();) 96 { 97 Backuper b = (Backuper) iter.next(); 98 if (format.equals(b.getDumpFormat())) 99 return b; 100 } 101 return null; 102 } 103 104 112 public synchronized void registerBackuper(String name, Backuper backuper) 113 throws BackupException 114 { 115 if (backupers.containsKey(name)) 116 throw new BackupException( 117 "A backuper has already been registered with name " + name); 118 if (backuper == null) 119 throw new BackupException( 120 "Trying to register a null backuper under name " + name); 121 122 if (logger.isInfoEnabled()) 123 logger.info("Registering backuper " + name + " to handle format " 124 + backuper.getDumpFormat()); 125 126 backupers.put(name, backuper); 127 } 128 129 136 public synchronized boolean unregisterBackuper(String name) 137 { 138 Object backuper = backupers.remove(name); 139 140 if (logger.isInfoEnabled() && (backuper != null)) 141 logger.info("Unregistering backuper " + name + " that handled format " 142 + ((Backuper) backuper).getDumpFormat()); 143 144 return backuper != null; 145 } 146 147 150 public synchronized String getXml() 151 { 152 StringBuffer sb = new StringBuffer ("<" + DatabasesXmlTags.ELT_Backup + "> "); 153 for (Iterator iter = backupers.keySet().iterator(); iter.hasNext();) 154 { 155 String backuperName = (String ) iter.next(); 156 Backuper b = (Backuper) backupers.get(backuperName); 157 sb.append("<" + DatabasesXmlTags.ELT_Backuper + " " 158 + DatabasesXmlTags.ATT_backuperName + "=\"" + backuperName + "\" " 159 + DatabasesXmlTags.ATT_className + "=\"" + b.getClass() + "\" " 160 + DatabasesXmlTags.ATT_options + "=\"" + b.getOptions() + "\" />"); 161 } 162 sb.append("</" + DatabasesXmlTags.ELT_Backup + ">"); 163 return sb.toString(); 164 } 165 } | Popular Tags |