From 89632aecbd40f973beca1ca84e409862f335481d Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 18 Jul 2001 21:16:12 +0000 Subject: [PATCH] Initial skeleton implementation of the resource manager. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@59 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/resource/Log.java | 38 ++++++ .../threerings/resource/ResourceManager.java | 108 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 src/java/com/threerings/resource/Log.java create mode 100644 src/java/com/threerings/resource/ResourceManager.java diff --git a/src/java/com/threerings/resource/Log.java b/src/java/com/threerings/resource/Log.java new file mode 100644 index 000000000..d2f79e010 --- /dev/null +++ b/src/java/com/threerings/resource/Log.java @@ -0,0 +1,38 @@ +// +// $Id: Log.java,v 1.1 2001/07/18 21:16:12 mdb Exp $ + +package com.threerings.resource; + +/** + * A placeholder class that contains a reference to the log object used by + * the resource management package. + */ +public class Log +{ + public static com.samskivert.util.Log log = + new com.samskivert.util.Log("resource"); + + /** Convenience function. */ + public static void debug (String message) + { + log.debug(message); + } + + /** Convenience function. */ + public static void info (String message) + { + log.info(message); + } + + /** Convenience function. */ + public static void warning (String message) + { + log.warning(message); + } + + /** Convenience function. */ + public static void logStackTrace (Throwable t) + { + log.logStackTrace(com.samskivert.util.Log.WARNING, t); + } +} diff --git a/src/java/com/threerings/resource/ResourceManager.java b/src/java/com/threerings/resource/ResourceManager.java new file mode 100644 index 000000000..889eeb272 --- /dev/null +++ b/src/java/com/threerings/resource/ResourceManager.java @@ -0,0 +1,108 @@ +// +// $Id: ResourceManager.java,v 1.1 2001/07/18 21:16:12 mdb Exp $ + +package com.threerings.resource; + +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * The resource manager is responsible for maintaining a repository of + * resources that are synchronized with a remote source. This is + * accomplished in the form of a set of jar files that contain resources + * and that are updated from a remote resource repository via HTTP. + */ +public class ResourceManager +{ + /** + * Temporary means by which to construct a resource manager that loads + * resources from the specified source. + */ + public ResourceManager (URL source) + { + _source = source; + _rootPath = _source.getPath(); + // make root path end with a slash + if (!_rootPath.endsWith("/")) { + _rootPath = _rootPath + "/"; + } + } + + /** + * Fetches a resource from the local repository. + * + * @param path the path to the resource + * (ie. "config/miso.properties"). This should not begin with a slash. + * + * @exception IOException thrown if a problem occurs locating or + * reading the resource. + */ + public InputStream getResource (String path) + throws IOException + { + try { + String rpath = _rootPath + path; + URL rurl = new URL(_source, rpath); + return rurl.openStream(); + + } catch (MalformedURLException mue) { + Log.warning("Unable to construct path to resource " + + "[path=" + path + "]."); + return null; + } + } + + /** + * Fetches the requested resource and loads its contents into a byte + * array, which is returned. Note: this is hugely inefficient because + * the data is copied twice while reading and then once entirely again + * when a brand new byte array is returned for you, the caller (thanks + * to the inflexibility of ByteArrayOutputStream). Anyone + * reading a lot of resources should obtain an + * InputStream and read the data directly into where it + * needs to go. + * + * @exception IOException thrown if a problem occurs locating or + * reading the resource. + * + * @see #getResource + */ + public byte[] getResourceAsBytes (String path) + throws IOException + { + InputStream in = getResource(path); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[512]; + + // i loathe to while(1), but we need a non-trivial loop condition + while (true) { + int bytes = in.read(buffer, 0, buffer.length); + if (bytes == 0) { + throw new IOException("Read zero bytes!?"); + } else if (bytes < 0) { + break; + } + out.write(buffer, 0, bytes); + } + + return out.toByteArray(); + } + + public static void main (String[] args) + { + try { + String root = System.getProperty("root", ""); + URL url = new URL("file:" + root + "/rsrc"); + ResourceManager rmgr = new ResourceManager(url); + byte[] data = rmgr.getResourceAsBytes("config/miso.properties"); + System.out.println(new String(data)); + + } catch (Exception e) { + e.printStackTrace(System.err); + } + } + + protected URL _source; + protected String _rootPath; +}