Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteEamDb.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.io.File;
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
28 import java.util.logging.Level;
29 import org.apache.commons.dbcp2.BasicDataSource;
31 import org.sleuthkit.datamodel.TskData;
33 
39 public class SqliteEamDb extends AbstractSqlEamDb {
40 
41  private final static Logger LOGGER = Logger.getLogger(SqliteEamDb.class.getName());
42 
43  private static SqliteEamDb instance;
44 
45  private BasicDataSource connectionPool = null;
46 
48 
49  // While the Sqlite database should only be used for single users, it is still
50  // possible for multiple threads to attempt to write to the database simultaneously.
51  private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(true);
52 
60  public synchronized static SqliteEamDb getInstance() throws EamDbException {
61  if (instance == null) {
62  instance = new SqliteEamDb();
63  }
64 
65  return instance;
66  }
67 
73  private SqliteEamDb() throws EamDbException {
74  dbSettings = new SqliteEamDbSettings();
76  }
77 
78  @Override
79  public void shutdownConnections() throws EamDbException {
80  try {
81  synchronized(this) {
82  if (null != connectionPool) {
83  connectionPool.close();
84  connectionPool = null; // force it to be re-created on next connect()
85  }
86  }
87  } catch (SQLException ex) {
88  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
89  }
90  }
91 
92  @Override
93  public void updateSettings() {
94  synchronized (this) {
95  dbSettings.loadSettings();
97  }
98  }
99 
100  @Override
101  public void saveSettings() {
102  synchronized (this) {
103  dbSettings.saveSettings();
104  }
105  }
106 
107  @Override
108  public void reset() throws EamDbException {
109  try{
111 
112  Connection conn = connect();
113 
114  try {
115 
116  Statement dropContent = conn.createStatement();
117  dropContent.executeUpdate("DELETE FROM organizations");
118  dropContent.executeUpdate("DELETE FROM cases");
119  dropContent.executeUpdate("DELETE FROM data_sources");
120  dropContent.executeUpdate("DELETE FROM reference_sets");
121  dropContent.executeUpdate("DELETE FROM artifact_types");
122  dropContent.executeUpdate("DELETE FROM db_info");
123 
124  String instancesTemplate = "DELETE FROM %s_instances";
125  String referencesTemplate = "DELETE FROM global_files";
127  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
128  // FUTURE: support other reference types
129  if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
130  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
131  }
132  }
133 
134  dropContent.executeUpdate("VACUUM");
135  } catch (SQLException ex) {
136  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
137  } finally {
139  }
140 
141  dbSettings.insertDefaultDatabaseContent();
142  } finally {
144  }
145  }
146 
151  private void setupConnectionPool() throws EamDbException {
152  connectionPool = new BasicDataSource();
153  connectionPool.setDriverClassName(dbSettings.getDriver());
154 
155  StringBuilder connectionURL = new StringBuilder();
156  connectionURL.append(dbSettings.getJDBCBaseURI());
157  connectionURL.append(dbSettings.getDbDirectory());
158  connectionURL.append(File.separator);
159  connectionURL.append(dbSettings.getDbName());
160 
161  connectionPool.setUrl(connectionURL.toString());
162 
163  // tweak pool configuration
164  connectionPool.setInitialSize(50);
165  connectionPool.setMaxTotal(-1);
166  connectionPool.setMaxIdle(-1);
167  connectionPool.setMaxWaitMillis(1000);
168  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
169  }
170 
178  @Override
179  protected Connection connect() throws EamDbException {
180  synchronized (this) {
181  if (!EamDb.isEnabled()) {
182  throw new EamDbException("Central Repository module is not enabled"); // NON-NLS
183  }
184 
185  if (connectionPool == null) {
187  }
188 
189  try {
190  return connectionPool.getConnection();
191  } catch (SQLException ex) {
192  throw new EamDbException("Error getting connection from connection pool.", ex); // NON-NLS
193  }
194  }
195  }
196 
197  @Override
198  protected String getConflictClause() {
199  // For sqlite, our conflict clause is part of the table schema
200  return "";
201  }
202 
203  @Override
204  public List<String> getBadTags() {
205  return dbSettings.getBadTags();
206  }
207 
208  @Override
209  public void setBadTags(List<String> badTags) {
210  dbSettings.setBadTags(badTags);
211  }
212 
221  @Override
222  public void newDbInfo(String name, String value) throws EamDbException {
223  try{
225  super.newDbInfo(name, value);
226  } finally {
228  }
229  }
230 
240  @Override
241  public String getDbInfo(String name) throws EamDbException {
242  try{
244  return super.getDbInfo(name);
245  } finally {
247  }
248  }
249 
258  @Override
259  public void updateDbInfo(String name, String value) throws EamDbException {
260  try{
262  super.updateDbInfo(name, value);
263  } finally {
265  }
266  }
267 
273  @Override
274  public CorrelationCase newCase(Case autopsyCase) throws EamDbException {
275  try{
277  return super.newCase(autopsyCase);
278  } finally {
280  }
281  }
282 
290  @Override
291  public void newCase(CorrelationCase eamCase) throws EamDbException {
292  try{
294  super.newCase(eamCase);
295  } finally {
297  }
298  }
299 
305  @Override
306  public void updateCase(CorrelationCase eamCase) throws EamDbException {
307  try{
309  super.updateCase(eamCase);
310  } finally {
312  }
313  }
314 
322  @Override
323  public CorrelationCase getCaseByUUID(String caseUUID) throws EamDbException {
324  try{
326  return super.getCaseByUUID(caseUUID);
327  } finally {
329  }
330  }
331 
337  @Override
338  public List<CorrelationCase> getCases() throws EamDbException {
339  try{
341  return super.getCases();
342  } finally {
344  }
345  }
346 
352  @Override
353  public void newDataSource(CorrelationDataSource eamDataSource) throws EamDbException {
354  try{
356  super.newDataSource(eamDataSource);
357  } finally {
359  }
360  }
361 
367 // @Override
368 // public void updateDataSource(CorrelationDataSource eamDataSource) throws EamDbException {
369 // try{
370 // acquireExclusiveLock();
371 // super.updateDataSource(eamDataSource);
372 // } finally {
373 // releaseExclusiveLock();
374 // }
375 // }
376 
384  @Override
385  public CorrelationDataSource getDataSourceDetails(String dataSourceDeviceId) throws EamDbException {
386  try{
388  return super.getDataSourceDetails(dataSourceDeviceId);
389  } finally {
391  }
392  }
393 
399  @Override
400  public List<CorrelationDataSource> getDataSources() throws EamDbException {
401  try{
403  return super.getDataSources();
404  } finally {
406  }
407  }
408 
415  @Override
416  public void addArtifact(CorrelationAttribute eamArtifact) throws EamDbException {
417  try{
419  super.addArtifact(eamArtifact);
420  } finally {
422  }
423  }
424 
433  @Override
434  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value) throws EamDbException {
435  try{
437  return super.getArtifactInstancesByTypeValue(aType, value);
438  } finally {
440  }
441  }
442 
454  @Override
455  public List<CorrelationAttributeInstance> getArtifactInstancesByPath(CorrelationAttribute.Type aType, String filePath) throws EamDbException {
456  try{
458  return super.getArtifactInstancesByPath(aType, filePath);
459  } finally {
461  }
462  }
463 
474  @Override
476  try{
478  return super.getCountArtifactInstancesByTypeValue(aType, value);
479  } finally {
481  }
482  }
483 
484  @Override
486  try{
488  return super.getFrequencyPercentage(corAttr);
489  } finally {
491  }
492  }
493 
504  @Override
506  try{
508  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
509  } finally {
511  }
512  }
513 
514 
515  @Override
517  try{
519  return super.getCountUniqueDataSources();
520  } finally {
522  }
523  }
524 
536  @Override
537  public Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID) throws EamDbException {
538  try{
540  return super.getCountArtifactInstancesByCaseDataSource(caseUUID, dataSourceID);
541  } finally {
543  }
544  }
545 
550  @Override
551  public void bulkInsertArtifacts() throws EamDbException {
552  try{
554  super.bulkInsertArtifacts();
555  } finally {
557  }
558  }
559 
563  @Override
564  public void bulkInsertCases(List<CorrelationCase> cases) throws EamDbException {
565  try{
567  super.bulkInsertCases(cases);
568  } finally {
570  }
571  }
572 
579  @Override
580  public void setArtifactInstanceKnownStatus(CorrelationAttribute eamArtifact, TskData.FileKnown knownStatus) throws EamDbException {
581  try{
583  super.setArtifactInstanceKnownStatus(eamArtifact, knownStatus);
584  } finally {
586  }
587  }
588 
598  @Override
599  public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value) throws EamDbException {
600  try{
602  return super.getArtifactInstancesKnownBad(aType, value);
603  } finally {
605  }
606  }
607 
616  @Override
618  try{
620  return super.getCountArtifactInstancesKnownBad(aType, value);
621  } finally {
623  }
624  }
625 
638  @Override
640  try{
642  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
643  } finally {
645  }
646  }
647 
656  @Override
657  public boolean isArtifactlKnownBadByReference(CorrelationAttribute.Type aType, String value) throws EamDbException {
658  try{
660  return super.isArtifactlKnownBadByReference(aType, value);
661  } finally {
663  }
664  }
665 
673  @Override
674  public void newOrganization(EamOrganization eamOrg) throws EamDbException {
675  try{
677  super.newOrganization(eamOrg);
678  } finally {
680  }
681  }
682 
690  @Override
691  public List<EamOrganization> getOrganizations() throws EamDbException {
692  try{
694  return super.getOrganizations();
695  } finally {
697  }
698  }
699 
709  @Override
711  try{
713  return super.getOrganizationByID(orgID);
714  } finally {
716  }
717  }
718 
728  @Override
729  public int newReferencelSet(EamGlobalSet eamGlobalSet) throws EamDbException {
730  try{
732  return super.newReferencelSet(eamGlobalSet);
733  } finally {
735  }
736  }
737 
747  @Override
748  public EamGlobalSet getReferenceSetByID(int referenceSetID) throws EamDbException {
749  try{
751  return super.getReferenceSetByID(referenceSetID);
752  } finally {
754  }
755  }
756 
766  @Override
767  public void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttribute.Type correlationType) throws EamDbException {
768  try{
770  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
771  } finally {
773  }
774  }
775 
781  @Override
782  public void bulkInsertReferenceTypeEntries(Set<EamGlobalFileInstance> globalInstances, CorrelationAttribute.Type contentType) throws EamDbException {
783  try{
785  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
786  } finally {
788  }
789  }
790 
801  @Override
802  public List<EamGlobalFileInstance> getReferenceInstancesByTypeValue(CorrelationAttribute.Type aType, String aValue) throws EamDbException {
803  try{
805  return super.getReferenceInstancesByTypeValue(aType, aValue);
806  } finally {
808  }
809  }
810 
820  @Override
822  try{
824  return super.newCorrelationType(newType);
825  } finally {
827  }
828  }
829 
839  @Override
841  try{
843  return super.getDefinedCorrelationTypes();
844  } finally {
846  }
847  }
848 
858  @Override
860  try{
862  return super.getEnabledCorrelationTypes();
863  } finally {
865  }
866  }
867 
877  @Override
879  try{
881  return super.getSupportedCorrelationTypes();
882  } finally {
884  }
885  }
886 
894  @Override
896  try{
898  super.updateCorrelationType(aType);
899  } finally {
901  }
902  }
903 
913  @Override
915  try{
917  return super.getCorrelationTypeById(typeId);
918  } finally {
920  }
921  }
922 
928  private void acquireExclusiveLock() {
929  rwLock.writeLock().lock();
930  }
931 
937  private void releaseExclusiveLock() {
938  rwLock.writeLock().unlock();
939  }
940 
946  private void acquireSharedLock() {
947  rwLock.readLock().lock();
948  }
949 
955  private void releaseSharedLock() {
956  rwLock.readLock().unlock();
957  }
958 
959 }
List< CorrelationAttribute.Type > getSupportedCorrelationTypes()
Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttribute.Type aType, String value)
void updateCorrelationType(CorrelationAttribute.Type aType)
CorrelationAttribute.Type getCorrelationTypeById(int typeId)
Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID)
CorrelationDataSource getDataSourceDetails(String dataSourceDeviceId)
Long getCountArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
List< CorrelationAttributeInstance > getArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
void setArtifactInstanceKnownStatus(CorrelationAttribute eamArtifact, TskData.FileKnown knownStatus)
void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttribute.Type correlationType)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
List< EamGlobalFileInstance > getReferenceInstancesByTypeValue(CorrelationAttribute.Type aType, String aValue)
void addArtifact(CorrelationAttribute eamArtifact)
void newDataSource(CorrelationDataSource eamDataSource)
List< CorrelationAttribute.Type > getEnabledCorrelationTypes()
void bulkInsertReferenceTypeEntries(Set< EamGlobalFileInstance > globalInstances, CorrelationAttribute.Type contentType)
boolean isArtifactlKnownBadByReference(CorrelationAttribute.Type aType, String value)
List< String > getListCasesHavingArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
Long getCountArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
List< CorrelationAttributeInstance > getArtifactInstancesByPath(CorrelationAttribute.Type aType, String filePath)
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
List< CorrelationAttribute.Type > getDefinedCorrelationTypes()
int newCorrelationType(CorrelationAttribute.Type newType)

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.