Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
RefreshThrottler.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-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.guiutils;
20 
21 import com.google.common.util.concurrent.ThreadFactoryBuilder;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.util.EnumSet;
25 import java.util.Set;
26 import java.util.concurrent.ScheduledThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
30 
36 public class RefreshThrottler {
37 
42  public interface Refresher {
43 
48  void refresh();
49 
57  boolean isRefreshRequired(PropertyChangeEvent evt);
58  }
59 
60  static ScheduledThreadPoolExecutor refreshExecutor = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder().setNameFormat("Node Refresh Thread").build());
61  // Keep a thread safe reference to the current refresh task (if any)
62  private final AtomicReference<RefreshTask> refreshTaskRef;
63 
64  // The factory instance that will be called when a refresh is due.
65  private final Refresher refresher;
66 
67  private static final long MIN_SECONDS_BETWEEN_REFRESH = 5;
68 
69  private static final Set<IngestManager.IngestModuleEvent> INGEST_MODULE_EVENTS_OF_INTEREST = EnumSet.of(
73 
78  private final class RefreshTask implements Runnable {
79 
80  @Override
81  public void run() {
82  // Call refresh on the factory
83  refresher.refresh();
84  // Clear the refresh task reference
85  refreshTaskRef.set(null);
86  }
87  }
88 
93  private final PropertyChangeListener pcl;
94 
96  this.refreshTaskRef = new AtomicReference<>(null);
97  refresher = r;
98 
99  pcl = (PropertyChangeEvent evt) -> {
100  String eventType = evt.getPropertyName();
101  if (eventType.equals(IngestManager.IngestModuleEvent.DATA_ADDED.toString())
102  || eventType.equals(IngestManager.IngestModuleEvent.CONTENT_CHANGED.toString())
103  || eventType.equals(IngestManager.IngestModuleEvent.FILE_DONE.toString())) {
104  if (!refresher.isRefreshRequired(evt)) {
105  return;
106  }
107 
108  RefreshTask task = new RefreshTask();
109  if (refreshTaskRef.compareAndSet(null, task)) {
110  refreshExecutor.schedule(task, MIN_SECONDS_BETWEEN_REFRESH, TimeUnit.SECONDS);
111  }
112  }
113  };
114  }
115 
121  }
122 
126  public void unregisterEventListener() {
128  }
129 }
void removeIngestModuleEventListener(final PropertyChangeListener listener)
static synchronized IngestManager getInstance()
static final Set< IngestManager.IngestModuleEvent > INGEST_MODULE_EVENTS_OF_INTEREST
boolean isRefreshRequired(PropertyChangeEvent evt)
void addIngestModuleEventListener(final PropertyChangeListener listener)
final AtomicReference< RefreshTask > refreshTaskRef

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