Autopsy  4.1
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-2015 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;
27 
35 final class LocalEventPublisher {
36 
37  private static final Logger logger = Logger.getLogger(LocalEventPublisher.class.getName());
38  private final Map<String, Set<PropertyChangeListener>> subscribersByEvent;
39 
44  LocalEventPublisher() {
45  subscribersByEvent = new ConcurrentHashMap<>();
46  }
47 
54  void addSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
55  for (String eventName : eventNames) {
56  addSubscriber(eventName, subscriber);
57  }
58  }
59 
66  void addSubscriber(String eventName, PropertyChangeListener subscriber) {
67  subscribersByEvent.putIfAbsent(eventName, ConcurrentHashMap.<PropertyChangeListener>newKeySet());
68  Set<PropertyChangeListener> subscribers = subscribersByEvent.get(eventName);
69  subscribers.add(subscriber);
70  }
71 
78  void removeSubscriber(Set<String> eventNames, PropertyChangeListener subscriber) {
79  for (String eventName : eventNames) {
80  removeSubscriber(eventName, subscriber);
81  }
82  }
83 
90  void removeSubscriber(String eventName, PropertyChangeListener subscriber) {
91  Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(eventName, null);
92  if (null != subscribers) {
93  subscribers.remove(subscriber);
94  }
95  }
96 
103  void publish(AutopsyEvent event) {
104  Set<PropertyChangeListener> subscribers = subscribersByEvent.getOrDefault(event.getPropertyName(), null);
105  if (null != subscribers) {
106  for (PropertyChangeListener subscriber : subscribers) {
107  try {
108  subscriber.propertyChange(event);
109  } catch (Exception ex) {
110  logger.log(Level.SEVERE, "Exception thrown by subscriber", ex); //NON-NLS
111  }
112  }
113  }
114  }
115 
116 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:161

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