You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2022/01/24 10:52:18 UTC

[cayenne] branch master updated: Fix bug with customs template's relative path in modeler

This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/master by this push:
     new 1552962  Fix bug with customs template's relative path in modeler
     new 9aab299  Merge pull request #487 from Ivan-nikitko/Fixing_bug_with_customs_template's_relative_path_in_modeler
1552962 is described below

commit 15529628ce27eff84888e95b14f8f882386573cc
Author: ivannikitka <ni...@gmail.com>
AuthorDate: Fri Jan 21 14:41:20 2022 +0300

    Fix bug with customs template's relative path in modeler
---
 .../apache/cayenne/gen/ClassGenerationAction.java  |  8 ++---
 .../cayenne/gen/ClassGeneratorResourceLoader.java  | 39 +++++++++++++---------
 2 files changed, 27 insertions(+), 20 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 caeaaee..5b10cdb 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.apache.velocity.context.Context;
 import org.apache.velocity.tools.ToolManager;
@@ -64,6 +65,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
@@ -134,23 +136,20 @@ public class ClassGenerationAction {
 	public String customTemplateName(TemplateType type) {
 		switch (type) {
 			case ENTITY_SINGLE_CLASS:
-				return cgenConfiguration.getTemplate();
 			case ENTITY_SUBCLASS:
 				return cgenConfiguration.getTemplate();
 			case ENTITY_SUPERCLASS:
 				return cgenConfiguration.getSuperTemplate();
 			case EMBEDDABLE_SINGLE_CLASS:
-				return cgenConfiguration.getEmbeddableTemplate();
 			case EMBEDDABLE_SUBCLASS:
 				return cgenConfiguration.getEmbeddableTemplate();
 			case EMBEDDABLE_SUPERCLASS:
 				return cgenConfiguration.getEmbeddableSuperTemplate();
 			case DATAMAP_SINGLE_CLASS:
+			case DATAMAP_SUBCLASS:
 				return cgenConfiguration.getQueryTemplate();
 			case DATAMAP_SUPERCLASS:
 				return cgenConfiguration.getQuerySuperTemplate();
-			case DATAMAP_SUBCLASS:
-				return cgenConfiguration.getQueryTemplate();
 			default:
 				throw new IllegalArgumentException("Invalid template type: " + type);
 		}
@@ -317,6 +316,7 @@ public class ClassGenerationAction {
 			props.put("resource.loader.cayenne.cache", "false");
 			if (cgenConfiguration.getRootPath() != null) {
 				props.put("resource.loader.cayenne.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..58cc7f4 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,20 +26,25 @@ 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;
 
 /**
  * Velocity template resource loader customized for Cayenne use. Supports loading
  * templates from the thread ClassLoader and from relative and absolute paths.
- * 
+ *
  * @since 1.2
  */
 // must be public top-level class as it is
 // 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) {
@@ -88,19 +96,18 @@ public class ClassGeneratorResourceLoader extends FileResourceLoader {
             return (file.canRead()) ? new BufferedReader(new FileReader(file
                     .getAbsolutePath())) : null;
 
-        }
-        catch (FileNotFoundException fnfe) {
+        } catch (FileNotFoundException fnfe) {
             return null;
         }
     }
 
     protected Reader loadFromThreadClassLoader(String name) {
-    	InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
+        InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
         return stream != null ? new InputStreamReader(stream) : null;
     }
 
     protected Reader loadFromThisClassLoader(String name) {
-    	InputStream stream = getClass().getClassLoader().getResourceAsStream(name);
+        InputStream stream = getClass().getClassLoader().getResourceAsStream(name);
         return stream != null ? new InputStreamReader(stream) : null;
     }
 }