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);
61 public synchronized static SqliteEamDb getInstance() throws EamDbException {
62 if (instance == null) {
63 instance =
new SqliteEamDb();
74 private SqliteEamDb() throws EamDbException {
75 dbSettings =
new SqliteEamDbSettings();
76 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
80 public void shutdownConnections() throws EamDbException {
83 if (null != connectionPool) {
84 connectionPool.close();
85 connectionPool = null;
88 }
catch (SQLException ex) {
89 throw new EamDbException(
"Failed to close existing database connections.", ex);
94 public void updateSettings() {
96 dbSettings.loadSettings();
97 bulkArtifactsThreshold = dbSettings.getBulkThreshold();
102 public void saveSettings() {
103 synchronized (
this) {
104 dbSettings.saveSettings();
109 public void reset() throws EamDbException {
111 acquireExclusiveLock();
113 Connection conn = connect();
117 Statement dropContent = conn.createStatement();
118 dropContent.executeUpdate(
"DELETE FROM organizations");
119 dropContent.executeUpdate(
"DELETE FROM cases");
120 dropContent.executeUpdate(
"DELETE FROM data_sources");
121 dropContent.executeUpdate(
"DELETE FROM reference_sets");
122 dropContent.executeUpdate(
"DELETE FROM artifact_types");
123 dropContent.executeUpdate(
"DELETE FROM db_info");
125 String instancesTemplate =
"DELETE FROM %s_instances";
126 String referencesTemplate =
"DELETE FROM global_files";
127 for (CorrelationAttribute.Type type : defaultCorrelationTypes) {
128 dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
130 if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
131 dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
135 dropContent.executeUpdate(
"VACUUM");
136 }
catch (SQLException ex) {
137 LOGGER.log(Level.WARNING,
"Failed to reset database.", ex);
139 EamDbUtil.closeConnection(conn);
142 dbSettings.insertDefaultDatabaseContent();
144 releaseExclusiveLock();
152 private void setupConnectionPool() throws EamDbException {
154 if (dbSettings.dbFileExists() ==
false) {
155 throw new EamDbException(
"Central repository database missing");
158 connectionPool =
new BasicDataSource();
159 connectionPool.setDriverClassName(dbSettings.getDriver());
160 connectionPool.setUrl(dbSettings.getConnectionURL());
163 connectionPool.setInitialSize(50);
164 connectionPool.setMaxTotal(-1);
165 connectionPool.setMaxIdle(-1);
166 connectionPool.setMaxWaitMillis(1000);
167 connectionPool.setValidationQuery(dbSettings.getValidationQuery());
168 connectionPool.setConnectionInitSqls(Arrays.asList(
"PRAGMA foreign_keys = ON"));
179 protected Connection connect() throws EamDbException {
180 synchronized (
this) {
181 if (!EamDb.isEnabled()) {
182 throw new EamDbException(
"Central Repository module is not enabled");
185 if (connectionPool == null) {
186 setupConnectionPool();
190 return connectionPool.getConnection();
191 }
catch (SQLException ex) {
192 throw new EamDbException(
"Error getting connection from connection pool.", ex);
198 protected String getConflictClause() {
213 public void newDbInfo(String name, String value)
throws EamDbException {
215 acquireExclusiveLock();
216 super.newDbInfo(name, value);
218 releaseExclusiveLock();
232 public String getDbInfo(String name)
throws EamDbException {
235 return super.getDbInfo(name);
250 public void updateDbInfo(String name, String value)
throws EamDbException {
252 acquireExclusiveLock();
253 super.updateDbInfo(name, value);
255 releaseExclusiveLock();
265 public CorrelationCase newCase(Case autopsyCase)
throws EamDbException {
267 acquireExclusiveLock();
268 return super.newCase(autopsyCase);
270 releaseExclusiveLock();
282 public CorrelationCase newCase(CorrelationCase eamCase)
throws EamDbException {
284 acquireExclusiveLock();
285 return super.newCase(eamCase);
287 releaseExclusiveLock();
297 public void updateCase(CorrelationCase eamCase)
throws EamDbException {
299 acquireExclusiveLock();
300 super.updateCase(eamCase);
302 releaseExclusiveLock();
314 public CorrelationCase getCaseByUUID(String caseUUID)
throws EamDbException {
317 return super.getCaseByUUID(caseUUID);
329 public List<CorrelationCase> getCases() throws EamDbException {
332 return super.getCases();
344 public void newDataSource(CorrelationDataSource eamDataSource)
throws EamDbException {
346 acquireExclusiveLock();
347 super.newDataSource(eamDataSource);
349 releaseExclusiveLock();
362 public CorrelationDataSource getDataSource(CorrelationCase correlationCase, String dataSourceDeviceId)
throws EamDbException {
365 return super.getDataSource(correlationCase, dataSourceDeviceId);
377 public List<CorrelationDataSource> getDataSources() throws EamDbException {
380 return super.getDataSources();
393 public void addArtifact(CorrelationAttribute eamArtifact)
throws EamDbException {
395 acquireExclusiveLock();
396 super.addArtifact(eamArtifact);
398 releaseExclusiveLock();
412 public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
throws EamDbException {
415 return super.getArtifactInstancesByTypeValue(aType, value);
433 public List<CorrelationAttributeInstance> getArtifactInstancesByPath(CorrelationAttribute.Type aType, String filePath)
throws EamDbException {
436 return super.getArtifactInstancesByPath(aType, filePath);
454 public Long getCountArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
throws EamDbException {
457 return super.getCountArtifactInstancesByTypeValue(aType, value);
464 public int getFrequencyPercentage(CorrelationAttribute corAttr)
throws EamDbException {
467 return super.getFrequencyPercentage(corAttr);
485 public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttribute.Type aType, String value)
throws EamDbException {
488 return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
496 public Long getCountUniqueDataSources() throws EamDbException {
499 return super.getCountUniqueDataSources();
517 public Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID)
throws EamDbException {
520 return super.getCountArtifactInstancesByCaseDataSource(caseUUID, dataSourceID);
531 public void bulkInsertArtifacts() throws EamDbException {
533 acquireExclusiveLock();
534 super.bulkInsertArtifacts();
536 releaseExclusiveLock();
544 public void bulkInsertCases(List<CorrelationCase> cases)
throws EamDbException {
546 acquireExclusiveLock();
547 super.bulkInsertCases(cases);
549 releaseExclusiveLock();
564 public void setArtifactInstanceKnownStatus(CorrelationAttribute eamArtifact, TskData.FileKnown knownStatus) throws EamDbException {
566 acquireExclusiveLock();
567 super.setArtifactInstanceKnownStatus(eamArtifact, knownStatus);
569 releaseExclusiveLock();
583 public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
throws EamDbException {
586 return super.getArtifactInstancesKnownBad(aType, value);
601 public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttribute.Type aType) throws EamDbException {
604 return super.getArtifactInstancesKnownBad(aType);
619 public Long getCountArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
throws EamDbException {
622 return super.getCountArtifactInstancesKnownBad(aType, value);
641 public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
throws EamDbException {
644 return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
656 public void deleteReferenceSet(
int referenceSetID)
throws EamDbException{
658 acquireExclusiveLock();
659 super.deleteReferenceSet(referenceSetID);
661 releaseExclusiveLock();
673 public boolean isValueInReferenceSet(String value,
int referenceSetID,
int correlationTypeID)
throws EamDbException {
676 return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
690 public void processInstanceTable(CorrelationAttribute.Type type, InstanceTableCallback instanceTableCallback)
throws EamDbException {
693 super.processInstanceTable(type, instanceTableCallback);
707 public boolean referenceSetExists(String referenceSetName, String version)
throws EamDbException {
710 return super.referenceSetExists(referenceSetName, version);
725 public boolean isArtifactKnownBadByReference(CorrelationAttribute.Type aType, String value)
throws EamDbException {
728 return super.isArtifactKnownBadByReference(aType, value);
744 public EamOrganization newOrganization(EamOrganization eamOrg)
throws EamDbException {
746 acquireExclusiveLock();
747 return super.newOrganization(eamOrg);
749 releaseExclusiveLock();
761 public List<EamOrganization> getOrganizations() throws EamDbException {
764 return super.getOrganizations();
780 public EamOrganization getOrganizationByID(
int orgID)
throws EamDbException {
783 return super.getOrganizationByID(orgID);
790 public void updateOrganization(EamOrganization updatedOrganization)
throws EamDbException {
792 acquireExclusiveLock();
793 super.updateOrganization(updatedOrganization);
795 releaseExclusiveLock();
800 public void deleteOrganization(EamOrganization organizationToDelete)
throws EamDbException {
802 acquireExclusiveLock();
803 super.deleteOrganization(organizationToDelete);
805 releaseExclusiveLock();
818 public int newReferenceSet(EamGlobalSet eamGlobalSet)
throws EamDbException {
820 acquireExclusiveLock();
821 return super.newReferenceSet(eamGlobalSet);
823 releaseExclusiveLock();
837 public EamGlobalSet getReferenceSetByID(
int referenceSetID)
throws EamDbException {
840 return super.getReferenceSetByID(referenceSetID);
856 public List<EamGlobalSet> getAllReferenceSets(CorrelationAttribute.Type correlationType) throws EamDbException {
859 return super.getAllReferenceSets(correlationType);
875 public void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttribute.Type correlationType) throws EamDbException {
877 acquireExclusiveLock();
878 super.addReferenceInstance(eamGlobalFileInstance, correlationType);
880 releaseExclusiveLock();
890 public void bulkInsertReferenceTypeEntries(Set<EamGlobalFileInstance> globalInstances, CorrelationAttribute.Type contentType) throws EamDbException {
892 acquireExclusiveLock();
893 super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
895 releaseExclusiveLock();
910 public List<EamGlobalFileInstance> getReferenceInstancesByTypeValue(CorrelationAttribute.Type aType, String aValue)
throws EamDbException {
913 return super.getReferenceInstancesByTypeValue(aType, aValue);
929 public int newCorrelationType(CorrelationAttribute.Type newType) throws EamDbException {
931 acquireExclusiveLock();
932 return super.newCorrelationType(newType);
934 releaseExclusiveLock();
948 public List<CorrelationAttribute.Type> getDefinedCorrelationTypes() throws EamDbException {
951 return super.getDefinedCorrelationTypes();
967 public List<CorrelationAttribute.Type> getEnabledCorrelationTypes() throws EamDbException {
970 return super.getEnabledCorrelationTypes();
986 public List<CorrelationAttribute.Type> getSupportedCorrelationTypes() throws EamDbException {
989 return super.getSupportedCorrelationTypes();
1003 public void updateCorrelationType(CorrelationAttribute.Type aType) throws EamDbException {
1005 acquireExclusiveLock();
1006 super.updateCorrelationType(aType);
1008 releaseExclusiveLock();
1022 public CorrelationAttribute.Type getCorrelationTypeById(
int typeId)
throws EamDbException {
1024 acquireSharedLock();
1025 return super.getCorrelationTypeById(typeId);
1027 releaseSharedLock();
1036 public void upgradeSchema() throws EamDbException, SQLException {
1038 acquireExclusiveLock();
1039 super.upgradeSchema();
1041 releaseExclusiveLock();
1054 public CoordinationService.Lock getExclusiveMultiUserDbLock() throws EamDbException{
1064 private void acquireExclusiveLock() {
1065 rwLock.writeLock().lock();
1073 private void releaseExclusiveLock() {
1074 rwLock.writeLock().unlock();
1082 private void acquireSharedLock() {
1083 rwLock.readLock().lock();
1091 private void releaseSharedLock() {
1092 rwLock.readLock().unlock();