public synchronized Reader getResourceReader()

in velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/URLResourceLoader.java [87:157]


    public synchronized Reader getResourceReader(String name, String encoding)
            throws ResourceNotFoundException
    {
        if (StringUtils.isEmpty(name))
        {
            throw new ResourceNotFoundException("URLResourceLoader: No template name provided");
        }

        Reader reader = null;
        Exception exception = null;
        for (String root : roots)
        {
            InputStream rawStream = null;
            try
            {
                URL u = new URL(root + name);
                URLConnection conn = u.openConnection();
                conn.setConnectTimeout(timeout);
                conn.setReadTimeout(timeout);
                rawStream = conn.getInputStream();
                reader = buildReader(rawStream, encoding);

                if (reader != null)
                {
                    log.debug("URLResourceLoader: Found '{}' at '{}'", name, root);

                    // save this root for later re-use
                    templateRoots.put(name, root);
                    break;
                }
            }
            catch (IOException ioe)
            {
                if (rawStream != null)
                {
                    try
                    {
                        rawStream.close();
                    }
                    catch (IOException e)
                    {
                    }
                }
                log.debug("URLResourceLoader: Exception when looking for '{}' at '{}'", name, root, ioe);

                // only save the first one for later throwing
                if (exception == null)
                {
                    exception = ioe;
                }
            }
        }

        // if we never found the template
        if (reader == null)
        {
            String msg;
            if (exception == null)
            {
                msg = "URLResourceLoader: Resource '" + name + "' not found.";
            }
            else
            {
                msg = exception.getMessage();
            }
            // convert to a general Velocity ResourceNotFoundException
            throw new ResourceNotFoundException(msg);
        }

        return reader;
    }