Skip to content

Commit

Permalink
Implemented copyResourceToDirectory + fixed usages
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Feb 20, 2024
1 parent 8663099 commit e17505f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
* <p>
* 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 '/'.
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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.
* <p>
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ public void creation(Server server) {
try {
//Get the keyfile names from the security service
List<String> 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 {
Expand Down

0 comments on commit e17505f

Please sign in to comment.