Break resource set checking out of NetworkResourceBundle into an abstract superclass such that non-network bundles might also share in the glory of not asking for things that aren't there

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@781 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Charlie Groves
2009-03-13 18:49:15 +00:00
parent 6fe87f6920
commit c32d68afd2
3 changed files with 40 additions and 36 deletions
@@ -0,0 +1,27 @@
package com.threerings.resource;
import java.util.Set;
/**
* Keeps a set of known resources and allows implementors to ask if a resource is in it before
* performing a potentially expensive failed lookup.
*/
public abstract class KnownAvailabilityResourceBundle extends ResourceBundle
{
public KnownAvailabilityResourceBundle (Set<String> availableResources)
{
_rsrcs = availableResources;
}
/**
* Returns whether we believe the path to be available. If we have no list, we assume
* everything may be available.
*/
protected boolean isPossiblyAvailable (String path)
{
return _rsrcs == null || _rsrcs.contains(path);
}
/** All the resources included in this bundle. */
protected Set<String> _rsrcs;
}
@@ -21,32 +21,25 @@
package com.threerings.resource; package com.threerings.resource;
import static com.threerings.resource.Log.log;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.security.AccessControlException; import java.security.AccessControlException;
import java.util.Set;
import java.util.HashSet;
import java.io.IOException;
import java.io.InputStream;
import java.awt.image.BufferedImage;
import static com.threerings.resource.Log.log;
/** /**
* Resource bundle that retrieves its contents via HTTP over the network from a root URL. * Resource bundle that retrieves its contents via HTTP over the network from a root URL.
*/ */
public class NetworkResourceBundle extends ResourceBundle public class NetworkResourceBundle extends KnownAvailabilityResourceBundle
{ {
public NetworkResourceBundle (String root, String path) public NetworkResourceBundle (String root, String path, Set<String> rsrcList)
{
this(root, path, null);
}
public NetworkResourceBundle (String root, String path, HashSet<String> rsrcList)
{ {
super(rsrcList);
if (!root.endsWith("/")) { if (!root.endsWith("/")) {
root += "/"; root += "/";
} }
@@ -56,8 +49,6 @@ public class NetworkResourceBundle extends ResourceBundle
log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path); log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path);
} }
_ident = path; _ident = path;
_rsrcList = rsrcList;
} }
@Override // documentation inherited @Override // documentation inherited
@@ -71,7 +62,7 @@ public class NetworkResourceBundle extends ResourceBundle
throws IOException throws IOException
{ {
// If we can reject it before opening a connection, then save the network latency. // If we can reject it before opening a connection, then save the network latency.
if (!inResourceList(path)) { if (!isPossiblyAvailable(_ident + path)) {
return null; return null;
} }
@@ -81,14 +72,11 @@ public class NetworkResourceBundle extends ResourceBundle
protected static InputStream getResource(URL resourceUrl) protected static InputStream getResource(URL resourceUrl)
{ {
URLConnection ucon = null; URLConnection ucon;
try { try {
ucon = resourceUrl.openConnection(); ucon = resourceUrl.openConnection();
} catch (IOException ioe) { } catch (IOException ioe) {
log.warning("Unable to open connection [url=" + resourceUrl + ", ex=" + ioe + "]"); log.warning("Unable to open connection [url=" + resourceUrl + ", ex=" + ioe + "]");
}
if (ucon == null) {
return null; return null;
} }
@@ -115,15 +103,6 @@ public class NetworkResourceBundle extends ResourceBundle
return ResourceManager.loadImage(in, false); return ResourceManager.loadImage(in, false);
} }
/**
* Returns whether we believe the path to be available. If we have no list, we assume
* everything may be available.
*/
protected boolean inResourceList (String path)
{
return _rsrcList == null || _rsrcList.contains(_ident + path);
}
/** /**
* Returns a string representation of this resource bundle. * Returns a string representation of this resource bundle.
*/ */
@@ -138,7 +117,4 @@ public class NetworkResourceBundle extends ResourceBundle
/** Our root url to the resources in this bundle. */ /** Our root url to the resources in this bundle. */
protected URL _bundleURL; protected URL _bundleURL;
/** A list of all the resources included in this bundle. */
protected HashSet<String> _rsrcList;
} }
@@ -32,6 +32,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -852,7 +853,7 @@ public class ResourceManager
* Creates an appropriate bundle for fetching resources from the network. * Creates an appropriate bundle for fetching resources from the network.
*/ */
protected ResourceBundle createNetworkResourceBundle (String root, String path, protected ResourceBundle createNetworkResourceBundle (String root, String path,
HashSet<String> rsrcList) Set<String> rsrcList)
{ {
return new NetworkResourceBundle(root, path, rsrcList); return new NetworkResourceBundle(root, path, rsrcList);
} }