Skip to content

Commit b6969e3

Browse files
committed
fixing discovery of structured apps for SSH
1 parent 0c02738 commit b6969e3

File tree

10 files changed

+170
-254
lines changed

10 files changed

+170
-254
lines changed

core/src/main/java/oracle/weblogic/deploy/util/FileUtils.java

+11-231
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2023, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
33
* Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.util;
@@ -8,7 +8,6 @@
88
import java.io.File;
99
import java.io.FileInputStream;
1010
import java.io.FileNotFoundException;
11-
import java.io.FilenameFilter;
1211
import java.io.IOException;
1312
import java.io.InputStream;
1413
import java.io.PrintWriter;
@@ -17,11 +16,7 @@
1716
import java.nio.file.attribute.PosixFilePermission;
1817
import java.security.MessageDigest;
1918
import java.security.NoSuchAlgorithmException;
20-
import java.util.ArrayList;
21-
import java.util.Arrays;
22-
import java.util.Collections;
2319
import java.util.HashSet;
24-
import java.util.List;
2520
import java.util.Locale;
2621
import java.util.Set;
2722
import javax.xml.bind.DatatypeConverter;
@@ -53,6 +48,7 @@ private FileUtils() {
5348
* @param fileName the resource to get
5449
* @return the InputStream, or null if it was not found
5550
*/
51+
@SuppressWarnings("unused")
5652
public static InputStream getResourceAsStream(String fileName) {
5753
return FileUtils.class.getClassLoader().getResourceAsStream(fileName);
5854
}
@@ -68,25 +64,12 @@ public static InputStream getResourceAsStream(String fileName) {
6864
* @return
6965
* @throws IOException
7066
*/
67+
@SuppressWarnings("unused")
7168
public static InputStream getFileAsStream(String fileName) throws IOException {
7269
File file = validateExistingFile(fileName);
7370
return new FileInputStream(getCanonicalFile(file));
7471
}
7572

76-
/**
77-
* Convert a file path into one that Jython will handle.
78-
*
79-
* @param filename the filename to convert
80-
* @return the converted filename that is Jython compatible
81-
*/
82-
public static String fixupFileSeparatorsForJython(String filename) {
83-
String newFileName = filename;
84-
if (WINDOWS) {
85-
newFileName = newFileName.replace('\\', '/');
86-
}
87-
return newFileName;
88-
}
89-
9073
/**
9174
* Create a temporary directory.
9275
*
@@ -106,6 +89,7 @@ public static File createTempDirectory(File parent, String dirBaseName) throws I
10689
* @return the temporary directory file
10790
* @throws IOException if an error occurs while create the temporary directory
10891
*/
92+
@SuppressWarnings("unused")
10993
public static File createTempDirectory(String dirBaseName) throws IOException {
11094
return getCanonicalFile(Files.createTempDirectory(dirBaseName).toFile());
11195
}
@@ -243,6 +227,7 @@ public static File validateExistingDirectory(String directoryName) {
243227
* @return the canonical file representing the path name
244228
* @throws IllegalArgumentException if the path name is empty or does not exist
245229
*/
230+
@SuppressWarnings("unused")
246231
public static File validateExistingPath(String pathName) {
247232
final String METHOD = "validateExistingPath";
248233

@@ -287,6 +272,7 @@ public static File validateWritableFile(String fileName) {
287272
* @return the canonical file representing the directory name
288273
* @throws IllegalArgumentException if the directory name is not valid, doesn't exist, or is not writable
289274
*/
275+
@SuppressWarnings("unused")
290276
public static File validateWritableDirectory(String directoryName) {
291277
final String METHOD = "validateWritableDirectory";
292278

@@ -376,7 +362,7 @@ public static boolean isYamlFile(File file) {
376362
}
377363

378364
/**
379-
* Whether or not the specified file has a JSON file extension.
365+
* Whether the specified file has a JSON file extension.
380366
*
381367
* @param file the file
382368
* @return true, if the file extension matches the known JSON file extensions
@@ -386,68 +372,6 @@ public static boolean isJsonFile(File file) {
386372
return fileName.endsWith(".json");
387373
}
388374

389-
/**
390-
* Get the model file from the specified, existing directory.
391-
*
392-
* @param modelDirectory the existing directory location to search for a model file.
393-
* @return the model file or null, if it does not exist
394-
* @throws IllegalArgumentException if the directory is not a valid, existing, and readable directory
395-
* @throws IllegalStateException if the modelDirectory contains more than one YAML and/or JSON file
396-
*/
397-
public static File getModelFile(File modelDirectory) {
398-
final String METHOD = "getModelFile";
399-
400-
LOGGER.entering(CLASS, METHOD, modelDirectory);
401-
if (modelDirectory == null) {
402-
String message = ExceptionHelper.getMessage("WLSDPLY-01104", METHOD, CLASS, "modelDirectory");
403-
IllegalArgumentException iae = new IllegalArgumentException(message);
404-
LOGGER.throwing(CLASS, METHOD, iae);
405-
throw iae;
406-
} else if (!modelDirectory.exists()) {
407-
String message = ExceptionHelper.getMessage("WLSDPLY-01102", modelDirectory.getAbsolutePath());
408-
IllegalArgumentException iae = new IllegalArgumentException(message);
409-
LOGGER.throwing(CLASS, METHOD, iae);
410-
throw iae;
411-
} else if (!modelDirectory.isDirectory()) {
412-
String message = ExceptionHelper.getMessage("WLSDPLY-01105", modelDirectory.getAbsolutePath());
413-
IllegalArgumentException iae = new IllegalArgumentException(message);
414-
LOGGER.throwing(CLASS, METHOD, iae);
415-
throw iae;
416-
} else if (!modelDirectory.canRead()) {
417-
String message = ExceptionHelper.getMessage("WLSDPLY-01106", modelDirectory.getAbsolutePath());
418-
IllegalArgumentException iae = new IllegalArgumentException(message);
419-
LOGGER.throwing(CLASS, METHOD, iae);
420-
throw iae;
421-
}
422-
423-
File[] dirEntries;
424-
try {
425-
dirEntries = modelDirectory.listFiles(new ModelFilenameFilter());
426-
} catch (SecurityException se) {
427-
String message = ExceptionHelper.getMessage("WLSDPLY-01107", se,
428-
modelDirectory.getAbsolutePath(), se.getLocalizedMessage());
429-
IllegalArgumentException iae = new IllegalArgumentException(message, se);
430-
LOGGER.throwing(CLASS, METHOD, iae);
431-
throw iae;
432-
}
433-
434-
File modelFile = getModelFileFromArray(dirEntries, modelDirectory);
435-
LOGGER.exiting(CLASS, METHOD, modelFile);
436-
return modelFile;
437-
}
438-
439-
/**
440-
* Find the model filename from the list.
441-
*
442-
* @param filenames the list of filenames
443-
* @param modelDirectoryName the directory name (for logging purposes only)
444-
* @return the model file name or null, if it was not found
445-
* @throws IllegalStateException if the modelDirectory contains more than one YAML and/or JSON file
446-
*/
447-
public static String getModelFileName(List<String> filenames, String modelDirectoryName) {
448-
return getModelFileNameFromList(filenames, modelDirectoryName);
449-
}
450-
451375
/**
452376
* Validate the file for the provided file name and return a File object handle. The file name
453377
* must not be a directory
@@ -638,6 +562,7 @@ public static byte[] readInputStreamToByteArray(InputStream input) throws IOExce
638562
return outputStream.toByteArray();
639563
}
640564

565+
@SuppressWarnings("unused")
641566
public static File getTmpDir() {
642567
return new File(System.getProperty("java.io.tmpdir"));
643568
}
@@ -648,6 +573,7 @@ public static File getTmpDir() {
648573
* @return PrintWriter instance which is automatically closed
649574
* @throws IllegalArgumentException if the file is not writable
650575
*/
576+
@SuppressWarnings("unused")
651577
public static PrintWriter getPrintWriter(String fileName) {
652578
final String METHOD = "getPrintWriter";
653579
validateWritableFile(fileName);
@@ -668,43 +594,13 @@ public static PrintWriter getPrintWriter(String fileName) {
668594
* @param octals octal number set like OS chmod permissions
669595
* @throws IOException if permissions update fails
670596
*/
597+
@SuppressWarnings("unused")
671598
public static void chmod(String path, int octals) throws IOException {
672599
if(!WINDOWS) {
673600
Files.setPosixFilePermissions(Paths.get(path), getPermissions(octals));
674601
}
675602
}
676603

677-
public static String getCommonRootDirectory(File firstDir, File secondDir) {
678-
if (firstDir == null || secondDir == null) {
679-
return null;
680-
}
681-
682-
String[] firstDirComponents = getFileComponents(firstDir);
683-
String[] secondDirComponents = getFileComponents(secondDir);
684-
int maxLength = Math.min(firstDirComponents.length, secondDirComponents.length);
685-
686-
List<String> resultComponents = new ArrayList<>();
687-
for (int i = 0; i < maxLength; i++) {
688-
if (firstDirComponents[i].equals(secondDirComponents[i])) {
689-
resultComponents.add(firstDirComponents[i]);
690-
} else {
691-
break;
692-
}
693-
}
694-
File result = getFileFromComponents(resultComponents.toArray(new String[0]));
695-
return result != null ? getCanonicalPath(result) : null;
696-
}
697-
698-
/**
699-
* Determines whether the argument contains an absolute path.
700-
*
701-
* @param filePath file path to check
702-
* @return true if the value is an absolute path; false otherwise
703-
*/
704-
public static boolean isAbsolutePath(String filePath) {
705-
return !StringUtils.isEmpty(filePath) && new File(filePath).isAbsolute();
706-
}
707-
708604
/**
709605
* Determines whether the argument contains an absolute path for either Unix- or Windows-based file systems.
710606
* This is necessary because java.io.File.isAbsolute() only tests based on the local file system type
@@ -723,6 +619,7 @@ public static boolean isRemotePathAbsolute(String filePath) {
723619
* @param directory the directory to be examined
724620
* @return the size of the directory's contents
725621
*/
622+
@SuppressWarnings("unused")
726623
public static long getDirectorySize(File directory) {
727624
long size = 0;
728625
File[] files = directory.listFiles();
@@ -747,89 +644,6 @@ private static boolean isWindowsRemoteAbsolutePath(String path) {
747644
return windowsPath.startsWith(":\\", 1) || windowsPath.startsWith("\\\\");
748645
}
749646

750-
private static File getModelFileFromArray(File[] files, File modelDirectory) {
751-
File modelFile = null;
752-
if (files != null && files.length > 0) {
753-
modelFile = getModelFileFromList(Arrays.asList(files), modelDirectory);
754-
}
755-
return modelFile;
756-
}
757-
758-
private static String getModelFileNameFromList(List<String> filenames, String modelDirectoryName) {
759-
String result = null;
760-
if (filenames != null && !filenames.isEmpty()) {
761-
List<File> files = new ArrayList<>(filenames.size());
762-
for (String filename : filenames) {
763-
files.add(new File(filename));
764-
}
765-
File modelFile = getModelFileFromList(files, new File(modelDirectoryName));
766-
if (modelFile != null) {
767-
result = modelFile.getPath();
768-
}
769-
}
770-
return result;
771-
}
772-
773-
private static File getModelFileFromList(List<File> files, File modelDirectory) {
774-
final String METHOD = "getModelFileFromList";
775-
776-
File modelFile = null;
777-
if (files != null && !files.isEmpty()) {
778-
File yamlFile = null;
779-
File jsonFile = null;
780-
781-
for (File file : files) {
782-
if (FileUtils.isYamlFile(file)) {
783-
if (yamlFile == null) {
784-
yamlFile = file;
785-
} else {
786-
String message = ExceptionHelper.getMessage("WLSDPLY-01117", modelDirectory.getAbsolutePath(),
787-
"YAML", file.getName(), yamlFile.getName());
788-
IllegalStateException ise = new IllegalStateException(message);
789-
LOGGER.throwing(CLASS, METHOD, ise);
790-
throw ise;
791-
}
792-
} else if (isJsonFile(file)) {
793-
if (jsonFile == null) {
794-
jsonFile = file;
795-
} else {
796-
String message = ExceptionHelper.getMessage("WLSDPLY-01117", modelDirectory.getAbsolutePath(),
797-
"JSON", file.getName(), jsonFile.getName());
798-
IllegalStateException ise = new IllegalStateException(message);
799-
LOGGER.throwing(CLASS, METHOD, ise);
800-
throw ise;
801-
}
802-
}
803-
}
804-
if (yamlFile != null) {
805-
modelFile = yamlFile;
806-
} else if (jsonFile != null) {
807-
modelFile = jsonFile;
808-
}
809-
}
810-
return modelFile;
811-
}
812-
813-
/**
814-
* FilenameFilter class for model files.
815-
*/
816-
private static class ModelFilenameFilter implements FilenameFilter {
817-
/**
818-
* {@inheritDoc}
819-
*/
820-
@Override
821-
public boolean accept(File dir, String name) {
822-
boolean result = false;
823-
if (!StringUtils.isEmpty(name)) {
824-
File f = new File(dir, name);
825-
if (FileUtils.isYamlFile(f) || FileUtils.isJsonFile(f)) {
826-
result = true;
827-
}
828-
}
829-
return result;
830-
}
831-
}
832-
833647
/**
834648
* Convert an octal number into Posix File Permissions.
835649
* @param octals 3 octal digits representing posix file permissions rwxrwxrwx
@@ -868,40 +682,6 @@ static Set<PosixFilePermission> getPermissions(int octals) {
868682
return result;
869683
}
870684

871-
private static String[] getFileComponents(File file) {
872-
String[] result;
873-
if (file != null) {
874-
File canonicalFile = getCanonicalFile(file);
875-
List<String> names = new ArrayList<>();
876-
while(true) {
877-
File parent = canonicalFile.getParentFile();
878-
if (parent == null) {
879-
names.add(canonicalFile.getPath());
880-
break;
881-
} else {
882-
names.add(canonicalFile.getName());
883-
canonicalFile = parent;
884-
}
885-
}
886-
Collections.reverse(names);
887-
result = names.toArray(new String[0]);
888-
} else {
889-
result = new String[0];
890-
}
891-
return result;
892-
}
893-
894-
private static File getFileFromComponents(String[] components) {
895-
File result = null;
896-
if (components != null && components.length > 0) {
897-
result = new File(components[0]);
898-
for (int i = 1; i < components.length; i++) {
899-
result = new File(result, components[i]);
900-
}
901-
}
902-
return result;
903-
}
904-
905685
private static File validatePathNameInternal(String pathName, String emptyErrorKey) {
906686
final String METHOD = "validatePathyNameInternal";
907687

0 commit comments

Comments
 (0)