public synchronized Reader getResourceReader()

in velocity-tools-view/src/main/java/org/apache/velocity/tools/view/WebappResourceLoader.java [127:220]


    public synchronized Reader getResourceReader(String name, String encoding)
            throws ResourceNotFoundException
    {
        Reader result = null;

        if (name == null || name.length() == 0)
        {
            throw new ResourceNotFoundException("WebappResourceLoader: 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++)
        {
            final String path = paths[i] + name;
            InputStream rawStream = null;
            try
            {
                if (System.getSecurityManager() != null)
                {
                    rawStream = AccessController.doPrivileged(
                        new PrivilegedAction<InputStream>()
                        {
                            @Override
                            public InputStream run()
                            {
                                return servletContext.getResourceAsStream(path);
                            }
                        });
                }
                else
                {
                    rawStream = servletContext.getResourceAsStream(path);
                }
                if (rawStream != null)
                {
                    result = buildReader(rawStream, encoding);
                }

                /* 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)
            {
                if (rawStream != null)
                {
                    try
                    {
                        rawStream.close();
                    }
                    catch(Exception ee) {}
                }
                /* only save the first one for later throwing */
                if (exception == null)
                {
                    log.debug("WebappResourceLoader: Could not load {}", path, e);
                    exception = e;
                }
            }
        }

        /* if we never found the template */
        if (result == null)
        {
            String msg = "WebappResourceLoader: 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;
    }