Skip to content

Add VelocityLanguageDriverConfig #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2019
Merged
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
<artifactId>commons-text</artifactId>
<version>1.6</version>
</dependency>

<!-- TEST -->
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/org/mybatis/scripting/velocity/Driver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
Expand All @@ -26,6 +26,24 @@

public class Driver implements LanguageDriver {

/**
* Default constructor.
*/
public Driver() {
this(VelocityLanguageDriverConfig.newInstance());
}

/**
* Constructor.
*
* @param driverConfig
* a language driver configuration
* @since 2.1.0
*/
public Driver(VelocityLanguageDriverConfig driverConfig) {
VelocityFacade.initialize(driverConfig);
}

@Override
public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
BoundSql boundSql) {
Expand Down
102 changes: 38 additions & 64 deletions src/main/java/org/mybatis/scripting/velocity/VelocityFacade.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
Expand All @@ -17,41 +17,59 @@

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.scripting.ScriptingException;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.RuntimeInstance;
import org.apache.velocity.runtime.parser.node.SimpleNode;

public class VelocityFacade {

private static final String ADDITIONAL_CTX_ATTRIBUTES_KEY = "additional.context.attributes";
private static final String EXTERNAL_PROPERTIES = "mybatis-velocity.properties";
private static final String DIRECTIVES = TrimDirective.class.getName() + "," + WhereDirective.class.getName() + ","
+ SetDirective.class.getName() + "," + InDirective.class.getName() + "," + RepeatDirective.class.getName();
private static final RuntimeInstance engine = new RuntimeInstance();
private static final Map<String, Object> additionalCtxAttributes = new HashMap<>();

private static final RuntimeInstance engine;

/** Contains thread safe objects to be set in the velocity context. */
private static final Map<String, Object> additionalCtxAttributes;
private static final Properties settings;

static {
private VelocityFacade() {
// Prevent instantiation
}

settings = loadProperties();
additionalCtxAttributes = Collections.unmodifiableMap(loadAdditionalCtxAttributes());
engine = new RuntimeInstance();
engine.init(settings);
/**
* Initialize a template engine.
*
* @param driverConfig
* a language driver configuration
* @since 2.1.0
*/
public static void initialize(VelocityLanguageDriverConfig driverConfig) {
Properties properties = new Properties();
driverConfig.getVelocitySettings().forEach(properties::setProperty);
properties.setProperty(RuntimeConstants.CUSTOM_DIRECTIVES, driverConfig.generateCustomDirectivesString());
engine.init(properties);
additionalCtxAttributes.putAll(driverConfig.getAdditionalContextAttributes().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, v -> {
try {
return Resources.classForName(v.getValue()).getConstructor().newInstance();
} catch (Exception e) {
throw new ScriptingException("Cannot load additional context attribute class.", e);
}
})));
}

private VelocityFacade() {
// Prevent instantiation
/**
* Destroy a template engine.
*
* @since 2.1.0
*/
public static void destroy() {
engine.reset();
additionalCtxAttributes.clear();
}

public static Object compile(String script, String name) {
Expand All @@ -76,48 +94,4 @@ public static String apply(Object template, Map<String, Object> context) {
return out.toString();
}

private static Properties loadProperties() {
final Properties props = new Properties();
// Defaults
props.setProperty("resource.loader", "class");
props.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

try {
// External properties
ClassLoader cl = Thread.currentThread().getContextClassLoader();
props.load(cl.getResourceAsStream(EXTERNAL_PROPERTIES));
} catch (Exception ex) {
// No custom properties
}

// Append the user defined directives if provided
String userDirective = StringUtils.trim(props.getProperty("userdirective"));
if (userDirective == null) {
userDirective = DIRECTIVES;
} else {
userDirective += "," + DIRECTIVES;
}
props.setProperty("userdirective", userDirective);
return props;
}

private static Map<String, Object> loadAdditionalCtxAttributes() {
Map<String, Object> attributes = new HashMap<>();
String additionalContextAttributes = settings.getProperty(ADDITIONAL_CTX_ATTRIBUTES_KEY);
if (additionalContextAttributes == null) {
return attributes;
}

try {
String[] entries = additionalContextAttributes.split(",");
for (String str : entries) {
String[] entry = str.trim().split(":");
attributes.put(entry[0].trim(), Class.forName(entry[1].trim()).newInstance());
}
} catch (Exception ex) {
throw new BuilderException("Error parsing velocity property '" + ADDITIONAL_CTX_ATTRIBUTES_KEY + "'", ex);
}
return attributes;
}
}
Loading