001package co.codewizards.cloudstore.core.util;
002
003import static co.codewizards.cloudstore.core.util.Util.*;
004
005import java.io.File;
006import java.sql.DriverManager;
007import java.sql.SQLException;
008
009public class DerbyUtil {
010
011        /**
012         * The Derby database was shut down successfully.
013         */
014        private static final int DERBY_ERROR_CODE_SHUTDOWN_DATABASE_SUCCESSFULLY = 45000;
015        /**
016         * The Derby database which was shut down was not running (the shut down had no effect).
017         */
018        private static final int DERBY_ERROR_CODE_SHUTDOWN_DATABASE_WAS_NOT_RUNNING = 40000;
019
020        private DerbyUtil() { }
021
022        public static void shutdownDerbyDatabase(String connectionURL) {
023                String shutdownConnectionURL = assertNotNull("connectionURL", connectionURL) + ";shutdown=true";
024                try {
025                        DriverManager.getConnection(shutdownConnectionURL);
026                } catch (SQLException e) {
027                        int errorCode = e.getErrorCode();
028                        if (DERBY_ERROR_CODE_SHUTDOWN_DATABASE_SUCCESSFULLY != errorCode &&
029                                        DERBY_ERROR_CODE_SHUTDOWN_DATABASE_WAS_NOT_RUNNING != errorCode) {
030                                throw new RuntimeException(e);
031                        }
032                }
033        }
034
035        public static void setLogFile(File file) {
036                System.setProperty("derby.stream.error.file", assertNotNull("file", file).getAbsolutePath());
037        }
038}