This is an automated email from the ASF dual-hosted git repository. ntimofeev pushed a commit to branch STABLE-4.1 in repository https://gitbox.apache.org/repos/asf/cayenne.git
The following commit(s) were added to refs/heads/STABLE-4.1 by this push: new 0776e4b Fix bug with custom template in modeler new 82cddee Merge pull request #489 from Ivan-nikitko/STABLE-4.1_Fixing_bug_with_customs_template's_relative_path_in_modeler 0776e4b is described below commit 0776e4b11026d079a365b76222d3a6c68f28c2ab Author: Ivan-nikitko <70625960+ivan-niki...@users.noreply.github.com> AuthorDate: Sun Jan 23 21:45:58 2022 +0300 Fix bug with custom template in modeler --- .../apache/cayenne/gen/ClassGenerationAction.java | 4 ++- .../cayenne/gen/ClassGeneratorResourceLoader.java | 30 ++++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java index 1c2ec70..cd91a8f 100644 --- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java +++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGenerationAction.java @@ -39,6 +39,7 @@ import org.apache.cayenne.map.ObjEntity; import org.apache.cayenne.map.QueryDescriptor; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; +import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import org.slf4j.Logger; @@ -60,7 +61,7 @@ public class ClassGenerationAction { public static final String SUPERCLASS_PREFIX = "_"; private static final String WILDCARD = "*"; - + private static final String CGEN_ROOT_PATH = "cayenne.cgen.rootpath"; /** * @since 4.1 */ @@ -285,6 +286,7 @@ public class ClassGenerationAction { props.put("cayenne.resource.loader.cache", "false"); if (cgenConfiguration.getRootPath() != null) { props.put("cayenne.resource.loader.path", cgenConfiguration.getRootPath().toString()); + Velocity.setProperty(CGEN_ROOT_PATH, cgenConfiguration.getRootPath().toString()); } VelocityEngine velocityEngine = new VelocityEngine(); diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java index 44f5d19..dc55832 100644 --- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java +++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClassGeneratorResourceLoader.java @@ -26,7 +26,10 @@ import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.file.Path; +import java.nio.file.Paths; +import org.apache.velocity.app.Velocity; import org.apache.velocity.exception.ResourceNotFoundException; import org.apache.velocity.runtime.resource.loader.FileResourceLoader; @@ -40,6 +43,8 @@ import org.apache.velocity.runtime.resource.loader.FileResourceLoader; // instantiated via reflection by Velocity public class ClassGeneratorResourceLoader extends FileResourceLoader { + private static final String CGEN_ROOT_PATH = "cayenne.cgen.rootpath"; + /** * Returns resource as InputStream. First calls super implementation. If resource * wasn't found, it attempts to load it from current directory or as an absolute path. @@ -48,38 +53,41 @@ public class ClassGeneratorResourceLoader extends FileResourceLoader { public synchronized Reader getResourceReader(String name, String charset) throws ResourceNotFoundException { - Reader stream = loadFromRelativePath(name, charset); + Reader stream; + + stream = loadFromThreadClassLoader(name); if (stream != null) { return stream; } - stream = loadFromAbsPath(name); + stream = loadFromThisClassLoader(name); if (stream != null) { return stream; } - stream = loadFromThreadClassLoader(name); + stream = loadFromRelativePath(name); if (stream != null) { return stream; } - stream = loadFromThisClassLoader(name); + stream = loadFromAbsPath(name); if (stream != null) { return stream; } - throw new ResourceNotFoundException("Couldn't find resource '" + name + "'. Searched filesystem path and classpath"); } - protected Reader loadFromRelativePath(String name, String charset) { - try { - return super.getResourceReader(name, charset); - } - catch (ResourceNotFoundException rnfex) { - return null; + protected Reader loadFromRelativePath(String name) { + String rootPath = (String) Velocity.getProperty(CGEN_ROOT_PATH); + Path datamapPath; + if (rootPath != null) { + datamapPath = Paths.get(rootPath); + Path absolutePath = datamapPath.resolve(name).normalize(); + return loadFromAbsPath(absolutePath.toString()); } + return null; } protected Reader loadFromAbsPath(String name) {