Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
EamDbUtil.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2017 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.Connection;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.List;
27 import java.util.logging.Level;
31 
35 public class EamDbUtil {
36 
37  private final static Logger LOGGER = Logger.getLogger(EamDbUtil.class.getName());
38  private static final String CENTRAL_REPO_NAME = "CentralRepository";
39  private static final String CENTRAL_REPO_USE_KEY = "db.useCentralRepo";
40 
48  public static void closePreparedStatement(PreparedStatement preparedStatement) {
49  if (null != preparedStatement) {
50  try {
51  preparedStatement.close();
52  } catch (SQLException ex) {
53  LOGGER.log(Level.SEVERE, "Error closing PreparedStatement.", ex);
54  }
55  }
56  }
57 
65  public static void closeResultSet(ResultSet resultSet) {
66  if (null != resultSet) {
67  try {
68  resultSet.close();
69  } catch (SQLException ex) {
70  LOGGER.log(Level.SEVERE, "Error closing ResultSet.", ex);
71  }
72  }
73  }
74 
82  public static void closeConnection(Connection conn) {
83  if (null != conn) {
84  try {
85  conn.close();
86  } catch (SQLException ex) {
87  LOGGER.log(Level.SEVERE, "Error closing Connection.", ex);
88  }
89  }
90  }
91 
99  public static boolean insertDefaultCorrelationTypes(Connection conn) {
100  PreparedStatement preparedStatement = null;
101  String sql = "INSERT INTO correlation_types(id, display_name, db_table_name, supported, enabled) VALUES (?, ?, ?, ?, ?)";
102 
103  try {
104  List<CorrelationAttribute.Type> DEFAULT_CORRELATION_TYPES = CorrelationAttribute.getDefaultCorrelationTypes();
105  preparedStatement = conn.prepareStatement(sql);
106  for (CorrelationAttribute.Type newType : DEFAULT_CORRELATION_TYPES) {
107  preparedStatement.setInt(1, newType.getId());
108  preparedStatement.setString(2, newType.getDisplayName());
109  preparedStatement.setString(3, newType.getDbTableName());
110  preparedStatement.setInt(4, newType.isSupported() ? 1 : 0);
111  preparedStatement.setInt(5, newType.isEnabled() ? 1 : 0);
112 
113  preparedStatement.addBatch();
114  }
115  preparedStatement.executeBatch();
116  } catch (EamDbException | SQLException ex) {
117  LOGGER.log(Level.SEVERE, "Error inserting default correlation types.", ex); // NON-NLS
118  return false;
119  } finally {
120  EamDbUtil.closePreparedStatement(preparedStatement);
121  }
122  return true;
123  }
124 
135  public static boolean insertSchemaVersion(Connection conn) {
136  PreparedStatement preparedStatement = null;
137  String sql = "INSERT INTO db_info (name, value) VALUES (?, ?)";
138  try {
139  preparedStatement = conn.prepareStatement(sql);
140  preparedStatement.setString(1, "SCHEMA_VERSION");
141  preparedStatement.setString(2, String.valueOf(SCHEMA_VERSION));
142  preparedStatement.executeUpdate();
143  } catch (SQLException ex) {
144  LOGGER.log(Level.SEVERE, "Error adding schema version to db_info.", ex);
145  return false;
146  } finally {
147  EamDbUtil.closePreparedStatement(preparedStatement);
148  }
149  return true;
150  }
151 
157  public static boolean schemaVersionIsSet(Connection conn) {
158  if (null == conn) {
159  return false;
160  }
161 
162  ResultSet resultSet = null;
163  try {
164  Statement tester = conn.createStatement();
165  String sql = "SELECT value FROM db_info WHERE name='SCHEMA_VERSION'";
166  resultSet = tester.executeQuery(sql);
167  if (resultSet.next()) {
168  String value = resultSet.getString("value");
169  }
170  } catch (SQLException ex) {
171  return false;
172  } finally {
173  EamDbUtil.closeResultSet(resultSet);
174  }
175  return true;
176  }
177 
184  public static boolean useCentralRepo() {
185  return Boolean.parseBoolean(ModuleSettings.getConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY));
186  }
187 
195  public static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected) {
196  ModuleSettings.setConfigSetting(CENTRAL_REPO_NAME, CENTRAL_REPO_USE_KEY, Boolean.toString(centralRepoCheckBoxIsSelected));
197  }
198 
205  public static boolean executeValidationQuery(Connection conn, String validationQuery) {
206  if (null == conn) {
207  return false;
208  }
209 
210  ResultSet resultSet = null;
211  try {
212  Statement tester = conn.createStatement();
213  resultSet = tester.executeQuery(validationQuery);
214  if (resultSet.next()) {
215  return true;
216  }
217  } catch (SQLException ex) {
218  return false;
219  } finally {
220  EamDbUtil.closeResultSet(resultSet);
221  }
222 
223  return false;
224  }
225 
234  return type.getDbTableName() + "_instances";
235  }
236 
245  return "reference_" + type.getDbTableName();
246  }
247 
248 }
static boolean executeValidationQuery(Connection conn, String validationQuery)
Definition: EamDbUtil.java:205
static String correlationTypeToReferenceTableName(CorrelationAttribute.Type type)
Definition: EamDbUtil.java:244
static synchronized void setConfigSetting(String moduleName, String settingName, String settingVal)
static void setUseCentralRepo(boolean centralRepoCheckBoxIsSelected)
Definition: EamDbUtil.java:195
static boolean insertDefaultCorrelationTypes(Connection conn)
Definition: EamDbUtil.java:99
static String correlationTypeToInstanceTableName(CorrelationAttribute.Type type)
Definition: EamDbUtil.java:233
static String getConfigSetting(String moduleName, String settingName)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static void closePreparedStatement(PreparedStatement preparedStatement)
Definition: EamDbUtil.java:48

Copyright © 2012-2016 Basis Technology. Generated on: Fri Sep 29 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.