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

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