Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<module>velocity-engine-scripting</module>
<module>velocity-custom-parser-example</module>
<module>spring-velocity-support</module>
<module>spring6-velocity-support</module>
</modules>

<!-- This project is an effort by many people. If you feel that your name
Expand Down
4 changes: 2 additions & 2 deletions spring-velocity-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<version>2.5-SNAPSHOT</version>
</parent>
<artifactId>spring-velocity-support</artifactId>
<name>Spring framework Velocity support</name>
<description>Velocity Engine factory bean for Spring framework</description>
<name>Spring 5 framework Velocity support</name>
<description>Velocity Engine factory bean for Spring 5 framework</description>
<dependencyManagement>
<dependencies>
<dependency>
Expand Down
24 changes: 24 additions & 0 deletions spring6-velocity-support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Title: Apache Velocity Spring 6 Support

# Apache Velocity Spring 6 Support

This module is the Spring 6.x variant of the engine support initially hosted by the Spring project in its 4.x versions. It targets Java 17+.

For Spring 5.x integration on Java 8, use the sibling `spring-velocity-support` module instead.

Example configuration:

```xml
<bean id="velocityEngine"
class="org.apache.velocity.spring.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loaders">classpath</prop>
<prop key="resource.loader.classpath.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
```

108 changes: 108 additions & 0 deletions spring6-velocity-support/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-parent</artifactId>
<version>2.5-SNAPSHOT</version>
</parent>
<artifactId>spring6-velocity-support</artifactId>
<name>Spring 6 framework Velocity support</name>
<description>Velocity Engine factory bean for Spring 6 framework</description>
<properties>
<javaVersion>17</javaVersion>
<spring.version>6.2.12</spring.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${spring.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemProperties>
<property>
<name>test.resources.dir</name>
<value>${project.build.testOutputDirectory}</value>
</property>
</systemProperties>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<skip>false</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.velocity.spring;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;

import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

import org.apache.velocity.util.ExtProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
* Velocity ResourceLoader adapter that loads via a Spring ResourceLoader.
* Used by VelocityEngineFactory for any resource loader path that cannot
* be resolved to a {@code java.io.File}.
*
* <p>Note that this loader does not allow for modification detection:
* Use Velocity's default FileResourceLoader for {@code java.io.File}
* resources.
*
* <p>Expects "resource.loader.spring.instance" and "resource.loader.spring.path"
* application attributes in the Velocity runtime: the former of type
* {@code org.springframework.core.io.ResourceLoader}, the latter a String.
*
* @author Juergen Hoeller
* @author Claude Brisson
* @since 2020-05-29
* @see VelocityEngineFactory#setResourceLoaderPath
* @see org.springframework.core.io.ResourceLoader
* @see org.apache.velocity.runtime.resource.loader.FileResourceLoader
*/
public class SpringResourceLoader extends ResourceLoader {

public static final String NAME = "spring";

public static final String SPRING_RESOURCE_LOADER_CLASS = "resource.loader.spring.class";

public static final String SPRING_RESOURCE_LOADER_CACHE = "resource.loader.spring.cache";

public static final String SPRING_RESOURCE_LOADER_INSTANCE = "resource.loader.spring.instance";

public static final String SPRING_RESOURCE_LOADER_PATH = "resource.loader.spring.path";

// Deprecated constants
public static final String DEPRECATED_SPRING_RESOURCE_LOADER_INSTANCE = "spring.resource.loader";
public static final String DEPRECATED_SPRING_RESOURCE_LOADER_PATH = "spring.resource.loader.path";

protected final Logger logger = LoggerFactory.getLogger(getClass());

private org.springframework.core.io.ResourceLoader resourceLoader;

private String[] resourceLoaderPaths;


@Override
public void init(ExtProperties configuration) {
this.resourceLoader = (org.springframework.core.io.ResourceLoader)
this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_INSTANCE);
if (this.resourceLoader == null) {
this.resourceLoader = (org.springframework.core.io.ResourceLoader)
this.rsvc.getApplicationAttribute(DEPRECATED_SPRING_RESOURCE_LOADER_INSTANCE);
if (this.resourceLoader != null) {
logger.warn("Deprecated property: 'spring.resource.loader'. Please use 'resource.loader.spring.instance' instead.");
}
}
String resourceLoaderPath = (String) this.rsvc.getApplicationAttribute(SPRING_RESOURCE_LOADER_PATH);
if (resourceLoaderPath == null) {
resourceLoaderPath = (String) this.rsvc.getApplicationAttribute(DEPRECATED_SPRING_RESOURCE_LOADER_PATH);
if (resourceLoaderPath != null) {
logger.warn("Deprecated property: 'spring.resource.loader.path'. Please use 'resource.loader.spring.path' instead.");
}
}
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 [{}] and resource loader paths {}",
resourceLoader, Arrays.asList(this.resourceLoaderPaths));
}
}

/**
* Get the Reader that the Runtime will parse
* to create a template.
*
* @param source resource name
* @param encoding resource encoding
* @return The reader for the requested resource.
* @throws ResourceNotFoundException
* @since 2.0
*/
@Override
public Reader getResourceReader(String source, String encoding) throws ResourceNotFoundException {
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 {
if (resource != null) {
return new InputStreamReader(resource.getInputStream(), encoding);
}
}
catch (IOException ex) {
logger.debug("Could not find Velocity resource: {}", resource);
}
}
throw new ResourceNotFoundException(
"Could not find resource [" + source + "] in Spring resource loader path");
}

@Override
public boolean isSourceModified(Resource resource) {
return false;
}

@Override
public long getLastModified(Resource resource) {
return 0;
}

}
Loading