[hibernate-commits] Hibernate SVN: r10358 - in trunk/HibernateExt/tools/src: java/org/hibernate/tool/ant java/org/hibernate/tool/hbm2x test/org/hibernate/tool/hbm2x

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Aug 28 15:56:05 EDT 2006


Author: max.andersen at jboss.com
Date: 2006-08-28 15:55:58 -0400 (Mon, 28 Aug 2006)
New Revision: 10358

Modified:
   trunk/HibernateExt/tools/src/java/org/hibernate/tool/ant/GenericExporterTask.java
   trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/AbstractExporter.java
   trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Exporter.java
   trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/GenericExporter.java
   trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/HibernateMappingExporter.java
   trunk/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/GenericExporterTest.java
Log:
HBX-737 instantiate exporterclass directly instead of relaying through exporter

Modified: trunk/HibernateExt/tools/src/java/org/hibernate/tool/ant/GenericExporterTask.java
===================================================================
--- trunk/HibernateExt/tools/src/java/org/hibernate/tool/ant/GenericExporterTask.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/java/org/hibernate/tool/ant/GenericExporterTask.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -4,8 +4,10 @@
  */
 package org.hibernate.tool.ant;
 
+import org.apache.tools.ant.BuildException;
 import org.hibernate.tool.hbm2x.Exporter;
 import org.hibernate.tool.hbm2x.GenericExporter;
+import org.hibernate.util.ReflectHelper;
 
 /**
  * @author max
@@ -21,6 +23,10 @@
 	String exporterClass;
 	String filePattern;
 	
+	/**
+	 * The FilePattern defines the pattern used to generate files.
+	 * @param filePattern
+	 */
 	public void setFilePattern(String filePattern) {
 		this.filePattern = filePattern;
 	}
@@ -33,8 +39,21 @@
 		this.exporterClass = exporterClass;
 	}
 	
-	protected Exporter createExporter() {		
-		return new GenericExporter();
+	protected Exporter createExporter() {
+		if (exporterClass == null) {
+			return new GenericExporter();
+		} else {
+			try {
+				Class theClass = ReflectHelper.classForName(exporterClass);
+				return (Exporter) theClass.newInstance();
+			} catch (ClassNotFoundException e) {
+				throw new BuildException("Could not find custom exporter class: " + exporterClass, e);
+			} catch (InstantiationException e) {
+				throw new BuildException("Could not create custom exporter class: " + exporterClass, e);
+			} catch (IllegalAccessException e) {
+				throw new BuildException("Could not access custom exporter class: " + exporterClass, e);
+			}
+		}		
 	}
 	
 	protected Exporter configureExporter(Exporter exp) {
@@ -42,15 +61,14 @@
 		
 		if(exp instanceof GenericExporter) {
 			GenericExporter exporter = (GenericExporter) exp;
-			exporter.setFilePattern(filePattern);
-			exporter.setTemplateName(templateName);
-			exporter.setExporterClassName(exporterClass);
+			if(filePattern!=null) exporter.setFilePattern(filePattern);
+			if(templateName!=null) exporter.setTemplateName(templateName);			
 		}
 		
 		return exp;
 	}
 
 	public String getName() {
-		return "generic exporter" + ((exporterClass==null) ? "" : exporterClass);
+		return "generic exporter " + ((exporterClass==null) ? "" : exporterClass);
 	}
 }

Modified: trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/AbstractExporter.java
===================================================================
--- trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/AbstractExporter.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/AbstractExporter.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -86,6 +86,10 @@
 		return configuration;
 	}
 
