Wire up the magic so that data in resource bundles can be acquired via

URLs.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2449 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2003-04-24 07:23:47 +00:00
parent ea3adbb530
commit 5d6e746b88
2 changed files with 84 additions and 1 deletions
@@ -0,0 +1,79 @@
//
// $Id: Handler.java,v 1.1 2003/04/24 07:23:47 ray Exp $
package com.threerings.resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
/**
* This class is not used directly, except by a registering ResourceManager
* so that we can load data from the resource manager using URLs of the form
* <code>resource://&lt;bundle&gt;/&lt;path&gt;</code>.
*/
public class Handler extends URLStreamHandler
{
/**
* Register this class to handle "resource" urls
* ("resource://<i>bundlename</i>/<i>path</i>") with the specified
* ResourceManager.
*/
public static void registerHandler (ResourceManager rmgr)
{
_rmgr = rmgr;
String prop = System.getProperty(HANDLER_PROP, "");
if (!"".equals(prop)) {
prop += "|";
}
prop += "com.threerings";
System.setProperty(HANDLER_PROP, prop);
}
// documentation inherited
protected URLConnection openConnection (URL url)
throws IOException
{
return new URLConnection(url) {
// documentation inherited
public void connect ()
throws IOException
{
try {
// the host is the bundle name
_stream = _rmgr.getResource(this.url.getHost(),
// and we need to remove the leading '/' from path
this.url.getPath().substring(1));
this.connected = true;
} catch (IOException ioe) {
Log.warning("Could not find resource [url=" + this.url +
", error=" + ioe.getMessage() + "].");
throw ioe; // rethrow
}
}
// documentation inherited
public InputStream getInputStream ()
throws IOException
{
if (!this.connected) {
connect();
}
return _stream;
}
protected InputStream _stream;
};
}
/** Our singleton resource manager. */
protected static ResourceManager _rmgr;
/** Dug up from java.net.URL */
protected static final String HANDLER_PROP = "java.protocol.handler.pkgs";
}
@@ -1,5 +1,5 @@
//
// $Id: ResourceManager.java,v 1.23 2003/02/12 18:11:33 mdb Exp $
// $Id: ResourceManager.java,v 1.24 2003/04/24 07:23:47 ray Exp $
package com.threerings.resource;
@@ -148,6 +148,10 @@ public class ResourceManager
// use the classloader that loaded us
_loader = getClass().getClassLoader();
// set up a URL handler so that things can be loaded via
// urls with the 'resource' protocol
Handler.registerHandler(this);
}
/**