From e17505ff79cb829122d2070d2b3a65a1b3daefd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mat=C4=9Bj=C4=8Dek?= Date: Sun, 8 Oct 2023 03:41:56 +0200 Subject: [PATCH] Implemented copyResourceToDirectory + fixed usages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Matějček --- .../timer/PersistentEJBTimerService.java | 3 +- .../webservices/metroglue/MetroContainer.java | 4 +- .../com/sun/enterprise/util/io/FileUtils.java | 122 ++++++++++-------- .../common/EmbeddedSecurityLifeCycle.java | 11 +- 4 files changed, 75 insertions(+), 65 deletions(-) diff --git a/appserver/ejb/ejb-full-container/src/main/java/org/glassfish/ejb/persistent/timer/PersistentEJBTimerService.java b/appserver/ejb/ejb-full-container/src/main/java/org/glassfish/ejb/persistent/timer/PersistentEJBTimerService.java index 2e74d96967d..78db1f55363 100755 --- a/appserver/ejb/ejb-full-container/src/main/java/org/glassfish/ejb/persistent/timer/PersistentEJBTimerService.java +++ b/appserver/ejb/ejb-full-container/src/main/java/org/glassfish/ejb/persistent/timer/PersistentEJBTimerService.java @@ -1390,7 +1390,8 @@ private static boolean deployEJBTimerService(File root, File appScratchFile, logger.log (Level.INFO, "Loading EJBTimerService. Please wait."); File app = null; try { - app = FileUtils.copyResource(root, "lib", "install", "applications", TIMER_SERVICE_APP_NAME + ".war"); + File outputDir = new File(root, "lib/install/applications"); + app = FileUtils.copyResourceToDirectory(TIMER_SERVICE_APP_NAME + ".war", outputDir); } catch (Exception e) { logger.log (Level.WARNING, "Caught unexpected exception", e); } diff --git a/appserver/webservices/metro-glue/src/main/java/org/glassfish/webservices/metroglue/MetroContainer.java b/appserver/webservices/metro-glue/src/main/java/org/glassfish/webservices/metroglue/MetroContainer.java index c73c2f2ff8c..b84194aa826 100644 --- a/appserver/webservices/metro-glue/src/main/java/org/glassfish/webservices/metroglue/MetroContainer.java +++ b/appserver/webservices/metro-glue/src/main/java/org/glassfish/webservices/metroglue/MetroContainer.java @@ -168,8 +168,8 @@ public void deployWsTxServices(String target) { File root = serverContext.getInstallRoot(); File app = null; try { - app = FileUtils.copyResource(root, "lib", "install", "applications", "metro", - WSTX_SERVICES_APP_NAME + ".war"); + File outputDir = new File(root, "lib/install/applications/metro"); + app = FileUtils.copyResourceToDirectory(WSTX_SERVICES_APP_NAME + ".war", outputDir); } catch (Exception e) { logger.log(Level.WARNING, LogUtils.WSTX_SERVICE_UNEXPECTED_EXCEPTION, e); } diff --git a/nucleus/common/common-util/src/main/java/com/sun/enterprise/util/io/FileUtils.java b/nucleus/common/common-util/src/main/java/com/sun/enterprise/util/io/FileUtils.java index 57b747676e1..44884523d60 100644 --- a/nucleus/common/common-util/src/main/java/com/sun/enterprise/util/io/FileUtils.java +++ b/nucleus/common/common-util/src/main/java/com/sun/enterprise/util/io/FileUtils.java @@ -19,8 +19,6 @@ import com.sun.enterprise.util.OS; -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileFilter; @@ -41,7 +39,6 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.Collection; import java.util.Locale; @@ -641,29 +638,6 @@ private static int doWithRetry(RetriableWork work) { } - /** - * Copies a file. - * - * @param fin File to copy - * @param fout New file - * @throws IOException if an error while copying the content - */ - public static void copy(File fin, File fout) throws IOException { - if (safeIsDirectory(fin)) { - copyTree(fin, fout); - return; - } - if (!fin.exists()) { - throw new IllegalArgumentException("File source doesn't exist"); - } - if (!mkdirsMaybe(fout.getParentFile())) { - throw new RuntimeException("Can't create parent dir of output file: " + fout); - } - Files.copy(fin.toPath(), fout.toPath()); - LOG.log(Level.DEBUG, "Successfully copyied file {0} to {1}", fin, fout); - } - - /** * Copies the entire tree to a new location. * @@ -691,6 +665,66 @@ public static void copyTree(File din, File dout) throws IOException { } + public static File copyResourceToDirectory(String resourcePath, File outputDirectory) throws IOException { + int slashIndex = resourcePath.lastIndexOf('/'); + String fileName = slashIndex < 0 ? resourcePath : resourcePath.substring(slashIndex + 1); + File output = new File(outputDirectory, fileName); + if (output.exists()) { + return output; + } + return copyResource(resourcePath, output); + } + + + /** + * If the path dir/file does not exist, look for it in the classpath. + * If found in classpath, create dir/file. + *

+ * Existing file will not be overwritten. + * + * @param resourcePath - resource loadable by the thread context classloader. + * @param outputFile - if the file exists, it will be overwritten + * @return the File representing dir/file. If the resource does not exist, return null. + * @throws IOException + */ + public static File copyResource(String resourcePath, File outputFile) throws IOException { + LOG.log(Level.INFO, "copyResource(resourcePath={0}, outputFile={1})", resourcePath, outputFile); + try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourcePath)) { + if (is == null) { + return null; + } + if (!mkdirsMaybe(outputFile.getParentFile())) { + throw new IOException("Can't create parent dir of output file: " + outputFile); + } + copy(is, outputFile); + return outputFile; + } + } + + + /** + * Copies a file. + * + * @param fin File to copy + * @param fout New file + * @throws IOException if an error while copying the content + */ + public static void copy(File fin, File fout) throws IOException { + if (safeIsDirectory(fin)) { + copyTree(fin, fout); + return; + } + if (!fin.exists()) { + throw new IllegalArgumentException("File source doesn't exist"); + } + if (!mkdirsMaybe(fout.getParentFile())) { + throw new RuntimeException("Can't create parent dir of output file: " + fout); + } + Files.copy(fin.toPath(), fout.toPath(), StandardCopyOption.REPLACE_EXISTING); + LOG.log(Level.DEBUG, "Successfully copyied file {0} to {1}", fin, fout); + } + + /** * Returns a String with uniform slashes such that all the * occurances of '\\' are replaced with '/'. @@ -792,7 +826,11 @@ public static void copy(InputStream in, File out, long byteCount) throws IOExcep * @throws IOException */ public static void copy(InputStream in, File out) throws IOException { - Files.copy(in, out.toPath(), StandardCopyOption.REPLACE_EXISTING); + if (out.getParentFile().mkdirs()) { + LOG.log(Level.INFO, "Created directory {0}", out.getCanonicalPath()); + } + long bytes = Files.copy(in, out.toPath(), StandardCopyOption.REPLACE_EXISTING); + LOG.log(Level.INFO, "Copyied {0} bytes to {1}", bytes, out); } @@ -886,36 +924,6 @@ public static String readSmallFile(final File file) throws IOException { } - /** - * If the path dir/file does not exist, look for it in the classpath. - * If found in classpath, create dir/file. - *

- * Existing file will not be overwritten. - * - * @param dir - directory where the path file should exist (including resource path) - * @param resourcePath - resource loadable by the thread context classloader. - * @return the File representing dir/file. If that does not exist, return null. - * @throws IOException - */ - public static File copyResource(File dir, String... resourcePath) throws IOException { - File f = new File(dir, String.join(File.separator, resourcePath)); - if (f.exists()) { - return f; - } - try (InputStream is = Thread.currentThread().getContextClassLoader() - .getResourceAsStream(String.join("/", resourcePath))) { - if (is == null) { - return null; - } - if (!mkdirsMaybe(f.getParentFile())) { - throw new IOException("Can't create parent dir of output file: " + f); - } - copy(is, f); - return f; - } - } - - /** * Write the String to a file. Then make the file readable and writable. * If the file already exists it will be truncated and the contents replaced diff --git a/nucleus/security/core/src/main/java/com/sun/enterprise/security/common/EmbeddedSecurityLifeCycle.java b/nucleus/security/core/src/main/java/com/sun/enterprise/security/common/EmbeddedSecurityLifeCycle.java index afbc9c0b898..254d5b38267 100644 --- a/nucleus/security/core/src/main/java/com/sun/enterprise/security/common/EmbeddedSecurityLifeCycle.java +++ b/nucleus/security/core/src/main/java/com/sun/enterprise/security/common/EmbeddedSecurityLifeCycle.java @@ -68,16 +68,17 @@ public void creation(Server server) { try { //Get the keyfile names from the security service List keyFileNames = embeddedSecurity.getKeyFileNames(securityService); + File cfgDir = new File(instanceRoot, "config"); for (String keyFileName : keyFileNames) { // Copy the keyfiles in instanceRoot/config. If file is already present, then exit (handled by getManagedFile) - FileUtils.copyResource(instanceRoot, "config", embeddedSecurity.parseFileName(keyFileName)); + FileUtils.copyResourceToDirectory(embeddedSecurity.parseFileName(keyFileName), cfgDir); } //Copy the other security files to instanceRoot/config //Assuming that these files are present as config/filename in the embedded jar file and are to be extracted that way/ - FileUtils.copyResource(instanceRoot,"config", "login.conf"); - FileUtils.copyResource(instanceRoot,"config", "server.policy"); - FileUtils.copyResource(instanceRoot,"config", "cacerts.jks"); - FileUtils.copyResource(instanceRoot,"config", "keystore.jks"); + FileUtils.copyResourceToDirectory("login.conf", cfgDir); + FileUtils.copyResourceToDirectory("server.policy", cfgDir); + FileUtils.copyResourceToDirectory("cacerts.jks", cfgDir); + FileUtils.copyResourceToDirectory("keystore.jks", cfgDir); String keystoreFile = null; String truststoreFile = null; try {