package org.hibernate.tool.hbm2ddl;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.boot.registry.internal.StandardServiceRegistryImpl;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.cfg.NamingStrategy;
import org.hibernate.cfg.naming.NamingStrategyDelegator;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.jboss.logging.Logger;
public class SchemaValidator {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, SchemaValidator.class.getName());
private ConnectionHelper connectionHelper;
private Configuration configuration;
private Dialect dialect;
public SchemaValidator(Configuration cfg) throws HibernateException {
this( cfg, cfg.getProperties() );
}
public SchemaValidator(Configuration cfg, Properties connectionProperties) throws HibernateException {
this.configuration = cfg;
dialect = Dialect.getDialect( connectionProperties );
Properties props = new Properties();
props.putAll( dialect.getDefaultProperties() );
props.putAll( connectionProperties );
connectionHelper = new ManagedProviderConnectionHelper( props );
}
public SchemaValidator(ServiceRegistry serviceRegistry, Configuration cfg ) throws HibernateException {
this.configuration = cfg;
final JdbcServices jdbcServices = serviceRegistry.getService( JdbcServices.class );
this.dialect = jdbcServices.getDialect();
this.connectionHelper = new SuppliedConnectionProviderConnectionHelper( jdbcServices.getConnectionProvider() );
}
private static StandardServiceRegistryImpl createServiceRegistry(Properties properties) {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
return (StandardServiceRegistryImpl) new StandardServiceRegistryBuilder().applySettings( properties ).build();
}
public static void main(String[] args) {
try {
Configuration cfg = new Configuration();
String propFile = null;
boolean hasNaming = false;
boolean hasNamingDelegator = false;
for ( int i = 0; i < args.length; i++ ) {
if ( args[i].startsWith( "--" ) ) {
if ( args[i].startsWith( "--properties=" ) ) {
propFile = args[i].substring( 13 );
}
else if ( args[i].startsWith( "--config=" ) ) {
cfg.configure( args[i].substring( 9 ) );
}
else if ( args[i].startsWith( "--naming=" ) ) {
hasNaming = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategy(
( NamingStrategy ) ReflectHelper.classForName( args[i].substring( 9 ) ).newInstance()
);
}
else if ( args[i].startsWith( "--namingdelegator=" ) ) {
hasNamingDelegator = true;
checkNamingAndNamingDelegatorNotBothSpecified( hasNaming, hasNamingDelegator );
cfg.setNamingStrategyDelegator(
(NamingStrategyDelegator) ReflectHelper.classForName( args[i].substring( 18 ) )
.newInstance()
);
}
}
else {
cfg.addFile( args[i] );
}
}
if ( propFile != null ) {
Properties props = new Properties();
props.putAll( cfg.getProperties() );
props.load( new FileInputStream( propFile ) );
cfg.setProperties( props );
}
StandardServiceRegistryImpl serviceRegistry = createServiceRegistry( cfg.getProperties() );
try {
new SchemaValidator( serviceRegistry, cfg ).validate();
}
finally {
serviceRegistry.destroy();
}
}
catch ( Exception e ) {
LOG.unableToRunSchemaUpdate(e);
e.printStackTrace();
}
}
private static void checkNamingAndNamingDelegatorNotBothSpecified(boolean namingSpecified, boolean namingDelegatorSpecified) {
if ( namingSpecified && namingDelegatorSpecified ) {
throw new HibernateException( "--naming=<naming_strategy> and --namingdelegator=<naming_strategy_delegator> cannot be used together." );
}
}
public void validate() {
LOG.runningSchemaValidator();
Connection connection = null;
try {
DatabaseMetadata meta;
try {
LOG.fetchingDatabaseMetadata();
connectionHelper.prepare( false );
connection = connectionHelper.getConnection();
meta = new DatabaseMetadata( connection, dialect, configuration, false );
}
catch ( SQLException sqle ) {
LOG.unableToGetDatabaseMetadata(sqle);
throw sqle;
}
configuration.validateSchema( dialect, meta );
}
catch ( SQLException e ) {
LOG.unableToCompleteSchemaValidation(e);
}
finally {
try {
connectionHelper.release();
}
catch ( Exception e ) {
LOG.unableToCloseConnection(e);
}
}
}
}