KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backup > backupers > DerbyEmbeddedBackuper


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2005 Emic Networks
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

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

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

53   public DerbyEmbeddedBackuper()
54   {
55   }
56
57   /**
58    * @see org.continuent.sequoia.controller.backup.Backuper#getDumpFormat()
59    */

60   public String JavaDoc getDumpFormat()
61   {
62     return "Derby embedded compressed dump";
63   }
64
65   /**
66    * @see org.continuent.sequoia.controller.backup.Backuper#backup(org.continuent.sequoia.controller.backend.DatabaseBackend,
67    * java.lang.String, java.lang.String, java.lang.String,
68    * java.lang.String, java.util.ArrayList)
69    */

70   public Date JavaDoc backup(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
71       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
72   {
73     String JavaDoc derbyPath = getDerbyPath(backend, true);
74
75     try
76     {
77       File JavaDoc pathDir = new File JavaDoc(path);
78       if (!pathDir.exists())
79       {
80         pathDir.mkdirs();
81         pathDir.mkdir();
82       }
83
84       if (logger.isDebugEnabled())
85         logger.debug("Archiving " + derbyPath + " in " + path + File.separator
86             + dumpName + Zipper.ZIP_EXT);
87
88       Zipper.zip(getDumpPhysicalPath(path, dumpName), derbyPath,
89           Zipper.STORE_PATH_FROM_ZIP_ROOT);
90     }
91     catch (Exception JavaDoc e)
92     {
93       String JavaDoc msg = "Error while performing backup";
94       logger.error(msg, e);
95       throw new BackupException(msg, e);
96     }
97
98     return new Date JavaDoc(System.currentTimeMillis());
99   }
100
101   /**
102    * @see org.continuent.sequoia.controller.backup.Backuper#restore(org.continuent.sequoia.controller.backend.DatabaseBackend,
103    * java.lang.String, java.lang.String, java.lang.String,
104    * java.lang.String, java.util.ArrayList)
105    */

106   public void restore(DatabaseBackend backend, String JavaDoc login, String JavaDoc password,
107       String JavaDoc dumpName, String JavaDoc path, ArrayList JavaDoc tables) throws BackupException
108   {
109     String JavaDoc derbyPath = getDerbyPath(backend, false);
110
111     File JavaDoc derbyDir = new File JavaDoc(derbyPath);
112
113     // First delete any existing directory
114
if (FileManagement.deleteDir(derbyDir))
115       logger.info("Existing Derby directory " + derbyPath
116           + " has been deleted.");
117
118     // Now create the dir
119
derbyDir.mkdirs();
120     derbyDir.mkdir();
121
122     // Unzip the dump
123
try
124     {
125       if (logger.isDebugEnabled())
126         logger.debug("Uncompressing dump");
127       Zipper.unzip(getDumpPhysicalPath(path, dumpName), derbyPath);
128     }
129     catch (Exception JavaDoc e)
130     {
131       String JavaDoc msg = "Error while uncompressing dump";
132       logger.error(msg, e);
133       throw new BackupException(msg, e);
134     }
135   }
136
137   /**
138    * @see org.continuent.sequoia.controller.backup.Backuper#deleteDump(java.lang.String,
139    * java.lang.String)
140    */

141   public void deleteDump(String JavaDoc path, String JavaDoc dumpName) throws BackupException
142   {
143     File JavaDoc toRemove = new File JavaDoc(getDumpPhysicalPath(path, dumpName));
144     if (logger.isDebugEnabled())
145       logger.debug("Deleting compressed dump " + toRemove);
146     toRemove.delete();
147   }
148
149   /**
150    * Get the dump physical path from its logical name
151    *
152    * @param path the path where the dump is stored
153    * @param dumpName dump logical name
154    * @return path to zip file
155    */

156   private String JavaDoc getDumpPhysicalPath(String JavaDoc path, String JavaDoc dumpName)
157   {
158     return path + File.separator + dumpName + Zipper.ZIP_EXT;
159   }
160
161   /**
162    * Extract the path where the Derby database is stored by parsing the backend
163    * JDBC URL.
164    *
165    * @param backend the Derby backend
166    * @param checkPath if true we check if the path is a valid directory
167    * @return path to the Derby database
168    * @throws BackupException if the URL is not valid or the path not valid
169    */

170   private String JavaDoc getDerbyPath(DatabaseBackend backend, boolean checkPath)
171       throws BackupException
172   {
173     String JavaDoc url = backend.getURL();
174     if (!url.startsWith("jdbc:derby:"))
175       throw new BackupException("Unsupported url " + url
176           + " expecting jdbc:derby:pathToDb");
177
178     // Strip 'jdbc:derby:'
179
// 11 = "jdbc:derby:".length()
180
String JavaDoc derbyPath = url.substring(11);
181     // Remove all options that are after the first semicolon
182
int semicolon = derbyPath.indexOf(';');
183     if (semicolon > -1)
184       derbyPath = derbyPath.substring(0, semicolon);
185
186     if (checkPath)
187     {
188       File JavaDoc checkDerbyPath = new File JavaDoc(derbyPath);
189       if (!checkDerbyPath.isDirectory())
190         throw new BackupException(
191             "Directory "
192                 + derbyPath
193                 + " does not exist. This might be due to an unsupported URL format (expectin jdbc:derby:pathToDb)");
194     }
195
196     return derbyPath;
197   }
198
199   /**
200    * @see org.continuent.sequoia.controller.backup.Backuper#fetchDump(org.continuent.sequoia.controller.backup.DumpTransferInfo,
201    * java.lang.String, java.lang.String)
202    */

203   public void fetchDump(DumpTransferInfo dumpTransferInfo, String JavaDoc path,
204       String JavaDoc dumpName) throws BackupException, IOException JavaDoc
205   {
206     super.fetchDump(dumpTransferInfo, path, dumpName + Zipper.ZIP_EXT);
207   }
208
209 }
210
Popular Tags