KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > backup > OfflineBackup


1 package com.daffodilwoods.daffodildb.server.backup;
2
3 import java.io.File JavaDoc;
4 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
5
    DatabaseConstants;
6 import java.io.RandomAccessFile JavaDoc;
7 import com.daffodilwoods.database.resource.DException;
8 import java.io.*;
9 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
10
    DatabaseProperties;
11 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
12
    DRandomAccessFile;
13 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
14
    PersistentDatabase;
15 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
16
    FileGenerator;
17 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
18
    PersistentSystem;
19 import java.util.Properties JavaDoc;
20 import com.daffodilwoods.daffodildb.server.serversystem.ServerSystem;
21 import com.daffodilwoods.daffodildb.server.datadictionarysystem.SystemTables;
22 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DataTable;
23 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
24
    DatabaseUserTableIterator;
25 import com.daffodilwoods.daffodildb.utils.BufferRange;
26 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
27 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.
28
    _TableCharacteristics;
29 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
30 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DatabaseUser;
31 import com.daffodilwoods.daffodildb.utils.comparator.CTusjohJoTfotjujwfDpnqbsbups;
32 import java.util.ArrayList JavaDoc;
33 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._UserTableOperations;
34 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.IndexDatabase;
35 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.MergeDatabase;
36 import com.daffodilwoods.daffodildb.server.serversystem._Server;
37 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DRandomAccessFileUpto3_2;
38
39 /**
40  *
41  * <p>Title:Backup</p>
42  * <p>Description:Class came into Picture as a result of the Backup Facility provided
43  * in DaffodilDB </p>
44  * <p>Copyright: Copyright (c) 2003</p>
45  * <p>Company: </p>
46  * @author not attributable
47  * @version 1.0
48  */

