Skip to content

Commit

Permalink
More meaningful names for applib snapshots
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Jun 19, 2024
1 parent 1d05944 commit 05af896
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -254,6 +254,45 @@ public static boolean hasExtensionIgnoreCase(File f, String ext) {
return f.getName().toLowerCase(Locale.ENGLISH).endsWith(ext.toLowerCase(Locale.ENGLISH));
}

/**
* Gets the extension of the {@code file}.
* <p>
* This method returns the extension of the {@code file} <strong>with</strong> the leading dot.
*
* @param file the file
* @return the file extension
*/
public static String getExtension(File file) {
if (file == null) {
return null;
}
String fileName = file.getName();
int extensionIndex = fileName.lastIndexOf('.');
if (extensionIndex == -1) {
return "";
}
return fileName.substring(extensionIndex);
}

/**
* Removes the extension from a file name for the {@code file}.
* <p>
* This method returns the textual part of the file name before last dot.
*
* @param file the file
* @return the file name without extension or {@code null} if {@code file} is {@code null}
*/
public static String removeExtension(File file) {
if (file == null) {
return null;
}
String fileName = file.getName();
int extensionIndex = fileName.lastIndexOf('.');
if (extensionIndex == -1) {
return fileName;
}
return fileName.substring(0, extensionIndex);
}

public static boolean isLegalFilename(String filename) {
if (!isValidString(filename)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,14 @@ private BasicFileAttributes readAttributes(URI libURI) {
private File createSnapshot(ServerEnvironment serverEnvironment) {
LOG.log(DEBUG, "createSnapshot()");
File snapshotsDir = new File(serverEnvironment.getLibPath(), "snapshots");
File originalFile = new File(originalSource);
File snapshot = null;
try {
snapshot = Files.createTempFile(snapshotsDir.toPath(), "applib", ".jar").toFile();
String snapshotPrefix = FileUtils.removeExtension(originalFile) + "-";
String snapshotSuffix = FileUtils.getExtension(originalFile);
snapshot = Files.createTempFile(snapshotsDir.toPath(), snapshotPrefix, snapshotSuffix).toFile();
if (!copy(fileInputStream, snapshot)) {
FileUtils.copy(new File(originalSource), snapshot);
FileUtils.copy(originalFile, snapshot);
}
// Normally snapshots should be removed at server shutdown.
// This should remove snapshot if SIGTERM signal received.
Expand Down

0 comments on commit 05af896

Please sign in to comment.