From c5bb69a966f019600c76805f65dc1175a0ca4d93 Mon Sep 17 00:00:00 2001 From: "jiale.ju" Date: Tue, 12 Mar 2024 18:00:03 +0800 Subject: [PATCH] fix: release resource and some other optimize --- .../auto/install/maven/plugin/UploadMojo.java | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/open-exp-code/auto-install-exp-maven-plugin/src/main/java/cn/think/in/java/auto/install/maven/plugin/UploadMojo.java b/open-exp-code/auto-install-exp-maven-plugin/src/main/java/cn/think/in/java/auto/install/maven/plugin/UploadMojo.java index 7bd547e..e110665 100644 --- a/open-exp-code/auto-install-exp-maven-plugin/src/main/java/cn/think/in/java/auto/install/maven/plugin/UploadMojo.java +++ b/open-exp-code/auto-install-exp-maven-plugin/src/main/java/cn/think/in/java/auto/install/maven/plugin/UploadMojo.java @@ -18,7 +18,7 @@ import java.io.File; import java.io.IOException; -import java.net.MalformedURLException; +import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.util.Properties; @@ -54,16 +54,16 @@ public void execute() throws MojoExecutionException { throw new MojoExecutionException("Jar file does not exist: " + jarFile); } - URLClassLoader urlClassLoader = null; - try { - urlClassLoader = new URLClassLoader(new URL[]{jarFile.toURL()}, Thread.currentThread().getContextClassLoader()); - } catch (MalformedURLException e) { + URL resource; + try (URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{jarFile.toURI().toURL()}, Thread.currentThread().getContextClassLoader())) { + resource = urlClassLoader.findResource(PLUGIN_META_FILE_NAME); + } catch (IOException e) { throw new RuntimeException(e); } - URL resource = urlClassLoader.findResource(PLUGIN_META_FILE_NAME); + Properties properties = new Properties(); - try { - properties.load(resource.openStream()); + try(InputStream resourceStream = resource.openStream()) { + properties.load(resourceStream); } catch (IOException e) { throw new RuntimeException(e); } @@ -84,14 +84,22 @@ void tryUninstall(String pluginId) throws MojoExecutionException { return; } try (CloseableHttpClient httpClient = HttpClients.createDefault()) { - HttpGet uploadFile = new HttpGet(uninstallUrl + "?pluginid=" + pluginId); + HttpGet uploadFile = new HttpGet(uninstallUrl + "?pluginId=" + pluginId); CloseableHttpResponse response = httpClient.execute(uploadFile); HttpEntity responseEntity = response.getEntity(); + int statusCode = response.getStatusLine().getStatusCode(); + if (statusCode != 200) { + String reasonPhrase = response.getStatusLine().getReasonPhrase(); + getLog().info("卸载失败. Server response: " + EntityUtils.toString(responseEntity)); + throw new MojoExecutionException("Failed to uninstall plugin. Server returned status code: " + statusCode + + ", reasonPhrase = " + reasonPhrase + ", pluginId = " + pluginId); + } + getLog().info("卸载结束. Server response: " + EntityUtils.toString(responseEntity)); } catch (Exception e) { - throw new MojoExecutionException("Error uploading file", e); + throw new MojoExecutionException("Error uninstall plugin", e); } }