1 package net.sf.cantina.datasource;
2
3 import org.apache.log4j.Logger;
4
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import javax.servlet.ServletException;
9 import java.io.*;
10
11 import net.sf.cantina.DataSource;
12
13 /***
14 * @author Stephane JAIS
15 */
16 public class DownloadBackupServlet
17 extends HttpServlet
18 {
19 private static final Logger logger = Logger.getLogger(DownloadBackupServlet.class);
20 /***
21 * This method is synchronized, it will perform poorly when invoked simultaneously by many users.
22 */
23 public synchronized void doPost(HttpServletRequest request, HttpServletResponse response)
24 throws IOException, ServletException
25 {
26 try
27 {
28 File workDir = new File(getServletContext().getRealPath("/WEB-INF/tmp"));
29 String hypersonicPath = workDir.getAbsolutePath()+"/backup";
30 String scriptPath = hypersonicPath+".script";
31 if (!workDir.exists())
32 workDir.mkdir();
33
34 DataSource hypersonicCopy = new HypersonicDataSource(hypersonicPath,true);
35
36 BackupUtils.importDataSource(DataSource.getInstance(),hypersonicCopy);
37 hypersonicCopy.release();
38
39 response.setHeader("Content-Type","application/octet-stream");
40 response.setHeader("Content-Length",""+new File(scriptPath).length());
41
42 InputStream is = new FileInputStream(scriptPath);
43 OutputStream os = response.getOutputStream();
44 byte[] buf = new byte[1024];
45 int i = 0;
46 while ((i=is.read(buf)) != -1)
47 {
48 os.write(buf, 0, i);
49 }
50 is.close();
51 os.close();
52 } catch (Exception e)
53 {
54 logger.error(e);
55 response.sendError(500);
56 }
57
58 }
59
60 }