Autopsy  4.15.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 executeInsertSQL(String insertSQL) throws CentralRepoException {
838  try {
839  acquireSharedLock();
840  super.executeInsertSQL(insertSQL);
841  } finally {
842  releaseSharedLock();
843  }
844  }
845 
846  @Override
847  public void executeSelectSQL(String selectSQL, CentralRepositoryDbQueryCallback queryCallback) throws CentralRepoException {
848  try {
849  acquireSharedLock();
850  super.executeSelectSQL(selectSQL, queryCallback);
851  } finally {
852  releaseSharedLock();
853  }
854  }
855 
856  @Override
857  public void executeUpdateSQL(String updateSQL) throws CentralRepoException {
858  try {
859  acquireSharedLock();
860  super.executeUpdateSQL(updateSQL);
861  } finally {
862  releaseSharedLock();
863  }
864  }
865 
866  @Override
867  public void executeDeleteSQL(String deleteSQL) throws CentralRepoException {
868  try {
869  acquireSharedLock();
870  super.executeDeleteSQL(deleteSQL);
871  } finally {
872  releaseSharedLock();
873  }
874  }
875 
876 
889  @Override
890  public boolean referenceSetExists(String referenceSetName, String version) throws CentralRepoException {
891  try {
892  acquireSharedLock();
893  return super.referenceSetExists(referenceSetName, version);
894  } finally {
895  releaseSharedLock();
896  }
897  }
898 
907  @Override
908  public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
909  try {
910  acquireSharedLock();
911  return super.isArtifactKnownBadByReference(aType, value);
912  } finally {
913  releaseSharedLock();
914  }
915  }
916 
926  @Override
927  public CentralRepoOrganization newOrganization(CentralRepoOrganization eamOrg) throws CentralRepoException {
928  try {
929  acquireExclusiveLock();
930  return super.newOrganization(eamOrg);
931  } finally {
932  releaseExclusiveLock();
933  }
934  }
935 
943  @Override
944  public List<CentralRepoOrganization> getOrganizations() throws CentralRepoException {
945  try {
946  acquireSharedLock();
947  return super.getOrganizations();
948  } finally {
949  releaseSharedLock();
950  }
951  }
952 
962  @Override
963  public CentralRepoOrganization getOrganizationByID(int orgID) throws CentralRepoException {
964  try {
965  acquireSharedLock();
966  return super.getOrganizationByID(orgID);
967  } finally {
968  releaseSharedLock();
969  }
970  }
971 
972  @Override
973  public void updateOrganization(CentralRepoOrganization updatedOrganization) throws CentralRepoException {
974  try {
975  acquireExclusiveLock();
976  super.updateOrganization(updatedOrganization);
977  } finally {
978  releaseExclusiveLock();
979  }
980  }
981 
982  @Override
983  public void deleteOrganization(CentralRepoOrganization organizationToDelete) throws CentralRepoException {
984  try {
985  acquireExclusiveLock();
986  super.deleteOrganization(organizationToDelete);
987  } finally {
988  releaseExclusiveLock();
989  }
990  }
991 
1001  @Override
1002  public int newReferenceSet(CentralRepoFileSet eamGlobalSet) throws CentralRepoException {
1003  try {
1004  acquireExclusiveLock();
1005  return super.newReferenceSet(eamGlobalSet);
1006  } finally {
1007  releaseExclusiveLock();
1008  }
1009  }
1010 
1020  @Override
1021  public CentralRepoFileSet getReferenceSetByID(int referenceSetID) throws CentralRepoException {
1022  try {
1023  acquireSharedLock();
1024  return super.getReferenceSetByID(referenceSetID);
1025  } finally {
1026  releaseSharedLock();
1027  }
1028  }
1029 
1039  @Override
1040  public List<CentralRepoFileSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1041  try {
1042  acquireSharedLock();
1043  return super.getAllReferenceSets(correlationType);
1044  } finally {
1045  releaseSharedLock();
1046  }
1047  }
1048 
1058  @Override
1059  public void addReferenceInstance(CentralRepoFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1060  try {
1061  acquireExclusiveLock();
1062  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
1063  } finally {
1064  releaseExclusiveLock();
1065  }
1066  }
1067 
1073  @Override
1074  public void bulkInsertReferenceTypeEntries(Set<CentralRepoFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws CentralRepoException {
1075  try {
1076  acquireExclusiveLock();
1077  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
1078  } finally {
1079  releaseExclusiveLock();
1080  }
1081  }
1082 
1093  @Override
1094  public List<CentralRepoFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue) throws CentralRepoException, CorrelationAttributeNormalizationException {
1095  try {
1096  acquireSharedLock();
1097  return super.getReferenceInstancesByTypeValue(aType, aValue);
1098  } finally {
1099  releaseSharedLock();
1100  }
1101  }
1102 
1112  @Override
1113  public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws CentralRepoException {
1114  try {
1115  acquireExclusiveLock();
1116  return super.newCorrelationType(newType);
1117  } finally {
1118  releaseExclusiveLock();
1119  }
1120  }
1121 
1131  @Override
1132  public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws CentralRepoException {
1133  try {
1134  acquireSharedLock();
1135  return super.getDefinedCorrelationTypes();
1136  } finally {
1137  releaseSharedLock();
1138  }
1139  }
1140 
1150  @Override
1151  public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws CentralRepoException {
1152  try {
1153  acquireSharedLock();
1154  return super.getEnabledCorrelationTypes();
1155  } finally {
1156  releaseSharedLock();
1157  }
1158  }
1159 
1169  @Override
1170  public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws CentralRepoException {
1171  try {
1172  acquireSharedLock();
1173  return super.getSupportedCorrelationTypes();
1174  } finally {
1175  releaseSharedLock();
1176  }
1177  }
1178 
1186  @Override
1187  public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws CentralRepoException {
1188  try {
1189  acquireExclusiveLock();
1190  super.updateCorrelationType(aType);
1191  } finally {
1192  releaseExclusiveLock();
1193  }
1194  }
1195 
1205  @Override
1206  public CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId) throws CentralRepoException {
1207  try {
1208  acquireSharedLock();
1209  return super.getCorrelationTypeById(typeId);
1210  } finally {
1211  releaseSharedLock();
1212  }
1213  }
1214 
1220  @Override
1221  public void upgradeSchema() throws CentralRepoException, SQLException, IncompatibleCentralRepoException {
1222  try {
1223  acquireExclusiveLock();
1224  super.upgradeSchema();
1225  } finally {
1226  releaseExclusiveLock();
1227  }
1228  }
1229 
1241  @Override
1242  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws CentralRepoException {
1243  // Multiple users are not supported for SQLite
1244  return null;
1245  }
1246 
1252  private void acquireExclusiveLock() {
1253  rwLock.writeLock().lock();
1254  }
1255 
1261  private void releaseExclusiveLock() {
1262  rwLock.writeLock().unlock();
1263  }
1264 
1270  private void acquireSharedLock() {
1271  rwLock.readLock().lock();
1272  }
1273 
1279  private void releaseSharedLock() {
1280  rwLock.readLock().unlock();
1281  }
1282 
1283  @Override
1284  boolean doesColumnExist(Connection conn, String tableName, String columnName) throws SQLException {
1285  final String tableInfoQueryTemplate = "PRAGMA table_info(%s)"; //NON-NLS
1286  ResultSet resultSet = null;
1287  Statement statement = null;
1288  boolean columnExists = false;
1289  try {
1290  statement = conn.createStatement();
1291  resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName));
1292  while (resultSet.next()) {
1293  // the second value ( 2 ) is the column name
1294  if (resultSet.getString(2).equals(columnName)) {
1295  columnExists = true;
1296  break;
1297  }
1298  }
1299  } finally {
1300  CentralRepoDbUtil.closeResultSet(resultSet);
1301  CentralRepoDbUtil.closeStatement(statement);
1302  }
1303  return columnExists;
1304  }
1305 }

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