49
50 public class OfflineBackup {
51   _Server ss ;
52   public OfflineBackup(_Server serverSystem) throws DException {
53     ss = serverSystem;
54   }
55
56   /**
57    * takes backup of the database from the source to destination
58    * if SystemDatabase doesn't exist on the destination path
59    * then it simple copies SystemDatabase and for copying userDatabase
60    * it checks if the database is to be restored with same name then simply copies
61    * files form source path to the destination path
62    * else it first removes the entry from the system database for the sourceDatabaseName
63    * and then calls createDatabase with the new name.
64    * else it checks if userDatabase already exists at the destination
65    * if it exists there there then first we remove the existing database and then give call to create database
66    * -> if overwrite option is true
67    * -> else throw exception that Database already exists on the given path
68    * else it simply gives call to createDatabase method
69    *
70    * @param path source path from which the database is to be taken
71    * @param destination path at which backup is taken
72    * @param databaseNameSource name of the source database for taking backup
73    * @param databaseNameDestination name of the destination database for taking backup
74    * @param overwrite boolean whether to overwrite if the databse already exists.
75    * @throws DException DSE5564 if database to be backed up is in use at source path
76    */

77
78   public void offlineBackup(String JavaDoc destination,String JavaDoc databaseNameSource,
79                             String JavaDoc databaseNameDestination, boolean overwrite) throws DException {
80
81     String JavaDoc sourcePath = ((ServerSystem)ss).getDaffodilHome();
82     if (sourcePath.equalsIgnoreCase(destination))
83       throw new DException("DSE5574", null);
84
85     checkDatabase(sourcePath,databaseNameSource);
86
87     if (databaseNameSource.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE))
88       throw new DException("DSE5570", new Object JavaDoc[] {databaseNameSource});
89
90     if (databaseNameDestination.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE+"_Bk"))
91       throw new DException("DSE5571", new Object JavaDoc[] {databaseNameDestination});
92
93     PersistentDatabase systemDatabaseSourcePath = (PersistentDatabase) ( (
94         IndexDatabase) ( (
95         MergeDatabase) ((ServerSystem)ss).getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)).
96         getFileDatabase()).
97         getUnderLyingDatabase();
98
99     _DatabaseUser user1 = systemDatabaseSourcePath.getDatabaseUser();
100     try {
101       DRandomAccessFileUpto3_2 dRAM = systemDatabaseSourcePath.getDrandomAccessFile();
102       synchronized (dRAM) {
103
104         boolean ifExists = BackupHelper.backupSystemDatabase(dRAM,
105             destination, DatabaseConstants.SYSTEMDATABASE);
106         if (!ifExists) {
107           BackupPersistentSystem ps = new BackupPersistentSystem(destination);
108           PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(
109               DatabaseConstants.SYSTEMDATABASE+"_Bk");
110           BackupHelper.removingEntriesForSchedules(pd);
111           _DataTable databaseInfoTable = (_DataTable) pd.getTable(SystemTables.
112               DATABASEINFO);
113           DatabaseUserTableIterator databaseTableIteartor = (
114               DatabaseUserTableIterator) databaseInfoTable.getIterator();
115           ArrayList JavaDoc list = new ArrayList JavaDoc();
116           list.add(SystemTables.DATABASEINFO);
117           _DatabaseUser user = pd.getDatabaseUser(list);
118
119           if (databaseTableIteartor.first()) {
120             do {
121               databaseTableIteartor.delete(user); //deleting all entries of databases from SystemDatabase
122
}
123             while (databaseTableIteartor.next());
124           }
125           user.writeToFile();
126           BackupHelper.createDatabase(user1,systemDatabaseSourcePath,destination, databaseNameSource,
127                                       databaseNameDestination);
128           offLineBackupUserDatabase(sourcePath, destination, databaseNameSource,
129                                     databaseNameDestination);
130         }
131         else {
132           BackupPersistentSystem ps = new BackupPersistentSystem(destination);
133           PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(
134               DatabaseConstants.SYSTEMDATABASE+"_Bk");
135           checkDatabase(pd);
136           try {
137             pd = (PersistentDatabase) ps.getDatabase(databaseNameDestination);
138             if (pd != null && overwrite) {
139               ps.removeDatabase(databaseNameDestination);
140               ps.dropDatabase(databaseNameDestination);
141               BackupHelper.createDatabase(user1,systemDatabaseSourcePath,destination, databaseNameSource,
142                   databaseNameDestination);
143               offLineBackupUserDatabase(sourcePath, destination, databaseNameSource,
144                                         databaseNameDestination);
145             }
146             else {
147               throw new DException("DSE5567",new Object JavaDoc[] {databaseNameDestination});
148             }
149           }
150           catch (DException ex) {
151             if (ex.getDseCode().equalsIgnoreCase("DSE316")){
152               BackupHelper.createDatabase(user1,systemDatabaseSourcePath,destination, databaseNameSource,
153                   databaseNameDestination);
154               offLineBackupUserDatabase(sourcePath, destination, databaseNameSource,
155                                         databaseNameDestination);
156             }
157             else
158               throw ex;
159           }
160         }
161       }
162     }
163     finally {
164       user1.releaseCluster();
165     }
166   }
167   public void offlineRestore(String JavaDoc sourcePath, String JavaDoc destination,String JavaDoc databaseNameSource,
168                              String JavaDoc databaseNameDestination, boolean overwrite) throws DException {
169
170     synchronized(ss) {
171
172       RandomAccessFile JavaDoc raf = null;
173       ServerSystem ss = null;
174       try {
175         raf = checkDatabaseInUse(sourcePath);
176         String JavaDoc systemDatabaseSourcePath = sourcePath + File.separator +
177             DatabaseConstants.SYSTEMDATABASE + "_Bk" + File.separator +
178             DatabaseConstants.SYSTEMDATABASE + ".ddb";
179         String JavaDoc systemDatabaseDestinationPath = destination + File.separator +
180             DatabaseConstants.SYSTEMDATABASE + File.separator +
181             DatabaseConstants.SYSTEMDATABASE + ".ddb";
182         File JavaDoc ff = new File JavaDoc(systemDatabaseSourcePath);
183         File JavaDoc df = new File JavaDoc(systemDatabaseDestinationPath);
184         if (!df.exists()) {
185           copyDatabase(systemDatabaseSourcePath, systemDatabaseDestinationPath,
186                        destination, databaseNameDestination);
187           RestorePersistentSystem ps = new RestorePersistentSystem(destination);
188           PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(
189               DatabaseConstants.SYSTEMDATABASE);
190           _DataTable databaseInfoTable = (_DataTable) pd.getTable(SystemTables.
191               DATABASEINFO);
192           DatabaseUserTableIterator databaseTableIteartor = (
193               DatabaseUserTableIterator) databaseInfoTable.getIterator();
194           ArrayList JavaDoc list = new ArrayList JavaDoc();
195           list.add(SystemTables.DATABASEINFO);
196           _DatabaseUser user = pd.getDatabaseUser(list);
197
198           if (databaseTableIteartor.first()) {
199             do {
200               databaseTableIteartor.delete(user);
201             }
202             while (databaseTableIteartor.next());
203           }
204           user.writeToFile();
205
206
207         }
208         else {
209
210
211           RestorePersistentSystem ps = new RestorePersistentSystem(destination);
212           PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(
213               DatabaseConstants.SYSTEMDATABASE);
214           checkDatabase(pd);
215           try {
216             pd = (PersistentDatabase) ps.getDatabase(databaseNameDestination);
217             if (pd != null && overwrite) {
218               ps.removeDatabase(databaseNameDestination);
219               ps.dropDatabase(databaseNameDestination);
220             }
221             else {
222               throw new DException("DSE5566",
223                                    new Object JavaDoc[] {databaseNameDestination});
224             }
225           }
226           catch (DException ex) {
227             if (ex.getDseCode().equalsIgnoreCase("DSE316"))
228               createDatabase(sourcePath,// destination,
229
databaseNameSource,databaseNameDestination,null);
230             else
231               throw ex;
232           }
233         }
234       }
235       finally {
236         try {
237           if (raf != null)
238             raf.close();
239         }
240         catch (Exception JavaDoc e) {
241           throw new DException("DSE5564", new Object JavaDoc[] {e.getMessage()});
242         }
243       }
244     }
245
246   }
247
248   /**
249    * if userDatabse exists at the destination then first drop the database and then give call for create database
250    * if overwrite option is true else throws exception that database to be backed up
251    * already exists on the destination path
252    * else it just copies the file from source path to destination path
253    *
254    * @param path source path from which the database is to be taken
255    * @param destination path at which backup is taken
256    * @param databaseNameSource name of the database in the source
257    * @param databaseNameDestination name of the database with which it is to be restored or backed up
258    * @param ifExists is true if the userDatabse already exists at the path
259    */

