1
1
/*
2
- * Copyright (c) 2017, 2023 , Oracle and/or its affiliates.
2
+ * Copyright (c) 2017, 2024 , Oracle and/or its affiliates.
3
3
* Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
4
4
*/
5
5
package oracle .weblogic .deploy .util ;
8
8
import java .io .File ;
9
9
import java .io .FileInputStream ;
10
10
import java .io .FileNotFoundException ;
11
- import java .io .FilenameFilter ;
12
11
import java .io .IOException ;
13
12
import java .io .InputStream ;
14
13
import java .io .PrintWriter ;
17
16
import java .nio .file .attribute .PosixFilePermission ;
18
17
import java .security .MessageDigest ;
19
18
import java .security .NoSuchAlgorithmException ;
20
- import java .util .ArrayList ;
21
- import java .util .Arrays ;
22
- import java .util .Collections ;
23
19
import java .util .HashSet ;
24
- import java .util .List ;
25
20
import java .util .Locale ;
26
21
import java .util .Set ;
27
22
import javax .xml .bind .DatatypeConverter ;
@@ -53,6 +48,7 @@ private FileUtils() {
53
48
* @param fileName the resource to get
54
49
* @return the InputStream, or null if it was not found
55
50
*/
51
+ @ SuppressWarnings ("unused" )
56
52
public static InputStream getResourceAsStream (String fileName ) {
57
53
return FileUtils .class .getClassLoader ().getResourceAsStream (fileName );
58
54
}
@@ -68,25 +64,12 @@ public static InputStream getResourceAsStream(String fileName) {
68
64
* @return
69
65
* @throws IOException
70
66
*/
67
+ @ SuppressWarnings ("unused" )
71
68
public static InputStream getFileAsStream (String fileName ) throws IOException {
72
69
File file = validateExistingFile (fileName );
73
70
return new FileInputStream (getCanonicalFile (file ));
74
71
}
75
72
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
-
90
73
/**
91
74
* Create a temporary directory.
92
75
*
@@ -106,6 +89,7 @@ public static File createTempDirectory(File parent, String dirBaseName) throws I
106
89
* @return the temporary directory file
107
90
* @throws IOException if an error occurs while create the temporary directory
108
91
*/
92
+ @ SuppressWarnings ("unused" )
109
93
public static File createTempDirectory (String dirBaseName ) throws IOException {
110
94
return getCanonicalFile (Files .createTempDirectory (dirBaseName ).toFile ());
111
95
}
@@ -243,6 +227,7 @@ public static File validateExistingDirectory(String directoryName) {
243
227
* @return the canonical file representing the path name
244
228
* @throws IllegalArgumentException if the path name is empty or does not exist
245
229
*/
230
+ @ SuppressWarnings ("unused" )
246
231
public static File validateExistingPath (String pathName ) {
247
232
final String METHOD = "validateExistingPath" ;
248
233
@@ -287,6 +272,7 @@ public static File validateWritableFile(String fileName) {
287
272
* @return the canonical file representing the directory name
288
273
* @throws IllegalArgumentException if the directory name is not valid, doesn't exist, or is not writable
289
274
*/
275
+ @ SuppressWarnings ("unused" )
290
276
public static File validateWritableDirectory (String directoryName ) {
291
277
final String METHOD = "validateWritableDirectory" ;
292
278
@@ -376,7 +362,7 @@ public static boolean isYamlFile(File file) {
376
362
}
377
363
378
364
/**
379
- * Whether or not the specified file has a JSON file extension.
365
+ * Whether the specified file has a JSON file extension.
380
366
*
381
367
* @param file the file
382
368
* @return true, if the file extension matches the known JSON file extensions
@@ -386,68 +372,6 @@ public static boolean isJsonFile(File file) {
386
372
return fileName .endsWith (".json" );
387
373
}
388
374
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
-
451
375
/**
452
376
* Validate the file for the provided file name and return a File object handle. The file name
453
377
* must not be a directory
@@ -638,6 +562,7 @@ public static byte[] readInputStreamToByteArray(InputStream input) throws IOExce
638
562
return outputStream .toByteArray ();
639
563
}
640
564
565
+ @ SuppressWarnings ("unused" )
641
566
public static File getTmpDir () {
642
567
return new File (System .getProperty ("java.io.tmpdir" ));
643
568
}
@@ -648,6 +573,7 @@ public static File getTmpDir() {
648
573
* @return PrintWriter instance which is automatically closed
649
574
* @throws IllegalArgumentException if the file is not writable
650
575
*/
576
+ @ SuppressWarnings ("unused" )
651
577
public static PrintWriter getPrintWriter (String fileName ) {
652
578
final String METHOD = "getPrintWriter" ;
653
579
validateWritableFile (fileName );
@@ -668,43 +594,13 @@ public static PrintWriter getPrintWriter(String fileName) {
668
594
* @param octals octal number set like OS chmod permissions
669
595
* @throws IOException if permissions update fails
670
596
*/
597
+ @ SuppressWarnings ("unused" )
671
598
public static void chmod (String path , int octals ) throws IOException {
672
599
if (!WINDOWS ) {
673
600
Files .setPosixFilePermissions (Paths .get (path ), getPermissions (octals ));
674
601
}
675
602
}
676
603
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
-
708
604
/**
709
605
* Determines whether the argument contains an absolute path for either Unix- or Windows-based file systems.
710
606
* 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) {
723
619
* @param directory the directory to be examined
724
620
* @return the size of the directory's contents
725
621
*/
622
+ @ SuppressWarnings ("unused" )
726
623
public static long getDirectorySize (File directory ) {
727
624
long size = 0 ;
728
625
File [] files = directory .listFiles ();
@@ -747,89 +644,6 @@ private static boolean isWindowsRemoteAbsolutePath(String path) {
747
644
return windowsPath .startsWith (":\\ " , 1 ) || windowsPath .startsWith ("\\ \\ " );
748
645
}
749
646
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
-
833
647
/**
834
648
* Convert an octal number into Posix File Permissions.
835
649
* @param octals 3 octal digits representing posix file permissions rwxrwxrwx
@@ -868,40 +682,6 @@ static Set<PosixFilePermission> getPermissions(int octals) {
868
682
return result ;
869
683
}
870
684
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
-
905
685
private static File validatePathNameInternal (String pathName , String emptyErrorKey ) {
906
686
final String METHOD = "validatePathyNameInternal" ;
907
687
0 commit comments