@Override public void init(ExtendedProperties configuration) { this.resourceLoader = (org.springframework.core.io.ResourceLoader) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER); String resourceLoaderPath = (String) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH); if (this.resourceLoader == null) { throw new IllegalArgumentException( "'resourceLoader' application attribute must be present for SpringResourceLoader"); } if (resourceLoaderPath == null) { throw new IllegalArgumentException( "'resourceLoaderPath' application attribute must be present for SpringResourceLoader"); } this.resourceLoaderPaths = StringUtils.commaDelimitedListToStringArray(resourceLoaderPath); for (int i = 0; i < this.resourceLoaderPaths.length; i++) { String path = this.resourceLoaderPaths[i]; if (!path.endsWith("/")) { this.resourceLoaderPaths[i] = path + "/"; } } if (logger.isInfoEnabled()) { logger.info("SpringResourceLoader for Velocity: using resource loader [" + this.resourceLoader + "] and resource loader paths " + Arrays.asList(this.resourceLoaderPaths)); } }
private Template setupTemplate(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, InputStream templateContents) { try { Template template = new Template(); template.setRuntimeServices(runtimeServices); template.setResourceLoader(loader); template.setName(templateName); byte[] bytes = IOUtils.toByteArray(templateContents); templateContents.close(); when(loader.getResourceStream(templateName)).thenReturn(new ByteArrayInputStream(bytes)); doReturn(template).when(runtimeServices).getTemplate(templateName); doReturn(template).when(runtimeServices).getTemplate(eq(templateName), Matchers.<String>any()); return template; } catch (Exception e) { throw new RuntimeException(e); } }
@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; }
/** * Returns the first {@link ResourceLoader} in which the specified * resource exists. * * @param resourceName the resource name. * @return a resource loader for the given resource. */ @SuppressWarnings("unchecked") private ResourceLoader getLoaderForResource(final String resourceName) { for (final Iterator<ResourceLoader> i = resourceLoaders.iterator(); i.hasNext();) { final ResourceLoader loader = (ResourceLoader)i.next(); if (loader.resourceExists(resourceName)) { return loader; } } return null; }
public TestVelocityView(String templatePath, Map<String, Object> modelData) { this.templatePath = templatePath; this.modelData = modelData; this.additionalTemplates = new ArrayList<>(); loader = mock(ResourceLoader.class); runtimeServices = spy(new RuntimeInstance()); setupToolAttributes(); }
private void setupContentResource(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, String fakeContent) { try { ContentResource resource = new ContentResource(); resource.setRuntimeServices(runtimeServices); resource.setResourceLoader(loader); resource.setName(templateName); resource.setData(fakeContent); doReturn(resource).when(runtimeServices).getContent(templateName); doReturn(resource).when(runtimeServices).getContent(eq(templateName), Matchers.<String>any()); } catch (Exception e) { throw new RuntimeException(e); } }