260
261   private void offLineBackupUserDatabase(String JavaDoc sourcePath, String JavaDoc destination,
262       String JavaDoc databaseNameSource,
263       String JavaDoc databaseNameDestination) throws DException {
264
265
266     String JavaDoc userDatabaseSourcePath = sourcePath + File.separator + databaseNameSource +
267                                     File.separator + databaseNameSource + ".ddb";
268     String JavaDoc userDatabaseDestinationPath = destination + File.separator +
269         databaseNameDestination +"_Bk"+ File.separator + databaseNameDestination +
270         ".ddb";
271     copyDatabase(userDatabaseSourcePath, userDatabaseDestinationPath, destination,
272                  databaseNameDestination);
273   }
274
275
276   /**
277    * if userDatabse exists at the destination then first drop the database and then give call for create database
278    * if overwrite option is true else throws exception that database to be backed up
279    * already exists on the destination path
280    * else it just copies the file from source path to destination path
281    *
282    * @param path source path from which the database is to be taken
283    * @param destination path at which backup is taken
284    * @param databaseNameSource name of the database in the source
285    * @param databaseNameDestination name of the database with which it is to be restored or backed up
286    * @param ifExists is true if the userDatabse already exists at the path
287    */

288
289   private void offLineRestoreUserDatabase(String JavaDoc sourcePath, String JavaDoc destination,
290       String JavaDoc databaseNameSource,
291       String JavaDoc databaseNameDestination) throws DException {
292
293
294     String JavaDoc userDatabaseSourcePath = sourcePath + File.separator + databaseNameSource+"_Bk" +
295                                      File.separator + databaseNameSource + ".ddb";
296     String JavaDoc userDatabaseDestinationPath = destination + File.separator +
297         databaseNameDestination + File.separator + databaseNameDestination +
298         ".ddb";
299     copyDatabase(userDatabaseSourcePath, userDatabaseDestinationPath, destination,
300                  databaseNameDestination);
301   }
302
303   /**
304    * Creates database at the destination it takes properties of the database from the
305    * source SystemDatabase and then creates Database with these properties it also modifies
306    * the SystemTable DATABASEFILEINFO.
307    * Gives call for creation of database and then delete the files on the path and then just copy the files from source to destination.
308    * by giving call to offlineBackupUserDatabase.
309    *
310    * @param path source path from which the database is to be taken
311    * @param destination path at which backup is taken
312    * @param databaseNameSource name of the database in the source
313    * @param databaseNameDestination name of the database with which it is to be restored or backed up
314    *
315    */

