KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > backup > utils > BackupHelper


1 /*
2  * BackupHelper.java
3  *
4  * Created on December 22, 2003, 10:04 PM
5  */

6
7 package com.sun.enterprise.config.backup.utils;
8
9 //remove dependency. FIXME
10
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 JavaDoc;
31 /**
32  *
33  * @author sridatta
34  */

35 public class BackupHelper implements EnvironmentConstants {
36     
37     /** Creates a new instance of BackupHelper */
38     public BackupHelper() {
39     }
40  
41     public static void setExecutionTypeInEnvironment(String JavaDoc type) {
42         SystemPropsHelper.setProperty(EXECUTION_TYPE, type);
43     }
44
45     public static String JavaDoc getExecutionTypeFromEnvironment() {
46         return SystemPropsHelper.getProperty(EXECUTION_TYPE);
47     }
48     
49       /**
50      * Determine the next number to be created
51      * Managed the max numbers.
52      * Delete the oldest one if required
53      * get the prefix
54      * add timestamp
55      * create fileName = dir/prefix + timestamp+ number
56      *
57      * Update BackupNumber, TargetFile in BackupStatus
58      * update source files, etc
59      */

60     
61     public static String JavaDoc getAbsoluteBackupFileName(long timestamp) {
62         String JavaDoc fileName = getRelativeBackupFileName(timestamp);
63         return getAbsoluteBackupFileName(fileName);
64     }
65     
66     private static String JavaDoc getRelativeBackupFileName(long timestamp) {
67         String JavaDoc prefix = FactoryHelper.getEnv().getBackupFileNamePrefix();
68         return constructRelativeName(prefix, timestamp);
69     }
70
71     private static String JavaDoc constructRelativeName(String JavaDoc prefix, long timestamp) {
72         return prefix +
73                             DefaultConstants.INTRA_FILENAME_SEPARATOR +
74                             timestamp;
75     }
76     
77     public static String JavaDoc getRelativeSnapShotFileName(long timestamp)
78                                     throws BackupException {
79         
80         String JavaDoc prefix = FactoryHelper.getEnv().getSnapshotFileNamePrefix();
81         return constructRelativeName(prefix, timestamp);
82     }
83     
84     /**
85      * only set the exception if not already set
86      */

87     public static void setExceptionInStatus(Status bs, Exception JavaDoc e) {
88         try {
89             bs.setStatusCode(StatusConstants.STATUS_FAILURE);
90             if(bs.getException() == null) {
91                 bs.setException(e);
92             }
93         } catch(Exception JavaDoc ex) {
94             //ignore
95
}
96     }
97     public static String JavaDoc ArrayToString(String JavaDoc[] arr) {
98         String JavaDoc 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 JavaDoc userInfo, boolean init) {
109           BackupStatus backupStatus = null;
110           try {
111               backupStatus = new BackupStatus(userInfo, init);
112           } catch (Exception JavaDoc 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 JavaDoc getAbsoluteBackupFileName(BackupStatus bs) {
122         String JavaDoc targetFile = null;
123         try {
124             long timestamp = bs.getCreationTimeStamp();
125               targetFile = getAbsoluteBackupFileName(timestamp);
126           } catch (Exception JavaDoc 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 JavaDoc ex1) {
140                   //too late to do anything
141
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 JavaDoc ex1) {
150                   //too late to do anything
151
LoggerHelper.warning("error_releasing_lock");
152                   LoggerHelper.fine("error_releasing_lock", ex1);
153               }
154     }
155     
156     public static String JavaDoc getAbsoluteBackupFileName(String JavaDoc relativeName)
157     { // throws BackupInitException {
158
if(isAbsolute(relativeName)) {
159             return relativeName;
160         }
161         String JavaDoc dir = FactoryHelper.getEnv().getDirectoryToStoreBackupFiles();
162         File JavaDoc parent = new File JavaDoc(dir);
163         File JavaDoc ourfile = new File JavaDoc(parent, relativeName);
164         return FileUtils.safeGetCanonicalPath(ourfile);
165     }
166     
167     private static boolean isAbsolute(String JavaDoc name) {
168         return new File JavaDoc(name).isAbsolute();
169     }
170     
171     //restore apis
172
public static File JavaDoc getLastBackupFile() {
173         File JavaDoc f = null;
174         try {
175             f = FactoryHelper.getBackupStorageMonitor().getLatestFile();
176         } catch(Exception JavaDoc e) {
177             //ignore
178
}
179         return f;
180     }
181     
182     //restore apis
183
public static File JavaDoc getLastSnapShotFile() {
184         File JavaDoc f = null;
185         try {
186             f = FactoryHelper.getSnapShotStorageMonitor().getLatestFile();
187         } catch(Exception JavaDoc e) {
188             //ignore
189
}
190         return f;
191     }
192     
193     
194     public static RestoreStatus createRestoreStatus(File JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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      //restore apis
244
public static String JavaDoc getAbsoluteSnapshotFileName(long ts) { // throws BackupInitException {
245
String JavaDoc relativeName = FactoryHelper.getEnv().getSnapshotFileNamePrefix() +
246                             DefaultConstants.INTRA_FILENAME_SEPARATOR + ts;
247         
248         String JavaDoc dir = FactoryHelper.getEnv().getDirectoryToStoreSnapshotFile();
249         return dir + File.separator + relativeName;
250     }
251 }
252
Popular Tags