awcosand commented on code in PR #87:
URL: 
https://github.com/apache/tomcat-jakartaee-migration/pull/87#discussion_r2546767849


##########
src/main/java/org/apache/tomcat/jakartaee/MigrationCache.java:
##########
@@ -0,0 +1,498 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.jakartaee;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.security.MessageDigest;
+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.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Cache for storing and retrieving pre-converted archive files.
+ *
+ * <h2>Cache Structure</h2>
+ * <p>The cache organizes files in a directory structure based on hash 
values:</p>
+ * <pre>
+ * {cacheDir}/
+ *   ├── cache-metadata.txt      # Metadata file tracking access times
+ *   ├── {XX}/                    # Subdirectory named by first 2 chars of hash
+ *   │   └── {hash}.jar          # Cached converted archive (full SHA-256 hash)
+ *   ├── {YY}/
+ *   │   └── {hash}.jar
+ *   └── temp-{uuid}.tmp          # Temporary files during conversion
+ * </pre>
+ *
+ * <h2>Cache Key</h2>
+ * <p>Each cache entry is keyed by a SHA-256 hash computed from:</p>
+ * <ul>
+ *   <li>The migration profile name (e.g., "TOMCAT", "EE")</li>
+ *   <li>The pre-conversion archive content (as bytes)</li>
+ * </ul>
+ * <p>This ensures that the same archive converted with different profiles
+ * produces different cache entries.</p>
+ *
+ * <h2>Metadata Format</h2>
+ * <p>The {@code cache-metadata.txt} file tracks access times for cache 
pruning:</p>
+ * <pre>
+ * # Migration cache metadata - hash|last_access_date
+ * {hash}|{YYYY-MM-DD}
+ * {hash}|{YYYY-MM-DD}
+ * </pre>
+ *
+ * <h2>Temporary Files</h2>
+ * <p>During conversion, output is written to temporary files named {@code 
temp-{uuid}.tmp}.
+ * These files are cleaned up on startup to handle crashes or unexpected 
shutdowns.</p>
+ */
+public class MigrationCache {
+
+    private static final Logger logger = 
Logger.getLogger(MigrationCache.class.getCanonicalName());
+    private static final StringManager sm = 
StringManager.getManager(MigrationCache.class);
+    private static final String METADATA_FILE = "cache-metadata.txt";
+    private static final DateTimeFormatter DATE_FORMATTER = 
DateTimeFormatter.ISO_LOCAL_DATE;
+
+    private final File cacheDir;
+    private final boolean enabled;
+    private final int retentionDays;
+    private final Map<String, LocalDate> cacheMetadata;
+    private final File metadataFile;
+
+    /**
+     * Construct a new migration cache.
+     *
+     * @param cacheDir the directory to store cached files (null to disable 
caching)
+     * @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 
{
+        if (cacheDir == null) {
+            this.cacheDir = null;
+            this.enabled = false;
+            this.retentionDays = 0;
+            this.cacheMetadata = new HashMap<>();
+            this.metadataFile = null;
+        } else {

Review Comment:
   moved the final variable initialization outside the if block, so you only 
have 'if' instead of 'if/else'



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to