* Don't use TreeSet for resources unless we can get a ResourceComparator

* Check if resources exist before trying to read from them
This commit is contained in:
hns 2005-03-23 12:30:04 +00:00
parent 4f59be6597
commit e5514d7099

View file

@ -52,14 +52,17 @@ public final class ResourceProperties extends Properties {
private String resourceName; private String resourceName;
// Sorted map of resources // Sorted map of resources
private TreeSet resources; private Set resources;
/** /**
* Constructs an empty ResourceProperties * Constructs an empty ResourceProperties
* Resources must be added manually afterwards * Resources must be added manually afterwards
*/ */
public ResourceProperties() { public ResourceProperties() {
resources = new TreeSet(); // TODO: we can't use TreeSet because we don't have the app's resource comparator
// Since resources don't implement Comparable, we can't add them to a "naked" TreeSet
// As a result, resource ordering is random when updating.
resources = new HashSet();
} }
/** /**
@ -79,11 +82,12 @@ public final class ResourceProperties extends Properties {
* application using the given name to fetch resources and falling back * application using the given name to fetch resources and falling back
* to the given default properties * to the given default properties
* @param app application to fetch resources from * @param app application to fetch resources from
* @param sourceName name to use when fetching resources from the application * @param resourceName name to use when fetching resources from the application
* @param defaultProperties default properties * @param defaultProperties default properties
*/ */
public ResourceProperties(Application app, String sourceName, ResourceProperties defaultProperties) { public ResourceProperties(Application app, String resourceName,
this(app, sourceName); ResourceProperties defaultProperties) {
this(app, resourceName);
this.defaultProperties = defaultProperties; this.defaultProperties = defaultProperties;
forceUpdate(); forceUpdate();
} }
@ -111,7 +115,7 @@ public final class ResourceProperties extends Properties {
* @param resource resource to add * @param resource resource to add
*/ */
public void addResource(Resource resource) { public void addResource(Resource resource) {
if (resource != null) { if (resource != null && !resources.contains(resource)) {
resources.add(resource); resources.add(resource);
forceUpdate(); forceUpdate();
} }
@ -160,9 +164,9 @@ public final class ResourceProperties extends Properties {
while (iterator.hasNext()) { while (iterator.hasNext()) {
try { try {
Repository repository = (Repository) iterator.next(); Repository repository = (Repository) iterator.next();
Resource resource = repository.getResource(resourceName); Resource res = repository.getResource(resourceName);
if (resource != null) { if (res != null && res.exists()) {
load(resource.getInputStream()); load(res.getInputStream());
} }
} catch (IOException iox) { } catch (IOException iox) {
iox.printStackTrace(); iox.printStackTrace();
@ -175,8 +179,13 @@ public final class ResourceProperties extends Properties {
Iterator iterator = resources.iterator(); Iterator iterator = resources.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
try { try {
load(((Resource) iterator.next()).getInputStream()); Resource res = (Resource) iterator.next();
} catch (IOException ignore) {} if (res.exists()) {
load(res.getInputStream());
}
} catch (IOException iox) {
iox.printStackTrace();
}
} }
} }