1 6 7 package com.sun.enterprise.config.backup.utils; 8 9 import com.sun.enterprise.config.backup.status.BackupStatus; 11 import com.sun.enterprise.config.backup.status.RestoreStatus; 12 import com.sun.enterprise.config.backup.status.UndoRestoreStatus; 13 14 15 import com.sun.enterprise.config.backup.status.Status; 16 import com.sun.enterprise.config.backup.status.StatusConstants; 17 import com.sun.enterprise.config.backup.pluggable.BackupEnvironment; 18 import com.sun.enterprise.config.backup.pluggable.EnvironmentFactory; 19 import com.sun.enterprise.config.backup.pluggable.BackupSynchronizer; 20 import com.sun.enterprise.config.backup.pluggable.BackupStorage; 21 import com.sun.enterprise.config.backup.HistoryManager; 22 import com.sun.enterprise.config.backup.EnvironmentConstants; 23 import com.sun.enterprise.config.backup.DefaultConstants; 24 import com.sun.enterprise.config.backup.BackupException; 25 import com.sun.enterprise.config.backup.BackupStorageMonitor; 26 import com.sun.enterprise.config.backup.FeatureFactory; 27 import com.sun.enterprise.config.backup.utils.SystemPropsHelper; 28 import com.sun.enterprise.util.io.FileUtils; 29 30 import java.io.File ; 31 35 public class BackupHelper implements EnvironmentConstants { 36 37 38 public BackupHelper() { 39 } 40 41 public static void setExecutionTypeInEnvironment(String type) { 42 SystemPropsHelper.setProperty(EXECUTION_TYPE, type); 43 } 44 45 public static String getExecutionTypeFromEnvironment() { 46 return SystemPropsHelper.getProperty(EXECUTION_TYPE); 47 } 48 49 60 61 public static String getAbsoluteBackupFileName(long timestamp) { 62 String fileName = getRelativeBackupFileName(timestamp); 63 return getAbsoluteBackupFileName(fileName); 64 } 65 66 private static String getRelativeBackupFileName(long timestamp) { 67 String prefix = FactoryHelper.getEnv().getBackupFileNamePrefix(); 68 return constructRelativeName(prefix, timestamp); 69 } 70 71 private static String constructRelativeName(String prefix, long timestamp) { 72 return prefix + 73 DefaultConstants.INTRA_FILENAME_SEPARATOR + 74 timestamp; 75 } 76 77 public static String getRelativeSnapShotFileName(long timestamp) 78 throws BackupException { 79 80 String prefix = FactoryHelper.getEnv().getSnapshotFileNamePrefix(); 81 return constructRelativeName(prefix, timestamp); 82 } 83 84 87 public static void setExceptionInStatus(Status bs, Exception e) { 88 try { 89 bs.setStatusCode(StatusConstants.STATUS_FAILURE); 90 if(bs.getException() == null) { 91 bs.setException(e); 92 } 93 } catch(Exception ex) { 94 } 96 } 97 public static String ArrayToString(String [] arr) { 98 String res = ""; 99 if (arr == null || arr.length == 0) return res; 100 101 for (int i = 0; i < arr.length ; i++ ) { 102 if(res != "") res+=", "; 103 res += arr[i]; 104 } 105 return res; 106 } 107 108 public static BackupStatus createBackupStatus(String userInfo, boolean init) { 109 BackupStatus backupStatus = null; 110 try { 111 backupStatus = new BackupStatus(userInfo, init); 112 } catch (Exception e) { 113 LoggerHelper.warning("error_creating_backup_status"); 114 BackupStatus bs = new BackupStatus(); 115 BackupHelper.setExceptionInStatus(bs, e); 116 return bs; 117 } 118 return backupStatus; 119 } 120 121 public static String getAbsoluteBackupFileName(BackupStatus bs) { 122 String targetFile = null; 123 try { 124 long timestamp = bs.getCreationTimeStamp(); 125 targetFile = getAbsoluteBackupFileName(timestamp); 126 } catch (Exception e) { 127 LoggerHelper.warning("error_getting_backup_filename"); 128 BackupHelper.setExceptionInStatus(bs, e); 129 } 130 131 132 133 return targetFile; 134 } 135 136 public static void addHistoryEntrySafe(Status status) { 137 try { 138 FactoryHelper.getHistoryManager().addHistoryEntry(status); 139 } catch(Exception ex1) { 140 LoggerHelper.warning("error_adding_history_entry"); 142 LoggerHelper.fine("error_adding_history_entry", ex1); 143 } 144 } 145 146 public static void releaseLockSafe() { 147 try { 148 FactoryHelper.getSynchronizer().release(); 149 } catch(Exception ex1) { 150 LoggerHelper.warning("error_releasing_lock"); 152 LoggerHelper.fine("error_releasing_lock", ex1); 153 } 154 } 155 156 public static String getAbsoluteBackupFileName(String relativeName) 157 { if(isAbsolute(relativeName)) { 159 return relativeName; 160 } 161 String dir = FactoryHelper.getEnv().getDirectoryToStoreBackupFiles(); 162 File parent = new File (dir); 163 File ourfile = new File (parent, relativeName); 164 return FileUtils.safeGetCanonicalPath(ourfile); 165 } 166 167 private static boolean isAbsolute(String name) { 168 return new File (name).isAbsolute(); 169 } 170 171 public static File getLastBackupFile() { 173 File f = null; 174 try { 175 f = FactoryHelper.getBackupStorageMonitor().getLatestFile(); 176 } catch(Exception e) { 177 } 179 return f; 180 } 181 182 public static File getLastSnapShotFile() { 184 File f = null; 185 try { 186 f = FactoryHelper.getSnapShotStorageMonitor().getLatestFile(); 187 } catch(Exception e) { 188 } 190 return f; 191 } 192 193 194 public static RestoreStatus createRestoreStatus(File f) { 195 196 if(f==null || !f.exists()) { 197 RestoreStatus rs1 = new RestoreStatus(); 198 setExceptionInStatus(rs1, 199 new BackupException( 200 "missing_backup_files", 201 LocalStringsHelper.getString("missing_backup_files", f))); 202 return rs1; 203 } 204 RestoreStatus rs = null; 205 try { 206 rs = new RestoreStatus(f.getAbsolutePath()); 207 } catch(Exception e) { 208 RestoreStatus rs1 = new RestoreStatus(); 209 setExceptionInStatus(rs1, 210 new BackupException( 211 "error_creating_restore_status", 212 LocalStringsHelper.getString("error_creating_restore_status"))); 213 return rs1; 214 } 215 return rs; 216 } 217 218 219 public static UndoRestoreStatus createUndoRestoreStatus(File f) { 220 221 if(f==null || !f.exists()) { 222 UndoRestoreStatus rs1 = new UndoRestoreStatus(); 223 setExceptionInStatus(rs1, 224 new BackupException( 225 "missing_snapshot_file", 226 LocalStringsHelper.getString("missing_snapshot_file",f))); 227 return rs1; 228 } 229 UndoRestoreStatus rs = null; 230 try { 231 rs = new UndoRestoreStatus(f.getAbsolutePath()); 232 } catch(Exception e) { 233 UndoRestoreStatus rs1 = new UndoRestoreStatus(); 234 setExceptionInStatus(rs1, 235 new BackupException( 236 "error_creating_undorestore_status", 237 LocalStringsHelper.getString("error_creating_undorestore_status",f))); 238 return rs1; 239 } 240 return rs; 241 } 242 243 public static String getAbsoluteSnapshotFileName(long ts) { String relativeName = FactoryHelper.getEnv().getSnapshotFileNamePrefix() + 246 DefaultConstants.INTRA_FILENAME_SEPARATOR + ts; 247 248 String dir = FactoryHelper.getEnv().getDirectoryToStoreSnapshotFile(); 249 return dir + File.separator + relativeName; 250 } 251 } 252 | Popular Tags |