Autopsy  4.16.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteCentralRepo.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2020 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.ResultSet;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.locks.ReentrantReadWriteLock;
29 import java.util.logging.Level;
30 import org.apache.commons.dbcp2.BasicDataSource;
31 import org.openide.util.NbBundle.Messages;
33 import org.sleuthkit.datamodel.TskData;
36 
42 final class SqliteCentralRepo extends RdbmsCentralRepo {
43 
44  private final static Logger LOGGER = Logger.getLogger(SqliteCentralRepo.class.getName());
45 
46  private static SqliteCentralRepo instance;
47 
48  private BasicDataSource connectionPool = null;
49 
50  private final SqliteCentralRepoSettings dbSettings;
51 
52  // While the Sqlite database should only be used for single users, it is still
53  // possible for multiple threads to attempt to write to the database simultaneously.
54  private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(true);
55 
64  public synchronized static SqliteCentralRepo getInstance() throws CentralRepoException {
65  if (instance == null) {
66  instance = new SqliteCentralRepo();
67  }
68 
69  return instance;
70  }
71 
78  private SqliteCentralRepo() throws CentralRepoException {
79  dbSettings = new SqliteCentralRepoSettings();
80  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
81  }
82 
83  @Override
84  public void shutdownConnections() throws CentralRepoException {
85  try {
86  synchronized (this) {
87  if (null != connectionPool) {
88  connectionPool.close();
89  connectionPool = null; // force it to be re-created on next connect()
90  }
91  clearCaches();
92  }
93  } catch (SQLException ex) {
94  throw new CentralRepoException("Failed to close existing database connections.", ex); // NON-NLS
95  }
96  }
97 
98  @Override
99  public void updateSettings() {
100  synchronized (this) {
101  dbSettings.loadSettings();
102  bulkArtifactsThreshold = dbSettings.getBulkThreshold();
103  }
104  }
105 
106  @Override
107  public void saveSettings() {
108  synchronized (this) {
109  dbSettings.saveSettings();
110  }
111  }
112 
113  @Override
114  public void reset() throws CentralRepoException {
115  try {
116  acquireExclusiveLock();
117 
118  Connection conn = connect();
119 
120  try {
121 
122  Statement dropContent = conn.createStatement();
123  dropContent.executeUpdate("DELETE FROM organizations");
124  dropContent.executeUpdate("DELETE FROM cases");
125  dropContent.executeUpdate("DELETE FROM data_sources");
126  dropContent.executeUpdate("DELETE FROM reference_sets");
127  dropContent.executeUpdate("DELETE FROM artifact_types");
128  dropContent.executeUpdate("DELETE FROM db_info");
129 
130  String instancesTemplate = "DELETE FROM %s_instances";
131  String referencesTemplate = "DELETE FROM global_files";
132  for (CorrelationAttributeInstance.Type type : defaultCorrelationTypes) {
133  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
134  // FUTURE: support other reference types
135  if (type.getId() == CorrelationAttributeInstance.FILES_TYPE_ID) {
136  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
137  }
138  }
139 
140  dropContent.executeUpdate("VACUUM");
141  } catch (SQLException ex) {
142  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
143  } finally {
144  CentralRepoDbUtil.closeConnection(conn);
145  }
146 
147  RdbmsCentralRepoFactory centralRepoSchemaFactory = new RdbmsCentralRepoFactory(CentralRepoPlatforms.SQLITE, dbSettings);
148  centralRepoSchemaFactory.insertDefaultDatabaseContent();
149  } finally {
150  releaseExclusiveLock();
151  }
152  }
153 
158  @Messages({"SqliteEamDb.databaseMissing.message=Central repository database missing"})
159  private void setupConnectionPool(boolean foreignKeysEnabled) throws CentralRepoException {
160 
161  if (dbSettings.dbFileExists() == false) {
162  throw new CentralRepoException("Central repository database missing", Bundle.SqliteEamDb_databaseMissing_message());
163  }
164 
165  connectionPool = new BasicDataSource();
166  connectionPool.setDriverClassName(dbSettings.getDriver());
167  connectionPool.setUrl(dbSettings.getConnectionURL());
168 
169  // tweak pool configuration
170  connectionPool.setInitialSize(50);
171  connectionPool.setMaxTotal(-1);
172  connectionPool.setMaxIdle(-1);
173  connectionPool.setMaxWaitMillis(1000);
174  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
175  if (foreignKeysEnabled) {
176  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = ON"));
177  } else {
178  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = OFF"));
179  }
180  }
181 
192  @Messages({"SqliteEamDb.connectionFailedMessage.message=Error getting connection to database.",
193  "SqliteEamDb.centralRepositoryDisabled.message=Central Repository module is not enabled."})
194  @Override
195  protected Connection connect(boolean foreignKeys) throws CentralRepoException {
196  synchronized (this) {
197  if (!CentralRepository.isEnabled()) {
198  throw new CentralRepoException("Central repository database missing", Bundle.SqliteEamDb_centralRepositoryDisabled_message()); // NON-NLS
199  }
200  if (connectionPool == null) {
201  setupConnectionPool(foreignKeys);
202  }
203  try {
204  return connectionPool.getConnection();
205  } catch (SQLException ex) {
206  throw new CentralRepoException("Error getting connection from connection pool.", Bundle.SqliteEamDb_connectionFailedMessage_message(), ex); // NON-NLS
207  }
208  }
209  }
210 
219  @Override
220  protected Connection connect() throws CentralRepoException {
221  return connect(true);
222  }
223 
224  @Override
225  protected String getConflictClause() {
226  // For sqlite, our conflict clause is part of the table schema
227  return "";
228  }
229 
230  @Override
231  protected Connection getEphemeralConnection() {
232  return this.dbSettings.getEphemeralConnection();
233  }
242  @Override
243  public void newDbInfo(String name, String value) throws CentralRepoException {
244  try {
245  acquireExclusiveLock();
246  super.newDbInfo(name, value);
247  } finally {
248  releaseExclusiveLock();
249  }
250  }
251 
261  @Override
262  public String getDbInfo(String name) throws CentralRepoException {
263  try {
264  acquireSharedLock();
265  return super.getDbInfo(name);
266  } finally {
267  releaseSharedLock();
268  }
269  }
270 
279  @Override
280  public void updateDbInfo(String name, String value) throws CentralRepoException {
281  try {
282  acquireExclusiveLock();
283  super.updateDbInfo(name, value);
284  } finally {
285  releaseExclusiveLock();
286  }
287  }
288 
294  @Override
295  public CorrelationCase newCase(Case autopsyCase) throws CentralRepoException {
296  try {
297  acquireExclusiveLock();
298  return super.newCase(autopsyCase);
299  } finally {
300  releaseExclusiveLock();
301  }
302  }
303 
304  @Override
305  public void addDataSourceObjectId(int rowId, long dataSourceObjectId) throws CentralRepoException {
306  try {
307  acquireExclusiveLock();
308  super.addDataSourceObjectId(rowId, dataSourceObjectId);
309  } finally {
310  releaseExclusiveLock();
311  }
312  }
313 
321  @Override
322  public CorrelationCase newCase(CorrelationCase eamCase) throws CentralRepoException {
323  try {
324  acquireExclusiveLock();
325  return super.newCase(eamCase);
326  } finally {
327  releaseExclusiveLock();
328  }
329  }
330 
336  @Override
337  public void updateCase(CorrelationCase eamCase) throws CentralRepoException {
338  try {
339  acquireExclusiveLock();
340  super.updateCase(eamCase);
341  } finally {
342  releaseExclusiveLock();
343  }
344  }
345 
353  @Override
354  public CorrelationCase getCaseByUUID(String caseUUID) throws CentralRepoException {
355  try {
356  acquireSharedLock();
357  return super.getCaseByUUID(caseUUID);
358  } finally {
359  releaseSharedLock();
360  }
361  }
362 
370  @Override
371  public CorrelationCase getCaseById(int caseId) throws CentralRepoException {
372  try {
373  acquireSharedLock();
374  return super.getCaseById(caseId);
375  } finally {
376  releaseSharedLock();
377  }
378 
379  }
380 
386  @Override
387  public List<CorrelationCase> getCases() throws CentralRepoException {
388  try {
389  acquireSharedLock();
390  return super.getCases();
391  } finally {
392  releaseSharedLock();
393  }
394  }
395 
401  @Override
402  public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) throws CentralRepoException {
403  try {
404  acquireExclusiveLock();
405  return super.newDataSource(eamDataSource);
406  } finally {
407  releaseExclusiveLock();
408  }
409  }
410 
420  @Override
421  public CorrelationDataSource getDataSource(CorrelationCase correlationCase, Long caseDbDataSourceId) throws CentralRepoException {
422  try {
423  acquireSharedLock();
424  return super.getDataSource(correlationCase, caseDbDataSourceId);
425  } finally {
426  releaseSharedLock();
427  }
428  }
429 
439  @Override
440  public CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId) throws CentralRepoException {
441  try {
442  acquireSharedLock();
443  return super.getDataSourceById(correlationCase, dataSourceId);
444  } finally {
445  releaseSharedLock();
446  }
447  }
448 
454  @Override
455  public List<CorrelationDataSource> getDataSources() throws CentralRepoException {
456  try {
457  acquireSharedLock();
458  return super.getDataSources();
459  } finally {
460  releaseSharedLock();
461  }
462  }
463 
472  @Override
473  public void updateDataSourceName(CorrelationDataSource eamDataSource, String newName) throws CentralRepoException {
474  try {
475  acquireExclusiveLock();
476  super.updateDataSourceName(eamDataSource, newName);
477  } finally {
478  releaseExclusiveLock();
479  }
480  }
481 
487  @Override
488  public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
489  try {
490  acquireExclusiveLock();
491  super.updateDataSourceMd5Hash(eamDataSource);
492  } finally {
493  releaseExclusiveLock();
494  }
495  }
496 
502  @Override
503  public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
504  try {
505  acquireExclusiveLock();
506  super.updateDataSourceSha1Hash(eamDataSource);
507  } finally {
508  releaseExclusiveLock();
509  }
510  }
511 
518  @Override
519  public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
520  try {
521  acquireExclusiveLock();
522  super.updateDataSourceSha256Hash(eamDataSource);
523  } finally {
524  releaseExclusiveLock();
525  }
526  }
527 
534  @Override
535  public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws CentralRepoException {
536  try {
537  acquireExclusiveLock();
538  super.addArtifactInstance(eamArtifact);
539  } finally {
540  releaseExclusiveLock();
541  }
542  }
543 
544  @Override
545  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
546  try {
547  acquireSharedLock();
548  return super.getArtifactInstancesByTypeValue(aType, value);
549  } finally {
550  releaseSharedLock();
551  }
552  }
553 
554  @Override
555  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List<String> values) throws CentralRepoException, CorrelationAttributeNormalizationException {
556  try {
557  acquireSharedLock();
558  return super.getArtifactInstancesByTypeValues(aType, values);
559  } finally {
560  releaseSharedLock();
561  }
562  }
563 
564  @Override
565  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List<String> values, List<Integer> caseIds) throws CentralRepoException, CorrelationAttributeNormalizationException {
566  try {
567  acquireSharedLock();
568  return super.getArtifactInstancesByTypeValuesAndCases(aType, values, caseIds);
569  } finally {
570  releaseSharedLock();
571  }
572  }
573 
586  @Override
587  public Long getCountArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
588  try {
589  acquireSharedLock();
590  return super.getCountArtifactInstancesByTypeValue(aType, value);
591  } finally {
592  releaseSharedLock();
593  }
594  }
595 
596  @Override
597  public int getFrequencyPercentage(CorrelationAttributeInstance corAttr) throws CentralRepoException, CorrelationAttributeNormalizationException {
598  try {
599  acquireSharedLock();
600  return super.getFrequencyPercentage(corAttr);
601  } finally {
602  releaseSharedLock();
603  }
604  }
605 
618  @Override
619  public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
620  try {
621  acquireSharedLock();
622  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
623  } finally {
624  releaseSharedLock();
625  }
626  }
627 
628  @Override
629  public Long getCountUniqueDataSources() throws CentralRepoException {
630  try {
631  acquireSharedLock();
632  return super.getCountUniqueDataSources();
633  } finally {
634  releaseSharedLock();
635  }
636  }
637 
649  @Override
650  public Long getCountArtifactInstancesByCaseDataSource(CorrelationDataSource correlationDataSource) throws CentralRepoException {
651  try {
652  acquireSharedLock();
653  return super.getCountArtifactInstancesByCaseDataSource(correlationDataSource);
654  } finally {
655  releaseSharedLock();
656  }
657  }
658 
663  @Override
664  public void commitAttributeInstancesBulk() throws CentralRepoException {
665  try {
666  acquireExclusiveLock();
667  super.commitAttributeInstancesBulk();
668  } finally {
669  releaseExclusiveLock();
670  }
671  }
672 
676  @Override
677  public void bulkInsertCases(List<CorrelationCase> cases) throws CentralRepoException {
678  try {
679  acquireExclusiveLock();
680  super.bulkInsertCases(cases);
681  } finally {
682  releaseExclusiveLock();
683  }
684  }
685 
696  @Override
697  public void setAttributeInstanceKnownStatus(CorrelationAttributeInstance eamArtifact, TskData.FileKnown knownStatus) throws CentralRepoException {
698  try {
699  acquireExclusiveLock();
700  super.setAttributeInstanceKnownStatus(eamArtifact, knownStatus);
701  } finally {
702  releaseExclusiveLock();
703  }
704  }
705 
714  @Override
715  public Long getCountArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
716  try {
717  acquireSharedLock();
718  return super.getCountArtifactInstancesKnownBad(aType, value);
719  } finally {
720  releaseSharedLock();
721  }
722  }
723 
736  @Override
737  public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
738  try {
739  acquireSharedLock();
740  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
741  } finally {
742  releaseSharedLock();
743  }
744  }
745 
753  @Override
754  public void deleteReferenceSet(int referenceSetID) throws CentralRepoException {
755  try {
756  acquireExclusiveLock();
757  super.deleteReferenceSet(referenceSetID);
758  } finally {
759  releaseExclusiveLock();
760  }
761  }
762 
772  @Override
773  public boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID) throws CentralRepoException, CorrelationAttributeNormalizationException {
774  try {
775  acquireSharedLock();
776  return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
777  } finally {
778  releaseSharedLock();
779  }
780  }
781 
790  @Override
791  public void processInstanceTable(CorrelationAttributeInstance.Type type, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
792  try {
793  acquireSharedLock();
794  super.processInstanceTable(type, instanceTableCallback);
795  } finally {
796  releaseSharedLock();
797  }
798  }
799 
808  @Override
809  public void processInstanceTableWhere(CorrelationAttributeInstance.Type type, String whereClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
810  try {
811  acquireSharedLock();
812  super.processInstanceTableWhere(type, whereClause, instanceTableCallback);
813  } finally {
814  releaseSharedLock();
815  }
816  }
817 
826  @Override
827  public void processSelectClause(String selectClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
828  try {
829  acquireSharedLock();
830  super.processSelectClause(selectClause, instanceTableCallback);
831  } finally {
832  releaseSharedLock();
833  }
834  }
835 
836  @Override
837  public void executeCommand(String sql, List<Object> params) throws CentralRepoException {
838  try {
839  acquireExclusiveLock();
840  super.executeCommand(sql, params);
841  } finally {
842  releaseExclusiveLock();
843  }
844  }
845 
846  @Override
847  public void executeQuery(String sql, List<Object> params, CentralRepositoryDbQueryCallback queryCallback) throws CentralRepoException {
848  try {
849  acquireSharedLock();
850  super.executeQuery(sql, params, queryCallback);
851  } finally {
852  releaseSharedLock();
853  }
854  }
855 
868  @Override
869  public boolean referenceSetExists(String referenceSetName, String version) throws CentralRepoException {
870  try {
871  acquireSharedLock();
872  return super.referenceSetExists(referenceSetName, version);
873  } finally {
874  releaseSharedLock();
875  }
876  }
877 
886  @Override
887  public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
888  try {
889  acquireSharedLock();
890  return super.isArtifactKnownBadByReference(aType, value);
891  } finally {
892  releaseSharedLock();
893  }
894  }
895 
905  @Override
906  public CentralRepoOrganization newOrganization(CentralRepoOrganization eamOrg) throws CentralRepoException {
907  try {
908  acquireExclusiveLock();
909  return super.newOrganization(eamOrg);
910  } finally {
911  releaseExclusiveLock();
912  }
913  }
914 
922  @Override
923  public List<CentralRepoOrganization> getOrganizations() throws CentralRepoException {
924  try {
925  acquireSharedLock();
926  return super.getOrganizations();
927  } finally {
928  releaseSharedLock();
929  }
930  }
931 
941  @Override
942  public CentralRepoOrganization getOrganizationByID(int orgID) throws CentralRepoException {
943  try {
944  acquireSharedLock();
945  return super.getOrganizationByID(orgID);
946  } finally {
947  releaseSharedLock();
948  }
949  }
950 
951  @Override
952  public void updateOrganization(CentralRepoOrganization updatedOrganization) throws CentralRepoException {
953  try {
954  acquireExclusiveLock();
955  super.updateOrganization(updatedOrganization);
956  } finally {
957  releaseExclusiveLock();
958  }
959  }
960 
961  @Override
962  public void deleteOrganization(CentralRepoOrganization organizationToDelete) throws CentralRepoException {
963  try {
964  acquireExclusiveLock();
965  super.deleteOrganization(organizationToDelete);
966  } finally {
967  releaseExclusiveLock();
968  }
969  }
970 
980  @Override
981  public int newReferenceSet(CentralRepoFileSet eamGlobalSet) throws CentralRepoException {
982  try {
983  acquireExclusiveLock();
984  return super.newReferenceSet(eamGlobalSet);
985  } finally {
986  releaseExclusiveLock();
987  }
988  }
989 
999  @Override
1000  public CentralRepoFileSet getReferenceSetByID(int referenceSetID) throws CentralRepoException {
1001  try {
1002  acquireSharedLock();
1003  return super.getReferenceSetByID(referenceSetID);
1004  } finally {
1005  releaseSharedLock();
1006  }
1007  }
1008 
1018  @Override
1019  public List<CentralRepoFileSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1020  try {
1021  acquireSharedLock();
1022  return super.getAllReferenceSets(correlationType);
1023  } finally {
1024  releaseSharedLock();
1025  }
1026  }
1027 
1037  @Override
1038  public void addReferenceInstance(CentralRepoFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1039  try {
1040  acquireExclusiveLock();
1041  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
1042  } finally {
1043  releaseExclusiveLock();
1044  }
1045  }
1046 
1052  @Override
1053  public void bulkInsertReferenceTypeEntries(Set<CentralRepoFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws CentralRepoException {
1054  try {
1055  acquireExclusiveLock();
1056  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
1057  } finally {
1058  releaseExclusiveLock();
1059  }
1060  }
1061 
1072  @Override
1073  public List<CentralRepoFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue) throws CentralRepoException, CorrelationAttributeNormalizationException {
1074  try {
1075  acquireSharedLock();
1076  return super.getReferenceInstancesByTypeValue(aType, aValue);
1077  } finally {
1078  releaseSharedLock();
1079  }
1080  }
1081 
1091  @Override
1092  public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws CentralRepoException {
1093  try {
1094  acquireExclusiveLock();
1095  return super.newCorrelationType(newType);
1096  } finally {
1097  releaseExclusiveLock();
1098  }
1099  }
1100 
1110  @Override
1111  public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws CentralRepoException {
1112  try {
1113  acquireSharedLock();
1114  return super.getDefinedCorrelationTypes();
1115  } finally {
1116  releaseSharedLock();
1117  }
1118  }
1119 
1129  @Override
1130  public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws CentralRepoException {
1131  try {
1132  acquireSharedLock();
1133  return super.getEnabledCorrelationTypes();
1134  } finally {
1135  releaseSharedLock();
1136  }
1137  }
1138 
1148  @Override
1149  public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws CentralRepoException {
1150  try {
1151  acquireSharedLock();
1152  return super.getSupportedCorrelationTypes();
1153  } finally {
1154  releaseSharedLock();
1155  }
1156  }
1157 
1165  @Override
1166  public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws CentralRepoException {
1167  try {
1168  acquireExclusiveLock();
1169  super.updateCorrelationType(aType);
1170  } finally {
1171  releaseExclusiveLock();
1172  }
1173  }
1174 
1184  @Override
1185  public CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId) throws CentralRepoException {
1186  try {
1187  acquireSharedLock();
1188  return super.getCorrelationTypeById(typeId);
1189  } finally {
1190  releaseSharedLock();
1191  }
1192  }
1193 
1199  @Override
1200  public void upgradeSchema() throws CentralRepoException, SQLException, IncompatibleCentralRepoException {
1201  try {
1202  acquireExclusiveLock();
1203  super.upgradeSchema();
1204  } finally {
1205  releaseExclusiveLock();
1206  }
1207  }
1208 
1220  @Override
1221  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws CentralRepoException {
1222  // Multiple users are not supported for SQLite
1223  return null;
1224  }
1225 
1231  private void acquireExclusiveLock() {
1232  rwLock.writeLock().lock();
1233  }
1234 
1240  private void releaseExclusiveLock() {
1241  rwLock.writeLock().unlock();
1242  }
1243 
1249  private void acquireSharedLock() {
1250  rwLock.readLock().lock();
1251  }
1252 
1258  private void releaseSharedLock() {
1259  rwLock.readLock().unlock();
1260  }
1261 
1262  @Override
1263  boolean doesColumnExist(Connection conn, String tableName, String columnName) throws SQLException {
1264  final String tableInfoQueryTemplate = "PRAGMA table_info(%s)"; //NON-NLS
1265  ResultSet resultSet = null;
1266  Statement statement = null;
1267  boolean columnExists = false;
1268  try {
1269  statement = conn.createStatement();
1270  resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName));
1271  while (resultSet.next()) {
1272  // the second value ( 2 ) is the column name
1273  if (resultSet.getString(2).equals(columnName)) {
1274  columnExists = true;
1275  break;
1276  }
1277  }
1278  } finally {
1279  CentralRepoDbUtil.closeResultSet(resultSet);
1280  CentralRepoDbUtil.closeStatement(statement);
1281  }
1282  return columnExists;
1283  }
1284 }

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.