()
| 105 | } |
| 106 | |
| 107 | public synchronized Database initDatabase() throws IllegalStateException, DatabaseException { |
| 108 | if (dbProperties != null) { |
| 109 | throw new IllegalStateException("Database already initialised"); |
| 110 | } |
| 111 | File file = new File(Constant.getZapHome() + File.separator + "db", "db.properties"); |
| 112 | if (!file.exists()) { |
| 113 | file = new File(Constant.getZapInstall() + File.separator + "db", "db.properties"); |
| 114 | } |
| 115 | if (!file.exists()) { |
| 116 | throw new DatabaseException("Missing DB properties file: " + file.getAbsolutePath()); |
| 117 | } |
| 118 | |
| 119 | dbProperties = new Properties(); |
| 120 | try (Reader reader = new FileReader(file)) { |
| 121 | dbProperties.load(reader); |
| 122 | } catch (IOException e) { |
| 123 | throw new DatabaseException("I/O error while reading DB properties file.", e); |
| 124 | } |
| 125 | dbType = dbProperties.getProperty("db.type"); |
| 126 | |
| 127 | // Load the SQL properties |
| 128 | sqlProperties = new Properties(); |
| 129 | File sqlFile = |
| 130 | new File(Constant.getZapInstall() + File.separator + "db", dbType + ".properties"); |
| 131 | try (Reader sqlReader = new FileReader(sqlFile)) { |
| 132 | sqlProperties.load(sqlReader); |
| 133 | } catch (Exception e) { |
| 134 | LOGGER.error("No SQL properties file for db type {}", sqlFile.getAbsolutePath()); |
| 135 | throw new DatabaseException( |
| 136 | "Missing SQL properties file: " + sqlFile.getAbsolutePath()); |
| 137 | } |
| 138 | |
| 139 | String className = dbProperties.getProperty("db.class"); |
| 140 | Object dbObj; |
| 141 | try { |
| 142 | Class<?> dbClass = Class.forName(className); |
| 143 | dbObj = dbClass.getDeclaredConstructor().newInstance(); |
| 144 | } catch (Exception e) { |
| 145 | throw new DatabaseException("Failed to create the instance for: " + className, e); |
| 146 | } |
| 147 | |
| 148 | if (!(dbObj instanceof Database)) { |
| 149 | throw new DatabaseException( |
| 150 | "db.class is not an instance of Database: " |
| 151 | + dbObj.getClass().getCanonicalName()); |
| 152 | } |
| 153 | return (Database) dbObj; |
| 154 | } |
| 155 | |
| 156 | public static void addSqlProperties(InputStream inStream) throws IOException { |
| 157 | sqlProperties.load(inStream); |
no test coverage detected