1 23 24 29 30 package com.sun.enterprise.config.backup; 31 32 import com.sun.enterprise.config.backup.util.FileUtils; 33 import java.io.*; 34 import java.util.*; 35 import java.util.zip.*; 36 37 41 class Status 42 { 43 String write(BackupRequest req) 44 { 45 props = new Properties(); 46 request = req; 47 statusFile = new File(request.domainDir, Constants.PROPS_FILENAME); 48 49 try 50 { 51 setProps(); 52 FileOutputStream out = new FileOutputStream(statusFile); 53 props.store(out, Constants.PROPS_HEADER); 54 return propsToString(false); 55 } 56 catch(Exception e) 57 { 58 return StringHelper.get("backup-res.CantWriteStatus", statusFile); 59 } 60 } 61 62 64 69 String read(File file) 70 { 71 return read(file, true); 72 } 73 74 76 82 String read(File file, boolean terse) 83 { 84 props = null; 85 86 setPropsFromFile(file); 87 if(props == null) 88 { 89 return badStatusFileMessage(file); 90 } 91 92 return propsToString(terse); 93 } 94 95 99 100 long getInternalTimestamp(File f) 101 { 102 props = null; 103 setPropsFromFile(f); 104 105 try 106 { 107 String s = props.getProperty(Constants.PROPS_TIMESTAMP_MSEC); 108 return Long.parseLong(s); 109 } 110 catch(Exception e) 111 { 112 LoggerHelper.warning(badStatusFileMessage(f)); 113 return 0; 114 } 115 } 116 117 119 void delete() 120 { 121 if(!statusFile.delete()) 122 { 123 statusFile.deleteOnExit(); 125 } 126 } 127 128 130 String getDomainName() 131 { 132 if(props == null) 133 return null; 134 135 return props.getProperty(Constants.PROPS_DOMAIN_NAME); 136 } 137 138 142 148 private void setPropsFromFile(File file) 149 { 150 props = null; 151 ZipInputStream zis = null; 152 153 154 if(file.getName().toLowerCase().endsWith(".properties")) 155 { 156 readPropertiesFile(file); 157 return; 159 } 160 161 try 162 { 163 zis = new ZipInputStream(new FileInputStream(file)); 164 ZipEntry ze; 165 166 while( (ze = zis.getNextEntry()) != null ) 167 { 168 if(ze.getName().equals(Constants.PROPS_FILENAME)) 169 { 170 props = new Properties(); 171 props.load(zis); 172 break; 173 } 174 } 175 } 177 catch(Exception e) 178 { 179 props = null; 181 } 182 finally 183 { 184 if(zis != null) 185 { 186 try 187 { 188 zis.close(); 189 } 190 catch(Exception e) 191 { 192 } 193 } 194 } 195 } 196 197 199 private void readPropertiesFile(File propsFile) 200 { 201 try 202 { 203 BufferedInputStream in = new BufferedInputStream(new FileInputStream(propsFile)); 204 props = new Properties(); 205 props.load(in); 206 in.close(); 207 } 208 catch(IOException ioe) 209 { 210 props = null; 211 } 212 } 213 214 216 private void setProps() 217 { 218 props.setProperty(Constants.PROPS_USER_NAME, System.getProperty(Constants.PROPS_USER_NAME)); 219 props.setProperty(Constants.PROPS_TIMESTAMP_MSEC, "" + request.timestamp); 220 props.setProperty(Constants.PROPS_DOMAINS_DIR, FileUtils.safeGetCanonicalPath(request.domainsDir)); 221 props.setProperty(Constants.PROPS_DOMAIN_DIR, FileUtils.safeGetCanonicalPath(request.domainDir)); 222 props.setProperty(Constants.PROPS_BACKUP_FILE, FileUtils.safeGetCanonicalPath(request.backupFile)); 223 props.setProperty(Constants.PROPS_DOMAIN_NAME, request.domainName); 224 props.setProperty(Constants.PROPS_DESCRIPTION, request.description); 225 props.setProperty(Constants.PROPS_TIMESTAMP_HUMAN, new Date(request.timestamp).toString()); 226 } 227 228 230 private String propsToString(boolean terse) 231 { 232 final String pre = "backup-res.Props."; 233 StringBuffer sb = new StringBuffer (); 234 235 236 if(terse) 237 { 238 sb.append(props.getProperty(Constants.PROPS_BACKUP_FILE)); 239 } 240 241 else 242 { 243 sb.append(StringHelper.get(pre + Constants.PROPS_DESCRIPTION, props.getProperty(Constants.PROPS_DESCRIPTION))); 244 sb.append("\n"); 245 sb.append(StringHelper.get(pre + Constants.PROPS_BACKUP_FILE, props.getProperty(Constants.PROPS_BACKUP_FILE))); 246 sb.append("\n"); 247 sb.append(StringHelper.get(pre + Constants.PROPS_TIMESTAMP_HUMAN, props.getProperty(Constants.PROPS_TIMESTAMP_HUMAN))); 248 sb.append("\n"); 249 sb.append(StringHelper.get(pre + Constants.PROPS_DOMAINS_DIR, props.getProperty(Constants.PROPS_DOMAINS_DIR))); 250 sb.append("\n"); 251 sb.append(StringHelper.get(pre + Constants.PROPS_DOMAIN_DIR, props.getProperty(Constants.PROPS_DOMAIN_DIR))); 252 sb.append("\n"); 253 sb.append(StringHelper.get(pre + Constants.PROPS_DOMAIN_NAME, props.getProperty(Constants.PROPS_DOMAIN_NAME))); 254 sb.append("\n"); 255 sb.append(StringHelper.get(pre + Constants.PROPS_USER_NAME, props.getProperty(Constants.PROPS_USER_NAME))); 256 } 257 258 return sb.toString(); 259 } 260 261 263 private String badStatusFileMessage(File file) 264 { 265 String msg = StringHelper.get("backup-res.Props.backup.file", file); 266 msg += "\n"; 267 msg += StringHelper.get("backup-res.CorruptBackupFile.NoStatusFile"); 268 return msg; 269 } 270 271 273 private BackupRequest request; 274 private File statusFile; 275 private Properties props; 276 } 277 278 279 | Popular Tags |