1 package org.hibernate.tool.hbm2ddl; 3 4 import org.hibernate.HibernateException; 5 import org.hibernate.cfg.Configuration; 6 import org.hibernate.cfg.NamingStrategy; 7 import org.hibernate.util.ArrayHelper; 8 import org.hibernate.util.ReflectHelper; 9 10 import org.apache.tools.ant.BuildException; 11 import org.apache.tools.ant.DirectoryScanner; 12 import org.apache.tools.ant.Project; 13 import org.apache.tools.ant.taskdefs.MatchingTask; 14 import org.apache.tools.ant.types.FileSet; 15 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileNotFoundException ; 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Properties ; 24 25 49 public class SchemaExportTask extends MatchingTask { 50 51 private List fileSets = new LinkedList (); 52 private File propertiesFile = null; 53 private String configurationFile = null; 54 private String outputFile = null; 55 private boolean quiet = false; 56 private boolean text = false; 57 private boolean drop = false; 58 private String delimiter = null; 59 private String namingStrategy = null; 60 61 public void addFileset(FileSet set) { 62 fileSets.add(set); 63 } 64 65 69 public void setProperties(File propertiesFile) { 70 if ( !propertiesFile.exists() ) { 71 throw new BuildException("Properties file: " + propertiesFile + " does not exist."); 72 } 73 74 log("Using properties file " + propertiesFile, Project.MSG_DEBUG); 75 this.propertiesFile = propertiesFile; 76 } 77 78 83 public void setConfig(String configurationFile) { 84 this.configurationFile = configurationFile; 85 } 86 87 92 public void setQuiet(boolean quiet) { 93 this.quiet = quiet; 94 } 95 96 101 public void setText(boolean text) { 102 this.text = text; 103 } 104 105 110 public void setDrop(boolean drop) { 111 this.drop = drop; 112 } 113 114 118 public void setDelimiter(String delimiter) { 119 this.delimiter = delimiter; 120 } 121 122 126 public void setOutput(String outputFile) { 127 this.outputFile = outputFile; 128 } 129 130 133 public void execute() throws BuildException { 134 try { 135 Configuration cfg = getConfiguration(); 136 SchemaExport schemaExport = getSchemaExport(cfg); 137 138 if (drop) { 139 schemaExport.drop(!quiet, !text); 140 } 141 else { 142 schemaExport.create(!quiet, !text); 143 } 144 } 145 catch (HibernateException e) { 146 throw new BuildException("Schema text failed: " + e.getMessage(), e); 147 } 148 catch (FileNotFoundException e) { 149 throw new BuildException("File not found: " + e.getMessage(), e); 150 } 151 catch (IOException e) { 152 throw new BuildException("IOException : " + e.getMessage(), e); 153 } 154 catch (Exception e) { 155 throw new BuildException(e); 156 } 157 } 158 159 private String [] getFiles() { 160 161 List files = new LinkedList (); 162 for ( Iterator i = fileSets.iterator(); i.hasNext(); ) { 163 164 FileSet fs = (FileSet) i.next(); 165 DirectoryScanner ds = fs.getDirectoryScanner( getProject() ); 166 167 String [] dsFiles = ds.getIncludedFiles(); 168 for (int j = 0; j < dsFiles.length; j++) { 169 File f = new File (dsFiles[j]); 170 if ( !f.isFile() ) { 171 f = new File ( ds.getBasedir(), dsFiles[j] ); 172 } 173 174 files.add( f.getAbsolutePath() ); 175 } 176 } 177 178 return ArrayHelper.toStringArray(files); 179 } 180 181 private Configuration getConfiguration() throws Exception { 182 Configuration cfg = new Configuration(); 183 if (namingStrategy!=null) cfg.setNamingStrategy( 184 (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance() 185 ); 186 if (configurationFile != null) cfg.configure( new File (configurationFile) ); 187 188 String [] files = getFiles(); 189 for (int i = 0; i < files.length; i++) { 190 String filename = files[i]; 191 if ( filename.endsWith(".jar") ) { 192 cfg.addJar( new File (filename) ); 193 } 194 else { 195 cfg.addFile(filename); 196 } 197 } 198 return cfg; 199 } 200 201 private SchemaExport getSchemaExport(Configuration cfg) throws HibernateException, IOException { 202 Properties properties = new Properties (); 203 properties.putAll( cfg.getProperties() ); 204 if (propertiesFile == null) { 205 properties.putAll( getProject().getProperties() ); 206 } 207 else { 208 properties.load( new FileInputStream (propertiesFile) ); 209 } 210 SchemaExport schemaExport = new SchemaExport(cfg, properties); 211 schemaExport.setOutputFile(outputFile); 212 schemaExport.setDelimiter(delimiter); 213 return schemaExport; 214 } 215 216 public void setNamingStrategy(String namingStrategy) { 217 this.namingStrategy = namingStrategy; 218 } 219 220 } 221 | Popular Tags |