package org.hibernate.tool.hbm2ddl;
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;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.collections.ArrayHelper;
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;
public class SchemaValidatorTask extends MatchingTask {
private List fileSets = new LinkedList();
private File propertiesFile;
private File configurationFile;
private String namingStrategy;
private String namingStrategyDelegator;
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(File configurationFile) {
this.configurationFile = configurationFile;
}
@Override
public void execute() throws BuildException {
try {
Configuration cfg = getConfiguration();
getSchemaValidator(cfg).validate();
}
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);
}
catch (Exception e) {
throw new BuildException(e);
}
}
private String[] getFiles() {
List files = new LinkedList();
for ( Iterator i = fileSets.iterator(); i.hasNext(); ) {
FileSet fs = (FileSet) i.next();
DirectoryScanner ds = fs.getDirectoryScanner( getProject() );
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 ArrayHelper.toStringArray( files );
}
private Configuration getConfiguration() throws Exception {
Configuration cfg = new Configuration();
if ( namingStrategy != null && namingStrategyDelegator != null ) {
throw new HibernateException( "namingStrategy and namingStrategyDelegator cannot be specified together." );
}
if (namingStrategy!=null) {
cfg.setNamingStrategy(
(NamingStrategy) ReflectHelper.classForName(namingStrategy).newInstance()
);
}
else if ( namingStrategyDelegator != null) {
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( namingStrategyDelegator ).newInstance()
);
}
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( new File(filename) );
}
else {
cfg.addFile(filename);
}
}
return cfg;
}
private SchemaValidator getSchemaValidator(Configuration cfg) throws HibernateException, IOException {
Properties properties = new Properties();
properties.putAll( cfg.getProperties() );
if (propertiesFile == null) {
properties.putAll( getProject().getProperties() );
}
else {
properties.load( new FileInputStream(propertiesFile) );
}
cfg.setProperties(properties);
return new SchemaValidator(cfg);
}
public void setNamingStrategy(String namingStrategy) {
this.namingStrategy = namingStrategy;
}
public void setNamingStrategyDelegator(String namingStrategyDelegator) {
this.namingStrategyDelegator = namingStrategyDelegator;
}
}