+	/**
+	 * Builds template context and performs file generation
+	 * Subclasses mostly implement doStart() instead.
+	 */
 	public void start() {
 		setTemplateHelper( new TemplateHelper() );
 		setupTemplates();
@@ -229,6 +233,7 @@
 	
 	public void setProperties(Properties properties) {
 		this.properties = properties;
+		
 	}
 
 	public void setArtifactCollector(ArtifactCollector collector) {

Modified: trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Exporter.java
===================================================================
--- trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Exporter.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/Exporter.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -14,20 +14,48 @@
  */
 public interface Exporter {
 		
+	/** 
+	 * @param cfg An Hibernate {@link org.hibernate.Configuration} or subclass instance that defines the hibernate meta model to be exported.
+	 */
 	public void setConfiguration(Configuration cfg);
-	
+
+	/**
+	 * @param file basedirectory to be used for generated files.
+	 */
 	public void setOutputDirectory(File file);
-	
+
+	/**
+	 * @param templatePath array of directories used sequentially to lookup templates
+	 */
 	public void setTemplatePath(String[] templatePath);
 	
+	/**
+	 * @param templatePrefix template prefix to be used. e.g. if set to "dao/", "dao/" will be prefixed all lookups before the simple name will looked up. Used to allow seperation of templates within a templatepath.  
+	 */
 	public void setTemplatePrefix(String templatePrefix);
-	
+
+	/**
+	 * 
+	 * @param properties set of properties to be used by exporter.
+	 */
 	public void setProperties(Properties properties);
 	
 	
+	/**
+	 * 
+	 * @param collector Instance to be consulted when adding a new file.
+	 */
 	public void setArtifactCollector(ArtifactCollector collector);
+	
+	/**
+	 * 
+	 * @return artifact collector
+	 */
 	public ArtifactCollector getArtifactCollector();
 	
+	/**
+	 * Called when exporter should start generating its output
+	 */
 	public void start();
 	
 }

Modified: trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/GenericExporter.java
===================================================================
--- trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/GenericExporter.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/GenericExporter.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -16,9 +16,8 @@
 
 public class GenericExporter extends AbstractExporter {
 
-	String templateName;
+	private String templateName;
 	private String filePattern;
-	private String exporterClassName;
 	
 	public GenericExporter(Configuration cfg, File outputdir) {
 		super(cfg,outputdir);
@@ -34,36 +33,11 @@
 	public void setTemplateName(String templateName) {
 		this.templateName = templateName;
 	}
-		
-	public void start() {
-		if(exporterClassName!=null) {
-			Exporter exporter = null;
-			try {
-				Class exporterClass = ReflectHelper.classForName(exporterClassName);
-				exporter = (Exporter) exporterClass.newInstance();
-				Properties p = getProperties();
-				if(getTemplateName()!=null) p.put(ExporterSettings.PREFIX_KEY + "template_name", getTemplateName());
-				if(filePattern!=null) p.put(ExporterSettings.PREFIX_KEY + "file_pattern", filePattern);
-				exporter.setProperties(getProperties());
-				exporter.setArtifactCollector(getArtifactCollector());
-				exporter.setConfiguration(getConfiguration());
-				exporter.setOutputDirectory(getOutputDirectory());
-				exporter.setTemplatePath(getTemplatePaths());							
-			}
-			catch (Exception e) {
-				throw new ExporterException("Could not instantiate/configure exporter of class " + exporterClassName,e);
-			}
 			
-			exporter.start();
-		} else {
-			super.start();
-		}
-	}
-	
 	protected void doStart() {
 				
-		if(filePattern==null) throw new ExporterException("File pattern not set on GenericExporter");
-		if(templateName==null) throw new ExporterException("Template pattern not set on GenericExporter");
+		if(filePattern==null) throw new ExporterException("File pattern not set on " + this.getClass());
+		if(templateName==null) throw new ExporterException("Template pattern not set on " + this.getClass());
 		
 		if(filePattern.indexOf("{class-name}")>=0) {				
 			exportClasses();
@@ -133,8 +107,5 @@
 	public void setFilePattern(String filePattern) {
 		this.filePattern = filePattern;		
 	}
-
-	public void setExporterClassName(String exporterClassName) {
-		this.exporterClassName = exporterClassName;
-	}
+	
 }

Modified: trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/HibernateMappingExporter.java
===================================================================
--- trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/HibernateMappingExporter.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/java/org/hibernate/tool/hbm2x/HibernateMappingExporter.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -39,7 +39,7 @@
 		Configuration cfg = getConfiguration();
 		if(c2h.isImportData(cfg) && (c2h.isNamedQueries(cfg)) && (c2h.isNamedSQLQueries(cfg)) && (c2h.isFilterDefinitions(cfg))) {
 			TemplateProducer producer = new TemplateProducer(getTemplateHelper(),getArtifactCollector());
-			producer.produce(new HashMap(), "generalhbm.hbm.ftl", new File(getOutputDirectory(),"GeneralHbmSettings.hbm.xml"), templateName);
+			producer.produce(new HashMap(), "generalhbm.hbm.ftl", new File(getOutputDirectory(),"GeneralHbmSettings.hbm.xml"), getTemplateName());
 		}
 	}
 	

Modified: trunk/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/GenericExporterTest.java
===================================================================
--- trunk/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/GenericExporterTest.java	2006-08-28 19:37:54 UTC (rev 10357)
+++ trunk/HibernateExt/tools/src/test/org/hibernate/tool/hbm2x/GenericExporterTest.java	2006-08-28 19:55:58 UTC (rev 10358)
@@ -122,17 +122,6 @@
 		assertEquals(generated.getProperty("myTool.value"), "value");
 	}
 	
-	public void testGenericClassExporter() {
-		GenericExporter ge = new GenericExporter();
-		ge.setConfiguration(getCfg());
-		ge.setOutputDirectory(getOutputDir());
-		ge.setProperties(new Properties());		
-		ge.setExporterClassName("org.hibernate.tool.hbm2x.POJOExporter");
-		
-		ge.start();
-		
-	}
-	
 	protected String getBaseForMappings() {
 		return "org/hibernate/tool/hbm2x/";
 	}




More information about the hibernate-commits mailing list