814920a099ee946453263e682c7bb2cda00b7983ludo/*
814920a099ee946453263e682c7bb2cda00b7983ludo * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
54c356b692bd520cec70e821a41c97c39b76aac0mark * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
814920a099ee946453263e682c7bb2cda00b7983ludo *
77ca522cbfce7ae2cfdd56c44de525097821bb15mark * This code is free software; you can redistribute it and/or modify it
814920a099ee946453263e682c7bb2cda00b7983ludo * under the terms of the GNU General Public License version 2 only, as
814920a099ee946453263e682c7bb2cda00b7983ludo * published by the Free Software Foundation.
814920a099ee946453263e682c7bb2cda00b7983ludo *
814920a099ee946453263e682c7bb2cda00b7983ludo * This code is distributed in the hope that it will be useful, but WITHOUT
77ca522cbfce7ae2cfdd56c44de525097821bb15mark * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
814920a099ee946453263e682c7bb2cda00b7983ludo * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
814920a099ee946453263e682c7bb2cda00b7983ludo * version 2 for more details (a copy is included in the LICENSE file that
814920a099ee946453263e682c7bb2cda00b7983ludo * accompanied this code).
814920a099ee946453263e682c7bb2cda00b7983ludo *
814920a099ee946453263e682c7bb2cda00b7983ludo * You should have received a copy of the GNU General Public License version
814920a099ee946453263e682c7bb2cda00b7983ludo * 2 along with this work; if not, write to the Free Software Foundation,
814920a099ee946453263e682c7bb2cda00b7983ludo * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
814920a099ee946453263e682c7bb2cda00b7983ludo *
814920a099ee946453263e682c7bb2cda00b7983ludo * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
814920a099ee946453263e682c7bb2cda00b7983ludo * or visit www.oracle.com if you need additional information or have any
814920a099ee946453263e682c7bb2cda00b7983ludo * questions.
814920a099ee946453263e682c7bb2cda00b7983ludo */
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo/* @test
814920a099ee946453263e682c7bb2cda00b7983ludo @bug 6713352
814920a099ee946453263e682c7bb2cda00b7983ludo @summary Deadlock in JFileChooser with synchronized custom FileSystemView
814920a099ee946453263e682c7bb2cda00b7983ludo @author Pavel Porvatov
814920a099ee946453263e682c7bb2cda00b7983ludo @run main bug6713352
814920a099ee946453263e682c7bb2cda00b7983ludo*/
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludoimport sun.awt.shell.ShellFolder;
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludoimport javax.swing.*;
814920a099ee946453263e682c7bb2cda00b7983ludoimport javax.swing.filechooser.FileSystemView;
814920a099ee946453263e682c7bb2cda00b7983ludoimport java.io.File;
814920a099ee946453263e682c7bb2cda00b7983ludoimport java.io.FileNotFoundException;
814920a099ee946453263e682c7bb2cda00b7983ludoimport java.io.IOException;
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludopublic class bug6713352 {
814920a099ee946453263e682c7bb2cda00b7983ludo public static void main(String[] args) throws Exception {
814920a099ee946453263e682c7bb2cda00b7983ludo SwingUtilities.invokeAndWait(new Runnable() {
814920a099ee946453263e682c7bb2cda00b7983ludo public void run() {
814920a099ee946453263e682c7bb2cda00b7983ludo String tempDir = System.getProperty("java.io.tmpdir");
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo if (tempDir == null || !new File(tempDir).isDirectory()) {
814920a099ee946453263e682c7bb2cda00b7983ludo tempDir = System.getProperty("user.home");
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo MyFileSystemView systemView = new MyFileSystemView();
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo synchronized (systemView) { // Get SystemView lock
814920a099ee946453263e682c7bb2cda00b7983ludo new JFileChooser(systemView);
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo // Wait a little bit. BasicDirectoryModel will lock Invoker and stop on
814920a099ee946453263e682c7bb2cda00b7983ludo // the bug6713352.MyFileSystemView.getFiles() method
814920a099ee946453263e682c7bb2cda00b7983ludo try {
814920a099ee946453263e682c7bb2cda00b7983ludo Thread.sleep(5000);
814920a099ee946453263e682c7bb2cda00b7983ludo } catch (InterruptedException e) {
814920a099ee946453263e682c7bb2cda00b7983ludo throw new RuntimeException(e);
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo try {
814920a099ee946453263e682c7bb2cda00b7983ludo System.out.println("Try to get Invokers lock");
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo ShellFolder.getShellFolder(new File(tempDir)).listFiles(true);
814920a099ee946453263e682c7bb2cda00b7983ludo } catch (FileNotFoundException e) {
814920a099ee946453263e682c7bb2cda00b7983ludo throw new RuntimeException(e);
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo // To avoid RejectedExecutionException in BasicDirectoryModel wait a second
814920a099ee946453263e682c7bb2cda00b7983ludo try {
814920a099ee946453263e682c7bb2cda00b7983ludo Thread.sleep(1000);
814920a099ee946453263e682c7bb2cda00b7983ludo } catch (InterruptedException e) {
814920a099ee946453263e682c7bb2cda00b7983ludo throw new RuntimeException(e);
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo });
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo private static class MyFileSystemView extends FileSystemView {
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo public File createNewFolder(File containingDir) throws IOException {
814920a099ee946453263e682c7bb2cda00b7983ludo return null;
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo public File[] getFiles(File dir, boolean useFileHiding) {
814920a099ee946453263e682c7bb2cda00b7983ludo System.out.println("getFiles start");
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo File[] result;
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo synchronized (this) {
814920a099ee946453263e682c7bb2cda00b7983ludo result = super.getFiles(dir, useFileHiding);
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo System.out.println("getFiles finished");
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo return result;
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo
814920a099ee946453263e682c7bb2cda00b7983ludo public synchronized Boolean isTraversable(File f) {
814920a099ee946453263e682c7bb2cda00b7983ludo return super.isTraversable(f);
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo }
814920a099ee946453263e682c7bb2cda00b7983ludo}
814920a099ee946453263e682c7bb2cda00b7983ludo