Autopsy  4.7.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-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.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  }
82  } catch (SQLException ex) {
83  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
84  }
85  }
86 
87  @Override
88  public void updateSettings() {
89  synchronized (this) {
90  dbSettings.loadSettings();
91  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
92  }
93  }
94 
95  @Override
96  public void saveSettings() {
97  synchronized (this) {
98  dbSettings.saveSettings();
99  }
100  }
101 
102  @Override
103  public void reset() throws EamDbException {
104  Connection conn = connect();
105 
106  try {
107  Statement dropContent = conn.createStatement();
108  dropContent.executeUpdate("TRUNCATE TABLE organizations RESTART IDENTITY CASCADE");
109  dropContent.executeUpdate("TRUNCATE TABLE cases RESTART IDENTITY CASCADE");
110  dropContent.executeUpdate("TRUNCATE TABLE data_sources RESTART IDENTITY CASCADE");
111  dropContent.executeUpdate("TRUNCATE TABLE reference_sets RESTART IDENTITY CASCADE");
112  dropContent.executeUpdate("TRUNCATE TABLE correlation_types RESTART IDENTITY CASCADE");
113  dropContent.executeUpdate("TRUNCATE TABLE db_info RESTART IDENTITY CASCADE");
114 
115  String instancesTemplate = "TRUNCATE TABLE %s_instances RESTART IDENTITY CASCADE";
116  String referencesTemplate = "TRUNCATE TABLE reference_%s RESTART IDENTITY CASCADE";
117  for (CorrelationAttribute.Type type : defaultCorrelationTypes) {
118  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
119  // FUTURE: support other reference types
120  if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
121  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
122  }
123  }
124  } catch (SQLException ex) {
125  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
126  } finally {
127  EamDbUtil.closeConnection(conn);
128  }
129 
130  dbSettings.insertDefaultDatabaseContent();
131  }
132 
137  private void setupConnectionPool() throws EamDbException {
138  connectionPool = new BasicDataSource();
139  connectionPool.setUsername(dbSettings.getUserName());
140  connectionPool.setPassword(dbSettings.getPassword());
141  connectionPool.setDriverClassName(dbSettings.getDriver());
142 
143  StringBuilder connectionURL = new StringBuilder();
144  connectionURL.append(dbSettings.getJDBCBaseURI());
145  connectionURL.append(dbSettings.getHost());
146  connectionURL.append(":");
147  connectionURL.append(dbSettings.getPort());
148  connectionURL.append("/");
149  connectionURL.append(dbSettings.getDbName());
150 
151  connectionPool.setUrl(connectionURL.toString());
152  connectionPool.setUsername(dbSettings.getUserName());
153  connectionPool.setPassword(dbSettings.getPassword());
154 
155  // tweak pool configuration
156  connectionPool.setInitialSize(5); // start with 5 connections
157  connectionPool.setMaxIdle(CONN_POOL_SIZE); // max of 10 idle connections
158  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
159  }
160 
168  @Override
169  protected Connection connect() throws EamDbException {
170  synchronized (this) {
171  if (!EamDb.isEnabled()) {
172  throw new EamDbException("Central Repository module is not enabled"); // NON-NLS
173  }
174 
175  if (connectionPool == null) {
176  setupConnectionPool();
177  }
178  }
179 
180  try {
181  return connectionPool.getConnection();
182  } catch (SQLException ex) {
183  throw new EamDbException("Error getting connection from connection pool.", ex); // NON-NLS
184  }
185  }
186 
187  @Override
188  protected String getConflictClause() {
189  return CONFLICT_CLAUSE;
190  }
191 
200  @Override
201  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException{
202  try {
203  // First check if multi user mode is enabled - if not there's no point trying to get a lock
204  if( ! UserPreferences.getIsMultiUserModeEnabled()){
205  return null;
206  }
207 
208  String databaseNodeName = dbSettings.getHost() + "_" + dbSettings.getDbName();
209  CoordinationService.Lock lock = CoordinationService.getInstance().tryGetExclusiveLock(CoordinationService.CategoryNode.CENTRAL_REPO, databaseNodeName, 5, TimeUnit.MINUTES);
210 
211  if(lock != null){
212  return lock;
213  }
214  throw new EamDbException("Error acquiring database lock");
215  } catch (InterruptedException ex){
216  throw new EamDbException("Error acquiring database lock");
217  } catch (CoordinationService.CoordinationServiceException ex) {
218  // This likely just means the coordination service isn't running, which is ok
219  return null;
220  }
221  }
222 
223 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.