Autopsy  4.19.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
AbstractNodePropertySheetTask.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 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.datamodel.utils;
20 
21 import com.google.common.util.concurrent.ThreadFactoryBuilder;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24 import java.lang.ref.WeakReference;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.Future;
28 import java.util.logging.Level;
29 import org.openide.nodes.AbstractNode;
32 
49 public abstract class AbstractNodePropertySheetTask<T extends AbstractNode> implements Runnable {
50 
51  private static final Logger LOGGER = Logger.getLogger(AbstractContentNode.class.getName());
52  private static final Integer THREAD_POOL_SIZE = 10;
53  private static final ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE, new ThreadFactoryBuilder().setNameFormat("node-background-task-%d").build());
54  private final WeakReference<T> weakNodeRef;
55  private final WeakReference<PropertyChangeListener> weakListenerRef;
56 
67  private static Future<?> submitTask(AbstractNodePropertySheetTask<?> task) {
68  return executor.submit(task);
69  }
70 
91  protected AbstractNodePropertySheetTask(T node, PropertyChangeListener listener) {
92  this.weakNodeRef = new WeakReference<>(node);
93  this.weakListenerRef = new WeakReference<>(listener);
94  }
95 
110  protected abstract PropertyChangeEvent computePropertyValue(T node) throws Exception;
111 
117  public final Future<?> submit() {
118  return submitTask(this);
119  }
120 
121  @Override
122  public final void run() {
123  try {
124  T node = this.weakNodeRef.get();
125  PropertyChangeListener listener = this.weakListenerRef.get();
126  if (node == null || listener == null) {
127  return;
128  }
129 
130  if (Thread.currentThread().isInterrupted()) {
131  return;
132  }
133 
134  PropertyChangeEvent changeEvent = computePropertyValue(node);
135 
136  if (Thread.currentThread().isInterrupted()) {
137  return;
138  }
139 
140  if (changeEvent != null) {
141  listener.propertyChange(changeEvent);
142  }
143 
144  } catch (Exception ex) {
145  LOGGER.log(Level.WARNING, "Error executing property sheet values computation background task", ex);
146  }
147 
148  }
149 
150 }
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static Future<?> submitTask(AbstractNodePropertySheetTask<?> task)

Copyright © 2012-2021 Basis Technology. Generated on: Fri Aug 6 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.