19 package org.sleuthkit.autopsy.communications;
21 import com.google.common.cache.CacheBuilder;
22 import com.google.common.cache.CacheLoader;
23 import com.google.common.cache.LoadingCache;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.sql.SQLException;
27 import java.util.ArrayList;
28 import java.util.EnumSet;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.concurrent.ExecutionException;
33 import java.util.concurrent.TimeUnit;
34 import java.util.logging.Level;
51 final class ContactCache {
53 private static final Logger logger = Logger.getLogger(ContactCache.class.getName());
55 private static ContactCache instance;
57 private final LoadingCache<String, Map<String, List<BlackboardArtifact>>> accountMap;
69 static synchronized List<BlackboardArtifact> getContacts(Account account)
throws ExecutionException {
70 return getInstance().accountMap.get(
"realMap").get(account.getTypeSpecificID());
76 static synchronized void invalidateCache() {
77 getInstance().accountMap.invalidateAll();
83 private ContactCache() {
85 accountMap = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).build(
86 new CacheLoader<String, Map<String, List<BlackboardArtifact>>>() {
88 public Map<String, List<BlackboardArtifact>> load(String key) {
91 }
catch (SQLException | TskCoreException ex) {
92 logger.log(Level.WARNING,
"Failed to build account to contact map", ex);
94 return new HashMap<>();
98 PropertyChangeListener ingestListener = pce -> {
99 String eventType = pce.getPropertyName();
100 if (eventType.equals(DATA_ADDED.toString())) {
101 ModuleDataEvent eventData = (ModuleDataEvent) pce.getOldValue();
102 if (eventData.getBlackboardArtifactType().getTypeID() == BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT.getTypeID()) {
108 Case.addEventTypeSubscriber(EnumSet.of(Case.Events.CURRENT_CASE), (PropertyChangeEvent event) -> {
109 if (event.getNewValue() == null) {
114 IngestManager.getInstance().addIngestModuleEventListener(EnumSet.of(DATA_ADDED), ingestListener);
122 private static synchronized ContactCache getInstance() {
123 if (instance == null) {
124 instance =
new ContactCache();
138 private Map<String, List<BlackboardArtifact>> buildMap() throws TskCoreException, SQLException {
139 Map<String, List<BlackboardArtifact>> acctMap =
new HashMap<>();
140 List<BlackboardArtifact> contactList = Case.getCurrentCase().getSleuthkitCase().getBlackboardArtifacts(BlackboardArtifact.ARTIFACT_TYPE.TSK_CONTACT);
142 for (BlackboardArtifact contactArtifact : contactList) {
143 List<BlackboardAttribute> contactAttributes = contactArtifact.getAttributes();
144 for (BlackboardAttribute attribute : contactAttributes) {
145 String typeName = attribute.getAttributeType().getTypeName();
147 if (typeName.startsWith(
"TSK_EMAIL")
148 || typeName.startsWith(
"TSK_PHONE")
149 || typeName.startsWith(
"TSK_NAME")
150 || typeName.startsWith(
"TSK_ID")) {
151 String accountID = attribute.getValueString();
152 List<BlackboardArtifact> artifactList = acctMap.getOrDefault(accountID,
new ArrayList<>());
154 acctMap.put(accountID, artifactList);
156 if (!artifactList.contains(contactArtifact)) {
157 artifactList.add(contactArtifact);