Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.plugins; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import java.io.IOException; 19 : import java.nio.file.Files; 20 : import java.nio.file.Path; 21 : import java.util.jar.JarFile; 22 : 23 : class CleanupHandle { 24 1 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 25 : 26 : private final Path tmp; 27 : private final JarFile jarFile; 28 : 29 1 : CleanupHandle(Path tmp, JarFile jarFile) { 30 1 : this.tmp = tmp; 31 1 : this.jarFile = jarFile; 32 1 : } 33 : 34 : void cleanup() { 35 : try { 36 1 : jarFile.close(); 37 0 : } catch (IOException err) { 38 0 : logger.atSevere().withCause(err).log("Cannot close %s", jarFile.getName()); 39 1 : } 40 : try { 41 1 : Files.deleteIfExists(tmp); 42 1 : logger.atInfo().log("Cleaned plugin %s", tmp.getFileName()); 43 0 : } catch (IOException e) { 44 0 : logger.atWarning().withCause(e).log( 45 : "Cannot delete %s, retrying to delete it on termination of the virtual machine", 46 0 : tmp.toAbsolutePath()); 47 0 : tmp.toFile().deleteOnExit(); 48 1 : } 49 1 : } 50 : }