2 package nicobrowser.update;
4 import java.io.BufferedInputStream;
5 import java.io.BufferedOutputStream;
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;
16 public class DBUpdater {
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";
22 public void sync() throws IOException {
23 File updateFile = new File(Config.getAppHome(), UPDATE_FILE);
24 InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + SYNC_FILE);
26 createFile(resource, updateFile);
28 String[] args = new String[]{
29 "--driver=org.h2.Driver",
30 "--changeLogFile=" + updateFile.toString(),
31 "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
37 log.info("DBのアップデートが終了しました.");
38 } catch (CommandLineParsingException ex) {
39 log.fatal("DBのアップデートに失敗しました.", ex);
40 throw new IOException(ex);
44 public void update() throws IOException {
45 File updateFile = new File(Config.getAppHome(), UPDATE_FILE);
46 InputStream resource = ClassLoader.getSystemResourceAsStream("resources/" + UPDATE_FILE);
48 createFile(resource, updateFile);
50 String[] args = new String[]{
51 "--driver=org.h2.Driver",
52 "--changeLogFile=" + updateFile.toString(),
53 "--url=jdbc:h2:" + Config.getInstance().getDbFile(),
59 log.info("DBのアップデートが終了しました.");
60 } catch (CommandLineParsingException ex) {
61 log.fatal("DBのアップデートに失敗しました.", ex);
62 throw new IOException(ex);
66 private void createFile(InputStream resource, File updateFile) throws IOException {
67 BufferedInputStream is = null;
68 BufferedOutputStream os = null;
70 is = new BufferedInputStream(resource);
71 os = new BufferedOutputStream(new FileOutputStream(updateFile));
72 byte[] b = new byte[1024];
74 while ((l = is.read(b)) != -1) {
82 } catch (IOException ex) {
87 } catch (IOException ex) {