org.apache.velocity.runtime.resource.Resource Java Examples

The following examples show how to use org.apache.velocity.runtime.resource.Resource. 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: SpringResourceLoader.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for Velocity resource with name [" + source + "]");
	}
	for (String resourceLoaderPath : this.resourceLoaderPaths) {
		org.springframework.core.io.Resource resource =
				this.resourceLoader.getResource(resourceLoaderPath + source);
		try {
			return resource.getInputStream();
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not find Velocity resource: " + resource);
			}
		}
	}
	throw new ResourceNotFoundException(
			"Could not find resource [" + source + "] in Spring resource loader path");
}
 
Example #2
Source File: RegistryBasedResourceLoader.java    From carbon-device-mgt with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getResourceStream(String name) throws ResourceNotFoundException {
    try {
        Registry registry =
                CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_CONFIGURATION);
        if (registry == null) {
            throw new IllegalStateException("No valid registry instance is attached to the current carbon context");
        }
        if (!registry.resourceExists(EMAIL_CONFIG_BASE_LOCATION + "/" + name)) {
            throw new ResourceNotFoundException("Resource '" + name + "' does not exist");
        }
        org.wso2.carbon.registry.api.Resource resource =
                registry.get(EMAIL_CONFIG_BASE_LOCATION + "/" + name);
        resource.setMediaType("text/plain");
        return resource.getContentStream();
    } catch (RegistryException e) {
        throw new ResourceNotFoundException("Error occurred while retrieving resource", e);
    }
}
 
Example #3
Source File: WebappResourceLoader.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 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 read
 */
public long getLastModified(Resource resource) {
    String rootPath = servletContext.getRealPath("/");
    if (rootPath == null) {
        // rootPath is null if the servlet container cannot translate the
        // virtual path to a real path for any reason (such as when the
        // content is being made available from a .war archive)
        return 0;
    }

    File cachedFile = getCachedFile(rootPath, resource.getName());
    if (cachedFile.canRead()) {
        return cachedFile.lastModified();
    } else {
        return 0;
    }
}
 
Example #4
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 #5
Source File: SpringResourceLoader.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for Velocity resource with name [" + source + "]");
	}
	for (String resourceLoaderPath : this.resourceLoaderPaths) {
		org.springframework.core.io.Resource resource =
				this.resourceLoader.getResource(resourceLoaderPath + source);
		try {
			return resource.getInputStream();
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not find Velocity resource: " + resource);
			}
		}
	}
	throw new ResourceNotFoundException(
			"Could not find resource [" + source + "] in Spring resource loader path");
}
 
Example #6
Source File: SpringResourceLoader.java    From MaxKey with Apache License 2.0 6 votes vote down vote up
@Override
public InputStream getResourceStream(String source) throws ResourceNotFoundException {
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for Velocity resource with name [" + source + "]");
	}
	for (String resourceLoaderPath : this.resourceLoaderPaths) {
		org.springframework.core.io.Resource resource =
				this.resourceLoader.getResource(resourceLoaderPath + source);
		try {
			return resource.getInputStream();
		}
		catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not find Velocity resource: " + resource);
			}
		}
	}
	throw new ResourceNotFoundException(
			"Could not find resource [" + source + "] in Spring resource loader path");
}
 
Example #7
Source File: SpringResourceLoader.java    From scoold with Apache License 2.0 6 votes vote down vote up
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
	if (logger.isDebugEnabled()) {
		logger.debug("Looking for Velocity resource with name [" + source + "]");
	}
	for (String resourceLoaderPath : this.resourceLoaderPaths) {
		org.springframework.core.io.Resource resource =
				this.resourceLoader.getResource(resourceLoaderPath + source);
		try {
			return new InputStreamReader(resource.getInputStream());
		} catch (IOException ex) {
			if (logger.isDebugEnabled()) {
				logger.debug("Could not find Velocity resource: " + resource);
			}
		}
	}
	throw new ResourceNotFoundException(
			"Could not find resource [" + source + "] in Spring resource loader path");
}
 
Example #8
Source File: WebappResourceLoader.java    From velocity-tools 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 read
 */
public long getLastModified(Resource resource)
{
    String rootPath = servletContext.getRealPath("/");
    if (rootPath == null) {
        // rootPath is null if the servlet container cannot translate the
        // virtual path to a real path for any reason (such as when the
        // content is being made available from a .war archive)
        return 0;
    }

    File cachedFile = getCachedFile(rootPath, resource.getName());
    if (cachedFile.canRead())
    {
        return cachedFile.lastModified();
    }
    else
    {
        return 0;
    }
}
 
