1 23 24 package org.objectweb.cjdbc.controller.backup; 25 26 import java.io.File ; 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 30 import org.objectweb.cjdbc.common.exceptions.BackupException; 31 import org.objectweb.cjdbc.common.log.Trace; 32 import org.objectweb.cjdbc.common.util.FileManagement; 33 import org.objectweb.cjdbc.common.util.Zipper; 34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend; 35 36 47 public class DerbyEmbeddedBackuper implements Backuper 48 { 49 static Trace logger = Trace.getLogger(DerbyEmbeddedBackuper.class.getName()); 50 51 54 public DerbyEmbeddedBackuper() 55 { 56 } 57 58 61 public String getDumpFormat() 62 { 63 return "Derby embedded compressed dump"; 64 } 65 66 69 public String getOptions() 70 { 71 return null; 72 } 73 74 77 public void setOptions(String options) 78 { 79 } 81 82 87 public Date backup(DatabaseBackend backend, String login, String password, 88 String dumpName, String path, ArrayList tables) throws BackupException 89 { 90 String derbyPath = getDerbyPath(backend, true); 91 92 try 93 { 94 File pathDir = new File (path); 95 if (!pathDir.exists()) 96 { 97 pathDir.mkdirs(); 98 pathDir.mkdir(); 99 } 100 101 if (logger.isDebugEnabled()) 102 logger.debug("Archiving " + derbyPath + " in " + path + File.separator 103 + dumpName + Zipper.ZIP_EXT); 104 105 Zipper.zip(getDumpPhysicalPath(path, dumpName), derbyPath, 106 Zipper.STORE_PATH_FROM_ZIP_ROOT); 107 } 108 catch (Exception e) 109 { 110 String msg = "Error while performing backup"; 111 logger.error(msg, e); 112 throw new BackupException(msg, e); 113 } 114 115 return new Date (System.currentTimeMillis()); 116 } 117 118 123 public void restore(DatabaseBackend backend, String login, String password, 124 String dumpName, String path, ArrayList tables) throws BackupException 125 { 126 String derbyPath = getDerbyPath(backend, false); 127 128 File derbyDir = new File (derbyPath); 129 130 if (FileManagement.deleteDir(derbyDir)) 132 logger.info("Existing Derby directory " + derbyPath 133 + " has been deleted."); 134 135 derbyDir.mkdirs(); 137 derbyDir.mkdir(); 138 139 try 141 { 142 if (logger.isDebugEnabled()) 143 logger.debug("Uncompressing dump"); 144 Zipper.unzip(getDumpPhysicalPath(path, dumpName), derbyPath); 145 } 146 catch (Exception e) 147 { 148 String msg = "Error while uncompressing dump"; 149 logger.error(msg, e); 150 throw new BackupException(msg, e); 151 } 152 } 153 154 158 public void deleteDump(String path, String dumpName) throws BackupException 159 { 160 File toRemove = new File (getDumpPhysicalPath(path, dumpName)); 161 if (logger.isDebugEnabled()) 162 logger.debug("Deleting compressed dump " + toRemove); 163 toRemove.delete(); 164 } 165 166 173 private String getDumpPhysicalPath(String path, String dumpName) 174 { 175 return path + File.separator + dumpName + Zipper.ZIP_EXT; 176 } 177 178 187 private String getDerbyPath(DatabaseBackend backend, boolean checkPath) 188 throws BackupException 189 { 190 String url = backend.getURL(); 191 if (!url.startsWith("jdbc:derby:")) 192 throw new BackupException("Unsupported url " + url 193 + " expecting jdbc:derby:pathToDb"); 194 195 String derbyPath = url.substring(11); 198 int semicolon = derbyPath.indexOf(';'); 200 if (semicolon > -1) 201 derbyPath = derbyPath.substring(0, semicolon); 202 203 if (checkPath) 204 { 205 File checkDerbyPath = new File (derbyPath); 206 if (!checkDerbyPath.isDirectory()) 207 throw new BackupException( 208 "Directory " 209 + derbyPath 210 + " does not exist. This might be due to an unsupported URL format (expectin jdbc:derby:pathToDb)"); 211 } 212 213 return derbyPath; 214 } 215 216 220 public void fetchDump(DumpTransferInfo dumpTransferInfo, String path, 221 String dumpName) throws BackupException 222 { 223 } 227 228 231 public DumpTransferInfo setupServer() 232 { 233 return null; 235 } 236 237 240 public BackupManager getBackupManager() 241 { 242 return null; 244 } 246 247 } 248 | Popular Tags |