Skip to content

Commit afb30e5

Browse files
committed
Failed to load resource input for ECBCurrentRateProvider from org/javamoney/moneta/convert/ecb/defaults/eurofxref-daily.xml #431
1 parent dc4bcf7 commit afb30e5

4 files changed

Lines changed: 60 additions & 15 deletions

File tree

moneta-core/src/main/java/org/javamoney/moneta/spi/loader/LoadDataInformation.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.javamoney.moneta.spi.loader;
1717

18+
import java.io.InputStream;
1819
import java.net.URI;
1920
import java.util.Arrays;
2021
import java.util.Map;
@@ -40,17 +41,24 @@ public class LoadDataInformation {
4041

4142
private final URI backupResource;
4243

44+
public InputStream getFallbackStream() {
45+
return fallbackStream;
46+
}
47+
48+
private final InputStream fallbackStream;
49+
4350
private final URI[] resourceLocations;
4451

4552
private final boolean startRemote;
4653

4754
LoadDataInformation(String resourceId, UpdatePolicy updatePolicy,
4855
Map<String, String> properties, LoaderService.Listener listener,
49-
URI backupResource, URI[] resourceLocations, boolean startRemote) {
56+
InputStream fallbackStream, URI backupResource, URI[] resourceLocations, boolean startRemote) {
5057
this.resourceId = resourceId;
5158
this.updatePolicy = updatePolicy;
5259
this.properties = properties;
5360
this.listener = listener;
61+
this.fallbackStream = fallbackStream;
5462
this.backupResource = backupResource;
5563
this.resourceLocations = resourceLocations;
5664
this.startRemote = startRemote;

moneta-core/src/main/java/org/javamoney/moneta/spi/loader/LoadDataInformationBuilder.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.javamoney.moneta.spi.loader;
1717

18+
import java.io.InputStream;
1819
import java.net.URI;
1920
import java.util.Map;
2021
import java.util.Objects;
@@ -36,11 +37,17 @@ public class LoadDataInformationBuilder {
3637
private UpdatePolicy updatePolicy;
3738

3839
private Map<String, String> properties;
40+
3941
/** An (optional) Listener to be registered. */
4042
private Listener listener;
43+
44+
/**
45+
* The fallback input stream in the classpath.
46+
*/
47+
private InputStream fallbackStream;
48+
4149
/**
42-
* The backup resource location in the classpath, not
43-
* {@code null}.
50+
* The backup resource location in the local filesystem.
4451
*/
4552
private URI backupResource;
4653

@@ -69,6 +76,11 @@ public LoadDataInformationBuilder withLoaderListener(Listener listener) {
6976
return this;
7077
}
7178

79+
public LoadDataInformationBuilder withFallbackStream(InputStream stream) {
80+
this.fallbackStream = stream;
81+
return this;
82+
}
83+
7284
public LoadDataInformationBuilder withBackupResource(URI backupResource) {
7385
this.backupResource = backupResource;
7486
return this;
@@ -98,8 +110,7 @@ else if (Objects.isNull(resourceLocations)) {
98110
throw new IllegalStateException("The properties should be informed");
99111
}
100112
return new LoadDataInformation(resourceId, updatePolicy, properties,
101-
listener, backupResource, resourceLocations, startRemote);
113+
listener, fallbackStream, backupResource, resourceLocations, startRemote);
102114
}
103115

104-
105116
}

moneta-core/src/main/java/org/javamoney/moneta/spi/loader/okhttp/HttpLoadDataService.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ public boolean execute(String resourceId,
5050
}
5151
try {
5252
if (load.loadFallback()) {
53-
LOG.log(Level.WARNING, "Read fallback data from: " + load.getFallbackResource());
54-
listener.trigger(resourceId, load);
55-
LOG.log(Level.WARNING, "Loaded fallback data from: " + load.getFallbackResource());
56-
return true;
53+
if (load.getFallbackStream() != null) {
54+
LOG.log(Level.FINE, "Read fallback data from stream");
55+
listener.trigger(resourceId, load);
56+
LOG.log(Level.FINE, "Loaded fallback data from input stream");
57+
return true;
58+
} else if (load.getFallbackResource() != null) {
59+
LOG.log(Level.FINE, "Read fallback data from: " + load.getFallbackResource());
60+
listener.trigger(resourceId, load);
61+
LOG.log(Level.FINE, "Loaded fallback data from: " + load.getFallbackResource());
62+
return true;
63+
}
5764
}
5865
} catch (Exception e) {
5966
LOG.log(Level.SEVERE, "Failed to read/load fallback resource: " + resourceId,

moneta-core/src/main/java/org/javamoney/moneta/spi/loader/okhttp/LoadableHttpResource.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ class LoadableHttpResource implements DataStreamFactory {
5959
*/
6060
private final List<URI> remoteResources = new ArrayList<>();
6161
/**
62-
* The fallback location (classpath).
62+
* The fallback location (local).
6363
*/
6464
private final URI fallbackLocation;
65+
66+
/**
67+
* The fallback stream (classpath).
68+
*/
69+
private final InputStream fallbackStream;
6570
/**
6671
* The cache used.
6772
*/
@@ -110,6 +115,7 @@ class LoadableHttpResource implements DataStreamFactory {
110115
this.updatePolicy = loadDataInformation.getUpdatePolicy();
111116
this.properties = loadDataInformation.getProperties();
112117
this.fallbackLocation = loadDataInformation.getBackupResource();
118+
this.fallbackStream = loadDataInformation.getFallbackStream();
113119
this.remoteResources.addAll(Arrays.asList(loadDataInformation.getResourceLocations()));
114120
}
115121

@@ -171,6 +177,15 @@ public final List<URI> getRemoteResources() {
171177
return Collections.unmodifiableList(remoteResources);
172178
}
173179

180+
/**
181+
* Return the fallback input stream.
182+
*
183+
* @return the fallback stream, or null.
184+
*/
185+
public InputStream getFallbackStream() {
186+
return fallbackStream;
187+
}
188+
174189
/**
175190
* Return the fallback location.
176191
*
@@ -224,7 +239,7 @@ public final long getLastLoaded() {
224239
public boolean loadRemote() {
225240
for (URI itemToLoad : remoteResources) {
226241
try {
227-
return load(itemToLoad, false);
242+
return load(itemToLoad, false, null);
228243
} catch (Exception e) {
229244
LOG.log(Level.WARNING, "Failed to load resource: " + itemToLoad, e);
230245
}
@@ -245,7 +260,7 @@ public boolean loadFallback() {
245260
", loadFallback not supported.");
246261
return false;
247262
}
248-
load(fallbackLocation, true);
263+
load(fallbackLocation, true, fallbackStream);
249264
clearCache();
250265
return true;
251266
} catch (Exception e) {
@@ -302,18 +317,22 @@ protected void writeCache() throws IOException {
302317
/**
303318
* Tries to load the data from the given location. The location hereby can be a remote location or a local
304319
* location. Also it can be an URL pointing to a current dataset, or an url directing to fallback resources,
305-
* e.g. within the current classpath.
320+
* e.g. within the current file system.
306321
*
307322
* @param itemToLoad the target {@link URI}
308323
* @param fallbackLoad true, for a fallback URL.
309324
*/
310-
protected boolean load(URI itemToLoad, boolean fallbackLoad) {
325+
private boolean load(URI itemToLoad, boolean fallbackLoad, InputStream fallbackStream) {
311326
InputStream is = null;
312327
ByteArrayOutputStream stream = new ByteArrayOutputStream();
313328

314329
try {
315330
if (fallbackLoad) {
316-
is = new BufferedInputStream(itemToLoad.toURL().openStream());
331+
if (fallbackStream != null) {
332+
is = new BufferedInputStream(fallbackStream);
333+
} else {
334+
is = new BufferedInputStream(itemToLoad.toURL().openStream());
335+
}
317336
} else {
318337
OkHttpClient.Builder builder = new OkHttpClient.Builder();
319338

0 commit comments

Comments
 (0)