Example #9
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 #10
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 #11
Source File: URLResourceLoader.java    From velocity-engine with Apache License 2.0 5 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, moved, or unreachable
 */
public boolean isSourceModified(Resource resource)
{
    long fileLastModified = getLastModified(resource);
    // if the file is unreachable or otherwise changed
    return fileLastModified == 0 ||
        fileLastModified != resource.getLastModified();
}
 
Example #12
Source File: StringResourceLoader.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @param resource
 * @return last modified timestamp
 * @see ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
 */
public long getLastModified(final Resource resource)
{
    StringResource original = null;

    original = this.repository.getStringResource(resource.getName());

    return (original != null)
            ? original.getLastModified()
            : 0;
}
 
Example #13
Source File: StringResourceLoader.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @param resource
 * @return whether resource was modified
 * @see ResourceLoader#isSourceModified(org.apache.velocity.runtime.resource.Resource)
 */
public boolean isSourceModified(final Resource resource)
{
    StringResource original = null;
    boolean result = true;

    original = this.repository.getStringResource(resource.getName());

    if (original != null)
    {
        result =  original.getLastModified() != resource.getLastModified();
    }

    return result;
}
 
Example #14
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 #15
Source File: FileResourceLoader.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * @see ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
 */
public long getLastModified(Resource resource)
{
    String path = (String) templatePaths.get(resource.getName());
    File file = getFile(path, resource.getName());

    if (file.canRead())
    {
        return file.lastModified();
    }
    else
    {
        return 0;
    }
}
 
Example #16
Source File: AliadaResourceFactory.java    From aliada-tool with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns a resource according with given data.
 * 
 * @param resourceName the resource name.
 * @param resourceType the resource kind.
 * @return The resource described by name and type.
 */
public static Resource getResource(final String resourceName, final int resourceType) {
    switch (resourceType) {
        case ResourceManager.RESOURCE_TEMPLATE:
            return new AliadaTemplate();
        case ResourceManager.RESOURCE_CONTENT:
            return new ContentResource();
        default:
        	throw new IllegalArgumentException("WARNIGN: Unknown resource type: " + resourceType);
    }
}
 
Example #17
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 #18
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 #19
Source File: InputBase.java    From velocity-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Decides the encoding used during input processing of this
 * directive.
 *
 * Get the resource, and assume that we use the encoding of the
 * current template the 'current resource' can be
 * <code>null</code> if we are processing a stream....
 *
 * @param context The context to derive the default input encoding
 * from.
 * @return The encoding to use when processing this directive.
 */
protected String getInputEncoding(InternalContextAdapter context)
{
    Resource current = context.getCurrentResource();
    if (current != null)
    {
        return current.getEncoding();
    }
    else
    {
        return (String) rsvc.getProperty(RuntimeConstants.INPUT_ENCODING);
    }
}
 
Example #20
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 #21
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;
}
 
Example #22
Source File: JarResourceLoader.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
/**
 * @see ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource)
 */
public long getLastModified(Resource resource)
{
    return 0;
}
 
Example #23
Source File: VelocityWrapper.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public long getLastModified(Resource resource) {
  return 0L;
}
 
Example #24
Source File: LocalizedTemplateResourceLoaderImpl.java    From gazpachoquest with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean isSourceModified(final Resource resource) {
    return resource.getLastModified() != readLastModified(resource, "checking timestamp");
}
 
Example #25
Source File: VelocityWrapper.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isSourceModified(Resource resource) {
  return true;
}
 
Example #26
Source File: StringResourceLoader.java    From ApprovalTests.Java with Apache License 2.0 4 votes vote down vote up
public long getLastModified(Resource resource)
{
  return 0;
}
 
Example #27
Source File: StringResourceLoader.java    From ApprovalTests.Java with Apache License 2.0 4 votes vote down vote up
public boolean isSourceModified(Resource resource)
{
  return true;
}
 
Example #28
Source File: ServletContextLoader.java    From ApprovalTests.Java with Apache License 2.0 4 votes vote down vote up
/**
 * Defaults to return 0
 */
public long getLastModified(Resource resource)
{
  return 0;
}
 
Example #29
Source File: ServletContextLoader.java    From ApprovalTests.Java with Apache License 2.0 4 votes vote down vote up
/**
 * Defaults to return false.
 */
public boolean isSourceModified(Resource resource)
{
  return false;
}
 
Example #30
Source File: ExceptionGeneratingResourceLoader.java    From velocity-engine with Apache License 2.0 4 votes vote down vote up
public long getLastModified(Resource resource)
{
    return 0;
}