bug6713352.java revision 17565:b762aafa34e3
1111888Sjlemon/*
2193219Srwatson * Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.
3222249Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4103781Sjake *
5103781Sjake * This code is free software; you can redistribute it and/or modify it
6204199Srwatson * under the terms of the GNU General Public License version 2 only, as
7204199Srwatson * published by the Free Software Foundation.
8204199Srwatson *
9103781Sjake * This code is distributed in the hope that it will be useful, but WITHOUT
10103781Sjake * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11103781Sjake * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12103781Sjake * version 2 for more details (a copy is included in the LICENSE file that
13111888Sjlemon * accompanied this code).
14103781Sjake *
15103781Sjake * You should have received a copy of the GNU General Public License version
16103781Sjake * 2 along with this work; if not, write to the Free Software Foundation,
17103781Sjake * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18111888Sjlemon *
19111888Sjlemon * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20111888Sjlemon * or visit www.oracle.com if you need additional information or have any
21111888Sjlemon * questions.
22111888Sjlemon */
23111888Sjlemon
24111888Sjlemon/**
25111888Sjlemon * @test
26111888Sjlemon * @key headful
27111888Sjlemon * @bug 6713352
28111888Sjlemon * @summary Deadlock in JFileChooser with synchronized custom FileSystemView
29193219Srwatson * @author Pavel Porvatov
30193219Srwatson * @modules java.desktop/sun.awt.shell
31193219Srwatson * @run main bug6713352
32193219Srwatson */
33193219Srwatson
34193219Srwatsonimport sun.awt.shell.ShellFolder;
35193219Srwatson
36193219Srwatsonimport javax.swing.*;
37193219Srwatsonimport javax.swing.filechooser.FileSystemView;
38193219Srwatsonimport java.io.File;
39200898Srwatsonimport java.io.FileNotFoundException;
40200898Srwatsonimport java.io.IOException;
41200898Srwatson
42103781Sjakepublic class bug6713352 {
43200898Srwatson    public static void main(String[] args) throws Exception {
44193219Srwatson        SwingUtilities.invokeAndWait(new Runnable() {
45200898Srwatson            public void run() {
46193219Srwatson                String tempDir = System.getProperty("java.io.tmpdir");
47193219Srwatson
48193219Srwatson                if (tempDir == null || !new File(tempDir).isDirectory()) {
49193219Srwatson                    tempDir = System.getProperty("user.home");
50193219Srwatson                }
51193219Srwatson
52193219Srwatson                MyFileSystemView systemView = new MyFileSystemView();
53193219Srwatson
54193219Srwatson                synchronized (systemView) { // Get SystemView lock
55193219Srwatson                    new JFileChooser(systemView);
56193219Srwatson
57193219Srwatson                    // Wait a little bit. BasicDirectoryModel will lock Invoker and stop on
58193219Srwatson                    // the bug6713352.MyFileSystemView.getFiles() method
59193219Srwatson                    try {
60193219Srwatson                        Thread.sleep(5000);
61200898Srwatson                    } catch (InterruptedException e) {
62193219Srwatson                        throw new RuntimeException(e);
63200898Srwatson                    }
64103781Sjake
65103781Sjake                    try {
66193219Srwatson                        System.out.println("Try to get Invokers lock");
67150968Sglebius
68134443Srwatson                        ShellFolder.getShellFolder(new File(tempDir)).listFiles(true);
69103781Sjake                    } catch (FileNotFoundException e) {
70111888Sjlemon                        throw new RuntimeException(e);
71103781Sjake                    }
72111888Sjlemon                }
73193219Srwatson
74111888Sjlemon                // To avoid RejectedExecutionException in BasicDirectoryModel wait a second
75193219Srwatson                try {
76193219Srwatson                    Thread.sleep(1000);
77195019Srwatson                } catch (InterruptedException e) {
78111888Sjlemon                    throw new RuntimeException(e);
79193219Srwatson                }
80193219Srwatson            }
81193219Srwatson        });
82193219Srwatson    }
83111888Sjlemon
84193219Srwatson    private static class MyFileSystemView extends FileSystemView {
85103781Sjake
86193219Srwatson        public File createNewFolder(File containingDir) throws IOException {
87193219Srwatson            return null;
88193219Srwatson        }
89111888Sjlemon
90204497Srwatson        public File[] getFiles(File dir, boolean useFileHiding) {
91111888Sjlemon            System.out.println("getFiles start");
92111888Sjlemon
93103781Sjake            File[] result;
94204497Srwatson
95196019Srwatson            synchronized (this) {
96103781Sjake                result = super.getFiles(dir, useFileHiding);
97193219Srwatson            }
98193219Srwatson
99193219Srwatson            System.out.println("getFiles finished");
100193219Srwatson
101193219Srwatson            return result;
102193219Srwatson        }
103193219Srwatson
104204497Srwatson        public synchronized Boolean isTraversable(File f) {
105193219Srwatson            return super.isTraversable(f);
106193219Srwatson        }
107193219Srwatson    }
108193219Srwatson}
109193219Srwatson