You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/08/26 15:17:51 UTC

(maven) 01/01: Use try-with-resources for AuthenticationContext

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch feature/maven-4.1-try-with-resources
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 4a95b16514718b22349f74097096dd23dcb593b0
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Wed Aug 26 17:08:22 2026 +0200

    Use try-with-resources for AuthenticationContext
    
    Closes #669
---
 .../repository/legacy/LegacyRepositorySystem.java  | 27 +++++++++++-----------
 .../apache/maven/bridge/MavenRepositorySystem.java | 27 +++++++++++-----------
 .../LegacyRepositorySystemSessionExtender.java     | 26 ++++++++++-----------
 3 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/compat/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java b/compat/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
index f748a477d2..281f4069e6 100644
--- a/compat/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
+++ b/compat/maven-compat/src/main/java/org/apache/maven/repository/legacy/LegacyRepositorySystem.java
@@ -526,13 +526,14 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
                     repo = new RemoteRepository.Builder(repo)
                             .setAuthentication(auth)
                             .build();
-                    AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
-                    Authentication result = new Authentication(
-                            authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
-                    result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
-                    result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
-                    authCtx.close();
-                    return result;
+                    try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) {
+                        Authentication result = new Authentication(
+                                authCtx.get(AuthenticationContext.USERNAME),
+                                authCtx.get(AuthenticationContext.PASSWORD));
+                        result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
+                        result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
+                        return result;
+                    }
                 }
             }
         }
@@ -623,12 +624,12 @@ private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repos
                         repo = new RemoteRepository.Builder(repo)
                                 .setProxy(proxy)
                                 .build();
-                        AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
-                        p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
-                        p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
-                        p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
-                        p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
-                        authCtx.close();
+                        try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) {
+                            p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
+                            p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
+                            p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
+                            p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
+                        }
                     }
                     return p;
                 }
diff --git a/impl/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java b/impl/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
index 1617b19c37..e7f4451c47 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/bridge/MavenRepositorySystem.java
@@ -230,13 +230,14 @@ private Authentication getAuthentication(RepositorySystemSession session, Artifa
                     repo = new RemoteRepository.Builder(repo)
                             .setAuthentication(auth)
                             .build();
-                    AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo);
-                    Authentication result = new Authentication(
-                            authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
-                    result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
-                    result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
-                    authCtx.close();
-                    return result;
+                    try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) {
+                        Authentication result = new Authentication(
+                                authCtx.get(AuthenticationContext.USERNAME),
+                                authCtx.get(AuthenticationContext.PASSWORD));
+                        result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
+                        result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
+                        return result;
+                    }
                 }
             }
         }
@@ -266,12 +267,12 @@ private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repos
                         repo = new RemoteRepository.Builder(repo)
                                 .setProxy(proxy)
                                 .build();
-                        AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo);
-                        p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
-                        p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
-                        p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
-                        p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
-                        authCtx.close();
+                        try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) {
+                            p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
+                            p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
+                            p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
+                            p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
+                        }
                     }
                     return p;
                 }
diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/LegacyRepositorySystemSessionExtender.java b/impl/maven-core/src/main/java/org/apache/maven/internal/aether/LegacyRepositorySystemSessionExtender.java
index ee7129246d..ea580f2101 100644
--- a/impl/maven-core/src/main/java/org/apache/maven/internal/aether/LegacyRepositorySystemSessionExtender.java
+++ b/impl/maven-core/src/main/java/org/apache/maven/internal/aether/LegacyRepositorySystemSessionExtender.java
@@ -110,12 +110,12 @@ private org.apache.maven.repository.Proxy getProxy(ProxySelector selector, Artif
                 p.setPort(proxy.getPort());
                 if (proxy.getAuthentication() != null) {
                     repo = new RemoteRepository.Builder(repo).setProxy(proxy).build();
-                    AuthenticationContext authCtx = AuthenticationContext.forProxy(null, repo);
-                    p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
-                    p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
-                    p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
-                    p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
-                    authCtx.close();
+                    try (AuthenticationContext authCtx = AuthenticationContext.forProxy(null, repo)) {
+                        p.setUserName(authCtx.get(AuthenticationContext.USERNAME));
+                        p.setPassword(authCtx.get(AuthenticationContext.PASSWORD));
+                        p.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN));
+                        p.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION));
+                    }
                 }
                 return p;
             }
@@ -139,13 +139,13 @@ private Authentication getAuthentication(AuthenticationSelector selector, Artifa
                 repo = new RemoteRepository.Builder(repo)
                         .setAuthentication(auth)
                         .build();
-                AuthenticationContext authCtx = AuthenticationContext.forRepository(null, repo);
-                Authentication result = new Authentication(
-                        authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
-                result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
-                result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
-                authCtx.close();
-                return result;
+                try (AuthenticationContext authCtx = AuthenticationContext.forRepository(null, repo)) {
+                    Authentication result = new Authentication(
+                            authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD));
+                    result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH));
+                    result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE));
+                    return result;
+                }
             }
         }
         return null;