316   private void createDatabase1(String JavaDoc sourcePath, String JavaDoc destination,
317                                String JavaDoc databaseNameSource,
318                                String JavaDoc databaseNameDestination) throws DException {
319     int index = 0;
320     BackupPersistentSystem ps = new BackupPersistentSystem(sourcePath);
321     PersistentDatabase systemDatabaseSourcePath = (PersistentDatabase)ps.getDatabase(DatabaseConstants.SYSTEMDATABASE+"_Bk");
322     _DataTable databaseInfoTable = (_DataTable) systemDatabaseSourcePath.getTable(SystemTables.DATABASEINFO);
323     DatabaseUserTableIterator databaseTableIteartor = (
324         DatabaseUserTableIterator) databaseInfoTable.getIterator();
325     Object JavaDoc[] databaseProperties = null;
326     BufferRange bytesOfDestinationDatabaseName = new BufferRange(CCzufDpowfsufs.
327         getBytes(databaseNameDestination, databaseNameDestination.length(), false));
328     BufferRange bytesOfSourceDatabaseName = new BufferRange(CCzufDpowfsufs.
329         getBytes(databaseNameSource, databaseNameSource.length(), false));
330     CTusjohJoTfotjujwfDpnqbsbups stringComparator = new CTusjohJoTfotjujwfDpnqbsbups();
331
332     if (databaseTableIteartor.first())
333       do {
334       BufferRange[] bytesGot = (BufferRange[]) databaseTableIteartor.
335                                getColumnValues();
336       if (stringComparator.compare(bytesGot[0], bytesOfSourceDatabaseName) ==
337           0) {
338         _TableCharacteristics tableCharacteristics = databaseInfoTable.
339             getTableCharacteristics();
340         databaseProperties = (Object JavaDoc[]) tableCharacteristics.getObject(
341             bytesGot);
342         break;
343       }
344     }
345     while (databaseTableIteartor.next());
346     Object JavaDoc[] databaseProps = Utility.convertIntoFieldBase(databaseProperties);
347     String JavaDoc initialSize = (String JavaDoc) databaseProps[1];
348     int incrementFactor = databaseProps[4].hashCode();
349     boolean unicodeSupport = ( (Boolean JavaDoc) databaseProps[5]).booleanValue();
350     boolean multiFilesSupport = ( (Boolean JavaDoc) databaseProps[6]).booleanValue();
351     boolean encryptionSupport = ( (Boolean JavaDoc) databaseProps[7]).booleanValue();
352     String JavaDoc encrytpionAlgo = (String JavaDoc) databaseProps[8];
353     String JavaDoc encryptionKey = (String JavaDoc) databaseProps[9];
354     String JavaDoc clusterSize = (String JavaDoc) databaseProps[10];
355     if (multiFilesSupport) {
356       DatabaseUserTableIterator databaseFileInfoIterator = (
357           DatabaseUserTableIterator) ( (_DataTable) systemDatabaseSourcePath.getTable(
358           SystemTables.DATABASEFILEINFO)).getIterator();
359       if (databaseFileInfoIterator.first())
360         do {
361         BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.
362                                  getColumnValues();
363         if (stringComparator.compare(bytesGot[0],
364                                      bytesOfSourceDatabaseName) == 0) {
365           index = CCzufDpowfsufs.getInt(bytesGot[1].getBytes()).hashCode();
366           break;
367         }
368       }
369       while (databaseFileInfoIterator.next());
370     }
371
372     DatabaseUserTableIterator scheduleInfoTableIteartor = null; ArrayList JavaDoc
373     scheduleEntries = null;
374     try {
375       _DataTable table = (_DataTable) systemDatabaseSourcePath.getTable(
376           SystemTables.SCHEDULEINFO);
377       scheduleInfoTableIteartor = (DatabaseUserTableIterator) table.
378                                   getIterator();
379       scheduleEntries = new ArrayList JavaDoc();
380       if (scheduleInfoTableIteartor.first()) {
381         do {
382           BufferRange[] bytesGot = (BufferRange[])
383                                    scheduleInfoTableIteartor.getColumnValues();
384           if (stringComparator.compare(bytesGot[0],
385                                        bytesOfSourceDatabaseName) == 0) {
386             scheduleEntries.add(bytesGot);
387           }
388         }
389         while (scheduleInfoTableIteartor.next());
390       }
391     }
392     catch (DException ex) {
393       if (!ex.getDseCode().equalsIgnoreCase("DSE959"))
394         throw ex;
395     }
396     RestorePersistentSystem ps1 = new RestorePersistentSystem(destination);
397     ps1.createDatabase(databaseNameDestination, initialSize, incrementFactor,
398                        unicodeSupport, multiFilesSupport, encryptionSupport,
399                        encrytpionAlgo, encryptionKey, clusterSize);
400
401     if (multiFilesSupport) {
402       PersistentDatabase systemDatabaseDestinationPath = (PersistentDatabase) ps1.getDatabase(
403           DatabaseConstants.SYSTEMDATABASE);
404       DatabaseUserTableIterator databaseFileInfoIterator = (
405           DatabaseUserTableIterator) ( (_DataTable) systemDatabaseDestinationPath.getTable(
406           SystemTables.DATABASEFILEINFO)).getIterator();
407       if (databaseFileInfoIterator.first())
408         do {
409         BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.getColumnValues();
410         if (stringComparator.compare(bytesGot[0],bytesOfDestinationDatabaseName) == 0) {
411           _DatabaseUser user = systemDatabaseDestinationPath.getDatabaseUser();
412           databaseFileInfoIterator.update(user,
413               new BufferRange[] {bytesOfDestinationDatabaseName,
414               new
415               BufferRange(CCzufDpowfsufs.
416               getBytes(new Integer JavaDoc(index)))});
417               user.writeToFile();
418               break;
419         }
420       }
421       while (databaseTableIteartor.next());
422     }
423
424     if (scheduleEntries!= null && scheduleEntries.size() > 0) {
425       PersistentDatabase systemDatabaseDestination = (PersistentDatabase) ps1.getDatabase(DatabaseConstants.SYSTEMDATABASE);
426       DatabaseUserTableIterator scheduleInfoTableIteartorDestination = null;
427       _DatabaseUser user = null;
428       try {
429         _DataTable scheduleInfoTable = (_DataTable) systemDatabaseDestination.
430                                        getTable(SystemTables.SCHEDULEINFO);
431         scheduleInfoTableIteartorDestination = (DatabaseUserTableIterator)
432             scheduleInfoTable.getIterator();
433         ArrayList JavaDoc list = new ArrayList JavaDoc();
434         list.add(SystemTables.SCHEDULEINFO);
435         user = systemDatabaseDestination.getDatabaseUser(list);
436
437         if (scheduleInfoTableIteartorDestination.first()) {
438           do {
439             BufferRange bytesGot = (BufferRange)
440                                    scheduleInfoTableIteartorDestination.getColumnValues(0);
441             if (stringComparator.compare(bytesGot, bytesOfSourceDatabaseName) ==
442                 0) {
443               scheduleInfoTableIteartorDestination.delete(user);
444             }
445           }
446           while (scheduleInfoTableIteartor.next());
447         }
448       }
449       catch (DException ex1) {
450         if (!ex1.getDseCode().equalsIgnoreCase("DSE959"))
451           throw ex1;
452       }
453
454
455       for (int i = 0; i < scheduleEntries.size(); i++) {
456         BufferRange[] entry = (BufferRange[]) scheduleEntries.get(i);
457         entry[0] = bytesOfDestinationDatabaseName;
458         ( (_UserTableOperations) scheduleInfoTableIteartorDestination).insert(user,entry);
459       }
460       user.writeToFile();
461     }
462
463     deleteFiles(databaseNameDestination, destination);
464     offLineRestoreUserDatabase(sourcePath, destination, databaseNameSource,databaseNameDestination);
465   }
466
467   private void createDatabase(String JavaDoc sourcePath,
468                               String JavaDoc databaseNameSource,
469                               String JavaDoc databaseNameDestination,PersistentSystem ps1) throws DException {
470     int index = 0;
471     BackupPersistentSystem ps = new BackupPersistentSystem(sourcePath);
472     PersistentDatabase systemDatabaseSourcePath = (PersistentDatabase)ps.getDatabase(DatabaseConstants.SYSTEMDATABASE+"_Bk");
473     _DataTable databaseInfoTable = (_DataTable) systemDatabaseSourcePath.getTable(SystemTables.DATABASEINFO);
474     DatabaseUserTableIterator databaseTableIteartor = (
475         DatabaseUserTableIterator) databaseInfoTable.getIterator();
476     Object JavaDoc[] databaseProperties = null;
477     BufferRange bytesOfDestinationDatabaseName = new BufferRange(CCzufDpowfsufs.
478         getBytes(databaseNameDestination, databaseNameDestination.length(), false));
479     BufferRange bytesOfSourceDatabaseName = new BufferRange(CCzufDpowfsufs.
480         getBytes(databaseNameSource, databaseNameSource.length(), false));
481     CTusjohJoTfotjujwfDpnqbsbups stringComparator = new CTusjohJoTfotjujwfDpnqbsbups();
482
483     if (databaseTableIteartor.first())
484       do {
485       BufferRange[] bytesGot = (BufferRange[]) databaseTableIteartor.
486                                getColumnValues();
487       if (stringComparator.compare(bytesGot[0], bytesOfSourceDatabaseName) ==
488           0) {
489         _TableCharacteristics tableCharacteristics = databaseInfoTable.
490             getTableCharacteristics();
491         databaseProperties = (Object JavaDoc[]) tableCharacteristics.getObject(
492             bytesGot);
493         break;
494       }
495     }
496     while (databaseTableIteartor.next());
497     Object JavaDoc[] databaseProps = Utility.convertIntoFieldBase(databaseProperties);
498     String JavaDoc initialSize = (String JavaDoc) databaseProps[1];
499     int incrementFactor = databaseProps[4].hashCode();
500     boolean unicodeSupport = ( (Boolean JavaDoc) databaseProps[5]).booleanValue();
501     boolean multiFilesSupport = ( (Boolean JavaDoc) databaseProps[6]).booleanValue();
502     boolean encryptionSupport = ( (Boolean JavaDoc) databaseProps[7]).booleanValue();
503     String JavaDoc encrytpionAlgo = (String JavaDoc) databaseProps[8];
504     String JavaDoc encryptionKey = (String JavaDoc) databaseProps[9];
505     String JavaDoc clusterSize = (String JavaDoc) databaseProps[10];
506     if (multiFilesSupport) {
507       DatabaseUserTableIterator databaseFileInfoIterator = (
508           DatabaseUserTableIterator) ( (_DataTable) systemDatabaseSourcePath.getTable(
509           SystemTables.DATABASEFILEINFO)).getIterator();
510       if (databaseFileInfoIterator.first())
511         do {
512         BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.
513                                  getColumnValues();
514         if (stringComparator.compare(bytesGot[0],
515                                      bytesOfSourceDatabaseName) == 0) {
516           index = CCzufDpowfsufs.getInt(bytesGot[1].getBytes()).hashCode();
517           break;
518         }
519       }
520       while (databaseTableIteartor.next());
521     }
522
523
524
525     createDatabaseAtCurrentPath(ps1, databaseNameSource,
526                                 databaseNameDestination, initialSize,
527                                 incrementFactor, unicodeSupport,
528                                 multiFilesSupport, encryptionSupport,
529                                 encrytpionAlgo, encryptionKey, clusterSize,
530                                 null, index);
531
532     String JavaDoc destination = ((ServerSystem)ss).getDaffodilHome();
533     deleteFiles(databaseNameDestination, destination);
534     offLineRestoreUserDatabase(sourcePath, destination, databaseNameSource,databaseNameDestination);
535   }
536
537   /**
538    * Method to delete .ddb file and database folder created through create databse method before
539    * giving call to the copy file methof from source to destination
540    * @param databaseName
541    * @param destination
542    * @throws DException
543    */

