KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > tools > BackupUtility


1 package com.daffodilwoods.tools;
2
3 import java.rmi.*;
4 import java.sql.*;
5 import java.sql.Connection JavaDoc;
6 import java.util.*;
7
8 import com.daffodilwoods.daffodildb.server.serversystem.*;
9 import com.daffodilwoods.database.resource.*;
10 import com.daffodilwoods.rmi.*;
11 import com.daffodilwoods.rmi.interfaces.*;
12 import in.co.daffodil.db.jdbc.*;
13
14 /**
15  * <p>Title: Daffodil DB backup and restore utility.</p>
16      * <p>Description: Backup and restore utility for online and offline backup.</p>
17  * <p>Copyright: Daffodil software Limited (INDIA)</p>
18  * <p>Company:Daffodil Software Limited. (INIDA) </p>
19  * <p><br>
20  * First version date : Feb. 5 , 2005
21  * @author Manoj Kr. Sheoran < manoj.kumar@daffodildb.com >
22  * @version 1.0
23  */

24
25 public class BackupUtility
26     extends AbstractDaffodilDBDriver {
27   private int workingMode = EMBEDDED_MODE;
28   private boolean isOffline = false;
29   private String JavaDoc hostName = "localhost";
30   private int portNumber = 3456;
31   public static int EMBEDDED_MODE = 0;
32   public static int SERVER_MODE = 1;
33   private boolean verbose = false;
34
35   public BackupUtility() {}
36
37   public Connection JavaDoc getConnection(String JavaDoc driverClass, String JavaDoc url) throws
38       ClassNotFoundException JavaDoc, SQLException {
39     Class.forName(driverClass);
40     return DriverManager.getConnection(url);
41
42   }
43
44   public void setWorkingMode(int mode) {
45     workingMode = mode;
46   }
47
48   protected _Server getServer(String JavaDoc url) throws SQLException {
49     _Server server = super.server;
50     if (server != null)
51       return server;
52     if (!isOffline) {
53       throw new SQLException(
54           "Connect before taking online backup...");
55     }
56     try {
57       if (workingMode == SERVER_MODE) {
58         _RmiServer rmiServer = (_RmiServer) Naming.lookup("rmi://" + hostName +
59             ":" + portNumber + "/DaffodilDB");
60         return new RmiServer(rmiServer);
61       }
62       else {
63         return ( (_Server) Class.forName(
64             "com.daffodilwoods.daffodildb.server.serversystem.ServerSystem").
65                 newInstance());
66       }
67     }
68     catch (Exception JavaDoc ex) {
69       throw new SQLException("DaffodilDB Engine not found.....");
70     }
71
72   }
73
74   protected Properties breakURL(String JavaDoc string, Properties properties) throws
75       SQLException {
76     throw new UnsupportedOperationException JavaDoc(
77         "Method breakURL yet not implemented");
78   }
79
80   protected Connection JavaDoc getConnection(String JavaDoc string, Properties properties) throws
81       SQLException {
82     throw new UnsupportedOperationException JavaDoc(
83         "Method getConnection yet not implemented");
84   }
85
86   public boolean acceptsURL(String JavaDoc string) throws SQLException {
87     throw new UnsupportedOperationException JavaDoc(
88         "Method acceptsURL yet not implemented");
89   }
90
91   public DriverPropertyInfo[] getPropertyInfo(String JavaDoc string,
92                                               Properties properties) throws
93       SQLException {
94     throw new UnsupportedOperationException JavaDoc(
95         "Method getPropertyInfo yet not implemented");
96   }
97
98   public void takeOfflinebakupServer(String JavaDoc hostName, int port, String JavaDoc userName,
99                                      String JavaDoc password,
100                                      String JavaDoc databaseNameSource,
101                                      String JavaDoc databaseNameDestination,
102                                      String JavaDoc path,
103                                      boolean overwrite) throws SQLException {
104
105     print("Going to take offline backup ........");
106     this.hostName = hostName;
107     this.portNumber = port;
108     try {
109       this.isOffline = true;
110       getServer("DaffodilDB").offlineBackup(userName, password, path,
111                                             databaseNameSource,
112                                             databaseNameDestination, overwrite);
113     }
114     catch (DException ex) {
115       throw new SQLException(ex.getMessage());
116     }
117     print("Backup taken successfully............");
118   }
119
120   public void takeOfflinebakupEmbedded(String JavaDoc sourcePath, String JavaDoc userName,
121                                        String JavaDoc password,
122                                        String JavaDoc databaseNameSource,
123                                        String JavaDoc databaseNameDestination,
124                                        String JavaDoc path,
125                                        boolean overwrite) throws SQLException {
126     print("Going to take offline backup ........");
127     try {
128       this.isOffline = true;
129       System.setProperty("daffodilDB_home", sourcePath);
130       getServer("DaffodilDB").offlineBackup(userName, password, path,
131                                             databaseNameSource,
132                                             databaseNameDestination, overwrite);
133     }
134     catch (DException ex) {
135       throw new SQLException(ex.getMessage());
136     }
137     print("Backup taken successfully............");
138   }
139
140   public void takeOnlineBackup(String JavaDoc path, String JavaDoc sourceDatabase,
141                                String JavaDoc destinationDatabase, boolean overwrite) throws
142       SQLException {
143     print("Going to take offline backup ........");
144     try {
145       getServer("DaffodilDB").getInconsistentOnlineBackup(path, sourceDatabase,
146           destinationDatabase,
147           overwrite);
148     }
149     catch (DException ex) {
150       throw new SQLException(ex.getMessage());
151     }
152     print("Backup taken successfully............");
153   }
154
155   public void restoreBackup(String JavaDoc userName, String JavaDoc password,
156                             String JavaDoc databaseNameSource,
157                             String JavaDoc databaseNameDestination, String JavaDoc path,
158                             boolean overwrite) throws
159       SQLException {
160     print("Going to take offline backup ........");
161     try {
162       getServer("DaffodilDB").restore(userName, password, path,
163                                       databaseNameSource,
164                                       databaseNameDestination, overwrite);
165
166     }
167     catch (DException ex) {
168       throw new SQLException(ex.getMessage());
169     }
170     print("Backup taken successfully............");
171   }
172
173   private void print(String JavaDoc str) {
174     if (verbose)
175          ;//// Removed By Program ** System.out.println(str);
176
}
177
178   public void setVerbose(boolean verbose) {
179     this.verbose = verbose;
180   }
181
182 }
183
Popular Tags