Java Code Examples for org.apache.velocity.runtime.resource.Resource#getName()

The following examples show how to use org.apache.velocity.runtime.resource.Resource#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LibraryWebappLoader.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Checks to see if a resource has been deleted, moved or modified.
 *
 * @param resource Resource The resource to check for modification
 * @return boolean True if the resource has been modified
 */
@Override
public boolean isSourceModified(Resource resource) {

    // first, try getting the previously found file
    String fileName = resource.getName();
    Date fileLastLoaded = getCachedFileLastLoaded(fileName);
    if (fileLastLoaded == null) {
        return true;
    }

    if (new Date().getTime() - fileLastLoaded.getTime() > CACHE_EXPIRATION_IN_MILLIS) {
        return true;
    }

    return false;
}
 
Example 2
Source File: LibraryWebappLoader.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Checks to see if a resource has been deleted, moved or modified.
 *
 * @param resource Resource The resource to check for modification
 * @return boolean True if the resource has been modified
 */
@Override
public boolean isSourceModified(Resource resource) {

    // first, try getting the previously found file
    String fileName = resource.getName();
    Date fileLastLoaded = getCachedFileLastLoaded(fileName);
    if (fileLastLoaded == null) {
        return true;
    }

    if (new Date().getTime() - fileLastLoaded.getTime() > CACHE_EXPIRATION_IN_MILLIS) {
        return true;
    }

    return false;
}
 
Example 3
Source File: URLResourceLoader.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see when a resource was last modified
 *
 * @param resource Resource the resource to check
 * @return long The time when the resource was last modified or 0 if the file can't be reached
 */
public long getLastModified(Resource resource)
{
    // get the previously used root
    String name = resource.getName();
    String root = (String)templateRoots.get(name);

    try
    {
        // get a connection to the URL
        URL u = new URL(root + name);
        URLConnection conn = u.openConnection();
        conn.setConnectTimeout(timeout);
        conn.setReadTimeout(timeout);
        return conn.getLastModified();
    }
    catch (IOException ioe)
    {
        // the file is not reachable at its previous address
        String msg = "URLResourceLoader: '"+name+"' is no longer reachable at '"+root+"'";
        log.error(msg, ioe);
        throw new ResourceNotFoundException(msg, ioe, rsvc.getLogContext().getStackTrace());
    }
}
 
Example 4
Source File: LibraryWebappLoader.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Checks to see when a resource was last modified
 *
 * @param resource Resource the resource to check
 * @return long The time when the resource was last modified or 0 if the
 *         file can't be read
 */
@Override
public long getLastModified(Resource resource) {
    String fileName = resource.getName();
    Date fileLastLoaded = getCachedFileLastLoaded(fileName);
    if (fileLastLoaded == null) {
        return 0;
    }
    return fileLastLoaded.getTime();

}
 
Example 5
Source File: AliadaResourceManager.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected Resource refreshResource(final Resource resource, final String encoding) {
    resource.touch();

    final ResourceLoader loader = resource.getResourceLoader();
    if (resourceLoaders.size() > 0 && resourceLoaders.indexOf(loader) > 0) {
        final String name = resource.getName();
        if (loader != getLoaderForResource(name)) {
            return loadResource(name, resource.getType(), encoding);
        }
    }

    if (resource.isSourceModified()) {
        if (!StringUtils.equals(resource.getEncoding(), encoding)) {
            resource.setEncoding(encoding);
        }
        
        final String resourceName = resource.getName();
        final Resource newResource = AliadaResourceFactory.getResource(resourceName, resource.getType());

        newResource.setRuntimeServices(rsvc);
        newResource.setName(resourceName);
        newResource.setEncoding(resource.getEncoding());
        newResource.setResourceLoader(loader);
        newResource.setModificationCheckInterval(loader.getModificationCheckInterval());

        newResource.process();
        newResource.setLastModified(loader.getLastModified(resource));

        globalCache.put(
        		new StringBuilder(resource.getType())
        			.append(resource.getName())
        			.toString(), 
        		newResource);
        return newResource;
    }
    return resource;
}
 
Example 6
Source File: LibraryWebappLoader.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * Checks to see when a resource was last modified
 *
 * @param resource Resource the resource to check
 * @return long The time when the resource was last modified or 0 if the
 *         file can't be read
 */
@Override
public long getLastModified(Resource resource) {
    String fileName = resource.getName();
    Date fileLastLoaded = getCachedFileLastLoaded(fileName);
    if (fileLastLoaded == null) {
        return 0;
    }
    return fileLastLoaded.getTime();

}
 
Example 7
Source File: LocalizedTemplateResourceLoaderImpl.java    From gazpachoquest with GNU General Public License v3.0 5 votes vote down vote up
private long readLastModified(final Resource resource, final String operation) {
    String templateName = resource.getName();
    Integer templateId = readTemplateId(templateName);
    logger.info("{} from mail message template {} ", operation, templateId);
    MailMessageTemplate template = templateRepository.findOne(templateId);
    return template.getLastModifiedDate().toEpochSecond(ZoneOffset.UTC);
}
 
Example 8
Source File: FileResourceLoader.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * How to keep track of all the modified times
 * across the paths.  Note that a file might have
 * appeared in a directory which is earlier in the
 * path; so we should search the path and see if
 * the file we find that way is the same as the one
 * that we have cached.
 * @param resource
 * @return True if the source has been modified.
 */
public boolean isSourceModified(Resource resource)
{
    /*
     * we assume that the file needs to be reloaded;
     * if we find the original file and it's unchanged,
     * then we'll flip this.
     */
    boolean modified = true;

    String fileName = resource.getName();
    String path = (String) templatePaths.get(fileName);
    File currentFile = null;

    for (int i = 0; currentFile == null && i < paths.size(); i++)
    {
        String testPath = (String) paths.get(i);
        File testFile = getFile(testPath, fileName);
        if (testFile.canRead())
        {
            currentFile = testFile;
        }
    }
    File file = getFile(path, fileName);
    if (currentFile == null || !file.exists())
    {
        /*
         * noop: if the file is missing now (either the cached
         * file is gone, or the file can no longer be found)
         * then we leave modified alone (it's set to true); a
         * reload attempt will be done, which will either use
         * a new template or fail with an appropriate message
         * about how the file couldn't be found.
         */
    }
    else if (currentFile.equals(file) && file.canRead())
    {
        /*
         * if only if currentFile is the same as file and
         * file.lastModified() is the same as
         * resource.getLastModified(), then we should use the
         * cached version.
         */
        modified = (file.lastModified() != resource.getLastModified());
    }

    /*
     * rsvc.debug("isSourceModified for " + fileName + ": " + modified);
     */
    return modified;
}