src/jdk.jfr/share/classes/jdk/jfr/internal/Repository.java
changeset 58863 c16ac7a2eba4
parent 58240 046533575954
child 59226 a0f39cc47387
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    41     private static final JVM jvm = JVM.getJVM();
    41     private static final JVM jvm = JVM.getJVM();
    42     private static final Repository instance = new Repository();
    42     private static final Repository instance = new Repository();
    43 
    43 
    44     public final static DateTimeFormatter REPO_DATE_FORMAT = DateTimeFormatter
    44     public final static DateTimeFormatter REPO_DATE_FORMAT = DateTimeFormatter
    45             .ofPattern("yyyy_MM_dd_HH_mm_ss");
    45             .ofPattern("yyyy_MM_dd_HH_mm_ss");
       
    46     private static final String JFR_REPOSITORY_LOCATION_PROPERTY = "jdk.jfr.repository";
    46 
    47 
    47     private final Set<SafePath> cleanupDirectories = new HashSet<>();
    48     private final Set<SafePath> cleanupDirectories = new HashSet<>();
    48     private SafePath baseLocation;
    49     private SafePath baseLocation;
    49     private SafePath repository;
    50     private SafePath repository;
    50 
    51 
    53 
    54 
    54     public static Repository getRepository() {
    55     public static Repository getRepository() {
    55         return instance;
    56         return instance;
    56     }
    57     }
    57 
    58 
    58     public synchronized void setBasePath(SafePath baseLocation) throws Exception {
    59     public synchronized void setBasePath(SafePath baseLocation) throws IOException {
    59 
       
    60         if(baseLocation.equals(this.baseLocation)) {
    60         if(baseLocation.equals(this.baseLocation)) {
    61             Logger.log(LogTag.JFR, LogLevel.INFO, "Same base repository path " + baseLocation.toString() + " is set");
    61             Logger.log(LogTag.JFR, LogLevel.INFO, "Same base repository path " + baseLocation.toString() + " is set");
    62             return;
    62             return;
    63         }
    63         }
    64         // Probe to see if repository can be created, needed for fail fast
    64         // Probe to see if repository can be created, needed for fail fast
    72             Logger.log(LogTag.JFR, LogLevel.INFO, "Could not delete disk repository " + repository);
    72             Logger.log(LogTag.JFR, LogLevel.INFO, "Could not delete disk repository " + repository);
    73         }
    73         }
    74         this.baseLocation = baseLocation;
    74         this.baseLocation = baseLocation;
    75     }
    75     }
    76 
    76 
    77     synchronized void ensureRepository() throws Exception {
    77     public synchronized void ensureRepository() throws IOException {
    78         if (baseLocation == null) {
    78         if (baseLocation == null) {
    79             setBasePath(SecuritySupport.JAVA_IO_TMPDIR);
    79             setBasePath(SecuritySupport.JAVA_IO_TMPDIR);
    80         }
    80         }
    81     }
    81     }
    82 
    82 
    94             jvm.abort(errorMsg);
    94             jvm.abort(errorMsg);
    95             throw new InternalError("Could not abort after JFR disk creation error");
    95             throw new InternalError("Could not abort after JFR disk creation error");
    96         }
    96         }
    97     }
    97     }
    98 
    98 
    99     private static SafePath createRepository(SafePath basePath) throws Exception {
    99     private static SafePath createRepository(SafePath basePath) throws IOException {
   100         SafePath canonicalBaseRepositoryPath = createRealBasePath(basePath);
   100         SafePath canonicalBaseRepositoryPath = createRealBasePath(basePath);
   101         SafePath f = null;
   101         SafePath f = null;
   102 
   102 
   103         String basename = REPO_DATE_FORMAT.format(LocalDateTime.now()) + "_" + JVM.getJVM().getPid();
   103         String basename = REPO_DATE_FORMAT.format(LocalDateTime.now()) + "_" + JVM.getJVM().getPid();
   104         String name = basename;
   104         String name = basename;
   111             }
   111             }
   112             name = basename + "_" + i;
   112             name = basename + "_" + i;
   113         }
   113         }
   114 
   114 
   115         if (i == MAX_REPO_CREATION_RETRIES) {
   115         if (i == MAX_REPO_CREATION_RETRIES) {
   116             throw new Exception("Unable to create JFR repository directory using base location (" + basePath + ")");
   116             throw new IOException("Unable to create JFR repository directory using base location (" + basePath + ")");
   117         }
   117         }
   118         SafePath canonicalRepositoryPath = SecuritySupport.toRealPath(f);
   118         SafePath canonicalRepositoryPath = SecuritySupport.toRealPath(f);
       
   119         SecuritySupport.setProperty(JFR_REPOSITORY_LOCATION_PROPERTY, canonicalRepositoryPath.toString());
   119         return canonicalRepositoryPath;
   120         return canonicalRepositoryPath;
   120     }
   121     }
   121 
   122 
   122     private static SafePath createRealBasePath(SafePath safePath) throws Exception {
   123     private static SafePath createRealBasePath(SafePath safePath) throws IOException {
   123         if (SecuritySupport.exists(safePath)) {
   124         if (SecuritySupport.exists(safePath)) {
   124             if (!SecuritySupport.isWritable(safePath)) {
   125             if (!SecuritySupport.isWritable(safePath)) {
   125                 throw new IOException("JFR repository directory (" + safePath.toString() + ") exists, but isn't writable");
   126                 throw new IOException("JFR repository directory (" + safePath.toString() + ") exists, but isn't writable");
   126             }
   127             }
   127             return SecuritySupport.toRealPath(safePath);
   128             return SecuritySupport.toRealPath(safePath);
   157         for (SafePath p : cleanupDirectories) {
   158         for (SafePath p : cleanupDirectories) {
   158             try {
   159             try {
   159                 SecuritySupport.clearDirectory(p);
   160                 SecuritySupport.clearDirectory(p);
   160                 Logger.log(LogTag.JFR, LogLevel.INFO, "Removed repository " + p);
   161                 Logger.log(LogTag.JFR, LogLevel.INFO, "Removed repository " + p);
   161             } catch (IOException e) {
   162             } catch (IOException e) {
   162                 Logger.log(LogTag.JFR, LogLevel.ERROR, "Repository " + p + " could not be removed at shutdown: " + e.getMessage());
   163                 Logger.log(LogTag.JFR, LogLevel.INFO, "Repository " + p + " could not be removed at shutdown: " + e.getMessage());
   163             }
   164             }
   164         }
   165         }
   165     }
   166     }
   166 
   167 
   167     public synchronized SafePath getRepositoryPath() {
   168     public synchronized SafePath getRepositoryPath() {