Autopsy 4.22.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
JFileChooserFactory.java
Go to the documentation of this file.
1/*
2 * Autopsy Forensic Browser
3 *
4 * Copyright 2021 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 */
19package org.sleuthkit.autopsy.guiutils;
20
21import com.google.common.util.concurrent.ThreadFactoryBuilder;
22import java.awt.Cursor;
23import java.util.concurrent.Callable;
24import java.util.concurrent.ExecutionException;
25import java.util.concurrent.ExecutorService;
26import java.util.concurrent.Executors;
27import java.util.concurrent.FutureTask;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30import javax.swing.JFileChooser;
31import org.openide.windows.WindowManager;
32import org.sleuthkit.autopsy.coreutils.ThreadConfined;
33
51public final class JFileChooserFactory {
52
53 private static final Logger logger = Logger.getLogger(JFileChooserFactory.class.getName());
54
55 private final FutureTask<JFileChooser> futureFileChooser;
56 private JFileChooser chooser;
57 private final ExecutorService executor;
58
64 this(null);
65 }
66
77 public JFileChooserFactory(Class<? extends JFileChooser> cls) {
78 if (cls == null) {
79 futureFileChooser = new FutureTask<>(JFileChooser::new);
80 } else {
81 futureFileChooser = new FutureTask<>(new ChooserCallable(cls));
82 }
83
84 // Append the caller class name to the thread name to add information to thread dumps.
85 String threadName = "JFileChooser-background-thread";
86 String callerName = getCallerClassName();
87 if (callerName != null) {
88 threadName += "-" + callerName;
89 }
90
91 executor = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat(threadName).build());
93 }
94
101 private static String getCallerClassName() {
102 StackTraceElement[] stElements = Thread.currentThread().getStackTrace();
103 for (int i=1; i<stElements.length; i++) {
104 StackTraceElement ste = stElements[i];
105
106 // Look for the first class that is not this one.
107 if (!ste.getClassName().equals(JFileChooserFactory.class.getName())&& ste.getClassName().indexOf("java.lang.Thread")!=0) {
108 String resultClassName = ste.getClassName();
109 if (resultClassName.contains(".")) {
110 // For brevity, omit the package name
111 int index = resultClassName.lastIndexOf(".") + 1;
112 if (index < resultClassName.length()) {
113 resultClassName = resultClassName.substring(index);
114 }
115 }
116 return resultClassName;
117 }
118 }
119 return null;
120 }
121
131 public JFileChooser getChooser() {
132 if (chooser == null) {
133 // In case this takes a moment show the wait cursor.
134 try {
135 WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
136 try {
137 // get() will only return when the initilization of the
138 // JFileChooser has completed.
140 } catch (InterruptedException | ExecutionException ex) {
141 // An exception is generally not expected. On the off chance
142 // one does occur save the situation by created a new
143 // instance in the EDT.
144 logger.log(Level.WARNING, "Failed to initialize JFileChooser in background thread.");
145 chooser = new JFileChooser();
146 }
147 } finally {
148 WindowManager.getDefault().getMainWindow().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
149 executor.shutdown();
150 }
151 }
152
153 return chooser;
154 }
155
163 private class ChooserCallable implements Callable<JFileChooser> {
164
165 private final Class<? extends JFileChooser> type;
166
172 ChooserCallable(Class<? extends JFileChooser> type) {
173 this.type = type;
174 }
175
176 @Override
177 public JFileChooser call() throws Exception {
178 return type.newInstance();
179 }
180 }
181}
synchronized static Logger getLogger(String name)
Definition Logger.java:124
JFileChooserFactory(Class<? extends JFileChooser > cls)

Copyright © 2012-2024 Sleuth Kit Labs. Generated on:
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.