1
- // Copyright (c) 2019, 2021 , Oracle and/or its affiliates.
1
+ // Copyright (c) 2019, 2024 , Oracle and/or its affiliates.
2
2
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3
3
4
4
package com .oracle .weblogic .imagetool .api .model ;
15
15
import com .oracle .weblogic .imagetool .installer .InstallerType ;
16
16
import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
17
17
import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18
+ import com .oracle .weblogic .imagetool .util .BuildPlatform ;
18
19
import com .oracle .weblogic .imagetool .util .Utils ;
19
20
20
21
/**
@@ -24,31 +25,55 @@ public class CachedFile {
24
25
25
26
private static final LoggingFacade logger = LoggingFactory .getLogger (CachedFile .class );
26
27
27
- private String id ;
28
- private String version ;
28
+ private final String id ;
29
+ private final String version ;
30
+ private final String architecture ;
29
31
30
32
/**
31
33
* Represents a locally cached file.
32
34
*
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
35
38
*/
36
- public CachedFile (String id , String version ) {
39
+ public CachedFile (String id , String version , String architecture ) {
37
40
Objects .requireNonNull (id , "key for the cached file cannot be null" );
38
- logger .entering (id , version );
41
+ logger .entering (id , version , architecture );
39
42
this .id = id ;
40
43
this .version = version ;
44
+ this .architecture = architecture ;
41
45
logger .exiting ();
42
46
}
43
47
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
+
44
69
/**
45
70
* Represents a locally cached file.
46
71
*
47
72
* @param id cache ID (like installer type)
48
73
* @param version version number for the patch or installer.
49
74
*/
50
75
public CachedFile (InstallerType id , String version ) {
51
- this (id .toString (), version );
76
+ this (id .toString (), version , null );
52
77
}
53
78
54
79
public static boolean isFileOnDisk (String filePath ) {
@@ -62,11 +87,23 @@ public static boolean isFileOnDisk(String filePath) {
62
87
* @return the key to use for this cache entry, like xxxx_yyyy.
63
88
*/
64
89
public String getKey () {
90
+ return getCacheKey (architecture );
91
+ }
92
+
93
+ private String getCacheKey (String architecture ) {
65
94
if (id .contains (CacheStore .CACHE_KEY_SEPARATOR )) {
66
95
return id ;
67
- } else {
68
- return id + CacheStore .CACHE_KEY_SEPARATOR + getVersion ();
69
96
}
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 ();
70
107
}
71
108
72
109
/**
@@ -77,8 +114,22 @@ public String getVersion() {
77
114
return version ;
78
115
}
79
116
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
+
80
125
/**
81
126
* 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.
82
133
* @param cacheStore the cache store to search
83
134
* @return the Path of the file, if found
84
135
* @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 {
88
139
String key = getKey ();
89
140
logger .entering (key );
90
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 );
156
+ }
157
+ // second attempt to find a reasonable cache entry
158
+ filePath = cacheStore .getValueFromCache (alternateKey );
159
+ }
160
+
91
161
if (!isFileOnDisk (filePath )) {
92
162
throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , key ));
93
163
}
@@ -103,7 +173,7 @@ public String resolve(CacheStore cacheStore) throws IOException {
103
173
* @return the path of the file copied to the Docker build context directory
104
174
*/
105
175
public Path copyFile (CacheStore cacheStore , String buildContextDir ) throws IOException {
106
- logger .entering ();
176
+ logger .entering (id , version , architecture , buildContextDir );
107
177
Path result ;
108
178
String sourceFile = resolve (cacheStore );
109
179
logger .info ("IMG-0043" , sourceFile );
0 commit comments