Widening.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@262 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2007-06-14 17:42:52 +00:00
parent 46248c0354
commit 7a65a62927
@@ -155,9 +155,8 @@ public class ResourceManager
} }
/** /**
* Creates a resource manager with the specified class loader via * Creates a resource manager with the specified class loader via which to load classes. See
* which to load classes. See {@link #ResourceManager(String)} for * {@link #ResourceManager(String)} for further documentation.
* further documentation.
*/ */
public ResourceManager (String resourceRoot, ClassLoader loader) public ResourceManager (String resourceRoot, ClassLoader loader)
{ {
@@ -215,24 +214,19 @@ public class ResourceManager
} }
/** /**
* Initializes the bundle sets to be made available by this resource * Initializes the bundle sets to be made available by this resource manager. Applications
* manager. Applications that wish to make use of resource bundles * that wish to make use of resource bundles should call this method after constructing the
* should call this method after constructing the resource manager. * resource manager.
* *
* @param resourceDir the base directory to which the paths in the * @param resourceDir the base directory to which the paths in the supplied configuration file
* supplied configuration file are relative. If this is null, the * are relative. If this is null, the system property <code>resource_dir</code> will be used,
* system property <code>resource_dir</code> will be used, if * if available.
* available. * @param configPath the path (relative to the resource dir) of the resource definition file.
* @param configPath the path (relative to the resource dir) of the * @param initObs a bundle initialization observer to notify of unpacking progress and success
* resource definition file. * or failure, or <code>null</code> if the caller doesn't care to be informed; note that in the
* @param initObs a bundle initialization observer to notify of * latter case, the calling thread will block until bundle unpacking is complete.
* unpacking progress and success or failure, or <code>null</code> if
* the caller doesn't care to be informed; note that in the latter
* case, the calling thread will block until bundle unpacking is
* complete.
* *
* @exception IOException thrown if we are unable to read our resource * @exception IOException thrown if we are unable to read our resource manager configuration.
* manager configuration.
*/ */
public void initBundles (String resourceDir, String configPath, InitObserver initObs) public void initBundles (String resourceDir, String configPath, InitObserver initObs)
throws IOException throws IOException
@@ -247,8 +241,8 @@ public class ResourceManager
try { try {
config.load(new FileInputStream(new File(_rdir, configPath))); config.load(new FileInputStream(new File(_rdir, configPath)));
} catch (Exception e) { } catch (Exception e) {
String errmsg = "Unable to load resource manager config " + String errmsg = "Unable to load resource manager config [rdir=" + _rdir +
"[rdir=" + _rdir + ", cpath=" + configPath + "]"; ", cpath=" + configPath + "]";
Log.warning(errmsg + "."); Log.warning(errmsg + ".");
Log.logStackTrace(e); Log.logStackTrace(e);
throw new IOException(errmsg); throw new IOException(errmsg);
@@ -266,8 +260,7 @@ public class ResourceManager
resolveResourceSet(setName, config.getProperty(key), dlist); resolveResourceSet(setName, config.getProperty(key), dlist);
} }
// if an observer was passed in, then we do not need to block // if an observer was passed in, then we do not need to block the caller
// the caller
final boolean[] shouldWait = new boolean[] { false }; final boolean[] shouldWait = new boolean[] { false };
if (initObs == null) { if (initObs == null) {
// if there's no observer, we'll need to block the caller // if there's no observer, we'll need to block the caller
@@ -276,9 +269,8 @@ public class ResourceManager
public void progress (int percent, long remaining) { public void progress (int percent, long remaining) {
if (percent >= 100) { if (percent >= 100) {
synchronized (this) { synchronized (this) {
// turn off shouldWait, in case we reached // turn off shouldWait, in case we reached 100% progress before the
// 100% progress before the calling thread even // calling thread even gets a chance to get to the blocking code, below
// gets a chance to get to the blocking code, below
shouldWait[0] = false; shouldWait[0] = false;
notify(); notify();
} }
@@ -303,8 +295,7 @@ public class ResourceManager
try { try {
initObs.wait(); initObs.wait();
} catch (InterruptedException ie) { } catch (InterruptedException ie) {
Log.warning("Interrupted while waiting for bundles " + Log.warning("Interrupted while waiting for bundles to unpack.");
"to unpack.");
} }
} }
} }
@@ -312,13 +303,12 @@ public class ResourceManager
} }
/** /**
* Given a path relative to the resource directory, the path is * Given a path relative to the resource directory, the path is properly jimmied (assuming we
* properly jimmied (assuming we always use /) and combined with the * always use /) and combined with the resource directory to yield a {@link File} object that
* resource directory to yield a {@link File} object that can be used * can be used to access the resource.
* to access the resource.
* *
* @return a file referencing the specified resource or null if the * @return a file referencing the specified resource or null if the resource manager was never
* resource manager was never configured with a resource directory. * configured with a resource directory.
*/ */
public File getResourceFile (String path) public File getResourceFile (String path)
{ {
@@ -332,8 +322,7 @@ public class ResourceManager
} }
/** /**
* Checks to see if the specified bundle exists, is unpacked and is * Checks to see if the specified bundle exists, is unpacked and is ready to be used.
* ready to be used.
*/ */
public boolean checkBundle (String path) public boolean checkBundle (String path)
{ {
@@ -342,19 +331,17 @@ public class ResourceManager
} }
/** /**
* Resolve the specified bundle (the bundle file must already exist in * Resolve the specified bundle (the bundle file must already exist in the appropriate place on
* the appropriate place on the file system) and return it on the * the file system) and return it on the specified result listener. Note that the result
* specified result listener. Note that the result listener may be * listener may be notified before this method returns on the caller's thread if the bundle is
* notified before this method returns on the caller's thread if the * already resolved, or it may be notified on a brand new thread if the bundle requires
* bundle is already resolved, or it may be notified on a brand new * unpacking.
* thread if the bundle requires unpacking.
*/ */
public void resolveBundle (String path, final ResultListener listener) public void resolveBundle (String path, final ResultListener listener)
{ {
File bfile = getResourceFile(path); File bfile = getResourceFile(path);
if (bfile == null) { if (bfile == null) {
String errmsg = "ResourceManager not configured with " + String errmsg = "ResourceManager not configured with resource directory.";
"resource directory.";
listener.requestFailed(new IOException(errmsg)); listener.requestFailed(new IOException(errmsg));
return; return;
} }
@@ -387,8 +374,8 @@ public class ResourceManager
} }
/** /**
* Returns the class loader being used to load resources if/when there * Returns the class loader being used to load resources if/when there are no resource bundles
* are no resource bundles from which to load them. * from which to load them.
*/ */
public ClassLoader getClassLoader () public ClassLoader getClassLoader ()
{ {
@@ -396,8 +383,8 @@ public class ResourceManager
} }
/** /**
* Configures the class loader this manager should use to load * Configures the class loader this manager should use to load resources if/when there are no
* resources if/when there are no bundles from which to load them. * bundles from which to load them.
*/ */
public void setClassLoader (ClassLoader loader) public void setClassLoader (ClassLoader loader)
{ {
@@ -407,11 +394,10 @@ public class ResourceManager
/** /**
* Fetches a resource from the local repository. * Fetches a resource from the local repository.
* *
* @param path the path to the resource * @param path the path to the resource (ie. "config/miso.properties"). This should not begin
* (ie. "config/miso.properties"). This should not begin with a slash. * with a slash.
* *
* @exception IOException thrown if a problem occurs locating or * @exception IOException thrown if a problem occurs locating or reading the resource.
* reading the resource.
*/ */
public InputStream getResource (String path) public InputStream getResource (String path)
throws IOException throws IOException
@@ -432,24 +418,21 @@ public class ResourceManager
return new FileInputStream(file); return new FileInputStream(file);
} }
// if we still didn't find anything, try the classloader // if we still didn't find anything, try the classloader; first try a locale-specific file
// First try a locale-specific file
if (_localePrefix != null) { if (_localePrefix != null) {
final String rpath = PathUtil.appendPath(_rootPath, final String rpath = PathUtil.appendPath(
PathUtil.appendPath(_localePrefix, path)); _rootPath, PathUtil.appendPath(_localePrefix, path));
in = (InputStream)AccessController.doPrivileged( in = (InputStream)AccessController.doPrivileged(new PrivilegedAction() {
new PrivilegedAction() { public Object run () {
public Object run () { return _loader.getResourceAsStream(rpath);
return _loader.getResourceAsStream(rpath); }
} });
});
if (in != null) { if (in != null) {
return in; return in;
} }
} }
// But if we didn't find that, try locale-neutral. // if we didn't find that, try locale-neutral
final String rpath = PathUtil.appendPath(_rootPath, path); final String rpath = PathUtil.appendPath(_rootPath, path);
in = (InputStream)AccessController.doPrivileged(new PrivilegedAction() { in = (InputStream)AccessController.doPrivileged(new PrivilegedAction() {
public Object run () { public Object run () {
@@ -466,15 +449,12 @@ public class ResourceManager
} }
/** /**
* Fetches the specified resource as an {@link ImageInputStream} and * Fetches the specified resource as an {@link ImageInputStream} and one that takes advantage,
* one that takes advantage, if possible, of caching of unpacked * if possible, of caching of unpacked resources on the local filesystem.
* resources on the local filesystem.
* *
* @exception FileNotFoundException thrown if the resource could not * @exception FileNotFoundException thrown if the resource could not be located in any of the
* be located in any of the bundles in the specified set, or if the * bundles in the specified set, or if the specified set does not exist.
* specified set does not exist. * @exception IOException thrown if a problem occurs locating or reading the resource.
* @exception IOException thrown if a problem occurs locating or
* reading the resource.
*/ */
public ImageInputStream getImageResource (String path) public ImageInputStream getImageResource (String path)
throws IOException throws IOException
@@ -493,26 +473,23 @@ public class ResourceManager
return new FileImageInputStream(file); return new FileImageInputStream(file);
} }
// First try a locale-specific file // first try a locale-specific file
if (_localePrefix != null) { if (_localePrefix != null) {
final String rpath = PathUtil.appendPath(_rootPath, final String rpath = PathUtil.appendPath(
PathUtil.appendPath(_localePrefix, path)); _rootPath, PathUtil.appendPath(_localePrefix, path));
InputStream in = (InputStream)AccessController.doPrivileged( InputStream in = (InputStream)AccessController.doPrivileged(new PrivilegedAction() {
new PrivilegedAction() { public Object run () {
public Object run () { return _loader.getResourceAsStream(rpath);
return _loader.getResourceAsStream(rpath); }
} });
});
if (in != null) { if (in != null) {
return new MemoryCacheImageInputStream( return new MemoryCacheImageInputStream(new BufferedInputStream(in));
new BufferedInputStream(in));
} }
} }
// if we still didn't find anything, try the classloader // if we still didn't find anything, try the classloader
final String rpath = PathUtil.appendPath(_rootPath, path); final String rpath = PathUtil.appendPath(_rootPath, path);
InputStream in = (InputStream) InputStream in = (InputStream)AccessController.doPrivileged(new PrivilegedAction() {
AccessController.doPrivileged(new PrivilegedAction() {
public Object run () { public Object run () {
return _loader.getResourceAsStream(rpath); return _loader.getResourceAsStream(rpath);
} }
@@ -527,18 +504,14 @@ public class ResourceManager
} }
/** /**
* Returns an input stream from which the requested resource can be * Returns an input stream from which the requested resource can be loaded. <em>Note:</em> this
* loaded. <em>Note:</em> this performs a linear search of all of the * performs a linear search of all of the bundles in the set and returns the first resource
* bundles in the set and returns the first resource found with the * found with the specified path, thus it is not extremely efficient and will behave
* specified path, thus it is not extremely efficient and will behave * unexpectedly if you use the same paths in different resource bundles.
* unexpectedly if you use the same paths in different resource
* bundles.
* *
* @exception FileNotFoundException thrown if the resource could not * @exception FileNotFoundException thrown if the resource could not be located in any of the
* be located in any of the bundles in the specified set, or if the * bundles in the specified set, or if the specified set does not exist.
* specified set does not exist. * @exception IOException thrown if a problem occurs locating or reading the resource.
* @exception IOException thrown if a problem occurs locating or
* reading the resource.
*/ */
public InputStream getResource (String rset, String path) public InputStream getResource (String rset, String path)
throws IOException throws IOException
@@ -547,8 +520,7 @@ public class ResourceManager
ResourceBundle[] bundles = getResourceSet(rset); ResourceBundle[] bundles = getResourceSet(rset);
if (bundles == null) { if (bundles == null) {
throw new FileNotFoundException( throw new FileNotFoundException(
"Unable to locate resource [set=" + rset + "Unable to locate resource [set=" + rset + ", path=" + path + "]");
", path=" + path + "]");
} }
// look for the resource in any of the bundles // look for the resource in any of the bundles
@@ -558,8 +530,7 @@ public class ResourceManager
// Try a localized version first. // Try a localized version first.
if (_localePrefix != null) { if (_localePrefix != null) {
instr = bundles[ii].getResource( instr = bundles[ii].getResource(PathUtil.appendPath(_localePrefix, path));
PathUtil.appendPath(_localePrefix, path));
} }
// If we didn't find that, try a generic. // If we didn't find that, try a generic.
if (instr == null) { if (instr == null) {
@@ -578,15 +549,12 @@ public class ResourceManager
} }
/** /**
* Fetches the specified resource as an {@link ImageInputStream} and * Fetches the specified resource as an {@link ImageInputStream} and one that takes advantage,
* one that takes advantage, if possible, of caching of unpacked * if possible, of caching of unpacked resources on the local filesystem.
* resources on the local filesystem.
* *
* @exception FileNotFoundException thrown if the resource could not * @exception FileNotFoundException thrown if the resource could not be located in any of the
* be located in any of the bundles in the specified set, or if the * bundles in the specified set, or if the specified set does not exist.
* specified set does not exist. * @exception IOException thrown if a problem occurs locating or reading the resource.
* @exception IOException thrown if a problem occurs locating or
* reading the resource.
*/ */
public ImageInputStream getImageResource (String rset, String path) public ImageInputStream getImageResource (String rset, String path)
throws IOException throws IOException
@@ -595,21 +563,19 @@ public class ResourceManager
ResourceBundle[] bundles = getResourceSet(rset); ResourceBundle[] bundles = getResourceSet(rset);
if (bundles == null) { if (bundles == null) {
throw new FileNotFoundException( throw new FileNotFoundException(
"Unable to locate image resource [set=" + rset + "Unable to locate image resource [set=" + rset + ", path=" + path + "]");
", path=" + path + "]");
} }
// look for the resource in any of the bundles // look for the resource in any of the bundles
int size = bundles.length; int size = bundles.length;
for (int ii = 0; ii < size; ii++) { for (int ii = 0; ii < size; ii++) {
File file = null; File file = null;
// Try a localized version first. // try a localized version first
if (_localePrefix != null) { if (_localePrefix != null) {
file = bundles[ii].getResourceFile( file = bundles[ii].getResourceFile(PathUtil.appendPath(_localePrefix, path));
PathUtil.appendPath(_localePrefix, path));
} }
// If we didn't find that, try generic. // if we didn't find that, try generic
if (file == null) { if (file == null) {
file = bundles[ii].getResourceFile(path); file = bundles[ii].getResourceFile(path);
} }
@@ -622,17 +588,14 @@ public class ResourceManager
} }
} }
String errmsg = "Unable to locate image resource [set=" + rset + String errmsg = "Unable to locate image resource [set=" + rset + ", path=" + path + "]";
", path=" + path + "]";
throw new FileNotFoundException(errmsg); throw new FileNotFoundException(errmsg);
} }
/** /**
* Returns a reference to the resource set with the specified name, or * Returns a reference to the resource set with the specified name, or null if no set exists
* null if no set exists with that name. Services that wish to load * with that name. Services that wish to load their own resources can allow the resource
* their own resources can allow the resource manager to load up a * manager to load up a resource set for them, from which they can easily load their resources.
* resource set for them, from which they can easily load their
* resources.
*/ */
public ResourceBundle[] getResourceSet (String name) public ResourceBundle[] getResourceSet (String name)
{ {
@@ -663,15 +626,12 @@ public class ResourceManager
} }
/** /**
* Loads up a resource set based on the supplied definition * Loads up a resource set based on the supplied definition information.
* information.
*/ */
protected void resolveResourceSet ( protected void resolveResourceSet (String setName, String definition, List dlist)
String setName, String definition, List dlist)
{ {
StringTokenizer tok = new StringTokenizer(definition, ":");
ArrayList set = new ArrayList(); ArrayList set = new ArrayList();
StringTokenizer tok = new StringTokenizer(definition, ":");
while (tok.hasMoreTokens()) { while (tok.hasMoreTokens()) {
String path = tok.nextToken().trim(); String path = tok.nextToken().trim();
ResourceBundle bundle = new ResourceBundle(getResourceFile(path), true, _unpack); ResourceBundle bundle = new ResourceBundle(getResourceFile(path), true, _unpack);
@@ -714,8 +674,7 @@ public class ResourceManager
for (Iterator iter = _bundles.iterator(); iter.hasNext(); ) { for (Iterator iter = _bundles.iterator(); iter.hasNext(); ) {
ResourceBundle bundle = (ResourceBundle)iter.next(); ResourceBundle bundle = (ResourceBundle)iter.next();
if (!bundle.sourceIsReady()) { if (!bundle.sourceIsReady()) {
Log.warning("Bundle failed to initialize " + Log.warning("Bundle failed to initialize " + bundle + ".");
bundle + ".");
} }
if (_obs != null) { if (_obs != null) {
int pct = count*100/_bundles.size(); int pct = count*100/_bundles.size();
@@ -746,8 +705,8 @@ public class ResourceManager
/** The directory that contains our resource bundles. */ /** The directory that contains our resource bundles. */
protected File _rdir; protected File _rdir;
/** The prefix we prepend to resource paths before attempting to load /** The prefix we prepend to resource paths before attempting to load them from the
* them from the classpath. */ * classpath. */
protected String _rootPath; protected String _rootPath;
/** Whether or not to unpack our resource bundles. */ /** Whether or not to unpack our resource bundles. */
@@ -762,8 +721,7 @@ public class ResourceManager
/** Locale to search for locale-specific resources, if any. */ /** Locale to search for locale-specific resources, if any. */
protected String _localePrefix = null; protected String _localePrefix = null;
/** The prefix of configuration entries that describe a resource /** The prefix of configuration entries that describe a resource set. */
* set. */
protected static final String RESOURCE_SET_PREFIX = "resource.set."; protected static final String RESOURCE_SET_PREFIX = "resource.set.";
/** The name of the default resource set. */ /** The name of the default resource set. */