cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject [cayenne] branch master updated: Cgen - optionally allow user-defined tools besides ImportUtils for working with velocity templates.
Date Tue, 11 Jun 2019 16:11:47 GMT
This is an automated email from the ASF dual-hosted git repository.

johnthuss 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 9e489dd  Cgen - optionally allow user-defined tools besides ImportUtils for working
with velocity templates.
9e489dd is described below

commit 9e489dded596ee10e1ef32ba8124c06d456bc212
Author: John Huss <johnthuss@apache.org>
AuthorDate: Fri May 31 16:59:17 2019 -0500

    Cgen - optionally allow user-defined tools besides ImportUtils for working with velocity
templates.
    
    This restores an ability that was present before upgrading to velocity 2.0.
---
 cayenne-cgen/pom.xml                               |  6 ++++++
 .../main/java/org/apache/cayenne/gen/Artifact.java |  4 ++--
 .../apache/cayenne/gen/ClassGenerationAction.java  | 24 +++++++++++++++++++---
 .../org/apache/cayenne/gen/DataMapArtifact.java    |  4 ++--
 .../org/apache/cayenne/gen/EmbeddableArtifact.java |  4 ++--
 .../org/apache/cayenne/gen/EntityArtifact.java     |  4 ++--
 pom.xml                                            |  5 +++++
 7 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/cayenne-cgen/pom.xml b/cayenne-cgen/pom.xml
index a332842..718a76f 100644
--- a/cayenne-cgen/pom.xml
+++ b/cayenne-cgen/pom.xml
@@ -55,6 +55,12 @@
         </dependency>
 
         <dependency>
+            <groupId>org.apache.velocity.tools</groupId>
+            <artifactId>velocity-tools-generic</artifactId>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.cayenne</groupId>
             <artifactId>cayenne-server</artifactId>
             <version>${project.version}</version>
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/Artifact.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/Artifact.java
index b04c9a3..f3b57d8 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/Artifact.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/Artifact.java
@@ -18,7 +18,7 @@
  ****************************************************************/
 package org.apache.cayenne.gen;
 
-import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
 
 /**
  * Represents a class generation "artifact" which is a facade to a metadata object used
@@ -63,5 +63,5 @@ public interface Artifact {
      * after the context is initialized by code generator, so this method can use
      * predefined keys from the context.
      */
-    void postInitContext(VelocityContext context);
+    void postInitContext(Context context);
 }
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 1363685..89f7660 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
@@ -34,12 +34,15 @@ import java.util.Properties;
 import java.util.stream.Collectors;
 
 import org.apache.cayenne.CayenneRuntimeException;
+import org.apache.cayenne.gen.ImportUtils;
 import org.apache.cayenne.map.Embeddable;
 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.VelocityEngine;
+import org.apache.velocity.context.Context;
+import org.apache.velocity.tools.ToolManager;
 import org.slf4j.Logger;
 
 public class ClassGenerationAction {
@@ -68,13 +71,28 @@ public class ClassGenerationAction {
 	protected Logger logger;
 
     // runtime ivars
-    protected VelocityContext context;
+    protected Context context;
     protected Map<String, Template> templateCache;
 
     private ToolsUtilsFactory utilsFactory;
 
+	/**
+	Optionally allows user-defined tools besides {@link ImportUtils} for working with velocity
templates.<br/>
+	To use this feature, set the java system property {@code -Dorg.apache.velocity.tools=tools.properties}
+	And create the file "tools.properties" in the working directory or in the 
+	root of the classpath with content like this: 
+	<pre>
+	tools.toolbox = application
+	tools.application.myTool = com.mycompany.MyTool</pre>
+	Then the methods in the MyTool class will be available for use in the template like ${myTool.myMethod(arg)}
+	 */
 	public ClassGenerationAction() {
-		this.context = new VelocityContext();
+		if (System.getProperty("org.apache.velocity.tools") != null) {
+			ToolManager manager = new ToolManager(true, true);
+			this.context = manager.createContext();
+		} else {
+			this.context = new VelocityContext();
+		}
 		this.templateCache = new HashMap<>(5);
 	}
 
@@ -467,7 +485,7 @@ public class ClassGenerationAction {
 	 * Sets an optional shared VelocityContext. Useful with tools like VPP that
 	 * can set custom values in the context, not known to Cayenne.
 	 */
-	public void setContext(VelocityContext context) {
+	public void setContext(Context context) {
 		this.context = context;
 	}
 
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DataMapArtifact.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DataMapArtifact.java
index 46fcaa6..119b18b 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DataMapArtifact.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DataMapArtifact.java
@@ -22,7 +22,7 @@ package org.apache.cayenne.gen;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.QueryDescriptor;
 import org.apache.cayenne.util.Util;
-import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
 
 import java.util.Collection;
 import java.util.LinkedList;
@@ -62,7 +62,7 @@ public class DataMapArtifact implements Artifact {
         return this;
     }
 
-    public void postInitContext(VelocityContext context) {
+    public void postInitContext(Context context) {
         DataMapUtils dataMapUtils = new DataMapUtils();
         context.put(DATAMAP_UTILS_KEY, dataMapUtils);
     }
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EmbeddableArtifact.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EmbeddableArtifact.java
index 0dc24db..7d3308e 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EmbeddableArtifact.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EmbeddableArtifact.java
@@ -19,7 +19,7 @@
 package org.apache.cayenne.gen;
 
 import org.apache.cayenne.map.Embeddable;
-import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
 
 /**
  * {@link Artifact} facade for an {@link Embeddable}.
@@ -62,7 +62,7 @@ public class EmbeddableArtifact implements Artifact {
         }
     }
 
-    public void postInitContext(VelocityContext context) {
+    public void postInitContext(Context context) {
         // noop - no special keys...
     }
 }
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
index 241b5b3..5875d32 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/EntityArtifact.java
@@ -20,7 +20,7 @@ package org.apache.cayenne.gen;
 
 import org.apache.cayenne.BaseDataObject;
 import org.apache.cayenne.map.ObjEntity;
-import org.apache.velocity.VelocityContext;
+import org.apache.velocity.context.Context;
 
 /**
  * {@link Artifact} facade for an ObjEntity.
@@ -81,7 +81,7 @@ public class EntityArtifact implements Artifact {
         }
     }
 
-    public void postInitContext(VelocityContext context) {
+    public void postInitContext(Context context) {
         EntityUtils metadata = new EntityUtils(
                 entity.getDataMap(),
                 entity,
diff --git a/pom.xml b/pom.xml
index 7bf76fe..70e68e5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -406,6 +406,11 @@
 				<version>2.0</version>
 			</dependency>
 			<dependency>
+				<groupId>org.apache.velocity.tools</groupId>
+				<artifactId>velocity-tools-generic</artifactId>
+				<version>3.0</version>
+			</dependency>
+			<dependency>
 				<groupId>foundrylogic.vpp</groupId>
 				<artifactId>vpp</artifactId>
 				<version>2.2.1</version>


Mime
View raw message