Search in sources :

Example 1 with DbImportConfigurationValidator

use of org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator in project cayenne by apache.

the class DbImporterTask method execute.

@Override
public void execute() {
    Logger logger = new AntLogger(this);
    final Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
    if (reverseEngineering.getCatalogs().size() == 0 && reverseEngineering.isEmptyContainer()) {
        config.setUseDataMapReverseEngineering(true);
    }
    DataSourceFactory dataSourceFactory = injector.getInstance(DataSourceFactory.class);
    DbAdapterFactory dbAdapterFactory = injector.getInstance(DbAdapterFactory.class);
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    try {
        DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
        DbAdapter dbAdapter = dbAdapterFactory.createAdapter(dataNodeDescriptor, dataSource);
        config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).dataSource(dataSource).dbAdapter(dbAdapter).build());
    } catch (Exception e) {
        throw new BuildException("Error getting dataSource", e);
    }
    validateAttributes();
    config.setLogger(logger);
    config.setSkipRelationshipsLoading(reverseEngineering.getSkipRelationshipsLoading());
    config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
    config.setTableTypes(reverseEngineering.getTableTypes());
    DbImportConfigurationValidator validator = new DbImportConfigurationValidator(reverseEngineering, config, injector);
    try {
        validator.validate();
    } catch (Exception ex) {
        throw new BuildException(ex.getMessage(), ex);
    }
    try {
        injector.getInstance(DbImportAction.class).execute(config);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error importing database schema";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        injector.shutdown();
    }
}
Also used : DataSourceFactory(org.apache.cayenne.configuration.server.DataSourceFactory) DbImportConfigurationValidator(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Logger(org.slf4j.Logger) DbImportAction(org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataSource(javax.sql.DataSource) DbAdapterFactory(org.apache.cayenne.configuration.server.DbAdapterFactory) DbAdapter(org.apache.cayenne.dba.DbAdapter) FiltersConfigBuilder(org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbImportModule(org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)

Example 2 with DbImportConfigurationValidator

use of org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator in project cayenne by apache.

the class DbImportTask method runImport.

@TaskAction
public void runImport() {
    dataSource.validate();
    final Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()), new DbImportModule(), binder -> binder.bind(ClassLoaderManager.class).toInstance(new DefaultClassLoaderManager()));
    final DbImportConfiguration config = createConfig();
    DataSourceFactory dataSourceFactory = injector.getInstance(DataSourceFactory.class);
    DbAdapterFactory dbAdapterFactory = injector.getInstance(DbAdapterFactory.class);
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    try {
        DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
        DbAdapter dbAdapter = dbAdapterFactory.createAdapter(dataNodeDescriptor, dataSource);
        config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).dataSource(dataSource).dbAdapter(dbAdapter).build());
    } catch (Exception e) {
        throw new TaskExecutionException(this, e);
    }
    final DbImportConfigurationValidator validator = new DbImportConfigurationValidator(reverseEngineering, config, injector);
    try {
        validator.validate();
    } catch (Exception ex) {
        throw new TaskExecutionException(this, ex);
    }
    try {
        injector.getInstance(DbImportAction.class).execute(config);
    } catch (Exception ex) {
        final Throwable th = Util.unwindException(ex);
        String message = "Error importing database schema";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        getLogger().error(message);
        throw new TaskExecutionException(this, th);
    }
}
Also used : DataSourceFactory(org.apache.cayenne.configuration.server.DataSourceFactory) DbImportConfigurationValidator(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) DbImportAction(org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) DataSource(javax.sql.DataSource) TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) DbAdapterFactory(org.apache.cayenne.configuration.server.DbAdapterFactory) DbImportConfiguration(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration) DbAdapter(org.apache.cayenne.dba.DbAdapter) FiltersConfigBuilder(org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) DefaultClassLoaderManager(org.apache.cayenne.di.spi.DefaultClassLoaderManager) DbImportModule(org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule) TaskAction(org.gradle.api.tasks.TaskAction)

Example 3 with DbImportConfigurationValidator

use of org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator in project cayenne by apache.

the class DbImporterMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    final Logger logger = new MavenLogger(this);
    if (project == null) {
        throw new MojoExecutionException("Can't load MavenProject.");
    }
    // check missing data source parameters
    dataSource.validate();
    final Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule(), binder -> binder.bind(ClassLoaderManager.class).toInstance(new MavenPluginClassLoaderManager(project)));
    final DbImportConfiguration config = createConfig(logger);
    DataSourceFactory dataSourceFactory = injector.getInstance(DataSourceFactory.class);
    DbAdapterFactory dbAdapterFactory = injector.getInstance(DbAdapterFactory.class);
    DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
    try {
        DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
        DbAdapter dbAdapter = dbAdapterFactory.createAdapter(dataNodeDescriptor, dataSource);
        config.setFiltersConfig(new FiltersConfigBuilder(dbImportConfig).dataSource(dataSource).dbAdapter(dbAdapter).build());
    } catch (Exception e) {
        throw new MojoExecutionException("Error getting dataSource", e);
    }
    final DbImportConfigurationValidator validator = new DbImportConfigurationValidator(dbImportConfig, config, injector);
    try {
        validator.validate();
    } catch (Exception ex) {
        throw new MojoExecutionException(ex.getMessage(), ex);
    }
    try {
        injector.getInstance(DbImportAction.class).execute(config);
    } catch (Exception ex) {
        final Throwable th = Util.unwindException(ex);
        String message = "Error importing database schema";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        getLog().error(message);
        throw new MojoExecutionException(message, th);
    }
}
Also used : DataSourceFactory(org.apache.cayenne.configuration.server.DataSourceFactory) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DbImportConfigurationValidator(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Logger(org.slf4j.Logger) DbImportAction(org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DataSource(javax.sql.DataSource) DbAdapterFactory(org.apache.cayenne.configuration.server.DbAdapterFactory) DbImportConfiguration(org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration) DbAdapter(org.apache.cayenne.dba.DbAdapter) FiltersConfigBuilder(org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) DbImportModule(org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)

Aggregations

DataSource (javax.sql.DataSource)3 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)3 DataSourceFactory (org.apache.cayenne.configuration.server.DataSourceFactory)3 DbAdapterFactory (org.apache.cayenne.configuration.server.DbAdapterFactory)3 DbAdapter (org.apache.cayenne.dba.DbAdapter)3 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)3 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)3 DbImportAction (org.apache.cayenne.dbsync.reverse.dbimport.DbImportAction)3 DbImportConfigurationValidator (org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfigurationValidator)3 DbImportModule (org.apache.cayenne.dbsync.reverse.dbimport.DbImportModule)3 FiltersConfigBuilder (org.apache.cayenne.dbsync.reverse.filters.FiltersConfigBuilder)3 Injector (org.apache.cayenne.di.Injector)3 DbImportConfiguration (org.apache.cayenne.dbsync.reverse.dbimport.DbImportConfiguration)2 Logger (org.slf4j.Logger)2 DefaultClassLoaderManager (org.apache.cayenne.di.spi.DefaultClassLoaderManager)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 BuildException (org.apache.tools.ant.BuildException)1 TaskAction (org.gradle.api.tasks.TaskAction)1 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)1