544   private void deleteFiles(String JavaDoc databaseName, String JavaDoc destination) throws
545   DException {
546     String JavaDoc path = destination + File.separator + databaseName + File.separator +
547                   databaseName + ".ddb";
548     File JavaDoc f = new File JavaDoc(path);
549     f.delete();
550     File JavaDoc ff1 = f.getParentFile();
551     path = destination + File.separator + databaseName + File.separator +
552            DatabaseConstants.TEMPDATABASE + ".ddb";
553     File JavaDoc f1 = new File JavaDoc(path);
554     f1.delete();
555     ff1.delete();
556   }
557
558   /**
559    * restores the database from the backup place to destination.
560    * first of all it checks that database to be restored is not in use.
561    * if SystemDatabase doesn't exist on the destination path
562    * then it simple copies SystemDatabase and for copying userDatabase
563    * it checks if the database is to be restored with same name then simply copies
564    * files form source path to the destination path
565    * else it first removes the entry from the system database for the sourceDatabaseName
566    * and then calls createDatabase with the new name.
567    * else it checks if userDatabase already exists at the destination
568    * if it exists there there then first we remove the existing database and then give call to create database
569    * -> if overwrite option is true
570    * -> else throw exception that Database already exists on the given path
571    * else it simply gives call to createDatabase method
572    *
573    * @param path source path from which the database is to be taken
574    * @param destination path at which backup is taken
575    * @param databaseNameSource name of the source database for taking backup
576    * @param databaseNameDestination name of the destination database for taking backup
577    * @param overwrite boolean whether to overwrite if the databse already exists.
578    * @throws DException DSE5566 if database to be restored alredy exists at destination path
579    */

