MCPcopy Create free account
hub / github.com/zaproxy/zaproxy / initDatabase

Method initDatabase

zap/src/main/java/org/zaproxy/zap/db/sql/DbSQL.java:107–154  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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);

Callers 1

initMethod · 0.80

Calls 7

getZapHomeMethod · 0.95
getZapInstallMethod · 0.95
getAbsolutePathMethod · 0.80
loadMethod · 0.45
getPropertyMethod · 0.45
errorMethod · 0.45
newInstanceMethod · 0.45

Tested by

no test coverage detected