99import java .nio .file .Files ;
1010import java .nio .file .Path ;
1111import java .nio .file .Paths ;
12+ import java .util .ArrayList ;
13+ import java .util .List ;
1214import java .util .Objects ;
1315
1416import com .oracle .weblogic .imagetool .cachestore .CacheStore ;
1517import com .oracle .weblogic .imagetool .installer .InstallerType ;
1618import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
1719import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18- import com .oracle .weblogic .imagetool .util .BuildPlatform ;
20+ import com .oracle .weblogic .imagetool .util .Architecture ;
1921import com .oracle .weblogic .imagetool .util .Utils ;
2022
2123/**
@@ -27,50 +29,40 @@ public class CachedFile {
2729
2830 private final String id ;
2931 private final String version ;
30- private final String architecture ;
32+ private final Architecture architecture ;
3133
3234 /**
3335 * Represents a locally cached file.
3436 *
35- * @param id cache ID (like installer type or patchId)
36- * @param version version number for the patch or installer.
37- * @param architecture the system architecture that this file/installer is applicable
37+ * @param id cache ID (like installer type or patchId)
38+ * @param version version number for the patch or installer.
39+ * @param target the system architecture that this file/installer is applicable
3840 */
39- public CachedFile (String id , String version , String architecture ) {
41+ public CachedFile (String id , String version , Architecture target ) {
4042 Objects .requireNonNull (id , "key for the cached file cannot be null" );
41- logger .entering (id , version , architecture );
43+ logger .entering (id , version , target );
4244 this .id = id ;
4345 this .version = version ;
44- this .architecture = architecture ;
46+ this .architecture = target ;
4547 logger .exiting ();
4648 }
4749
4850 /**
4951 * Represents a locally cached file.
5052 *
51- * @param id cache ID (like installer type or patchId)
52- * @param version version number for the patch or installer.
53+ * @param id cache ID (like installer type)
54+ * @param version version number for the patch or installer.
55+ * @param target the system architecture that this file/installer is applicable
5356 */
54- public CachedFile (String id , String version ) {
55- this (id , version , null );
57+ public CachedFile (InstallerType id , String version , Architecture target ) {
58+ this (id . toString () , version , target );
5659 }
5760
5861 /**
5962 * Represents a locally cached file.
6063 *
61- * @param id cache ID (like installer type)
62- * @param version version number for the patch or installer.
63- * @param architecture the system architecture that this file/installer is applicable
64- */
65- public CachedFile (InstallerType id , String version , String architecture ) {
66- this (id .toString (), version , architecture );
67- }
68-
69- /**
70- * Represents a locally cached file.
71- *
72- * @param id cache ID (like installer type)
73- * @param version version number for the patch or installer.
64+ * @param id cache ID (like installer type)
65+ * @param version version number for the patch or installer.
7466 */
7567 public CachedFile (InstallerType id , String version ) {
7668 this (id .toString (), version , null );
@@ -87,10 +79,14 @@ public static boolean isFileOnDisk(String filePath) {
8779 * @return the key to use for this cache entry, like xxxx_yyyy.
8880 */
8981 public String getKey () {
90- return getCacheKey (architecture );
82+ if (architecture == null ) {
83+ return getCacheKey (null );
84+ } else {
85+ return getCacheKey (architecture .toString ());
86+ }
9187 }
9288
93- private String getCacheKey (String architecture ) {
89+ private String getCacheKey (String arch ) {
9490 if (id .contains (CacheStore .CACHE_KEY_SEPARATOR )) {
9591 return id ;
9692 }
@@ -99,13 +95,19 @@ private String getCacheKey(String architecture) {
9995 key .append (id );
10096 key .append (CacheStore .CACHE_KEY_SEPARATOR );
10197 key .append (version );
102- if (architecture != null ) {
98+ if (arch != null ) {
10399 key .append (CacheStore .CACHE_KEY_SEPARATOR );
104- key .append (architecture );
100+ key .append (arch );
105101 }
106102 return key .toString ();
107103 }
108104
105+ private List <String > getPossibleKeys (Architecture architecture ) {
106+ ArrayList <String > result = new ArrayList <>();
107+ architecture .getAcceptableNames ().forEach (name -> result .add (getCacheKey (name )));
108+ return result ;
109+ }
110+
109111 /**
110112 * Get the version number for this cache entry/file.
111113 * @return the string version of this cached file.
@@ -118,7 +120,7 @@ public String getVersion() {
118120 * Get the system architecture name for this cache entry/file.
119121 * @return the system architecture name applicable fo this cached file.
120122 */
121- public String getArchitecture () {
123+ public Architecture getArchitecture () {
122124 return architecture ;
123125 }
124126
@@ -136,30 +138,29 @@ public String getArchitecture() {
136138 */
137139 public String resolve (CacheStore cacheStore ) throws IOException {
138140 // check entry exists in cache
139- String key = getKey ();
140- logger .entering (key );
141- String filePath = cacheStore .getValueFromCache (key );
142- if (filePath == null ) {
143- // The KEY for this CachedFile was not found in the local cache.
144- logger .fine ("Unable to find cache entry for {0}" , key );
145- String alternateKey ;
146- if (getArchitecture () == null ) {
147- // The user did not specify an architecture in the KEY and that key was not found in the cache.
148- // Try adding the local architecture to the key, and look for that entry.
149- alternateKey = getCacheKey (BuildPlatform .getPlatformName ());
150- logger .fine ("Trying local architecture: {0}" , alternateKey );
151- } else {
152- // The user specified an architecture in the KEY, but that key was not found.
153- // Try removing the architecture from the key, and look for that entry.
154- alternateKey = getCacheKey (null );
155- logger .fine ("Trying no-arch/generic architecture: {0}" , alternateKey );
141+ logger .entering ();
142+ String filePath = null ;
143+ List <String > keySearchOrder = new ArrayList <>();
144+ if (getArchitecture () == null ) {
145+ // architecture was not specified, search for cache key with no arch first, then look for local arch
146+ keySearchOrder .add (getCacheKey (null ));
147+ keySearchOrder .addAll (getPossibleKeys (Architecture .getLocalArchitecture ()));
148+ } else {
149+ // architecture was specified, search for cache key with arch first, then look for no arch key
150+ keySearchOrder .addAll (getPossibleKeys (getArchitecture ()));
151+ keySearchOrder .add (getCacheKey (null ));
152+ }
153+ for (String key : keySearchOrder ) {
154+ logger .finer ("Trying cache key {0}" , key );
155+ filePath = cacheStore .getValueFromCache (key );
156+ if (filePath != null ) {
157+ logger .finer ("Found cache key {0}" , key );
158+ break ;
156159 }
157- // second attempt to find a reasonable cache entry
158- filePath = cacheStore .getValueFromCache (alternateKey );
159160 }
160161
161162 if (!isFileOnDisk (filePath )) {
162- throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , key ));
163+ throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , getKey () ));
163164 }
164165
165166 logger .exiting (filePath );
0 commit comments