580   public void restoreBackup(String JavaDoc path,String JavaDoc databaseNameSource,
581                             String JavaDoc databaseNameDestination, boolean overwrite) throws DException {
582     if (databaseNameSource.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE))
583       throw new DException("DSE5568", new Object JavaDoc[] {databaseNameSource});
584     if (databaseNameDestination.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE))
585       throw new DException("DSE5569", new Object JavaDoc[] {databaseNameDestination});
586
587     checkDatabaseBeforeRestoring(path, databaseNameSource);
588
589     if (databaseNameSource.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE))
590       throw new DException("DSE5570", new Object JavaDoc[] {databaseNameSource});
591
592     if (databaseNameDestination.equalsIgnoreCase(DatabaseConstants.
593         SYSTEMDATABASE))
594       throw new DException("DSE5571", new Object JavaDoc[] {databaseNameDestination});
595
596
597     restoreAtCurrentPath(path, databaseNameSource,
598                          databaseNameDestination, overwrite);
599   }
600
601   /**
602    * private method to check if SystemDatabase is Complete
603    * @throws DException DSE5543 if system database is corrupt
604    */

605   private void checkDatabase(PersistentDatabase pd) throws DException {
606     byte[] bytes = pd.readBytes(pd.getDatabaseProperties().
607                                 ISCOMPLETE_BIT_IN_DATABASE, 1);
608     Boolean JavaDoc isComplete = CCzufDpowfsufs.getBoolean(bytes);
609     if (!isComplete.booleanValue()) {
610       throw new DException("DSE5543", null);
611     }
612   }
613
614   /**
615    * checks if database is in use at the source path
616    * @return arndom accessfile created on the log.lg file
617    * @throws DException DSE5564 that databse is in use if it doesn't allow to delete the log.lg file
618    * at the source path
619    */

620   private RandomAccessFile JavaDoc checkDatabaseInUse(String JavaDoc sourcePath) throws DException {
621     String JavaDoc logFile = sourcePath + File.separator + "log.lg";
622     File JavaDoc ff = new File JavaDoc(logFile);
623     boolean isDelete = false;
624     if (ff.exists()) {
625       isDelete = ff.delete();
626       if (!isDelete) {
627          ;//// Removed By Program ** System.out.println(" log file path " + logFile);
628
throw new DException("DSE5564", new Object JavaDoc[] {});
629       }
630     }
631     RandomAccessFile JavaDoc raf = null;
632     try {
633       raf = new RandomAccessFile JavaDoc(ff, "rw");
634     }
635     catch (FileNotFoundException ex) {
636     }
637     return raf;
638   }
639
640   /**
641    * Copies database files from source path to the destination path
642    * If power files exists then copies all the power files also to the destination path
643    * If multiFileSupport exists and multiple files (.dat) exists at the source path then
644    * copies all the multipleFiles to the destination with the databaseNameDestination
645    *
646    * @param sourcePath
647    * @param destinationPath
648    * @param destination
649    * @param databaseNameDestination
650    * @throws DException
651    */

652   private void copyDatabase(String JavaDoc sourcePath, String JavaDoc destinationPath, String JavaDoc destination,String JavaDoc databaseNameDestination) throws DException{
653     File JavaDoc ff = new File JavaDoc(sourcePath);
654     File JavaDoc df = new File JavaDoc(destinationPath,databaseNameDestination);
655     if(!df.getName().equals(databaseNameDestination))
656       throw new DException("DSE2049",new Object JavaDoc[] {databaseNameDestination});
657     df = new File JavaDoc(destinationPath);
658     try {
659       File JavaDoc ff1 = ff.getParentFile();
660       ff1.mkdirs();
661       File JavaDoc df1 = df.getParentFile();
662       df1.mkdirs();
663       FileInputStream file = new FileInputStream(ff);
664       FileOutputStream DestinationFile = new FileOutputStream(df);
665       CopyFile cf = new CopyFile();
666       cf.copyFile(file,DestinationFile);
667
668
669       destinationPath = destinationPath.substring(0,destinationPath.lastIndexOf(File.separator));
670       File JavaDoc [] files = ff1.listFiles(new PowerFileFilter());
671       for (int i = 0; i < files.length; i++) {
672         FileInputStream powerFile = new FileInputStream(files[i]);
673         String JavaDoc name = files[i].getName();
674         if(!name.startsWith("_"))
675           name = databaseNameDestination+"_" + i +"_PowerFile.log";
676         String JavaDoc destinationPath1 = destinationPath + File.separator + name;
677         DestinationFile = new FileOutputStream(new File JavaDoc(destinationPath1));
678         cf.copyFile(powerFile,DestinationFile);
679       }
680       files = ff1.listFiles(new MultiFileFilter());
681       for (int i = 0; i < files.length; i++) {
682         FileInputStream multiFile = new FileInputStream(files[i]);
683         String JavaDoc name = files[i].getName();
684         if(!name.startsWith("_"))
685           name = databaseNameDestination+(i+1)+".dat";
686         String JavaDoc destinationPath1 = destinationPath + File.separator + name;
687         DestinationFile = new FileOutputStream(new File JavaDoc(destinationPath1));
688         cf.copyFile(multiFile,DestinationFile);
689       }
690     }
691     catch (Exception JavaDoc ex) {
692       throw new DException("DSE0",new Object JavaDoc[] {ex.getMessage()});
693     }
694
695   }
696
697   /**
698    * Checks if database to be backed up exists on the source Path
699    * @param sourcePath path from which backup is taken
700    * @param databaseName database to be backed up
701    * @throws DException if Database doesn't exists on the path
702    */

