Autopsy  4.21.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
LocalEventPublisher.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 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.events;
20 
21 import java.beans.PropertyChangeListener;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.logging.Level;
26 import javax.annotation.concurrent.ThreadSafe;
28 
36 @ThreadSafe
37 final class LocalEventPublisher {
38 
39  private static final Logger logger = Logger.getLogger(LocalEventPublisher.class.getName());
40  private final Map<String, Set<PropertyChangeListener>> subscribersByEvent;
41 
46  LocalEventPublisher() {
47  subscribersByEvent = new ConcurrentHashMap<>();
48  }
49 
56  void addSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
57  for (String eventName : eventNames) {
58  addSubscriber(eventName, subscriber);
59  }
60  }
61 
68  void addSubscriber(String eventName, PropertyChangeListener subscriber) {
69  subscribersByEvent.putIfAbsent(eventName, ConcurrentHashMap.<PropertyChangeListener>newKeySet());
70  Set<PropertyChangeListener> subscribers = subscribersByEvent.get(eventName);
71  subscribers.add(subscriber);
72  }
73 
80  void removeSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
81  for (String eventName : eventNames) {
82  removeSubscriber(eventName, subscriber);
83  }
84  }
85 
92  void removeSubscriber(String eventName, PropertyChangeListener subscriber) {
93  Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(eventName, null);
94  if (null != subscribers) {
95  subscribers.remove(subscriber);
96  }
97  }
98 
105  void publish(AutopsyEvent event) {
106  Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(event.getPropertyName(), null);
107  if (null != subscribers) {
108  subscribers.forEach((subscriber) -> {
109  try {
110  subscriber.propertyChange(event);
111  } catch (Exception ex) {
112  logger.log(Level.SEVERE, "Exception thrown by subscriber", ex); //NON-NLS
113  }
114  });
115  }
116  }
117 
118 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124

Copyright © 2012-2022 Basis Technology. Generated on: Tue Feb 6 2024
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.