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 d6beb87  CAY-2729 minor code cleanup
d6beb87 is described below

commit d6beb87eacd9ef126c2ec6a1a89fb3b23dfb5912
Author: Nikita Timofeev <stari...@gmail.com>
AuthorDate: Tue Jan 25 17:22:57 2022 +0300

    CAY-2729 minor code cleanup
---
 .../apache/cayenne/gen/ClassGenerationAction.java  |  3 +--
 .../cayenne/gen/ClassGeneratorResourceLoader.java  | 28 +++++++++++++++-------
 .../apache/cayenne/gen/TemplateLocationTest.java   | 20 ++++++++++++++++
 3 files changed, 40 insertions(+), 11 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 6429f9d..cebaab1 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
@@ -315,8 +315,7 @@ public class ClassGenerationAction {
                        props.put("resource.loader.cayenne.class", 
ClassGeneratorResourceLoader.class.getName());
                        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());
+                               props.put("resource.loader.cayenne.root", 
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 58cc7f4..50c8d59 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
@@ -29,9 +29,9 @@ 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;
+import org.apache.velocity.util.ExtProperties;
 
 /**
  * Velocity template resource loader customized for Cayenne use. Supports 
loading
@@ -43,11 +43,24 @@ 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";
+    private Path root;
+
+    @Override
+    public void init(ExtProperties configuration) {
+        String rootPathStr = configuration.getString("root");
+        if(rootPathStr != null) {
+            root = Paths.get(rootPathStr);
+        }
+    }
 
     /**
-     * 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.
+     * Returns resource as InputStream. Searches resource in a following 
places:
+     * <ol>
+     *     <li>thread class loader</li>
+     *     <li>this class's class loader</li>
+     *     <li>tries a path relative to a <i>root</i> path, if set</li>
+     *     <li>an absolute path</li>
+     * </ol>
      */
     @Override
     public synchronized Reader getResourceReader(String name, String charset)
@@ -80,11 +93,8 @@ public class ClassGeneratorResourceLoader extends 
FileResourceLoader {
     }
 
     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();
+        if (root != null) {
+            Path absolutePath = root.resolve(name).normalize();
             return loadFromAbsPath(absolutePath.toString());
         }
         return null;
diff --git 
a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/TemplateLocationTest.java 
b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/TemplateLocationTest.java
index 40d6df8..18ab896 100644
--- 
a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/TemplateLocationTest.java
+++ 
b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/TemplateLocationTest.java
@@ -1,3 +1,22 @@
+/*****************************************************************
+ *   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
+ *
+ *    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.cayenne.gen;
 
 import org.junit.Before;
@@ -13,6 +32,7 @@ public class TemplateLocationTest {
 
     @Rule
     public TemporaryFolder tempFolder = new TemporaryFolder();
+
     private CgenConfiguration cgenConfiguration;
     private ClassGenerationAction action;
     private TemplateType templateType;

Reply via email to