src/jdk.jfr/share/classes/jdk/jfr/internal/FilePurger.java
branchJEP-349-branch
changeset 57361 53dccc90a5be
child 58120 630261dd77f9
equal deleted inserted replaced
57360:5d043a159d5c 57361:53dccc90a5be
       
     1 package jdk.jfr.internal;
       
     2 import java.io.IOException;
       
     3 import java.util.ArrayList;
       
     4 import java.util.LinkedHashSet;
       
     5 import java.util.Set;
       
     6 
       
     7 import jdk.jfr.internal.SecuritySupport.SafePath;
       
     8 
       
     9 // This class keeps track of files that can't be deleted
       
    10 // so they can a later staged be removed.
       
    11 final class FilePurger {
       
    12 
       
    13 	private final static Set<SafePath> paths = new LinkedHashSet<>();
       
    14 
       
    15 	public synchronized static void add(SafePath p) {
       
    16 		paths.add(p);
       
    17 		if (paths.size() > 1000) {
       
    18 			removeOldest();
       
    19 		}
       
    20 	}
       
    21 
       
    22 	public synchronized static void purge() {
       
    23 		if (paths.isEmpty()) {
       
    24 			return;
       
    25 		}
       
    26 
       
    27 		for (SafePath p : new ArrayList<>(paths)) {
       
    28 			if (delete(p)) {
       
    29 				paths.remove(p);
       
    30 			}
       
    31 		}
       
    32 	}
       
    33 
       
    34     private static void removeOldest() {
       
    35         SafePath oldest = paths.iterator().next();
       
    36         paths.remove(oldest);
       
    37     }
       
    38 
       
    39 	private static boolean delete(SafePath p) {
       
    40 		try {
       
    41 			SecuritySupport.delete(p);
       
    42 			return true;
       
    43 		} catch (IOException e) {
       
    44 			return false;
       
    45 		}
       
    46 	}
       
    47 }