Autopsy  4.4.1
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.List;
25 import java.util.logging.Level;
26 import org.apache.commons.dbcp2.BasicDataSource;
28 
33 public class PostgresEamDb extends AbstractSqlEamDb {
34 
35  private final static Logger LOGGER = Logger.getLogger(PostgresEamDb.class.getName());
36 
37  private final static String CONFLICT_CLAUSE = "ON CONFLICT DO NOTHING";
38 
39  private static PostgresEamDb instance;
40 
41  private static final int CONN_POOL_SIZE = 10;
42  private BasicDataSource connectionPool = null;
43 
45 
53  public synchronized static PostgresEamDb getInstance() throws EamDbException {
54  if (instance == null) {
55  instance = new PostgresEamDb();
56  }
57 
58  return instance;
59  }
60 
66  private PostgresEamDb() throws EamDbException {
67  dbSettings = new PostgresEamDbSettings();
69  }
70 
71  @Override
72  public void shutdownConnections() throws EamDbException {
73  try {
74  synchronized(this) {
75  if(connectionPool != null){
76  connectionPool.close();
77  connectionPool = null; // force it to be re-created on next connect()
78  }
79  }
80  } catch (SQLException ex) {
81  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
82  }
83  }
84 
85  @Override
86  public void updateSettings() {
87  synchronized (this) {
88  dbSettings.loadSettings();
90  }
91  }
92 
93  @Override
94  public void saveSettings() {
95  synchronized (this) {
96  dbSettings.saveSettings();
97  }
98  }
99 
100  @Override
101  public void reset() throws EamDbException {
102  Connection conn = connect();
103 
104  try {
105  Statement dropContent = conn.createStatement();
106  dropContent.executeUpdate("TRUNCATE TABLE organizations RESTART IDENTITY CASCADE");
107  dropContent.executeUpdate("TRUNCATE TABLE cases RESTART IDENTITY CASCADE");
108  dropContent.executeUpdate("TRUNCATE TABLE data_sources RESTART IDENTITY CASCADE");
109  dropContent.executeUpdate("TRUNCATE TABLE reference_sets RESTART IDENTITY CASCADE");
110  dropContent.executeUpdate("TRUNCATE TABLE correlation_types RESTART IDENTITY CASCADE");
111  dropContent.executeUpdate("TRUNCATE TABLE db_info RESTART IDENTITY CASCADE");
112 
113  String instancesTemplate = "TRUNCATE TABLE %s_instances RESTART IDENTITY CASCADE";
114  String referencesTemplate = "TRUNCATE TABLE reference_%s RESTART IDENTITY CASCADE";
116  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
117  // FUTURE: support other reference types
118  if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
119  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
120  }
121  }
122  } catch (SQLException ex) {
123  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
124  } finally {
126  }
127 
128  dbSettings.insertDefaultDatabaseContent();
129  }
130 
135  private void setupConnectionPool() throws EamDbException {
136  connectionPool = new BasicDataSource();
137  connectionPool.setUsername(dbSettings.getUserName());
138  connectionPool.setPassword(dbSettings.getPassword());
139  connectionPool.setDriverClassName(dbSettings.getDriver());
140 
141  StringBuilder connectionURL = new StringBuilder();
142  connectionURL.append(dbSettings.getJDBCBaseURI());
143  connectionURL.append(dbSettings.getHost());
144  connectionURL.append(":");
145  connectionURL.append(dbSettings.getPort());
146  connectionURL.append("/");
147  connectionURL.append(dbSettings.getDbName());
148 
149  connectionPool.setUrl(connectionURL.toString());
150  connectionPool.setUsername(dbSettings.getUserName());
151  connectionPool.setPassword(dbSettings.getPassword());
152 
153  // tweak pool configuration
154  connectionPool.setInitialSize(5); // start with 5 connections
155  connectionPool.setMaxIdle(CONN_POOL_SIZE); // max of 10 idle connections
156  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
157  }
158 
166  @Override
167  protected Connection connect() throws EamDbException {
168  synchronized (this) {
169  if (!EamDb.isEnabled()) {
170  throw new EamDbException("Central Repository module is not enabled"); // NON-NLS
171  }
172 
173  if (connectionPool == null) {
175  }
176  }
177 
178  try {
179  return connectionPool.getConnection();
180  } catch (SQLException ex) {
181  throw new EamDbException("Error getting connection from connection pool.", ex); // NON-NLS
182  }
183  }
184 
185  @Override
186  protected String getConflictClause() {
187  return CONFLICT_CLAUSE;
188  }
189 
190  @Override
191  public List<String> getBadTags() {
192  return dbSettings.getBadTags();
193  }
194 
195  @Override
196  public void setBadTags(List<String> badTags) {
197  dbSettings.setBadTags(badTags);
198  }
199 
200 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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.