Autopsy  4.8.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PostgresEamDb.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2018 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.SQLException;
23 import java.sql.Statement;
24 import java.util.concurrent.TimeUnit;
25 import java.util.logging.Level;
26 import org.apache.commons.dbcp2.BasicDataSource;
30 
35 final class PostgresEamDb extends AbstractSqlEamDb {
36 
37  private final static Logger LOGGER = Logger.getLogger(PostgresEamDb.class.getName());
38 
39  private final static String CONFLICT_CLAUSE = "ON CONFLICT DO NOTHING";
40 
41  private static PostgresEamDb instance;
42 
43  private static final int CONN_POOL_SIZE = 10;
44  private BasicDataSource connectionPool = null;
45 
46  private final PostgresEamDbSettings dbSettings;
47 
55  public synchronized static PostgresEamDb getInstance() throws EamDbException {
56  if (instance == null) {
57  instance = new PostgresEamDb();
58  }
59 
60  return instance;
61  }
62 
68  private PostgresEamDb() throws EamDbException {
69  dbSettings = new PostgresEamDbSettings();
70  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
71  }
72 
73  @Override
74  public void shutdownConnections() throws EamDbException {
75  try {
76  synchronized(this) {
77  if(connectionPool != null){
78  connectionPool.close();
79  connectionPool = null; // force it to be re-created on next connect()
80  }
81  clearCaches();
82  }
83  } catch (SQLException ex) {
84  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
85  }
86  }
87 
88  @Override
89  public void updateSettings() {
90  synchronized (this) {
91  dbSettings.loadSettings();
92  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
93  }
94  }
95 
96  @Override
97  public void saveSettings() {
98  synchronized (this) {
99  dbSettings.saveSettings();
100  }
101  }
102 
103  @Override
104  public void reset() throws EamDbException {
105  Connection conn = connect();
106 
107  try {
108  Statement dropContent = conn.createStatement();
109  dropContent.executeUpdate("TRUNCATE TABLE organizations RESTART IDENTITY CASCADE");
110  dropContent.executeUpdate("TRUNCATE TABLE cases RESTART IDENTITY CASCADE");
111  dropContent.executeUpdate("TRUNCATE TABLE data_sources RESTART IDENTITY CASCADE");
112  dropContent.executeUpdate("TRUNCATE TABLE reference_sets RESTART IDENTITY CASCADE");
113  dropContent.executeUpdate("TRUNCATE TABLE correlation_types RESTART IDENTITY CASCADE");
114  dropContent.executeUpdate("TRUNCATE TABLE db_info RESTART IDENTITY CASCADE");
115 
116  String instancesTemplate = "TRUNCATE TABLE %s_instances RESTART IDENTITY CASCADE";
117  String referencesTemplate = "TRUNCATE TABLE reference_%s RESTART IDENTITY CASCADE";
118  for (CorrelationAttributeInstance.Type type : defaultCorrelationTypes) {
119  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
120  // FUTURE: support other reference types
121  if (type.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
122  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
123  }
124  }
125  } catch (SQLException ex) {
126  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
127  } finally {
128  EamDbUtil.closeConnection(conn);
129  }
130 
131  dbSettings.insertDefaultDatabaseContent();
132  }
133 
138  private void setupConnectionPool() throws EamDbException {
139  connectionPool = new BasicDataSource();
140  connectionPool.setUsername(dbSettings.getUserName());
141  connectionPool.setPassword(dbSettings.getPassword());
142  connectionPool.setDriverClassName(dbSettings.getDriver());
143 
144  StringBuilder connectionURL = new StringBuilder();
145  connectionURL.append(dbSettings.getJDBCBaseURI());
146  connectionURL.append(dbSettings.getHost());
147  connectionURL.append(":");
148  connectionURL.append(dbSettings.getPort());
149  connectionURL.append("/");
150  connectionURL.append(dbSettings.getDbName());
151 
152  connectionPool.setUrl(connectionURL.toString());
153  connectionPool.setUsername(dbSettings.getUserName());
154  connectionPool.setPassword(dbSettings.getPassword());
155 
156  // tweak pool configuration
157  connectionPool.setInitialSize(5); // start with 5 connections
158  connectionPool.setMaxIdle(CONN_POOL_SIZE); // max of 10 idle connections
159  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
160  }
161 
169  @Override
170  protected Connection connect() throws EamDbException {
171  synchronized (this) {
172  if (!EamDb.isEnabled()) {
173  throw new EamDbException("Central Repository module is not enabled"); // NON-NLS
174  }
175 
176  if (connectionPool == null) {
177  setupConnectionPool();
178  }
179  }
180 
181  try {
182  return connectionPool.getConnection();
183  } catch (SQLException ex) {
184  throw new EamDbException("Error getting connection from connection pool.", ex); // NON-NLS
185  }
186  }
187 
188  @Override
189  protected String getConflictClause() {
190  return CONFLICT_CLAUSE;
191  }
192 
201  @Override
202  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException{
203  try {
204  // First check if multi user mode is enabled - if not there's no point trying to get a lock
205  if( ! UserPreferences.getIsMultiUserModeEnabled()){
206  return null;
207  }
208 
209  String databaseNodeName = dbSettings.getHost() + "_" + dbSettings.getDbName();
210  CoordinationService.Lock lock = CoordinationService.getInstance().tryGetExclusiveLock(CoordinationService.CategoryNode.CENTRAL_REPO, databaseNodeName, 5, TimeUnit.MINUTES);
211 
212  if(lock != null){
213  return lock;
214  }
215  throw new EamDbException("Error acquiring database lock");
216  } catch (InterruptedException ex){
217  throw new EamDbException("Error acquiring database lock");
218  } catch (CoordinationService.CoordinationServiceException ex) {
219  // This likely just means the coordination service isn't running, which is ok
220  return null;
221  }
222  }
223 
224 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2018 Basis Technology. Generated on: Thu Oct 4 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.