cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [cayenne] 01/05: Enable cgen task/mojo to specify path to an external velocity tool configuration file via CgenConfiguration parameter (externalToolConfig). Removed older method relying on system property manipulation.
Date Tue, 12 May 2020 10:48:36 GMT
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

commit 5e03fb6f8f04ab7bcff23b7a19fddaded721e6b6
Author: Faizel Dakri <faizel@me.com>
AuthorDate: Mon May 11 10:42:13 2020 -0500

    Enable cgen task/mojo to specify path to an external velocity tool configuration file
via CgenConfiguration parameter (externalToolConfig). Removed older method relying on system
property manipulation.
---
 .../apache/cayenne/tools/CayenneGeneratorTask.java | 16 ++++++++++++++-
 .../org/apache/cayenne/gen/CgenConfiguration.java  | 14 +++++++++++++
 .../apache/cayenne/gen/ClassGenerationAction.java  | 15 +++++++++++---
 .../cayenne/gen/ClientClassGenerationAction.java   |  4 ++--
 .../gen/DefaultClassGenerationActionFactory.java   |  5 ++---
 .../gen/mock/TestClassGenerationAction.java        |  3 +--
 .../apache/cayenne/tools/CayenneGeneratorMojo.java | 23 +++++++++-------------
 7 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
