This is an automated email from the ASF dual-hosted git repository.
rmaucher pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat-jakartaee-migration.git
The following commit(s) were added to refs/heads/main by this push:
new 1b6f789 Another round of minor fixes and cleanups
1b6f789 is described below
commit 1b6f78976c303e685aff2641cd107966aaa1bb3e
Author: remm <[email protected]>
AuthorDate: Mon Jun 8 23:23:02 2026 +0200
Another round of minor fixes and cleanups
---
.../org/apache/tomcat/jakartaee/AntHandler.java | 5 ++++
.../org/apache/tomcat/jakartaee/CacheEntry.java | 27 +++++++++++++++-----
.../java/org/apache/tomcat/jakartaee/Info.java | 14 +++++------
.../org/apache/tomcat/jakartaee/Migration.java | 3 +++
.../apache/tomcat/jakartaee/MigrationCache.java | 29 +++++++++++-----------
.../org/apache/tomcat/jakartaee/MigrationTask.java | 28 ++++++++++++++-------
.../java/org/apache/tomcat/jakartaee/Util.java | 3 ++-
.../tomcat/jakartaee/LocalStrings.properties | 1 +
8 files changed, 72 insertions(+), 38 deletions(-)
diff --git a/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java
b/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java
index 13fd21a..94a5281 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/AntHandler.java
@@ -17,6 +17,7 @@
package org.apache.tomcat.jakartaee;
+import java.text.MessageFormat;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
@@ -38,6 +39,10 @@ class AntHandler extends Handler {
@Override
public void publish(LogRecord record) {
String message = record.getMessage();
+ Object[] params = record.getParameters();
+ if (message != null && params != null && params.length > 0) {
+ message = MessageFormat.format(message, params);
+ }
if (message == null && record.getThrown() != null) {
message = record.getThrown().toString();
}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/CacheEntry.java
b/src/main/java/org/apache/tomcat/jakartaee/CacheEntry.java
index 244db38..0c6e127 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/CacheEntry.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/CacheEntry.java
@@ -21,6 +21,9 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.file.AtomicMoveNotSupportedException;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -94,19 +97,31 @@ class CacheEntry {
*/
public void commitStore() throws IOException {
if (fos != null) {
- fos.close();
+ try {
+ fos.close();
+ } catch (IOException e) {
+ logger.log(Level.WARNING,
sm.getString("cacheEntry.closeFail"), e);
+ }
+ fos = null;
}
if (!tempFile.exists()) {
throw new IOException(sm.getString("cacheEntry.tempNotExist",
tempFile));
}
// Ensure parent directory exists
File parentDir = cacheFile.getParentFile();
- if (!parentDir.exists()) {
- parentDir.mkdirs();
+ if (!parentDir.mkdirs() && !parentDir.exists()) {
+ throw new IOException(sm.getString("cache.cannotCreate",
parentDir.getAbsolutePath()));
}
- // Rename temp file to final cache location (usually atomic)
- if (!tempFile.renameTo(cacheFile)) {
- throw new IOException(sm.getString("cacheEntry.tempRenameFail",
tempFile, cacheFile));
+ // Move file to final cache location (atomic if possible)
+ try {
+ try {
+ Files.move(tempFile.toPath(), cacheFile.toPath(),
StandardCopyOption.REPLACE_EXISTING,
+ StandardCopyOption.ATOMIC_MOVE);
+ } catch (AtomicMoveNotSupportedException e) {
+ Files.move(tempFile.toPath(), cacheFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
+ }
+ } catch (Exception e) {
+ throw new IOException(sm.getString("cacheEntry.tempRenameFail",
tempFile, cacheFile), e);
}
}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Info.java
b/src/main/java/org/apache/tomcat/jakartaee/Info.java
index 83c22dc..6f26507 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Info.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Info.java
@@ -17,6 +17,7 @@
package org.apache.tomcat.jakartaee;
import java.io.IOException;
+import java.io.InputStream;
import java.util.Properties;
/**
@@ -27,14 +28,13 @@ public class Info {
private static final String VERSION;
static {
- Properties props = new Properties();
-
String version = null;
- try {
-
props.load(Info.class.getClassLoader().getResourceAsStream("info.properties"));
-
- version = props.getProperty("version");
-
+ try (InputStream is =
Info.class.getClassLoader().getResourceAsStream("info.properties")) {
+ if (is != null) {
+ Properties props = new Properties();
+ props.load(is);
+ version = props.getProperty("version");
+ }
} catch (IOException e) {
// Handled below
}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
index efb022a..bebae17 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Migration.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Migration.java
@@ -624,11 +624,13 @@ public class Migration {
return crc.getValue();
}
+ @Override
public void close() throws IOException {
if (fileOutput != null) {
try {
fileOutput.close();
} finally {
+ fileOutput = null;
try (FileInputStream fis = new FileInputStream(tempFile)) {
IOUtils.copy(fis, destStream);
} finally {
@@ -640,6 +642,7 @@ public class Migration {
buffer.writeTo(destStream);
} finally {
buffer.close();
+ buffer = null;
}
}
}
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
b/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
index b1107ef..0cf5f32 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java
@@ -28,11 +28,11 @@ import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -87,25 +87,23 @@ public class MigrationCache {
/**
* Construct a new migration cache.
*
- * @param cacheDir the directory to store cached files (null to disable
caching)
+ * @param cacheDir the directory to store cached files
* @param retentionDays the number of days to retain cached files
* @throws IOException if the cache directory cannot be created
*/
public MigrationCache(File cacheDir, int retentionDays) throws IOException
{
- this.retentionDays = retentionDays;
- this.cacheMetadata = new HashMap<>();
- this.cacheDir = cacheDir;
- this.metadataFile = cacheDir == null ? null : new File(cacheDir,
METADATA_FILE);
-
if (cacheDir == null) {
throw new
IllegalStateException(sm.getString("cache.nullDirectory"));
}
+ this.retentionDays = retentionDays;
+ this.cacheMetadata = new ConcurrentHashMap<>();
+ this.cacheDir = cacheDir;
+ this.metadataFile = new File(cacheDir, METADATA_FILE);
+
// Create cache directory if it doesn't exist
- if (!cacheDir.exists()) {
- if (!cacheDir.mkdirs()) {
- throw new IOException(sm.getString("cache.cannotCreate",
cacheDir.getAbsolutePath()));
- }
+ if (!cacheDir.mkdirs() && !cacheDir.exists()) {
+ throw new IOException(sm.getString("cache.cannotCreate",
cacheDir.getAbsolutePath()));
}
if (!cacheDir.isDirectory()) {
@@ -265,13 +263,14 @@ public class MigrationCache {
*
* @param hash the hash string
* @return the cache file
+ * @throws IOException if an I/O error occurs
*/
- private File getCacheFile(String hash) {
+ private File getCacheFile(String hash) throws IOException {
// Use subdirectories based on first 2 chars of hash to avoid too many
files in one directory
String subdir = hash.substring(0, 2);
File subdirFile = new File(cacheDir, subdir);
- if (!subdirFile.exists()) {
- subdirFile.mkdirs();
+ if (!subdirFile.mkdirs() && !subdirFile.exists()) {
+ throw new IOException(sm.getString("cache.cannotCreate",
subdirFile.getAbsolutePath()));
}
return new File(subdirFile, hash + ".jar");
}
@@ -312,7 +311,7 @@ public class MigrationCache {
public void clear() throws IOException {
deleteDirectory(cacheDir);
cacheMetadata.clear();
- if (!cacheDir.mkdirs()) {
+ if (!cacheDir.mkdirs() && !cacheDir.exists()) {
throw new IOException(sm.getString("cache.cannotCreate",
cacheDir.getAbsolutePath()));
}
logger.log(Level.INFO, sm.getString("cache.cleared"));
diff --git a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
index 02ecec7..6ac52e6 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/MigrationTask.java
@@ -96,15 +96,8 @@ public class MigrationTask extends Task {
@Override
public void execute() throws BuildException {
- // redirect the log messages to Ant
- Logger logger = Logger.getLogger(Migration.class.getCanonicalName());
- logger.setUseParentHandlers(false);
- for (Handler handler : logger.getHandlers()) {
- logger.removeHandler(handler);
- }
- logger.addHandler(new AntHandler(this));
- // check the parameters
+ // Check the parameters
EESpecProfile profile = null;
try {
profile =
EESpecProfiles.valueOf(this.profile.toUpperCase(Locale.ENGLISH));
@@ -125,17 +118,34 @@ public class MigrationTask extends Task {
migration.setZipInMemory(zipInMemory);
migration.setMatchExcludesAgainstPathName(matchExcludesAgainstPathName);
if (this.excludes != null) {
- String[] excludes= this.excludes.split(",");
+ String[] excludes = this.excludes.split(",");
for (String exclude : excludes) {
migration.addExclude(exclude.trim());
}
}
+ // Redirect the log messages to Ant
+ Logger logger = Logger.getLogger(Migration.class.getCanonicalName());
+ Handler[] originalHandlers = logger.getHandlers();
+ AntHandler antHandler = new AntHandler(this);
+ boolean originalUseParent = logger.getUseParentHandlers();
try {
+ logger.setUseParentHandlers(false);
+ for (Handler h : logger.getHandlers()) {
+ logger.removeHandler(h);
+ }
+ logger.addHandler(antHandler);
migration.execute();
} catch (IOException e) {
throw new BuildException(e, getLocation());
+ } finally {
+ logger.removeHandler(antHandler);
+ for (Handler h : originalHandlers) {
+ logger.addHandler(h);
+ }
+ logger.setUseParentHandlers(originalUseParent);
}
+
}
@Override
diff --git a/src/main/java/org/apache/tomcat/jakartaee/Util.java
b/src/main/java/org/apache/tomcat/jakartaee/Util.java
index 790e454..c345ab8 100644
--- a/src/main/java/org/apache/tomcat/jakartaee/Util.java
+++ b/src/main/java/org/apache/tomcat/jakartaee/Util.java
@@ -53,9 +53,10 @@ public class Util {
public static void copy(InputStream is, OutputStream os) throws
IOException {
byte[] buf = new byte[8192];
int numRead;
- while ( (numRead = is.read(buf) ) >= 0) {
+ while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
+ os.flush();
}
/**
diff --git
a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
index 91c8059..783a361 100644
--- a/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
+++ b/src/main/resources/org/apache/tomcat/jakartaee/LocalStrings.properties
@@ -111,6 +111,7 @@ cache.tempfile.cleaned=Cache temporary file [{0}] was
cleaned
cache.tempfile.cleanFailed=Cache temporary file [{0}] cleaning failed
cache.tempfiles.cleaned=[{0}] cache temporary files were cleaned
+cacheEntry.closeFail=Close failed
cacheEntry.copyNotExist=Cannot copy - cache entry does not exist
cacheEntry.tempNotExist=Temporary file [{0}] does not exist
cacheEntry.tempRenameFail=Failed to rename temporary file [{0}] to cache file
[{1}]
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]