1- // Copyright (c) 2019, 2021 , Oracle and/or its affiliates.
1+ // Copyright (c) 2019, 2024 , Oracle and/or its affiliates.
22// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
44package com .oracle .weblogic .imagetool .api .model ;
1515import com .oracle .weblogic .imagetool .installer .InstallerType ;
1616import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
1717import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18+ import com .oracle .weblogic .imagetool .util .BuildPlatform ;
1819import com .oracle .weblogic .imagetool .util .Utils ;
1920
2021/**
@@ -24,31 +25,55 @@ public class CachedFile {
2425
2526 private static final LoggingFacade logger = LoggingFactory .getLogger (CachedFile .class );
2627
27- private String id ;
28- private String version ;
28+ private final String id ;
29+ private final String version ;
30+ private final String architecture ;
2931
3032 /**
3133 * Represents a locally cached file.
3234 *
33- * @param id cache ID (like installer type or patchId)
34- * @param version version number for the patch or installer.
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
3538 */
36- public CachedFile (String id , String version ) {
39+ public CachedFile (String id , String version , String architecture ) {
3740 Objects .requireNonNull (id , "key for the cached file cannot be null" );
38- logger .entering (id , version );
41+ logger .entering (id , version , architecture );
3942 this .id = id ;
4043 this .version = version ;
44+ this .architecture = architecture ;
4145 logger .exiting ();
4246 }
4347
48+ /**
49+ * Represents a locally cached file.
50+ *
51+ * @param id cache ID (like installer type or patchId)
52+ * @param version version number for the patch or installer.
53+ */
54+ public CachedFile (String id , String version ) {
55+ this (id , version , null );
56+ }
57+
58+ /**
59+ * Represents a locally cached file.
60+ *
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+
4469 /**
4570 * Represents a locally cached file.
4671 *
4772 * @param id cache ID (like installer type)
4873 * @param version version number for the patch or installer.
4974 */
5075 public CachedFile (InstallerType id , String version ) {
51- this (id .toString (), version );
76+ this (id .toString (), version , null );
5277 }
5378
5479 public static boolean isFileOnDisk (String filePath ) {
@@ -62,11 +87,23 @@ public static boolean isFileOnDisk(String filePath) {
6287 * @return the key to use for this cache entry, like xxxx_yyyy.
6388 */
6489 public String getKey () {
90+ return getCacheKey (architecture );
91+ }
92+
93+ private String getCacheKey (String architecture ) {
6594 if (id .contains (CacheStore .CACHE_KEY_SEPARATOR )) {
6695 return id ;
67- } else {
68- return id + CacheStore .CACHE_KEY_SEPARATOR + getVersion ();
6996 }
97+
98+ StringBuilder key = new StringBuilder (32 );
99+ key .append (id );
100+ key .append (CacheStore .CACHE_KEY_SEPARATOR );
101+ key .append (version );
102+ if (architecture != null ) {
103+ key .append (CacheStore .CACHE_KEY_SEPARATOR );
104+ key .append (architecture );
105+ }
106+ return key .toString ();
70107 }
71108
72109 /**
@@ -77,8 +114,22 @@ public String getVersion() {
77114 return version ;
78115 }
79116
117+ /**
118+ * Get the system architecture name for this cache entry/file.
119+ * @return the system architecture name applicable fo this cached file.
120+ */
121+ public String getArchitecture () {
122+ return architecture ;
123+ }
124+
80125 /**
81126 * Get the path of the file stored locally in the cache.
127+ * Searching the cache starts with the specified key. If the key is not found in the cache,
128+ * one additional attempt is made to find an acceptable alternative. The second search is based on
129+ * whether the user specified a platform/architecture. If the user specified an architecture, check the cache
130+ * for an entry listing without the architecture in the key (generic architecture entry). If the user
131+ * did not specify an architecture, check the cache for an entry listing using the local architecture
132+ * in case the user added the cache entry with the architecture.
82133 * @param cacheStore the cache store to search
83134 * @return the Path of the file, if found
84135 * @throws IOException throws FileNotFoundException, if this cached file (key) could not be located in the cache
@@ -88,6 +139,25 @@ public String resolve(CacheStore cacheStore) throws IOException {
88139 String key = getKey ();
89140 logger .entering (key );
90141 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 );
156+ }
157+ // second attempt to find a reasonable cache entry
158+ filePath = cacheStore .getValueFromCache (alternateKey );
159+ }
160+
91161 if (!isFileOnDisk (filePath )) {
92162 throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , key ));
93163 }
@@ -103,7 +173,7 @@ public String resolve(CacheStore cacheStore) throws IOException {
103173 * @return the path of the file copied to the Docker build context directory
104174 */
105175 public Path copyFile (CacheStore cacheStore , String buildContextDir ) throws IOException {
106- logger .entering ();
176+ logger .entering (id , version , architecture , buildContextDir );
107177 Path result ;
108178 String sourceFile = resolve (cacheStore );
109179 logger .info ("IMG-0043" , sourceFile );
0 commit comments