19 package org.sleuthkit.autopsy.centralrepository.datamodel;
21 import java.sql.Connection;
22 import java.sql.SQLException;
23 import java.sql.Statement;
24 import java.util.Arrays;
25 import java.util.List;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
28 import java.util.logging.Level;
29 import org.apache.commons.dbcp2.BasicDataSource;
40 final class SqliteEamDb
extends AbstractSqlEamDb {
42 private final static Logger LOGGER = Logger.getLogger(SqliteEamDb.class.getName());
44 private static SqliteEamDb instance;
46 private BasicDataSource connectionPool = null;
48 private final SqliteEamDbSettings dbSettings;
52 private final ReentrantReadWriteLock rwLock =
new ReentrantReadWriteLock(
true);
62 public synchronized static SqliteEamDb getInstance() throws EamDbException {
63 if (instance == null) {
64 instance =
new SqliteEamDb();
75 private SqliteEamDb() throws EamDbException {
76 dbSettings =
new SqliteEamDbSettings();
77 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
81 public void shutdownConnections() throws EamDbException {
84 if (null != connectionPool) {
85 connectionPool.close();
86 connectionPool = null;
90 }
catch (SQLException ex) {
91 throw new EamDbException(
"Failed to close existing database connections.", ex);
96 public void updateSettings() {
98 dbSettings.loadSettings();
99 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
104 public void saveSettings() {
105 synchronized (
this) {
106 dbSettings.saveSettings();
111 public void reset() throws EamDbException {
113 acquireExclusiveLock();
115 Connection conn = connect();
119 Statement dropContent = conn.createStatement();
120 dropContent.executeUpdate(
"DELETE FROM organizations");
121 dropContent.executeUpdate(
"DELETE FROM cases");
122 dropContent.executeUpdate(
"DELETE FROM data_sources");
123 dropContent.executeUpdate(
"DELETE FROM reference_sets");
124 dropContent.executeUpdate(
"DELETE FROM artifact_types");
125 dropContent.executeUpdate(
"DELETE FROM db_info");
127 String instancesTemplate =
"DELETE FROM %s_instances";
128 String referencesTemplate =
"DELETE FROM global_files";
129 for (CorrelationAttributeInstance.Type type : defaultCorrelationTypes) {
130 dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
132 if (type.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
133 dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
137 dropContent.executeUpdate(
"VACUUM");
138 }
catch (SQLException ex) {
139 LOGGER.log(Level.WARNING,
"Failed to reset database.", ex);
141 EamDbUtil.closeConnection(conn);
144 dbSettings.insertDefaultDatabaseContent();
146 releaseExclusiveLock();
154 private void setupConnectionPool() throws EamDbException {
156 if (dbSettings.dbFileExists() ==
false) {
157 throw new EamDbException(
"Central repository database missing");
160 connectionPool =
new BasicDataSource();
161 connectionPool.setDriverClassName(dbSettings.getDriver());
162 connectionPool.setUrl(dbSettings.getConnectionURL());
165 connectionPool.setInitialSize(50);
166 connectionPool.setMaxTotal(-1);
167 connectionPool.setMaxIdle(-1);
168 connectionPool.setMaxWaitMillis(1000);
169 connectionPool.setValidationQuery(dbSettings.getValidationQuery());
170 connectionPool.setConnectionInitSqls(Arrays.asList(
"PRAGMA foreign_keys = ON"));
181 protected Connection connect() throws EamDbException {
182 synchronized (
this) {
183 if (!EamDb.isEnabled()) {
184 throw new EamDbException(
"Central Repository module is not enabled");
187 if (connectionPool == null) {
188 setupConnectionPool();
192 return connectionPool.getConnection();
193 }
catch (SQLException ex) {
194 throw new EamDbException(
"Error getting connection from connection pool.", ex);
200 protected String getConflictClause() {
214 public void newDbInfo(String name, String value)
throws EamDbException {
216 acquireExclusiveLock();
217 super.newDbInfo(name, value);
219 releaseExclusiveLock();
233 public String getDbInfo(String name)
throws EamDbException {
236 return super.getDbInfo(name);
251 public void updateDbInfo(String name, String value)
throws EamDbException {
253 acquireExclusiveLock();
254 super.updateDbInfo(name, value);
256 releaseExclusiveLock();
266 public CorrelationCase newCase(Case autopsyCase)
throws EamDbException {
268 acquireExclusiveLock();
269 return super.newCase(autopsyCase);
271 releaseExclusiveLock();
283 public CorrelationCase newCase(CorrelationCase eamCase)
throws EamDbException {
285 acquireExclusiveLock();
286 return super.newCase(eamCase);
288 releaseExclusiveLock();
298 public void updateCase(CorrelationCase eamCase)
throws EamDbException {
300 acquireExclusiveLock();
301 super.updateCase(eamCase);
303 releaseExclusiveLock();
315 public CorrelationCase getCaseByUUID(String caseUUID)
throws EamDbException {
318 return super.getCaseByUUID(caseUUID);
332 public CorrelationCase getCaseById(
int caseId)
throws EamDbException {
335 return super.getCaseById(caseId);
348 public List<CorrelationCase> getCases() throws EamDbException {
351 return super.getCases();
363 public void newDataSource(CorrelationDataSource eamDataSource)
throws EamDbException {
365 acquireExclusiveLock();
366 super.newDataSource(eamDataSource);
368 releaseExclusiveLock();
382 public CorrelationDataSource getDataSource(CorrelationCase correlationCase, String dataSourceDeviceId)
throws EamDbException {
385 return super.getDataSource(correlationCase, dataSourceDeviceId);
401 public CorrelationDataSource getDataSourceById(CorrelationCase correlationCase,
int dataSourceId)
throws EamDbException {
404 return super.getDataSourceById(correlationCase, dataSourceId);
416 public List<CorrelationDataSource> getDataSources() throws EamDbException {
419 return super.getDataSources();
432 public void addArtifactInstance(CorrelationAttributeInstance eamArtifact)
throws EamDbException {
434 acquireExclusiveLock();
435 super.addArtifactInstance(eamArtifact);
437 releaseExclusiveLock();
451 public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
454 return super.getArtifactInstancesByTypeValue(aType, value);
472 public List<CorrelationAttributeInstance> getArtifactInstancesByPath(CorrelationAttributeInstance.Type aType, String filePath)
throws EamDbException {
475 return super.getArtifactInstancesByPath(aType, filePath);
493 public Long getCountArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
496 return super.getCountArtifactInstancesByTypeValue(aType, value);
503 public int getFrequencyPercentage(CorrelationAttributeInstance corAttr)
throws EamDbException, CorrelationAttributeNormalizationException {
506 return super.getFrequencyPercentage(corAttr);
524 public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
527 return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
534 public Long getCountUniqueDataSources() throws EamDbException {
537 return super.getCountUniqueDataSources();
555 public Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID)
throws EamDbException {
558 return super.getCountArtifactInstancesByCaseDataSource(caseUUID, dataSourceID);
569 public void commitAttributeInstancesBulk() throws EamDbException {
571 acquireExclusiveLock();
572 super.commitAttributeInstancesBulk();
574 releaseExclusiveLock();
582 public void bulkInsertCases(List<CorrelationCase> cases)
throws EamDbException {
584 acquireExclusiveLock();
585 super.bulkInsertCases(cases);
587 releaseExclusiveLock();
602 public void setAttributeInstanceKnownStatus(CorrelationAttributeInstance eamArtifact, TskData.FileKnown knownStatus) throws EamDbException {
604 acquireExclusiveLock();
605 super.setAttributeInstanceKnownStatus(eamArtifact, knownStatus);
607 releaseExclusiveLock();
621 public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
624 return super.getArtifactInstancesKnownBad(aType, value);
640 public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType) throws EamDbException {
643 return super.getArtifactInstancesKnownBad(aType);
658 public Long getCountArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
661 return super.getCountArtifactInstancesKnownBad(aType, value);
680 public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
683 return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
696 public void deleteReferenceSet(
int referenceSetID)
throws EamDbException {
698 acquireExclusiveLock();
699 super.deleteReferenceSet(referenceSetID);
701 releaseExclusiveLock();
714 public boolean isValueInReferenceSet(String value,
int referenceSetID,
int correlationTypeID)
throws EamDbException, CorrelationAttributeNormalizationException {
717 return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
731 public void processInstanceTable(CorrelationAttributeInstance.Type type, InstanceTableCallback instanceTableCallback)
throws EamDbException {
734 super.processInstanceTable(type, instanceTableCallback);
748 public void processInstanceTableWhere(CorrelationAttributeInstance.Type type, String whereClause, InstanceTableCallback instanceTableCallback)
throws EamDbException {
751 super.processInstanceTableWhere(type, whereClause, instanceTableCallback);
768 public boolean referenceSetExists(String referenceSetName, String version)
throws EamDbException {
771 return super.referenceSetExists(referenceSetName, version);
786 public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value)
throws EamDbException, CorrelationAttributeNormalizationException {
789 return super.isArtifactKnownBadByReference(aType, value);
805 public EamOrganization newOrganization(EamOrganization eamOrg)
throws EamDbException {
807 acquireExclusiveLock();
808 return super.newOrganization(eamOrg);
810 releaseExclusiveLock();
822 public List<EamOrganization> getOrganizations() throws EamDbException {
825 return super.getOrganizations();
841 public EamOrganization getOrganizationByID(
int orgID)
throws EamDbException {
844 return super.getOrganizationByID(orgID);
851 public void updateOrganization(EamOrganization updatedOrganization)
throws EamDbException {
853 acquireExclusiveLock();
854 super.updateOrganization(updatedOrganization);
856 releaseExclusiveLock();
861 public void deleteOrganization(EamOrganization organizationToDelete)
throws EamDbException {
863 acquireExclusiveLock();
864 super.deleteOrganization(organizationToDelete);
866 releaseExclusiveLock();
880 public int newReferenceSet(EamGlobalSet eamGlobalSet)
throws EamDbException {
882 acquireExclusiveLock();
883 return super.newReferenceSet(eamGlobalSet);
885 releaseExclusiveLock();
899 public EamGlobalSet getReferenceSetByID(
int referenceSetID)
throws EamDbException {
902 return super.getReferenceSetByID(referenceSetID);
918 public List<EamGlobalSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws EamDbException {
921 return super.getAllReferenceSets(correlationType);
936 public void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws EamDbException {
938 acquireExclusiveLock();
939 super.addReferenceInstance(eamGlobalFileInstance, correlationType);
941 releaseExclusiveLock();
951 public void bulkInsertReferenceTypeEntries(Set<EamGlobalFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws EamDbException {
953 acquireExclusiveLock();
954 super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
956 releaseExclusiveLock();
971 public List<EamGlobalFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue)
throws EamDbException, CorrelationAttributeNormalizationException {
974 return super.getReferenceInstancesByTypeValue(aType, aValue);
990 public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws EamDbException {
992 acquireExclusiveLock();
993 return super.newCorrelationType(newType);
995 releaseExclusiveLock();
1009 public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws EamDbException {
1011 acquireSharedLock();
1012 return super.getDefinedCorrelationTypes();
1014 releaseSharedLock();
1028 public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws EamDbException {
1030 acquireSharedLock();
1031 return super.getEnabledCorrelationTypes();
1033 releaseSharedLock();
1047 public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws EamDbException {
1049 acquireSharedLock();
1050 return super.getSupportedCorrelationTypes();
1052 releaseSharedLock();
1064 public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws EamDbException {
1066 acquireExclusiveLock();
1067 super.updateCorrelationType(aType);
1069 releaseExclusiveLock();
1083 public CorrelationAttributeInstance.Type getCorrelationTypeById(
int typeId)
throws EamDbException {
1085 acquireSharedLock();
1086 return super.getCorrelationTypeById(typeId);
1088 releaseSharedLock();
1098 public void upgradeSchema() throws EamDbException, SQLException {
1100 acquireExclusiveLock();
1101 super.upgradeSchema();
1103 releaseExclusiveLock();
1118 public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException {
1128 private void acquireExclusiveLock() {
1129 rwLock.writeLock().lock();
1137 private void releaseExclusiveLock() {
1138 rwLock.writeLock().unlock();
1146 private void acquireSharedLock() {
1147 rwLock.readLock().lock();
1155 private void releaseSharedLock() {
1156 rwLock.readLock().unlock();