1 /*
   2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test %W% %E%
  25    @bug 6798062
  26    @summary Memory Leak on using getFiles of FileSystemView
  27    @author Pavel Porvatov
  28    @run applet/manual=done bug6798062.html
  29 */
  30 
  31 import sun.awt.OSInfo;
  32 import sun.awt.shell.ShellFolder;
  33 
  34 import javax.swing.*;
  35 import java.awt.event.ActionEvent;
  36 import java.awt.event.ActionListener;
  37 import java.awt.*;
  38 import java.io.File;
  39 import java.io.FileNotFoundException;
  40 
  41 public class bug6798062 extends JApplet {
  42 
  43     private final JSlider slider = new JSlider(0, 100);
  44 
  45     private final JTextField tfLink = new JTextField();
  46 
  47     private final JButton btnStart = new JButton("Start");
  48 
  49     private final JButton btnStop = new JButton("Stop");
  50 
  51     private final JButton btnGC = new JButton("Run System.gc()");
  52 
  53     private ShellFolder folder;
  54 
  55     private Thread thread;
  56 
  57     public static void main(String[] args) {
  58         JFrame frame = new JFrame("bug6798062");
  59 
  60         frame.setSize(400, 300);
  61         frame.setLocationRelativeTo(null);
  62         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63         frame.add(new bug6798062().initialize());
  64 
  65         frame.setVisible(true);
  66     }
  67 
  68     public void init() {
  69         add(initialize());
  70     }
  71 
  72     private JComponent initialize() {
  73         if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
  74             return new JLabel("The test is suitable only for Windows");
  75         }
  76 
  77         String tempDir = System.getProperty("java.io.tmpdir");
  78 
  79         if (tempDir.length() == 0) { // 'java.io.tmpdir' isn't guaranteed to be defined
  80             tempDir = System.getProperty("user.home");
  81         }
  82 
  83         System.out.println("Temp directory: " + tempDir);
  84 
  85         try {
  86             folder = ShellFolder.getShellFolder(new File(tempDir));
  87         } catch (FileNotFoundException e) {
  88             fail("Directory " + tempDir + " not found");
  89         }
  90 
  91         slider.setMajorTickSpacing(10);
  92         slider.setPaintTicks(true);
  93         slider.setPaintLabels(true);
  94         slider.setSnapToTicks(true);
  95         slider.setValue(10);
  96 
  97         btnStart.addActionListener(new ActionListener() {
  98             public void actionPerformed(ActionEvent e) {
  99                 setEnabledState(false);
 100 
 101                 thread = new MyThread(slider.getValue(), tfLink.getText());
 102                 thread.start();
 103             }
 104         });
 105 
 106         btnStop.setEnabled(false);
 107 
 108         btnStop.addActionListener(new ActionListener() {
 109             public void actionPerformed(ActionEvent e) {
 110                 thread.interrupt();
 111                 thread = null;
 112 
 113                 setEnabledState(true);
 114             }
 115         });
 116 
 117         btnGC.addActionListener(new ActionListener() {
 118             public void actionPerformed(ActionEvent e) {
 119                 System.gc();
 120             }
 121         });
 122 
 123         setEnabledState(true);
 124 
 125         JPanel pnButtons = new JPanel();
 126 
 127         pnButtons.setLayout(new BoxLayout(pnButtons, BoxLayout.X_AXIS));
 128 
 129         pnButtons.add(btnStart);
 130         pnButtons.add(btnStop);
 131         pnButtons.add(btnGC);
 132 
 133         tfLink.setMaximumSize(new Dimension(300, 20));
 134 
 135         JPanel pnContent = new JPanel();
 136 
 137         pnContent.setLayout(new BoxLayout(pnContent, BoxLayout.Y_AXIS));
 138         pnContent.add(new JLabel("Delay between listFiles() invocation (ms):"));
 139         pnContent.add(slider);
 140         pnContent.add(new JLabel("Provide link here:"));
 141         pnContent.add(tfLink);
 142         pnContent.add(pnButtons);
 143 
 144         return pnContent;
 145     }
 146 
 147     private void setEnabledState(boolean enabled) {
 148         slider.setEnabled(enabled);
 149         btnStart.setEnabled(enabled);
 150         btnStop.setEnabled(!enabled);
 151     }
 152 
 153     private static void fail(String msg) {
 154         throw new RuntimeException(msg);
 155     }
 156 
 157     private class MyThread extends Thread {
 158         private final int delay;
 159 
 160         private final ShellFolder link;
 161 
 162         private MyThread(int delay, String link) {
 163             this.delay = delay;
 164 
 165             ShellFolder linkFolder;
 166 
 167             try {
 168                 linkFolder = ShellFolder.getShellFolder(new File(link));
 169             } catch (FileNotFoundException e) {
 170                 e.printStackTrace();
 171 
 172                 linkFolder = null;
 173             }
 174 
 175             this.link = linkFolder;
 176         }
 177 
 178         public void run() {
 179             while (!isInterrupted()) {
 180                 folder.listFiles();
 181                 if (link != null) {
 182                     try {
 183                         link.getLinkLocation();
 184                     } catch (FileNotFoundException e) {
 185                         e.printStackTrace();
 186                     }
 187                 }
 188 
 189                 if (delay > 0) {
 190                     try {
 191                         Thread.sleep(delay);
 192                     } catch (InterruptedException e1) {
 193                         // The thread was interrupted
 194                         return;
 195                     }
 196                 }
 197             }
 198         }
 199     }
 200 }