OSDN Git Service

4e558a77caee26a5a9ee33d8896faaf910d0e2a6
[coroid/NicoBrowser.git] / src / nicobrowser / update / DBUpdater.java
1 /** */
2 package nicobrowser.update;
3
4 import java.io.BufferedInputStream;
5 import java.io.BufferedOutputStream;
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import liquibase.commandline.Main;
11 import liquibase.exception.CommandLineParsingException;
12 import nicobrowser.Config;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 public class DBUpdater {
17
18     private static Log log = LogFactory.getLog(DBUpdater.class);
19     private static final String UPDATE_FILE = "db_update_script.xml";
20     private static final String SYNC_FILE = "db_for_sync_script.xml";
21
22     public void sync() throws IOException {
23         File updateFile = new File(Config.getAppHome(), UPDATE_FILE);
24         InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + SYNC_FILE);
25
26         createFile(resource, updateFile);
27
28         String[] args = new String[]{
29             "--driver=org.h2.Driver",
30             "--changeLogFile=" + updateFile.toString(),
31             "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
32             "--username=sa",
33             "changeLogSync"};
34         try {
35             Main.main(args);
36
37             log.info("DBのアップデートが終了しました.");
38         } catch (CommandLineParsingException ex) {
39             log.fatal("DBのアップデートに失敗しました.", ex);
40             throw new IOException(ex);
41         }
42     }
43
44     public void update() throws IOException {
45         File updateFile = new File(Config.getAppHome(), UPDATE_FILE);
46         InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + UPDATE_FILE);
47
48         createFile(resource, updateFile);
49
50         String[] args = new String[]{
51             "--driver=org.h2.Driver",
52             "--changeLogFile=" + updateFile.toString(),
53             "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
54             "--username=sa",
55             "update"};
56         try {
57             Main.main(args);
58
59             log.info("DBのアップデートが終了しました.");
60         } catch (CommandLineParsingException ex) {
61             log.fatal("DBのアップデートに失敗しました.", ex);
62             throw new IOException(ex);
63         }
64     }
65
66     private void createFile(InputStream resource, File updateFile) throws IOException {
67         BufferedInputStream is = null;
68         BufferedOutputStream os = null;
69         try {
70             is = new BufferedInputStream(resource);
71             os = new BufferedOutputStream(new FileOutputStream(updateFile));
72             byte[] b = new byte[1024];
73             int l;
74             while ((l = is.read(b)) != -1) {
75                 os.write(b, 0, l);
76             }
77         } finally {
78             try {
79                 if (is != null) {
80                     is.close();
81                 }
82             } catch (IOException ex) {
83             }
84             if (os != null) {
85                 try {
86                     os.close();
87                 } catch (IOException ex) {
88                 }
89             }
90         }
91     }
92 }