1 6 7 package com.sun.enterprise.config.backup.pluggable.impl; 8 9 import java.io.*; 10 import java.util.*; 11 12 import com.sun.enterprise.util.io.FileUtils; 13 import com.sun.enterprise.config.backup.pluggable.BackupStorage; 14 import com.sun.enterprise.config.backup.utils.FactoryHelper; 15 import com.sun.enterprise.config.backup.utils.LoggerHelper; 16 import com.sun.enterprise.config.backup.BackupException; 17 import com.sun.enterprise.config.backup.DefaultConstants; 18 import com.sun.enterprise.config.backup.status.Status; 19 20 24 abstract class GenericStorage implements BackupStorage 25 { 26 abstract long copyFiles(File fromDir, File toDir) throws BackupException; 27 28 30 public long backup(String [] srcDirs, String targetString) throws BackupException 31 { 32 File target = new File(targetString); 33 34 if(srcDirs == null || srcDirs.length == 0) 36 throw new BackupException("No source directories specified", ""); 37 38 for (int i=0;i<srcDirs.length;i++) 40 if(!FileUtils.safeIsDirectory(srcDirs[i])) 41 throw new BackupException("A specified source directory is NOT a directory: " + srcDirs[i], ""); 42 43 if(!FileUtils.safeIsDirectory(target)) 45 target.mkdirs(); 46 47 if(!FileUtils.safeIsDirectory(target)) 49 throw new BackupException("The target directory is NOT a directory: " + target.getPath(), ""); 50 51 if(!target.canWrite()) 52 throw new BackupException("The target directory is read-only: " + target.getPath(), ""); 53 54 int totalSizeBackedUp = 0; 55 for (int i=0;i<srcDirs.length;i++) 56 { 57 62 try 63 { 64 File srcFile = new File(srcDirs[i]); 65 File currentTarget = new File(target, srcFile.getName()); 66 totalSizeBackedUp += copyFiles(srcFile, currentTarget); 67 } 68 catch(Exception ioe) 69 { 70 throw new BackupException("backup error", "Exception copying " 71 + srcDirs[i] + " to " + target.getPath(), ioe); 72 } 73 } 74 return totalSizeBackedUp; 75 } 76 77 79 public long restore(File file) throws BackupException 80 { 81 if(file == null || !file.exists() || !file.isDirectory()) 82 return -1; 83 84 File[] childrenFiles = getBUSubdirs(file); 85 86 String [] targetDirs = FactoryHelper.getEnv().getDirectoriesToBackup(); 87 88 if(targetDirs == null || targetDirs.length == 0) 89 return -1; 90 91 if(targetDirs.length == 1 && childrenFiles.length == 1) 92 return restoreSingle(childrenFiles[0], new File(targetDirs[0])); 93 else if(targetDirs.length == 1) 94 return restoreMultiToSingle(childrenFiles, new File(targetDirs[0])); 95 else 96 return restoreMultiToMulti(childrenFiles, targetDirs); 97 } 98 99 101 private String getTargetDirToRestore(String [] targetDirs, File child) 102 { 103 for(int i = 0; i < targetDirs.length;i++) 104 { 105 File f = new File(targetDirs[i]); 106 if(f.getName().equals(child.getName())) 107 { 108 return f.getParent(); 109 } 110 } 111 LoggerHelper.warning("no_target_directory_to_restore " + child); 112 return null; 113 } 114 115 117 public void createStatusFile(String target, Status status) 118 { 119 if (status == null) return; 120 121 File f = new File(target, FactoryHelper.getEnv().getStatusInfoFileName()); 122 FileOutputStream fos = null; 123 try 124 { 125 fos = new FileOutputStream(f); 126 fos.write(status.toString().getBytes()); 127 } catch(Exception e) 128 { 129 } finally 131 { 132 try 133 { 134 fos.close(); 135 } catch(Exception ex) 136 {} 137 } 138 } 139 140 142 public void deleteTarget() 143 { 144 String [] targetDirs = FactoryHelper.getEnv().getDirectoriesToBackup(); 145 146 for(int i = 0; i < targetDirs.length; i++) 147 { 148 FileUtils.whack(new File(targetDirs[i])); 149 } 150 } 151 152 154 private File[] pruneNonDirs(File[] files) 155 { 156 if(files == null) 158 return new File[0]; 159 160 ArrayList list = new ArrayList(files.length); 161 162 for(int i = 0; i < files.length; i++) 163 if(files[i].isDirectory()) 164 list.add(files[i]); 165 166 File[] ret = new File[list.size()]; 167 168 if(list.size() == 0) 169 return ret; 170 171 return (File[])(list.toArray(ret)); 172 } 173 174 176 private long restoreSingle(File from, File to) throws BackupException 177 { 178 if(from.getName().equals(to.getName())) 179 { 180 to = to.getParentFile(); 181 } 182 183 try 184 { 185 FileUtils.copyTree(from, to); 186 } 187 catch(IOException ioe) 188 { 189 throw new BackupException("xx", "IOException copying tree -- from: " + from + ", To: " + to, ioe); 190 } 191 192 return 100; 193 } 194 195 197 private long restoreMultiToSingle(File[] from, File to) throws BackupException 198 { 199 try 201 { 202 for(int i = 0; i < from.length; i++) 203 { 204 FileUtils.copyTree(from[i], to); 205 } 206 207 return 100; 208 } 209 catch(IOException ioe) 210 { 211 throw new BackupException("xx", "IOException copying tree -- from: " + from + ", To: " + to, ioe); 212 } 213 } 214 215 217 private long restoreMultiToMulti(File[] from, String [] toStrings) throws BackupException 218 { 219 File[] to = new File[toStrings.length]; 222 223 for(int i = 0; i < toStrings.length; i++) 224 to[i] = new File(toStrings[i]); 225 226 if(to.length != from.length) 227 throw new BackupException("restoreMultiToMulti", "Number of backup-dirs (" + from.length + " doesn't match number of target dirs (" + to.length + ")"); 228 229 int numMatches = 0; 230 231 for(int i = 0; i < to.length; i++) 233 { 234 String toName = to[i].getName(); 235 236 for(int j = 0; j < from.length; j++) 237 { 238 if(toName.equals(from[j].getName())) 239 { 240 ++numMatches; 241 break; 242 } 243 } 244 } 245 246 if(numMatches != to.length) 247 throw new BackupException("restoreMultiToMulti", "backup-dirs don't match up with target dirs (" + to.length + ")"); 248 249 try 250 { 251 for(int i = 0; i < to.length; i++) 252 { 253 String toName = to[i].getName(); 254 255 for(int j = 0; j < from.length; j++) 256 { 257 if(toName.equals(from[j].getName())) 258 { 259 FileUtils.copyTree(from[j], to[i].getParentFile()); 260 break; 261 } 262 } 263 } 264 return 100; 265 } 266 catch(IOException ioe) 267 { 268 throw new BackupException("xxx", "IOException copying tree -- from: " + from + ", To: " + to, ioe); 269 } 270 } 271 272 274 private File[] getBUSubdirs(File root) throws BackupException 275 { 276 File[] files = root.listFiles(); 277 files = pruneNonDirs(files); 278 279 if(files.length == 0) 280 throw new BackupException("getBUSubDirs", "Nothing in backup dir: " + root); 281 282 return files; 283 } 284 } 285 | Popular Tags |