703   private void checkDatabase(String JavaDoc sourcePath,String JavaDoc databaseName) throws DException {
704     String JavaDoc databaseURLSystemDatabase = sourcePath + File.separator +DatabaseConstants.SYSTEMDATABASE;
705     String JavaDoc databaseURL = sourcePath + File.separator + databaseName;
706     File JavaDoc fs = new File JavaDoc(databaseURLSystemDatabase);
707
708     if (!fs.exists())
709       throw new DException("DSE5572", new Object JavaDoc[]{sourcePath});
710     File JavaDoc f = new File JavaDoc(databaseURL);
711     if(!f.exists())
712       throw new DException("DSE5563", new Object JavaDoc[]{databaseName});
713   }
714
715   /**
716    * Checks if database to be backed up exists on the source Path
717    * @param sourcePath path from which backup is taken
718    * @param databaseName database to be backed up
719    * @throws DException if Database doesn't exists on the path
720    */

721   private void checkDatabaseBeforeRestoring(String JavaDoc sourcePath,String JavaDoc databaseName) throws DException {
722     String JavaDoc databaseURLSystemDatabase = sourcePath + File.separator +DatabaseConstants.SYSTEMDATABASE+"_Bk";
723     String JavaDoc databaseURL = sourcePath + File.separator + databaseName+"_Bk";
724     File JavaDoc fs = new File JavaDoc(databaseURLSystemDatabase);
725
726     if (!fs.exists())
727       throw new DException("DSE5572", new Object JavaDoc[]{sourcePath});
728     File JavaDoc f = new File JavaDoc(databaseURL);
729     if(!f.exists())
730       throw new DException("DSE5563", new Object JavaDoc[]{databaseName});
731     checkDatabaseEntryInSystemDatabase(sourcePath,databaseName);
732   }
733
734   /**
735    * checks if entry of user database exists in systemDatabase else
736    * throws exception that databases are incompatible
737    * @param sourcePath
738    * @param userDatabase
739    * @throws DException DSE5573 that databases are incompatible
740    */

