{
InputStream result = null;
if (name == null || name.length() == 0)
{
throw new ResourceNotFoundException ("WebappLoader : No template name provided");
}
/* since the paths always ends in '/',
* make sure the name never starts with one */
while (name.startsWith("/"))
{
name = name.substring(1);
}
Exception exception = null;
for (int i = 0; i < paths.length; i++)
{
String path = paths[i] + name;
try
{
result = servletContext.getResourceAsStream(path);
/* save the path and exit the loop if we found the template */
if (result != null)
{
templatePaths.put(name, paths[i]);
break;
}
}
catch (NullPointerException npe)
{
/* no servletContext was set, whine about it! */
throw npe;
}
catch (Exception e)
{
/* only save the first one for later throwing */
if (exception == null)
{
if (log.isDebugEnabled())
{
log.debug("WebappResourceLoader: Could not load "+path, e);
}
exception = e;
}
}
}
/* if we never found the template */
if (result == null)
{
String msg = "WebappLoader : Resource '" + name + "' not found.";
/* convert to a general Velocity ResourceNotFoundException */
if (exception == null)
{
throw new ResourceNotFoundException(msg);
}
else
{
msg += " Due to: " + exception;
throw new ResourceNotFoundException(msg, exception);
}
}
return result;
}