9
9
import java .nio .file .Files ;
10
10
import java .nio .file .Path ;
11
11
import java .nio .file .Paths ;
12
+ import java .util .ArrayList ;
13
+ import java .util .List ;
12
14
import java .util .Objects ;
13
15
14
16
import com .oracle .weblogic .imagetool .cachestore .CacheStore ;
15
17
import com .oracle .weblogic .imagetool .installer .InstallerType ;
16
18
import com .oracle .weblogic .imagetool .logging .LoggingFacade ;
17
19
import com .oracle .weblogic .imagetool .logging .LoggingFactory ;
18
- import com .oracle .weblogic .imagetool .util .BuildPlatform ;
20
+ import com .oracle .weblogic .imagetool .util .Architecture ;
19
21
import com .oracle .weblogic .imagetool .util .Utils ;
20
22
21
23
/**
@@ -27,50 +29,40 @@ public class CachedFile {
27
29
28
30
private final String id ;
29
31
private final String version ;
30
- private final String architecture ;
32
+ private final Architecture architecture ;
31
33
32
34
/**
33
35
* Represents a locally cached file.
34
36
*
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
38
40
*/
39
- public CachedFile (String id , String version , String architecture ) {
41
+ public CachedFile (String id , String version , Architecture target ) {
40
42
Objects .requireNonNull (id , "key for the cached file cannot be null" );
41
- logger .entering (id , version , architecture );
43
+ logger .entering (id , version , target );
42
44
this .id = id ;
43
45
this .version = version ;
44
- this .architecture = architecture ;
46
+ this .architecture = target ;
45
47
logger .exiting ();
46
48
}
47
49
48
50
/**
49
51
* Represents a locally cached file.
50
52
*
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
53
56
*/
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 );
56
59
}
57
60
58
61
/**
59
62
* Represents a locally cached file.
60
63
*
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.
74
66
*/
75
67
public CachedFile (InstallerType id , String version ) {
76
68
this (id .toString (), version , null );
@@ -87,10 +79,14 @@ public static boolean isFileOnDisk(String filePath) {
87
79
* @return the key to use for this cache entry, like xxxx_yyyy.
88
80
*/
89
81
public String getKey () {
90
- return getCacheKey (architecture );
82
+ if (architecture == null ) {
83
+ return getCacheKey (null );
84
+ } else {
85
+ return getCacheKey (architecture .toString ());
86
+ }
91
87
}
92
88
93
- private String getCacheKey (String architecture ) {
89
+ private String getCacheKey (String arch ) {
94
90
if (id .contains (CacheStore .CACHE_KEY_SEPARATOR )) {
95
91
return id ;
96
92
}
@@ -99,13 +95,19 @@ private String getCacheKey(String architecture) {
99
95
key .append (id );
100
96
key .append (CacheStore .CACHE_KEY_SEPARATOR );
101
97
key .append (version );
102
- if (architecture != null ) {
98
+ if (arch != null ) {
103
99
key .append (CacheStore .CACHE_KEY_SEPARATOR );
104
- key .append (architecture );
100
+ key .append (arch );
105
101
}
106
102
return key .toString ();
107
103
}
108
104
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
+
109
111
/**
110
112
* Get the version number for this cache entry/file.
111
113
* @return the string version of this cached file.
@@ -118,7 +120,7 @@ public String getVersion() {
118
120
* Get the system architecture name for this cache entry/file.
119
121
* @return the system architecture name applicable fo this cached file.
120
122
*/
121
- public String getArchitecture () {
123
+ public Architecture getArchitecture () {
122
124
return architecture ;
123
125
}
124
126
@@ -136,30 +138,29 @@ public String getArchitecture() {
136
138
*/
137
139
public String resolve (CacheStore cacheStore ) throws IOException {
138
140
// 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 ;
156
159
}
157
- // second attempt to find a reasonable cache entry
158
- filePath = cacheStore .getValueFromCache (alternateKey );
159
160
}
160
161
161
162
if (!isFileOnDisk (filePath )) {
162
- throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , key ));
163
+ throw new FileNotFoundException (Utils .getMessage ("IMG-0011" , getKey () ));
163
164
}
164
165
165
166
logger .exiting (filePath );
0 commit comments