From: <one...@us...> - 2003-02-15 08:03:42
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/tool/hbm2ddl In directory sc8-pr-cvs1:/tmp/cvs-serv29419/sf/hibernate/tool/hbm2ddl Added Files: SchemaExportTask.java Log Message: ant task by Rong C Ou --- NEW FILE: SchemaExportTask.java --- package net.sf.hibernate.tool.hbm2ddl; import net.sf.hibernate.HibernateException; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.tool.hbm2ddl.SchemaExport; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.MatchingTask; import org.apache.tools.ant.types.FileSet; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Properties; /** * An Ant task for <tt>SchemaExport</tt> * * <pre> * <taskdef name="schemaexport" * classname="blah.blah.package.SchemaExportTask" * classpathref="class.path"/> * * <schemaexport * properties="${build.classes.dir}/hibernate.properties" * quiet="no" * text="no" * drop="no" * output="${build.dir}/schema-export.sql"> * <fileset dir="${build.classes.dir}"> * <include name="*.hbm.xml"/> * </fileset> * </schemaexport> * </pre> */ public class SchemaExportTask extends MatchingTask { private List fileSets = new LinkedList(); private File propertiesFile = null; private String configurationFile = null; private String outputFile = null; private boolean quiet = false; private boolean text = false; private boolean drop = false; public void addFileset(FileSet set) { fileSets.add(set); } public void setProperties(File propertiesFile) { if (!propertiesFile.exists()) { throw new BuildException("Properties file: " + propertiesFile + " does not exist."); } log("Using properties file " + propertiesFile, Project.MSG_DEBUG); this.propertiesFile = propertiesFile; } public void setConfig(String configurationFile) { this.configurationFile = configurationFile; } public void setQuiet(boolean quiet) { this.quiet = quiet; } public void setText(boolean text) { this.text = text; } public void setDrop(boolean drop) { this.drop = drop; } public void setOutput(String outputFile) { this.outputFile = outputFile; } public void execute() throws BuildException { try { Configuration cfg = getConfiguration(); SchemaExport schemaExport = getSchemaExport(cfg); if (drop) { schemaExport.drop(!quiet, !text); } else { schemaExport.create(!quiet, !text); } } catch (HibernateException e) { throw new BuildException("Schema text failed: " + e.getMessage(), e); } catch (FileNotFoundException e) { throw new BuildException("File not found: " + e.getMessage(), e); } catch (IOException e) { throw new BuildException("IOException : " + e.getMessage(), e); } } private String[] getFiles() { List files = new LinkedList(); for ( Iterator i = fileSets.iterator(); i.hasNext(); ) { FileSet fs = (FileSet) i.next(); DirectoryScanner ds = fs.getDirectoryScanner(project); String[] dsFiles = ds.getIncludedFiles(); for (int j = 0; j < dsFiles.length; j++) { File f = new File(dsFiles[j]); if ( !f.isFile() ) { f = new File( ds.getBasedir(), dsFiles[j] ); } files.add( f.getAbsolutePath() ); } } return (String[]) files.toArray( new String[0] ); } private Configuration getConfiguration() throws HibernateException { Configuration cfg = new Configuration(); if (configurationFile != null) { cfg.configure(configurationFile); } String[] files = getFiles(); for (int i = 0; i < files.length; i++) { String filename = files[i]; if ( filename.endsWith(".jar") ) { cfg.addJar(filename); } else { cfg.addFile(filename); } } return cfg; } private SchemaExport getSchemaExport(Configuration cfg) throws HibernateException, IOException { SchemaExport schemaExport = null; if (propertiesFile == null) { schemaExport = new SchemaExport(cfg); } else { Properties properties = new Properties(); properties.load( new FileInputStream(propertiesFile) ); schemaExport = new SchemaExport(cfg, properties); } schemaExport.setOutputFile(outputFile); return schemaExport; } } |