src/jdk.jfr/share/classes/jdk/jfr/internal/FilePurger.java
author mseledtsov
Thu, 12 Sep 2019 20:45:23 -0700
branchJEP-349-branch
changeset 58120 630261dd77f9
parent 57361 53dccc90a5be
permissions -rw-r--r--
Fixed tabs and trailing white spaces
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58120
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     1
/*
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     4
 *
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    10
 *
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    15
 * accompanied this code).
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    16
 *
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    20
 *
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    23
 * questions.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    24
 */
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    25
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    26
package jdk.jfr.internal;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    27
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    28
import java.io.IOException;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    29
import java.util.ArrayList;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    30
import java.util.LinkedHashSet;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    31
import java.util.Set;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    32
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    33
import jdk.jfr.internal.SecuritySupport.SafePath;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    34
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    35
// This class keeps track of files that can't be deleted
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    36
// so they can a later staged be removed.
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    37
final class FilePurger {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    38
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    39
    private final static Set<SafePath> paths = new LinkedHashSet<>();
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    40
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    41
    public synchronized static void add(SafePath p) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    42
        paths.add(p);
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    43
        if (paths.size() > 1000) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    44
            removeOldest();
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    45
        }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    46
    }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    47
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    48
    public synchronized static void purge() {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    49
        if (paths.isEmpty()) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    50
            return;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    51
        }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    52
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    53
        for (SafePath p : new ArrayList<>(paths)) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    54
            if (delete(p)) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    55
                paths.remove(p);
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    56
            }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    57
        }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    58
    }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    59
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    60
    private static void removeOldest() {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    61
        SafePath oldest = paths.iterator().next();
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    62
        paths.remove(oldest);
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    63
    }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    64
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    65
    private static boolean delete(SafePath p) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    66
        try {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    67
            SecuritySupport.delete(p);
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    68
            return true;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    69
        } catch (IOException e) {
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    70
            return false;
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    71
        }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    72
    }
630261dd77f9 Fixed tabs and trailing white spaces
mseledtsov
parents: 57361
diff changeset
    73
}