Skip to content

Commit 9a1f0cc

Browse files
authored
Merge pull request #72 from kazuki43zoo/gh-66
Add VelocityLanguageDriverConfig
2 parents cd193e7 + 6c2cf1c commit 9a1f0cc

13 files changed

+692
-77
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
</dependency>
8080
<dependency>
8181
<groupId>org.apache.commons</groupId>
82-
<artifactId>commons-lang3</artifactId>
83-
<version>3.8.1</version>
82+
<artifactId>commons-text</artifactId>
83+
<version>1.6</version>
8484
</dependency>
8585

8686
<!-- TEST -->

src/main/java/org/mybatis/scripting/velocity/Driver.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,24 @@
2626

2727
public class Driver implements LanguageDriver {
2828

29+
/**
30+
* Default constructor.
31+
*/
32+
public Driver() {
33+
this(VelocityLanguageDriverConfig.newInstance());
34+
}
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @param driverConfig
40+
* a language driver configuration
41+
* @since 2.1.0
42+
*/
43+
public Driver(VelocityLanguageDriverConfig driverConfig) {
44+
VelocityFacade.initialize(driverConfig);
45+
}
46+
2947
@Override
3048
public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
3149
BoundSql boundSql) {
Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,41 +17,59 @@
1717

1818
import java.io.StringReader;
1919
import java.io.StringWriter;
20-
import java.util.Collections;
2120
import java.util.HashMap;
2221
import java.util.Map;
2322
import java.util.Properties;
23+
import java.util.stream.Collectors;
2424

25-
import org.apache.commons.lang3.StringUtils;
2625
import org.apache.ibatis.builder.BuilderException;
26+
import org.apache.ibatis.io.Resources;
27+
import org.apache.ibatis.scripting.ScriptingException;
2728
import org.apache.velocity.Template;
2829
import org.apache.velocity.VelocityContext;
30+
import org.apache.velocity.runtime.RuntimeConstants;
2931
import org.apache.velocity.runtime.RuntimeInstance;
3032
import org.apache.velocity.runtime.parser.node.SimpleNode;
3133

3234
public class VelocityFacade {
3335

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

39-
private static final RuntimeInstance engine;
40-
41-
/** Contains thread safe objects to be set in the velocity context. */
42-
private static final Map<String, Object> additionalCtxAttributes;
43-
private static final Properties settings;
44-
45-
static {
39+
private VelocityFacade() {
40+
// Prevent instantiation
41+
}
4642

47-
settings = loadProperties();
48-
additionalCtxAttributes = Collections.unmodifiableMap(loadAdditionalCtxAttributes());
49-
engine = new RuntimeInstance();
50-
engine.init(settings);
43+
/**
44+
* Initialize a template engine.
45+
*
46+
* @param driverConfig
47+
* a language driver configuration
48+
* @since 2.1.0
49+
*/
50+
public static void initialize(VelocityLanguageDriverConfig driverConfig) {
51+
Properties properties = new Properties();
52+
driverConfig.getVelocitySettings().forEach(properties::setProperty);
53+
properties.setProperty(RuntimeConstants.CUSTOM_DIRECTIVES, driverConfig.generateCustomDirectivesString());
54+
engine.init(properties);
55+
additionalCtxAttributes.putAll(driverConfig.getAdditionalContextAttributes().entrySet().stream()
56+
.collect(Collectors.toMap(Map.Entry::getKey, v -> {
57+
try {
58+
return Resources.classForName(v.getValue()).getConstructor().newInstance();
59+
} catch (Exception e) {
60+
throw new ScriptingException("Cannot load additional context attribute class.", e);
61+
}
62+
})));
5163
}
5264

53-
private VelocityFacade() {
54-
// Prevent instantiation
65+
/**
66+
* Destroy a template engine.
67+
*
68+
* @since 2.1.0
69+
*/
70+
public static void destroy() {
71+
engine.reset();
72+
additionalCtxAttributes.clear();
5573
}
5674

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

79-
private static Properties loadProperties() {
80-
final Properties props = new Properties();
81-
// Defaults
82-
props.setProperty("resource.loader", "class");
83-
props.setProperty("class.resource.loader.class",
84-
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
85-
86-
try {
87-
// External properties
88-
ClassLoader cl = Thread.currentThread().getContextClassLoader();
89-
props.load(cl.getResourceAsStream(EXTERNAL_PROPERTIES));
90-
} catch (Exception ex) {
91-
// No custom properties
92-
}
93-
94-
// Append the user defined directives if provided
95-
String userDirective = StringUtils.trim(props.getProperty("userdirective"));
96-
if (userDirective == null) {
97-
userDirective = DIRECTIVES;
98-
} else {
99-
userDirective += "," + DIRECTIVES;
100-
}
101-
props.setProperty("userdirective", userDirective);
102-
return props;
103-
}
104-
105-
private static Map<String, Object> loadAdditionalCtxAttributes() {
106-
Map<String, Object> attributes = new HashMap<>();
107-
String additionalContextAttributes = settings.getProperty(ADDITIONAL_CTX_ATTRIBUTES_KEY);
108-
if (additionalContextAttributes == null) {
109-
return attributes;
110-
}
111-
112-
try {
113-
String[] entries = additionalContextAttributes.split(",");
114-
for (String str : entries) {
115-
String[] entry = str.trim().split(":");
116-
attributes.put(entry[0].trim(), Class.forName(entry[1].trim()).newInstance());
117-
}
118-
} catch (Exception ex) {
119-
throw new BuilderException("Error parsing velocity property '" + ADDITIONAL_CTX_ATTRIBUTES_KEY + "'", ex);
120-
}
121-
return attributes;
122-
}
12397
}

0 commit comments

Comments
 (0)