741   private void checkDatabaseEntryInSystemDatabase(String JavaDoc sourcePath,String JavaDoc userDatabase) throws DException{
742     BackupPersistentSystem ps = new BackupPersistentSystem(sourcePath);
743     try {
744       PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(userDatabase);
745     }
746     catch (DException ex) {
747       if (ex.getDseCode().equalsIgnoreCase("DSE316"))
748         throw new DException("DSE5573",new Object JavaDoc[]{userDatabase});
749     }
750   }
751
752   private void restoreAtCurrentPath(String JavaDoc sourcePath,String JavaDoc databaseNameSource,
753                                     String JavaDoc databaseNameDestination, boolean overwrite) throws DException{
754     RandomAccessFile JavaDoc raf = null;
755     try{
756       raf = checkDatabaseInUse(sourcePath);
757       String JavaDoc systemDatabaseSourcePath = sourcePath + File.separator +
758                                         DatabaseConstants.SYSTEMDATABASE + "_Bk" + File.separator +
759                                         DatabaseConstants.SYSTEMDATABASE + ".ddb";
760       PersistentSystem ps = (PersistentSystem)(((ServerSystem)ss).getPersistentSystem());
761       PersistentDatabase systemDatabaseDestinationPath = (PersistentDatabase) ( (
762           IndexDatabase) ( (
763           MergeDatabase) ((ServerSystem)ss).getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)).
764           getFileDatabase()).
765           getUnderLyingDatabase();
766
767       try {
768         DRandomAccessFileUpto3_2 dRAM = systemDatabaseDestinationPath.
769                                         getDrandomAccessFile();
770         synchronized (dRAM) {
771           try {
772             PersistentDatabase pd = (PersistentDatabase) ps.getDatabase(databaseNameDestination);
773             if (pd != null && overwrite) {
774               ps.removeDatabase(databaseNameDestination);
775               ps.dropDatabase(databaseNameDestination);
776               createDatabase(sourcePath, databaseNameSource,
777                              databaseNameDestination,ps);
778             }
779             else {
780               throw new DException("DSE5566",
781                                    new Object JavaDoc[] {databaseNameDestination});
782             }
783           }
784           catch (DException ex) {
785             if (ex.getDseCode().equalsIgnoreCase("DSE316")){
786          ;//// Removed By Program ** System.out.println("Creating databse");
787
createDatabase(sourcePath, databaseNameSource,
788                              databaseNameDestination, ps);
789             }
790             else
791               throw ex;
792           }
793         }
794       }finally {
795       }
796
797     }finally {
798       try {
799         if (raf != null)
800           raf.close();
801       }
802       catch (Exception JavaDoc e) {
803         throw new DException("DSE5564", new Object JavaDoc[] {e.getMessage()});
804       }
805     }
806
807
808   }
809
810   private void createDatabaseAtCurrentPath(PersistentSystem ps1,String JavaDoc databaseNameSource ,String JavaDoc databaseNameDestination,String JavaDoc initialSize, int incrementFactor, boolean unicodeSupport, boolean multifileSupport,boolean encryptionSupport,String JavaDoc encryptionAlgo,String JavaDoc encryptionKey,String JavaDoc clusterSize,ArrayList JavaDoc scheduleEntries,int index) throws DException{
811
812     ps1.createDatabase(databaseNameDestination, initialSize, incrementFactor,
813                        unicodeSupport, multifileSupport, encryptionSupport,
814                        encryptionAlgo, encryptionKey, clusterSize);
815     CTusjohJoTfotjujwfDpnqbsbups stringComparator = new CTusjohJoTfotjujwfDpnqbsbups();
816     BufferRange bytesOfDestinationDatabaseName = new BufferRange(CCzufDpowfsufs.
817         getBytes(databaseNameDestination, databaseNameDestination.length(), false));
818     BufferRange bytesOfSourceDatabaseName = new BufferRange(CCzufDpowfsufs.
819         getBytes(databaseNameSource, databaseNameSource.length(), false));
820     _DatabaseUser user = null;
821
822     if (multifileSupport) {
823       try {
824         PersistentDatabase systemDatabaseDestinationPath = (PersistentDatabase)
825             ps1.getDatabase(
826             DatabaseConstants.SYSTEMDATABASE);
827         DatabaseUserTableIterator databaseFileInfoIterator = (
828             DatabaseUserTableIterator) ( (_DataTable)
829             systemDatabaseDestinationPath.getTable(
830             SystemTables.DATABASEFILEINFO)).getIterator();
831         if (databaseFileInfoIterator.first()) {
832           do {
833             BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.
834                                      getColumnValues();
835             if (stringComparator.compare(bytesGot[0],
836                 bytesOfDestinationDatabaseName) == 0) {
837               user = systemDatabaseDestinationPath.getDatabaseUser();
838               databaseFileInfoIterator.update(user,
839                   new BufferRange[] {
840                 bytesOfDestinationDatabaseName,
841                 new
842                     BufferRange(CCzufDpowfsufs.
843                   getBytes(new Integer JavaDoc(index)))});
844                   break;
845             }
846           }
847           while (databaseFileInfoIterator.next());
848         }
849       }
850       finally {
851         if(user!= null)
852           user.writeToFile();
853       }
854     }
855     if (scheduleEntries!= null && scheduleEntries.size() > 0) {
856       PersistentDatabase systemDatabaseDestination = (PersistentDatabase) ps1.getDatabase(DatabaseConstants.SYSTEMDATABASE);
857       DatabaseUserTableIterator scheduleInfoTableIteartorDestination = null;
858       _DatabaseUser user1 = null;
859       try {
860         _DataTable scheduleInfoTable = (_DataTable) systemDatabaseDestination.
861                                        getTable(SystemTables.SCHEDULEINFO);
862         scheduleInfoTableIteartorDestination = (DatabaseUserTableIterator)
863             scheduleInfoTable.getIterator();
864         ArrayList JavaDoc list = new ArrayList JavaDoc();
865         list.add(SystemTables.SCHEDULEINFO);
866         user1 = systemDatabaseDestination.getDatabaseUser(list);
867
868         if (scheduleInfoTableIteartorDestination.first()) {
869           do {
870             BufferRange bytesGot = (BufferRange)
871                                    scheduleInfoTableIteartorDestination.getColumnValues(0);
872             if (stringComparator.compare(bytesGot, bytesOfSourceDatabaseName) ==
873                 0) {
874               scheduleInfoTableIteartorDestination.delete(user1);
875             }
876           }
877           while (scheduleInfoTableIteartorDestination.next());
878         }
879
880
881
882         for (int i = 0; i < scheduleEntries.size(); i++) {
883           BufferRange[] entry = (BufferRange[]) scheduleEntries.get(i);
884           entry[0] = bytesOfDestinationDatabaseName;
885           ( (_UserTableOperations) scheduleInfoTableIteartorDestination).insert(user1,entry);
886         }
887       }
888       catch (DException ex1) {
889         if (!ex1.getDseCode().equalsIgnoreCase("DSE959"))
890           throw ex1;
891       }finally{
892         user1.writeToFile();
893       }
894     }
895
896   }
897
898   class PowerFileFilter
899       implements FilenameFilter {
900     public boolean accept(File JavaDoc dir, String JavaDoc name) {
901       return name.endsWith("PowerFile.log") ? true
902       : false;
903     }
904   }
905
906   class MultiFileFilter
907       implements FilenameFilter {
908     public boolean accept(File JavaDoc dir, String JavaDoc name) {
909       return name.endsWith(".dat") ? true
910       : false;
911     }
912   }
913
914
915   private void removingEntriesForSchedules(PersistentDatabase systemDatabaseSourcePath,_DatabaseUser user) throws DException{
916     try {
917       _DataTable table = (_DataTable) systemDatabaseSourcePath.getTable(
918           SystemTables.SCHEDULEINFO);
919       DatabaseUserTableIterator scheduleInfoTableIteartor = (DatabaseUserTableIterator) table.
920           getIterator();
921
922       if (scheduleInfoTableIteartor.first()) {
923         do {
924           scheduleInfoTableIteartor.delete(user);
925         }
926         while (scheduleInfoTableIteartor.next());
927       }
928     }
929     catch (DException ex) {
930       if (!ex.getDseCode().equalsIgnoreCase("DSE959"))
931         throw ex;
932     }
933
934   }
935
936 }
937
Popular Tags