b/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
index d6411b0..121b287 100644
--- a/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
+++ b/cayenne-ant/src/main/java/org/apache/cayenne/tools/CayenneGeneratorTask.java
@@ -88,6 +88,12 @@ public class CayenneGeneratorTask extends CayenneTask {
      */
     protected Boolean createpkproperties;
 
+    /**
+     * Optional path (classpath or filesystem) to external velocity tool configuration file
+     * @since 4.2 
+     */
+    protected String externaltoolconfig;
+
     public CayenneGeneratorTask() {
     }
 
@@ -158,7 +164,7 @@ public class CayenneGeneratorTask extends CayenneTask {
                 makepairs != null || mode != null || outputPattern != null || overwrite !=
null || superpkg != null ||
                 supertemplate != null || template != null || embeddabletemplate != null ||
embeddablesupertemplate != null ||
                 usepkgpath != null || createpropertynames != null || querytemplate != null
||
-                querysupertemplate != null || createpkproperties != null || force;
+                querysupertemplate != null || createpkproperties != null || force || externaltoolconfig
!= null;
     }
 
     private CgenConfiguration buildConfiguration(DataMap dataMap) {
@@ -200,6 +206,7 @@ public class CayenneGeneratorTask extends CayenneTask {
         cgenConfiguration.setQueryTemplate(querytemplate != null ? querytemplate : cgenConfiguration.getQueryTemplate());
         cgenConfiguration.setQuerySuperTemplate(querysupertemplate != null ? querysupertemplate
: cgenConfiguration.getQuerySuperTemplate());
         cgenConfiguration.setCreatePKProperties(createpkproperties != null ? createpkproperties
: cgenConfiguration.isCreatePKProperties());
+        cgenConfiguration.setExternalToolConfig(externaltoolconfig != null ? externaltoolconfig
: cgenConfiguration.getExternalToolConfig());
         if(!cgenConfiguration.isMakePairs()) {
             if(template == null) {
                 cgenConfiguration.setTemplate(cgenConfiguration.isClient() ? ClientClassGenerationAction.SINGLE_CLASS_TEMPLATE
: ClassGenerationAction.SINGLE_CLASS_TEMPLATE);
@@ -394,6 +401,13 @@ public class CayenneGeneratorTask extends CayenneTask {
     }
 
     /**
+     * @since 4.2
+     */
+    public void setExternaltoolconfig(String externaltoolconfig) {
+    	this.externaltoolconfig = externaltoolconfig;
+    }
+    
+    /**
      * Provides a <code>VPPConfig</code> object to configure. (Written with createConfig()
      * instead of addConfig() to avoid run-time dependency on VPP).
      */
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/CgenConfiguration.java b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/CgenConfiguration.java
index 2f6e2e4..cfa7480 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/CgenConfiguration.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/CgenConfiguration.java
@@ -81,6 +81,11 @@ public class CgenConfiguration implements Serializable, XMLSerializable
{
 
     private boolean client;
 
+    /**
+     * @since 4.2
+     */
+    private String externalToolConfig;
+    
     public CgenConfiguration(boolean client) {
         this.outputPattern = "*.java";
         this.timestamp = 0L;
@@ -318,6 +323,14 @@ public class CgenConfiguration implements Serializable, XMLSerializable
{
         this.client = client;
     }
 
+    public String getExternalToolConfig() {
+    	return externalToolConfig;
+    }
+    
+    public void setExternalToolConfig(String config) {
+    	this.externalToolConfig = config;
+    }
+    
     void addArtifact(Artifact artifact) {
         artifacts.add(artifact);
     }
@@ -404,6 +417,7 @@ public class CgenConfiguration implements Serializable, XMLSerializable
{
                 .simpleTag("superPkg", this.superPkg)
                 .simpleTag("createPKProperties", Boolean.toString(this.createPKProperties))
                 .simpleTag("client", Boolean.toString(client))
+                .simpleTag("externalToolConfig", this.externalToolConfig)
                 .end();
     }
 
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 85cade1..00a86ec 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,7 +34,6 @@ 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;
@@ -43,6 +42,8 @@ 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.apache.velocity.tools.config.ConfigurationUtils;
+import org.apache.velocity.tools.config.FactoryConfiguration;
 import org.slf4j.Logger;
 
 public class ClassGenerationAction {
@@ -86,9 +87,17 @@ public class ClassGenerationAction {
 	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() {
-		if (System.getProperty("org.apache.velocity.tools") != null) {
+	public ClassGenerationAction(CgenConfiguration cgenConfig) {
+		this.cgenConfiguration = cgenConfig;
+		String toolConfigFile = cgenConfig.getExternalToolConfig();
+		
+		if (System.getProperty("org.apache.velocity.tools") != null || toolConfigFile != null)
{
 			ToolManager manager = new ToolManager(true, true);
+			if (toolConfigFile != null) {
+				logger.info("Adding external tool configuration.");
+				FactoryConfiguration config = ConfigurationUtils.find(toolConfigFile);
+				manager.getToolboxFactory().configure(config);
+			}
 			this.context = manager.createContext();
 		} else {
 			this.context = new VelocityContext();
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
index 9e3927b..886383d 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/ClientClassGenerationAction.java
@@ -41,8 +41,8 @@ public class ClientClassGenerationAction extends ClassGenerationAction {
 
     public static final String CLIENT_SUPERCLASS_PREFIX = "_Client";
 
-    public ClientClassGenerationAction() {
-        super();
+    public ClientClassGenerationAction(CgenConfiguration config) {
+        super(config);
     }
 
     @Override
diff --git a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DefaultClassGenerationActionFactory.java
b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DefaultClassGenerationActionFactory.java
index b561547..d01a98b 100644
--- a/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DefaultClassGenerationActionFactory.java
+++ b/cayenne-cgen/src/main/java/org/apache/cayenne/gen/DefaultClassGenerationActionFactory.java
@@ -32,9 +32,8 @@ public class DefaultClassGenerationActionFactory implements ClassGenerationActio
     @Override
     public ClassGenerationAction createAction(CgenConfiguration cgenConfiguration) {
         ClassGenerationAction classGenerationAction = cgenConfiguration.isClient() ?
-                new ClientClassGenerationAction() :
-                new ClassGenerationAction();
-        classGenerationAction.setCgenConfiguration(cgenConfiguration);
+                new ClientClassGenerationAction(cgenConfiguration) :
+                new ClassGenerationAction(cgenConfiguration);
         classGenerationAction.setUtilsFactory(utilsFactory);
         return classGenerationAction;
     }
diff --git a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/mock/TestClassGenerationAction.java
b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/mock/TestClassGenerationAction.java
index c16aabc..b58b518 100644
--- a/cayenne-cgen/src/test/java/org/apache/cayenne/gen/mock/TestClassGenerationAction.java
+++ b/cayenne-cgen/src/test/java/org/apache/cayenne/gen/mock/TestClassGenerationAction.java
@@ -33,8 +33,7 @@ public class TestClassGenerationAction extends ClassGenerationAction {
     private Collection<StringWriter> writers;
 
     public TestClassGenerationAction(ClassGenerationAction classGenerationAction, Collection<StringWriter>
writers){
-        super();
-        setCgenConfiguration(classGenerationAction.getCgenConfiguration());
+        super(classGenerationAction.getCgenConfiguration());
         setUtilsFactory(classGenerationAction.getUtilsFactory());
         this.writers = writers;
     }
diff --git a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
index 6ad823c..9ac9329 100644
--- a/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
+++ b/maven-plugins/cayenne-maven-plugin/src/main/java/org/apache/cayenne/tools/CayenneGeneratorMojo.java
@@ -20,7 +20,6 @@
 package org.apache.cayenne.tools;
 
 import java.io.File;
-import java.util.Map;
 
 import org.apache.cayenne.configuration.xml.DataChannelMetaData;
 import org.apache.cayenne.dbsync.filter.NamePatternMatcher;
@@ -58,12 +57,6 @@ public class CayenneGeneratorMojo extends AbstractMojo {
     @Parameter
 	private File additionalMaps;
 
-    /**
-	 * Additional system properties that can be added before execution.
-	 */
-    @Parameter
-	private Map<String,String> systemProperties;
-
 	/**
 	 * Whether we are generating classes for the client tier in a Remote Object
 	 * Persistence application. Default is <code>false</code>.
@@ -231,6 +224,13 @@ public class CayenneGeneratorMojo extends AbstractMojo {
     @Parameter
     private Boolean createPKProperties;
 
+    /**
+     * Optional path (classpath or filesystem) to external velocity tool configuration file
+     * @since 4.2 
+     */
+    @Parameter
+    private String externalToolConfig;
+    
     private transient Injector injector;
 
     private static final Logger logger = LoggerFactory.getLogger(CayenneGeneratorMojo.class);
@@ -246,12 +246,6 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 				.addModule(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorMojo.class)))
 				.create();
 
-		// load additional system properties early for downstream access
-		if (systemProperties != null) {
-			logger.info("Setting system properties from pom.xml");
-			systemProperties.forEach((key,value) -> System.setProperty(key, value));
-		}
-
 		Logger logger = new MavenLogger(this);
 		CayenneGeneratorMapLoaderAction loaderAction = new CayenneGeneratorMapLoaderAction(injector);
 		loaderAction.setMainDataMapFile(map);
@@ -310,7 +304,7 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 				makePairs != null || mode != null || outputPattern != null || overwrite != null || superPkg
!= null ||
 				superTemplate != null || template != null || embeddableTemplate != null || embeddableSuperTemplate
!= null ||
 				usePkgPath != null || createPropertyNames != null || force || queryTemplate != null ||
-				querySuperTemplate != null || createPKProperties != null;
+				querySuperTemplate != null || createPKProperties != null || externalToolConfig != null;
 	}
 
 	/**
@@ -362,6 +356,7 @@ public class CayenneGeneratorMojo extends AbstractMojo {
 		cgenConfiguration.setQueryTemplate(queryTemplate != null ? queryTemplate : cgenConfiguration.getQueryTemplate());
 		cgenConfiguration.setQuerySuperTemplate(querySuperTemplate != null ? querySuperTemplate
: cgenConfiguration.getQuerySuperTemplate());
 		cgenConfiguration.setCreatePKProperties(createPKProperties != null ? createPKProperties
: cgenConfiguration.isCreatePKProperties());
+		cgenConfiguration.setExternalToolConfig(externalToolConfig != null ? externalToolConfig
: cgenConfiguration.getExternalToolConfig());
 		if(!cgenConfiguration.isMakePairs()) {
 			if(template == null) {
 				cgenConfiguration.setTemplate(cgenConfiguration.isClient() ? ClientClassGenerationAction.SINGLE_CLASS_TEMPLATE
: ClassGenerationAction.SINGLE_CLASS_TEMPLATE);


Mime
View raw message