1 23 24 29 30 package com.sun.enterprise.config.backup; 31 32 import java.io.*; 33 34 38 class BackupFilenameManager 39 { 40 BackupFilenameManager(File backupDir) throws BackupException 41 { 42 this.dir = backupDir; 43 findZips(); 44 findLatest(); 45 } 46 47 49 File next() throws BackupException 50 { 51 int newVersionNum = 1; 52 53 if(latestVersion != null) 54 newVersionNum = latestVersion.num + 1; 55 56 String suffix = padWithLeadingZeroes(newVersionNum); 57 return new File(dir, Constants.BACKUP_FILENAME_ROOT + suffix + ".zip"); 58 } 59 60 62 File latest() throws BackupWarningException 63 { 64 if(latestVersion == null) 65 throw new BackupWarningException("backup-res.NoBackupFiles", dir); 66 67 return latestVersion.zip; 68 } 69 70 72 76 private void findZips() throws BackupWarningException 77 { 78 File[] zips = dir.listFiles(new ZipFilenameFilter()); 79 int len = 0; 80 81 if(zips != null) 82 len = zips.length; 83 84 zipsAndNumbers = new ZipFileAndNumber[len]; 85 86 for(int i = 0; i < len; i++) 87 { 88 zipsAndNumbers[i] = new ZipFileAndNumber(zips[i]); 89 } 90 } 91 92 94 96 private void findLatest() throws BackupWarningException 97 { 98 int biggest = 0; 99 100 for(int i = 0; i < zipsAndNumbers.length; i++) 101 { 102 int curr = zipsAndNumbers[i].num; 103 104 if(curr > biggest) 105 { 106 biggest = curr; 107 latestVersion = zipsAndNumbers[i]; 108 LoggerHelper.fine(zipsAndNumbers[i].zip.toString() + " newest backup so far..."); 109 } 110 } 111 } 112 113 115 117 private String padWithLeadingZeroes(int num) throws BackupException 118 { 119 if(num < 10) 120 return "0000" + num; 121 122 if(num < 100) 123 return "000" + num; 124 125 if(num < 1000) 126 return "00" + num; 127 128 if(num < 10000) 129 return "0" + num; 130 131 if(num < 100000) 132 return "" + num; 133 134 throw new BackupException("Latest version >= 100,000. Delete some backup files."); 135 } 136 137 139 private static class ZipFileAndNumber 140 { 141 private ZipFileAndNumber(File zip) 142 { 143 this.zip = zip; 144 String fname = zip.getName(); 145 146 if(isValid()) 147 { 148 149 fname = fname.substring(Constants.BACKUP_FILENAME_ROOT.length(), fname.length() - 4); 150 try 151 { 152 num = Integer.parseInt(fname); 153 } 154 catch(Exception e) 155 { 156 } 158 } 159 } 160 161 166 167 private boolean isValid() 168 { 169 Status status = new Status(); 170 long time = status.getInternalTimestamp(zip); 171 172 return time > 0 && zip.getName().startsWith(Constants.BACKUP_FILENAME_ROOT); 173 } 174 175 private File zip; 176 private int num = -1; 177 } 178 179 181 private File dir; 182 private ZipFileAndNumber[] zipsAndNumbers; 183 private int[] numbers; 184 private ZipFileAndNumber latestVersion; 185 186 188 189 public static void main(String [] args) 190 { 191 try 192 { 193 File f = new File("c:/tmp/test"); 194 BackupFilenameManager mgr = new BackupFilenameManager(f); 195 File fnew = mgr.next(); 196 System.out.println("Next backup file: " + fnew); 197 File fold = mgr.latest(); 198 System.out.println("Latest backup file: " + fold); 199 } 200 catch(Exception e) 201 { 202 e.printStackTrace(); 203 } 204 } 205 } 206 | Popular Tags |