Autopsy  4.19.3
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-2021 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  }
234 
243  @Override
244  public void newDbInfo(String name, String value) throws CentralRepoException {
245  try {
246  acquireExclusiveLock();
247  super.newDbInfo(name, value);
248  } finally {
249  releaseExclusiveLock();
250  }
251  }
252 
262  @Override
263  public String getDbInfo(String name) throws CentralRepoException {
264  try {
265  acquireSharedLock();
266  return super.getDbInfo(name);
267  } finally {
268  releaseSharedLock();
269  }
270  }
271 
280  @Override
281  public void updateDbInfo(String name, String value) throws CentralRepoException {
282  try {
283  acquireExclusiveLock();
284  super.updateDbInfo(name, value);
285  } finally {
286  releaseExclusiveLock();
287  }
288  }
289 
295  @Override
296  public CorrelationCase newCase(Case autopsyCase) throws CentralRepoException {
297  try {
298  acquireExclusiveLock();
299  return super.newCase(autopsyCase);
300  } finally {
301  releaseExclusiveLock();
302  }
303  }
304 
305  @Override
306  public void addDataSourceObjectId(int rowId, long dataSourceObjectId) throws CentralRepoException {
307  try {
308  acquireExclusiveLock();
309  super.addDataSourceObjectId(rowId, dataSourceObjectId);
310  } finally {
311  releaseExclusiveLock();
312  }
313  }
314 
322  @Override
323  public CorrelationCase newCase(CorrelationCase eamCase) throws CentralRepoException {
324  try {
325  acquireExclusiveLock();
326  return super.newCase(eamCase);
327  } finally {
328  releaseExclusiveLock();
329  }
330  }
331 
337  @Override
338  public void updateCase(CorrelationCase eamCase) throws CentralRepoException {
339  try {
340  acquireExclusiveLock();
341  super.updateCase(eamCase);
342  } finally {
343  releaseExclusiveLock();
344  }
345  }
346 
354  @Override
355  public CorrelationCase getCaseByUUID(String caseUUID) throws CentralRepoException {
356  try {
357  acquireSharedLock();
358  return super.getCaseByUUID(caseUUID);
359  } finally {
360  releaseSharedLock();
361  }
362  }
363 
371  @Override
372  public CorrelationCase getCaseById(int caseId) throws CentralRepoException {
373  try {
374  acquireSharedLock();
375  return super.getCaseById(caseId);
376  } finally {
377  releaseSharedLock();
378  }
379 
380  }
381 
387  @Override
388  public List<CorrelationCase> getCases() throws CentralRepoException {
389  try {
390  acquireSharedLock();
391  return super.getCases();
392  } finally {
393  releaseSharedLock();
394  }
395  }
396 
402  @Override
403  public CorrelationDataSource newDataSource(CorrelationDataSource eamDataSource) throws CentralRepoException {
404  try {
405  acquireExclusiveLock();
406  return super.newDataSource(eamDataSource);
407  } finally {
408  releaseExclusiveLock();
409  }
410  }
411 
421  @Override
422  public CorrelationDataSource getDataSource(CorrelationCase correlationCase, Long caseDbDataSourceId) throws CentralRepoException {
423  try {
424  acquireSharedLock();
425  return super.getDataSource(correlationCase, caseDbDataSourceId);
426  } finally {
427  releaseSharedLock();
428  }
429  }
430 
440  @Override
441  public CorrelationDataSource getDataSourceById(CorrelationCase correlationCase, int dataSourceId) throws CentralRepoException {
442  try {
443  acquireSharedLock();
444  return super.getDataSourceById(correlationCase, dataSourceId);
445  } finally {
446  releaseSharedLock();
447  }
448  }
449 
455  @Override
456  public List<CorrelationDataSource> getDataSources() throws CentralRepoException {
457  try {
458  acquireSharedLock();
459  return super.getDataSources();
460  } finally {
461  releaseSharedLock();
462  }
463  }
464 
473  @Override
474  public void updateDataSourceName(CorrelationDataSource eamDataSource, String newName) throws CentralRepoException {
475  try {
476  acquireExclusiveLock();
477  super.updateDataSourceName(eamDataSource, newName);
478  } finally {
479  releaseExclusiveLock();
480  }
481  }
482 
488  @Override
489  public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
490  try {
491  acquireExclusiveLock();
492  super.updateDataSourceMd5Hash(eamDataSource);
493  } finally {
494  releaseExclusiveLock();
495  }
496  }
497 
503  @Override
504  public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
505  try {
506  acquireExclusiveLock();
507  super.updateDataSourceSha1Hash(eamDataSource);
508  } finally {
509  releaseExclusiveLock();
510  }
511  }
512 
519  @Override
520  public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws CentralRepoException {
521  try {
522  acquireExclusiveLock();
523  super.updateDataSourceSha256Hash(eamDataSource);
524  } finally {
525  releaseExclusiveLock();
526  }
527  }
528 
535  @Override
536  public void addArtifactInstance(CorrelationAttributeInstance eamArtifact) throws CentralRepoException {
537  try {
538  acquireExclusiveLock();
539  super.addArtifactInstance(eamArtifact);
540  } finally {
541  releaseExclusiveLock();
542  }
543  }
544 
545  @Override
546  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
547  try {
548  acquireSharedLock();
549  return super.getArtifactInstancesByTypeValue(aType, value);
550  } finally {
551  releaseSharedLock();
552  }
553  }
554 
555  @Override
556  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValues(CorrelationAttributeInstance.Type aType, List<String> values) throws CentralRepoException, CorrelationAttributeNormalizationException {
557  try {
558  acquireSharedLock();
559  return super.getArtifactInstancesByTypeValues(aType, values);
560  } finally {
561  releaseSharedLock();
562  }
563  }
564 
565  @Override
566  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValuesAndCases(CorrelationAttributeInstance.Type aType, List<String> values, List<Integer> caseIds) throws CentralRepoException, CorrelationAttributeNormalizationException {
567  try {
568  acquireSharedLock();
569  return super.getArtifactInstancesByTypeValuesAndCases(aType, values, caseIds);
570  } finally {
571  releaseSharedLock();
572  }
573  }
574 
587  @Override
588  public Long getCountArtifactInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
589  try {
590  acquireSharedLock();
591  return super.getCountArtifactInstancesByTypeValue(aType, value);
592  } finally {
593  releaseSharedLock();
594  }
595  }
596 
597  @Override
598  public int getFrequencyPercentage(CorrelationAttributeInstance corAttr) throws CentralRepoException, CorrelationAttributeNormalizationException {
599  try {
600  acquireSharedLock();
601  return super.getFrequencyPercentage(corAttr);
602  } finally {
603  releaseSharedLock();
604  }
605  }
606 
619  @Override
620  public Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
621  try {
622  acquireSharedLock();
623  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
624  } finally {
625  releaseSharedLock();
626  }
627  }
628 
629  @Override
630  public Long getCountCasesWithOtherInstances(CorrelationAttributeInstance instance) throws CentralRepoException, CorrelationAttributeNormalizationException {
631  try {
632  acquireSharedLock();
633  return super.getCountCasesWithOtherInstances(instance);
634  } finally {
635  releaseSharedLock();
636  }
637  }
638 
639  @Override
640  public Long getCountUniqueDataSources() throws CentralRepoException {
641  try {
642  acquireSharedLock();
643  return super.getCountUniqueDataSources();
644  } finally {
645  releaseSharedLock();
646  }
647  }
648 
660  @Override
661  public Long getCountArtifactInstancesByCaseDataSource(CorrelationDataSource correlationDataSource) throws CentralRepoException {
662  try {
663  acquireSharedLock();
664  return super.getCountArtifactInstancesByCaseDataSource(correlationDataSource);
665  } finally {
666  releaseSharedLock();
667  }
668  }
669 
674  @Override
675  public void commitAttributeInstancesBulk() throws CentralRepoException {
676  try {
677  acquireExclusiveLock();
678  super.commitAttributeInstancesBulk();
679  } finally {
680  releaseExclusiveLock();
681  }
682  }
683 
687  @Override
688  public void bulkInsertCases(List<CorrelationCase> cases) throws CentralRepoException {
689  try {
690  acquireExclusiveLock();
691  super.bulkInsertCases(cases);
692  } finally {
693  releaseExclusiveLock();
694  }
695  }
696 
707  @Override
708  public void setAttributeInstanceKnownStatus(CorrelationAttributeInstance eamArtifact, TskData.FileKnown knownStatus) throws CentralRepoException {
709  try {
710  acquireExclusiveLock();
711  super.setAttributeInstanceKnownStatus(eamArtifact, knownStatus);
712  } finally {
713  releaseExclusiveLock();
714  }
715  }
716 
725  @Override
726  public Long getCountArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
727  try {
728  acquireSharedLock();
729  return super.getCountArtifactInstancesKnownBad(aType, value);
730  } finally {
731  releaseSharedLock();
732  }
733  }
734 
747  @Override
748  public List<String> getListCasesHavingArtifactInstancesKnownBad(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
749  try {
750  acquireSharedLock();
751  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
752  } finally {
753  releaseSharedLock();
754  }
755  }
756 
764  @Override
765  public void deleteReferenceSet(int referenceSetID) throws CentralRepoException {
766  try {
767  acquireExclusiveLock();
768  super.deleteReferenceSet(referenceSetID);
769  } finally {
770  releaseExclusiveLock();
771  }
772  }
773 
783  @Override
784  public boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID) throws CentralRepoException, CorrelationAttributeNormalizationException {
785  try {
786  acquireSharedLock();
787  return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
788  } finally {
789  releaseSharedLock();
790  }
791  }
792 
801  @Override
802  public void processInstanceTable(CorrelationAttributeInstance.Type type, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
803  try {
804  acquireSharedLock();
805  super.processInstanceTable(type, instanceTableCallback);
806  } finally {
807  releaseSharedLock();
808  }
809  }
810 
819  @Override
820  public void processInstanceTableWhere(CorrelationAttributeInstance.Type type, String whereClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
821  try {
822  acquireSharedLock();
823  super.processInstanceTableWhere(type, whereClause, instanceTableCallback);
824  } finally {
825  releaseSharedLock();
826  }
827  }
828 
837  @Override
838  public void processSelectClause(String selectClause, InstanceTableCallback instanceTableCallback) throws CentralRepoException {
839  try {
840  acquireSharedLock();
841  super.processSelectClause(selectClause, instanceTableCallback);
842  } finally {
843  releaseSharedLock();
844  }
845  }
846 
847  @Override
848  public void executeCommand(String sql, List<Object> params) throws CentralRepoException {
849  try {
850  acquireExclusiveLock();
851  super.executeCommand(sql, params);
852  } finally {
853  releaseExclusiveLock();
854  }
855  }
856 
857  @Override
858  public void executeQuery(String sql, List<Object> params, CentralRepositoryDbQueryCallback queryCallback) throws CentralRepoException {
859  try {
860  acquireSharedLock();
861  super.executeQuery(sql, params, queryCallback);
862  } finally {
863  releaseSharedLock();
864  }
865  }
866 
879  @Override
880  public boolean referenceSetExists(String referenceSetName, String version) throws CentralRepoException {
881  try {
882  acquireSharedLock();
883  return super.referenceSetExists(referenceSetName, version);
884  } finally {
885  releaseSharedLock();
886  }
887  }
888 
897  @Override
898  public boolean isArtifactKnownBadByReference(CorrelationAttributeInstance.Type aType, String value) throws CentralRepoException, CorrelationAttributeNormalizationException {
899  try {
900  acquireSharedLock();
901  return super.isArtifactKnownBadByReference(aType, value);
902  } finally {
903  releaseSharedLock();
904  }
905  }
906 
916  @Override
917  public CentralRepoOrganization newOrganization(CentralRepoOrganization eamOrg) throws CentralRepoException {
918  try {
919  acquireExclusiveLock();
920  return super.newOrganization(eamOrg);
921  } finally {
922  releaseExclusiveLock();
923  }
924  }
925 
933  @Override
934  public List<CentralRepoOrganization> getOrganizations() throws CentralRepoException {
935  try {
936  acquireSharedLock();
937  return super.getOrganizations();
938  } finally {
939  releaseSharedLock();
940  }
941  }
942 
952  @Override
953  public CentralRepoOrganization getOrganizationByID(int orgID) throws CentralRepoException {
954  try {
955  acquireSharedLock();
956  return super.getOrganizationByID(orgID);
957  } finally {
958  releaseSharedLock();
959  }
960  }
961 
962  @Override
963  public void updateOrganization(CentralRepoOrganization updatedOrganization) throws CentralRepoException {
964  try {
965  acquireExclusiveLock();
966  super.updateOrganization(updatedOrganization);
967  } finally {
968  releaseExclusiveLock();
969  }
970  }
971 
972  @Override
973  public void deleteOrganization(CentralRepoOrganization organizationToDelete) throws CentralRepoException {
974  try {
975  acquireExclusiveLock();
976  super.deleteOrganization(organizationToDelete);
977  } finally {
978  releaseExclusiveLock();
979  }
980  }
981 
991  @Override
992  public int newReferenceSet(CentralRepoFileSet eamGlobalSet) throws CentralRepoException {
993  try {
994  acquireExclusiveLock();
995  return super.newReferenceSet(eamGlobalSet);
996  } finally {
997  releaseExclusiveLock();
998  }
999  }
1000 
1010  @Override
1011  public CentralRepoFileSet getReferenceSetByID(int referenceSetID) throws CentralRepoException {
1012  try {
1013  acquireSharedLock();
1014  return super.getReferenceSetByID(referenceSetID);
1015  } finally {
1016  releaseSharedLock();
1017  }
1018  }
1019 
1029  @Override
1030  public List<CentralRepoFileSet> getAllReferenceSets(CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1031  try {
1032  acquireSharedLock();
1033  return super.getAllReferenceSets(correlationType);
1034  } finally {
1035  releaseSharedLock();
1036  }
1037  }
1038 
1048  @Override
1049  public void addReferenceInstance(CentralRepoFileInstance eamGlobalFileInstance, CorrelationAttributeInstance.Type correlationType) throws CentralRepoException {
1050  try {
1051  acquireExclusiveLock();
1052  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
1053  } finally {
1054  releaseExclusiveLock();
1055  }
1056  }
1057 
1063  @Override
1064  public void bulkInsertReferenceTypeEntries(Set<CentralRepoFileInstance> globalInstances, CorrelationAttributeInstance.Type contentType) throws CentralRepoException {
1065  try {
1066  acquireExclusiveLock();
1067  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
1068  } finally {
1069  releaseExclusiveLock();
1070  }
1071  }
1072 
1083  @Override
1084  public List<CentralRepoFileInstance> getReferenceInstancesByTypeValue(CorrelationAttributeInstance.Type aType, String aValue) throws CentralRepoException, CorrelationAttributeNormalizationException {
1085  try {
1086  acquireSharedLock();
1087  return super.getReferenceInstancesByTypeValue(aType, aValue);
1088  } finally {
1089  releaseSharedLock();
1090  }
1091  }
1092 
1102  @Override
1103  public int newCorrelationType(CorrelationAttributeInstance.Type newType) throws CentralRepoException {
1104  try {
1105  acquireExclusiveLock();
1106  return super.newCorrelationType(newType);
1107  } finally {
1108  releaseExclusiveLock();
1109  }
1110  }
1111 
1121  @Override
1122  public List<CorrelationAttributeInstance.Type> getDefinedCorrelationTypes() throws CentralRepoException {
1123  try {
1124  acquireSharedLock();
1125  return super.getDefinedCorrelationTypes();
1126  } finally {
1127  releaseSharedLock();
1128  }
1129  }
1130 
1140  @Override
1141  public List<CorrelationAttributeInstance.Type> getEnabledCorrelationTypes() throws CentralRepoException {
1142  try {
1143  acquireSharedLock();
1144  return super.getEnabledCorrelationTypes();
1145  } finally {
1146  releaseSharedLock();
1147  }
1148  }
1149 
1159  @Override
1160  public List<CorrelationAttributeInstance.Type> getSupportedCorrelationTypes() throws CentralRepoException {
1161  try {
1162  acquireSharedLock();
1163  return super.getSupportedCorrelationTypes();
1164  } finally {
1165  releaseSharedLock();
1166  }
1167  }
1168 
1176  @Override
1177  public void updateCorrelationType(CorrelationAttributeInstance.Type aType) throws CentralRepoException {
1178  try {
1179  acquireExclusiveLock();
1180  super.updateCorrelationType(aType);
1181  } finally {
1182  releaseExclusiveLock();
1183  }
1184  }
1185 
1195  @Override
1196  public CorrelationAttributeInstance.Type getCorrelationTypeById(int typeId) throws CentralRepoException {
1197  try {
1198  acquireSharedLock();
1199  return super.getCorrelationTypeById(typeId);
1200  } finally {
1201  releaseSharedLock();
1202  }
1203  }
1204 
1210  @Override
1211  public void upgradeSchema() throws CentralRepoException, SQLException, IncompatibleCentralRepoException {
1212  try {
1213  acquireExclusiveLock();
1214  super.upgradeSchema();
1215  } finally {
1216  releaseExclusiveLock();
1217  }
1218  }
1219 
1231  @Override
1232  public CoordinationService.Lock getExclusiveMultiUserDbLock() throws CentralRepoException {
1233  // Multiple users are not supported for SQLite
1234  return null;
1235  }
1236 
1242  private void acquireExclusiveLock() {
1243  rwLock.writeLock().lock();
1244  }
1245 
1251  private void releaseExclusiveLock() {
1252  rwLock.writeLock().unlock();
1253  }
1254 
1260  private void acquireSharedLock() {
1261  rwLock.readLock().lock();
1262  }
1263 
1269  private void releaseSharedLock() {
1270  rwLock.readLock().unlock();
1271  }
1272 
1273  @Override
1274  boolean doesColumnExist(Connection conn, String tableName, String columnName) throws SQLException {
1275  final String tableInfoQueryTemplate = "PRAGMA table_info(%s)"; //NON-NLS
1276  ResultSet resultSet = null;
1277  Statement statement = null;
1278  boolean columnExists = false;
1279  try {
1280  statement = conn.createStatement();
1281  resultSet = statement.executeQuery(String.format(tableInfoQueryTemplate, tableName));
1282  while (resultSet.next()) {
1283  // the second value ( 2 ) is the column name
1284  if (resultSet.getString(2).equals(columnName)) {
1285  columnExists = true;
1286  break;
1287  }
1288  }
1289  } finally {
1290  CentralRepoDbUtil.closeResultSet(resultSet);
1291  CentralRepoDbUtil.closeStatement(statement);
1292  }
1293  return columnExists;
1294  }
1295 }

Copyright © 2012-2022 Basis Technology. Generated on: Wed Oct 5 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.