Add the ability for network resource bundles to have an index file which tells them which files they should expect to find. This turns out to be pretty useful since resources try to grab a locale-specific version first, which often fails, and then they have to grab their generic version. This allows us to bypass some of this latency when we're able to know ahead of time which resources we will have.
git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@500 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package com.threerings.media.tools;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.apache.tools.ant.BuildException;
|
||||
import org.apache.tools.ant.DirectoryScanner;
|
||||
import org.apache.tools.ant.Task;
|
||||
import org.apache.tools.ant.types.FileSet;
|
||||
|
||||
/**
|
||||
* Creates a file that lists all the resources in a fileset out to an index file.
|
||||
*/
|
||||
public class ResourceIndexerTask extends Task
|
||||
{
|
||||
/**
|
||||
* Adds a nested <fileset> element.
|
||||
*/
|
||||
public void addFileset (FileSet set)
|
||||
{
|
||||
_filesets.add(set);
|
||||
}
|
||||
|
||||
public void setIndexFile (String file)
|
||||
{
|
||||
_indexFile = file;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
public void execute () throws BuildException
|
||||
{
|
||||
PrintWriter fout = null;
|
||||
try {
|
||||
fout = new PrintWriter(new FileWriter(_indexFile));
|
||||
|
||||
for (FileSet fs : _filesets) {
|
||||
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
||||
String[] srcFiles = ds.getIncludedFiles();
|
||||
for (String filename : srcFiles) {
|
||||
fout.println(filename);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (IOException ioe) {
|
||||
throw new BuildException(ioe);
|
||||
} finally {
|
||||
if (fout != null) {
|
||||
fout.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A list of filesets that contain files to include in the index. */
|
||||
protected ArrayList<FileSet> _filesets = new ArrayList<FileSet>();
|
||||
|
||||
/** The name of the file to which we should write the index. */
|
||||
protected String _indexFile;
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.security.AccessControlException;
|
||||
import java.util.HashSet;
|
||||
|
||||
/**
|
||||
* Resource bundle that retrieves its contents via HTTP over the network from a root URL.
|
||||
@@ -35,6 +36,11 @@ import java.security.AccessControlException;
|
||||
public class NetworkResourceBundle extends ResourceBundle
|
||||
{
|
||||
public NetworkResourceBundle (String root, String path)
|
||||
{
|
||||
this(root, path, null);
|
||||
}
|
||||
|
||||
public NetworkResourceBundle (String root, String path, HashSet<String> rsrcList)
|
||||
{
|
||||
if (!root.endsWith("/")) {
|
||||
root += "/";
|
||||
@@ -45,6 +51,8 @@ public class NetworkResourceBundle extends ResourceBundle
|
||||
Log.warning("Created malformed URL for resource. [root=" + root + ", path=" + path);
|
||||
}
|
||||
_ident = path;
|
||||
|
||||
_rsrcList = rsrcList;
|
||||
}
|
||||
|
||||
@Override // documentation inherited
|
||||
@@ -57,6 +65,11 @@ public class NetworkResourceBundle extends ResourceBundle
|
||||
public InputStream getResource (String path)
|
||||
throws IOException
|
||||
{
|
||||
// If we can reject it before opening a connection, then save the network latency.
|
||||
if (!inResourceList(path)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
URL resourceUrl = new URL(_bundleURL, path);
|
||||
HttpURLConnection ucon = null;
|
||||
try {
|
||||
@@ -91,6 +104,15 @@ public class NetworkResourceBundle extends ResourceBundle
|
||||
return ResourceManager.loadImage(in);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this resource bundle.
|
||||
*/
|
||||
@@ -105,4 +127,7 @@ public class NetworkResourceBundle extends ResourceBundle
|
||||
|
||||
/** Our root url to the resources in this bundle. */
|
||||
protected URL _bundleURL;
|
||||
|
||||
/** A list of all the resources included in this bundle. */
|
||||
protected HashSet<String> _rsrcList;
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
@@ -655,6 +656,16 @@ public class ResourceManager
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* If we have a full list of the resources available, we return it. A return value of null
|
||||
* means that we do not know what's available and we'll have to try all possibilities. This
|
||||
* is fine for most applications.
|
||||
*/
|
||||
protected HashSet<String> getResourceList ()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void initResourceDir (String resourceDir)
|
||||
{
|
||||
// if none was specified, check the resource_dir system property
|
||||
@@ -698,7 +709,7 @@ public class ResourceManager
|
||||
dlist.add(bundle);
|
||||
} else if (setType.equals(NETWORK_SET_TYPE)) {
|
||||
NetworkResourceBundle bundle =
|
||||
new NetworkResourceBundle(_networkRootPath, path);
|
||||
new NetworkResourceBundle(_networkRootPath, path, getResourceList());
|
||||
set.add(bundle);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user