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 45 public class SchemaUpdateTask extends MatchingTask { 46 47 private List fileSets = new LinkedList (); 48 private File propertiesFile = null; 49 private String configurationFile = null; 50 private boolean quiet = false; 51 private boolean text = true; 52 private String namingStrategy = null; 53 54 public void addFileset(FileSet set) { 55 fileSets.add(set); 56 } 57 58 62 public void setProperties(File propertiesFile) { 63 if ( !propertiesFile.exists() ) { 64 throw new BuildException("Properties file: " + propertiesFile + " does not exist."); 65 } 66 67 log("Using properties file " + propertiesFile, Project.MSG_DEBUG); 68 this.propertiesFile = propertiesFile; 69 } 70 71 75 public void setConfig(String configurationFile) { 76 this.configurationFile = configurationFile; 77 } 78 79 84 public void setText(boolean text) { 85 this.text = text; 86 } 87 88 93 public void setQuiet(boolean quiet) { 94 this.quiet = quiet; 95 } 96 97 100 public void execute() throws BuildException { 101 try { 102 Configuration cfg = getConfiguration(); 103 getSchemaUpdate(cfg).execute(!quiet, !text); 104 } 105 catch (HibernateException e) { 106 throw new BuildException("Schema text failed: " + e.getMessage(), e); 107 } 108 catch (FileNotFoundException e) { 109 throw new BuildException("File not found: " + e.getMessage(), e); 110 } 111 catch (IOException e) { 112 throw new BuildException("IOException : " + e.getMessage(), e); 113 } 114 catch (Exception e) { 115 throw new BuildException(e); 116 } 117 } 118 119 private String [] getFiles() { 120 121 List files = new LinkedList (); 122 for ( Iterator i = fileSets.iterator(); i.hasNext(); ) { 123 124 FileSet fs = (FileSet) i.next(); 125 DirectoryScanner ds = fs.getDirectoryScanner( getProject() ); 126 127 String [] dsFiles = ds.getIncludedFiles(); 128 for (int j = 0; j < dsFiles.length; j++) { 129 File f = new File (dsFiles[j]); 130 if ( !f.isFile() ) { 131 f = new File ( ds.getBasedir(), dsFiles[j] ); 132 } 133 134 files.add( f.getAbsolutePath() ); 135 } 136 } 137 138 return ArrayHelper.toStringArray(files); 139 } 140 141 private Configuration getConfiguration() throws Exception { 142 Configuration cfg = new Configuration(); 143 if (namingStrategy!=null) cfg.setNamingStrategy( 144 (NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance() 145 ); 146 if (configurationFile!=null) cfg.configure( new File (configurationFile) ); 147 148 String [] files = getFiles(); 149 for (int i = 0; i < files.length; i++) { 150 String filename = files[i]; 151 if ( filename.endsWith(".jar") ) { 152 cfg.addJar( new File (filename) ); 153 } 154 else { 155 cfg.addFile(filename); 156 } 157 } 158 return cfg; 159 } 160 161 private SchemaUpdate getSchemaUpdate(Configuration cfg) throws HibernateException, IOException { 162 Properties properties = new Properties (); 163 properties.putAll( cfg.getProperties() ); 164 if (propertiesFile == null) { 165 properties.putAll( getProject().getProperties() ); 166 } 167 else { 168 properties.load( new FileInputStream (propertiesFile) ); 169 } 170 return new SchemaUpdate(cfg, properties); 171 } 172 173 public void setNamingStrategy(String namingStrategy) { 174 this.namingStrategy = namingStrategy; 175 } 176 177 } 178 | Popular Tags |