KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backup > DerbyEmbeddedBackuper


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: c-jdbc@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or any later
9  * version.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________.
22  */

23
24 package org.objectweb.cjdbc.controller.backup;
25
26 import java.io.File JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import org.objectweb.cjdbc.common.exceptions.BackupException;
31 import org.objectweb.cjdbc.common.log.Trace;
32 import org.objectweb.cjdbc.common.util.FileManagement;
33 import org.objectweb.cjdbc.common.util.Zipper;
34 import org.objectweb.cjdbc.controller.backend.DatabaseBackend;
35
36 /**
37  * This class defines a Backuper for Apache Derby databases.
38  * <p>
39  * Supported URLs are jdbc:derby:PathToDerbyDatabase[;options]
40  * <p>
41  * The Backuper itself does not take any option. It simply dumps the Derby
42  * directory into a zip file.
43  *
44  * @author <a HREF="mailto:emmanuel.cecchet@emicnetworks.com">Emmanuel Cecchet</a>
45  * @version 1.0
46  */

47 public class DerbyEmbeddedBackuper implements Backuper
48 {
49   static Trace logger = Trace.getLogger(DerbyEmbeddedBackuper.class.getName());
50
51   /**
52    * Creates a new <code>DerbyEmbeddedBackuper</code> object
53    */

54   public DerbyEmbeddedBackuper()
55   {
56   }
57
58   /**
59    * @see org.objectweb.cjdbc.controller.backup.Backuper#getDumpFormat()
60    */

61   public String JavaDoc getDumpFormat()
62   {
63     return "Derby embedded compressed dump";
64   }
65
66   /**
67    * @see org.objectweb.cjdbc.controller.backup.Backuper#getOptions()
68    */

69   public String JavaDoc getOptions()
70   {
71     return null;
72   }
73
74   /**
75    * @see org.objectweb.cjdbc.controller.backup.Backuper#setOptions(java.lang.String)
76    */

77   public void setOptions(String JavaDoc options)
78   {
79     // Ignored, no options
80
}
81
82   /**
83    * @see org.objectweb.cjdbc.controller.backup.Backuper#backup(org.objectweb.cjdbc.controller.backend.DatabaseBackend,
84    * java.lang.String, java.lang.String, java.lang.String,
85    * java.lang.String, java.util.ArrayList)
86    */

87   public Date JavaDoc backup(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
88       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
89   {
90     String JavaDoc derbyPath = getDerbyPath(backend, true);
91
92     try
93     {
94       File JavaDoc pathDir = new File JavaDoc(path);
95       if (!pathDir.exists())
96       {
97         pathDir.mkdirs();
98         pathDir.mkdir();
99       }
100
101       if (logger.isDebugEnabled())
102         logger.debug("Archiving " + derbyPath + " in " + path + File.separator
103             + dumpName + Zipper.ZIP_EXT);
104
105       Zipper.zip(getDumpPhysicalPath(path, dumpName), derbyPath,
106           Zipper.STORE_PATH_FROM_ZIP_ROOT);
107     }
108     catch (Exception JavaDoc e)
109     {
110       String JavaDoc msg = "Error while performing backup";
111       logger.error(msg, e);
112       throw new BackupException(msg, e);
113     }
114
115     return new Date JavaDoc(System.currentTimeMillis());
116   }
117
118   /**
119    * @see org.objectweb.cjdbc.controller.backup.Backuper#restore(org.objectweb.cjdbc.controller.backend.DatabaseBackend,
120    * java.lang.String, java.lang.String, java.lang.String,
121    * java.lang.String, java.util.ArrayList)
122    */

123   public void restore(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
124       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
125   {
126     String JavaDoc derbyPath = getDerbyPath(backend, false);
127
128     File JavaDoc derbyDir = new File JavaDoc(derbyPath);
129
130     // First delete any existing directory
131
if (FileManagement.deleteDir(derbyDir))
132       logger.info("Existing Derby directory " + derbyPath
133           + " has been deleted.");
134
135     // Now create the dir
136
derbyDir.mkdirs();
137     derbyDir.mkdir();
138
139     // Unzip the dump
140
try
141     {
142       if (logger.isDebugEnabled())
143         logger.debug("Uncompressing dump");
144       Zipper.unzip(getDumpPhysicalPath(path, dumpName), derbyPath);
145     }
146     catch (Exception JavaDoc e)
147     {
148       String JavaDoc msg = "Error while uncompressing dump";
149       logger.error(msg, e);
150       throw new BackupException(msg, e);
151     }
152   }
153
154   /**
155    * @see org.objectweb.cjdbc.controller.backup.Backuper#deleteDump(java.lang.String,
156    * java.lang.String)
157    */

158   public void deleteDump(String JavaDoc path, String JavaDoc dumpName) throws BackupException
159   {
160     File JavaDoc toRemove = new File JavaDoc(getDumpPhysicalPath(path, dumpName));
161     if (logger.isDebugEnabled())
162       logger.debug("Deleting compressed dump " + toRemove);
163     toRemove.delete();
164   }
165
166   /**
167    * Get the dump physical path from its logical name
168    *
169    * @param path the path where the dump is stored
170    * @param dumpName dump logical name
171    * @return path to zip file
172    */

173   private String JavaDoc getDumpPhysicalPath(String JavaDoc path, String JavaDoc dumpName)
174   {
175     return path + File.separator + dumpName + Zipper.ZIP_EXT;
176   }
177
178   /**
179    * Extract the path where the Derby database is stored by parsing the backend
180    * JDBC URL.
181    *
182    * @param backend the Derby backend
183    * @param checkPath if true we check if the path is a valid directory
184    * @return path to the Derby database
185    * @throws BackupException if the URL is not valid or the path not valid
186    */

187   private String JavaDoc getDerbyPath(DatabaseBackend backend, boolean checkPath)
188       throws BackupException
189   {
190     String JavaDoc url = backend.getURL();
191     if (!url.startsWith("jdbc:derby:"))
192       throw new BackupException("Unsupported url " + url
193           + " expecting jdbc:derby:pathToDb");
194
195     // Strip 'jdbc:derby:'
196
// 11 = "jdbc:derby:".length()
197
String JavaDoc derbyPath = url.substring(11);
198     // Remove all options that are after the first semicolon
199
int semicolon = derbyPath.indexOf(';');
200     if (semicolon > -1)
201       derbyPath = derbyPath.substring(0, semicolon);
202
203     if (checkPath)
204     {
205       File JavaDoc checkDerbyPath = new File JavaDoc(derbyPath);
206       if (!checkDerbyPath.isDirectory())
207         throw new BackupException(
208             "Directory "
209                 + derbyPath
210                 + " does not exist. This might be due to an unsupported URL format (expectin jdbc:derby:pathToDb)");
211     }
212
213     return derbyPath;
214   }
215
216   /**
217    * @see org.objectweb.cjdbc.controller.backup.Backuper#fetchDump(org.objectweb.cjdbc.controller.backup.DumpTransferInfo,
218    * java.lang.String, java.lang.String)
219    */

220   public void fetchDump(DumpTransferInfo dumpTransferInfo, String JavaDoc path,
221       String JavaDoc dumpName) throws BackupException
222   {
223     // TODO: Auto-generated method stub
224
// getBackupManager().fetchDump(this, backuperServerAddress, sessionKey,
225
// path, dumpName);
226
}
227
228   /**
229    * @see org.objectweb.cjdbc.controller.backup.Backuper#setupServer()
230    */

231   public DumpTransferInfo setupServer()
232   {
233     // TODO Auto-generated method stub
234
return null;
235   }
236
237   /**
238    * @see org.objectweb.cjdbc.controller.backup.Backuper#getBackupManager()
239    */

240   public BackupManager getBackupManager()
241   {
242     // TODO: Auto-generated method stub
243
return null;
244     // return BackupManager.TheOneAndOnlyBackupManager;
245
}
246
247 }
248
Popular Tags