Search in sources :

Example 1 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule 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 ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DbGeneratorTaskTest method testSetAdapter.

@Test
public void testSetAdapter() throws Exception {
    DataSource ds = mock(DataSource.class);
    Injector injector = DIBootstrap.createInjector(new ToolsModule(mock(Logger.class)));
    DbGeneratorTask task = new DbGeneratorTask();
    DbAdapter autoAdapter = task.getAdapter(injector, ds);
    assertTrue(autoAdapter instanceof AutoAdapter);
    task.setAdapter(SQLServerAdapter.class.getName());
    DbAdapter sqlServerAdapter = task.getAdapter(injector, ds);
    assertTrue(sqlServerAdapter instanceof SQLServerAdapter);
}
Also used : SQLServerAdapter(org.apache.cayenne.dba.sqlserver.SQLServerAdapter) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) AutoAdapter(org.apache.cayenne.dba.AutoAdapter) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 3 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class CayenneGeneratorTask method execute.

/**
 * Executes the task. It will be called by ant framework.
 */
@Override
public void execute() throws BuildException {
    validateAttributes();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    injector = new ToolsInjectorBuilder().addModule(new ToolsModule(LoggerFactory.getLogger(CayenneGeneratorTask.class))).create();
    logger = new AntLogger(this);
    CayenneGeneratorMapLoaderAction loadAction = new CayenneGeneratorMapLoaderAction(injector);
    loadAction.setMainDataMapFile(map);
    loadAction.setAdditionalDataMapFiles(additionalMaps);
    try {
        Thread.currentThread().setContextClassLoader(CayenneGeneratorTask.class.getClassLoader());
        DataMap dataMap = loadAction.getMainDataMap();
        ClassGenerationAction generatorAction = createGenerator(dataMap);
        CayenneGeneratorEntityFilterAction filterEntityAction = new CayenneGeneratorEntityFilterAction();
        filterEntityAction.setNameFilter(NamePatternMatcher.build(logger, includeEntitiesPattern, excludeEntitiesPattern));
        CayenneGeneratorEmbeddableFilterAction filterEmbeddableAction = new CayenneGeneratorEmbeddableFilterAction();
        filterEmbeddableAction.setNameFilter(NamePatternMatcher.build(logger, null, excludeEmbeddablesPattern));
        filterEntityAction.setClient(generatorAction.getCgenConfiguration().isClient());
        generatorAction.setLogger(logger);
        if (force) {
            // will (re-)generate all files
            generatorAction.getCgenConfiguration().setForce(true);
        }
        generatorAction.getCgenConfiguration().setTimestamp(map.lastModified());
        if (!hasConfig() && useConfigFromDataMap) {
            generatorAction.prepareArtifacts();
        } else {
            generatorAction.addEntities(filterEntityAction.getFilteredEntities(dataMap));
            generatorAction.addEmbeddables(filterEmbeddableAction.getFilteredEmbeddables(dataMap));
            generatorAction.addQueries(dataMap.getQueryDescriptors());
        }
        generatorAction.execute();
    } catch (Exception e) {
        throw new BuildException(e);
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
    }
}
Also used : BuildException(org.apache.tools.ant.BuildException) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) ClientClassGenerationAction(org.apache.cayenne.gen.ClientClassGenerationAction) ClassGenerationAction(org.apache.cayenne.gen.ClassGenerationAction)

Example 4 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DbGeneratorTask method execute.

@Override
public void execute() {
    Logger logger = new AntLogger(this);
    log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
    log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
    validateAttributes();
    ClassLoader loader = null;
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    try {
        loader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
        DbAdapter adapter = getAdapter(injector, dataSource);
        DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        generator.runGenerator(dataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        log(message, Project.MSG_ERR);
        throw new BuildException(message, th);
    } finally {
        Thread.currentThread().setContextClassLoader(loader);
        injector.shutdown();
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) BuildException(org.apache.tools.ant.BuildException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) BuildException(org.apache.tools.ant.BuildException) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 5 with ToolsModule

use of org.apache.cayenne.dbsync.reverse.configuration.ToolsModule in project cayenne by apache.

the class DefaultDbImportActionTest method testSaveLoadedNoProject.

@Test
public void testSaveLoadedNoProject() throws Exception {
    Logger log = mock(Logger.class);
    Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
    DbImportConfiguration params = mock(DbImportConfiguration.class);
    when(params.getCayenneProject()).thenReturn(null);
    URL outUrl = new URL(getPackageURL(), "dbimport/testSaveLoaded1.map.xml");
    DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
    File out = new File(outUrl.toURI());
    out.delete();
    assertFalse(out.exists());
    DataMap map = new DataMap("testSaveLoaded1");
    map.setConfigurationSource(new URLResource(outUrl));
    action.saveLoaded(map, params);
    assertTrue(out.isFile());
    String contents = Util.stringFromFile(out);
    assertTrue("Has no project version saved", contents.contains("project-version=\""));
}
Also used : Logger(org.slf4j.Logger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) URL(java.net.URL) DataMap(org.apache.cayenne.map.DataMap) URLResource(org.apache.cayenne.resource.URLResource) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) File(java.io.File) Test(org.junit.Test)

Aggregations

ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)16 Injector (org.apache.cayenne.di.Injector)13 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)12 Logger (org.slf4j.Logger)10 DataMap (org.apache.cayenne.map.DataMap)9 DbAdapter (org.apache.cayenne.dba.DbAdapter)7 Test (org.junit.Test)6 File (java.io.File)5 URL (java.net.URL)4 DataSource (javax.sql.DataSource)4 URLResource (org.apache.cayenne.resource.URLResource)4 DbGenerator (org.apache.cayenne.access.DbGenerator)3 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)3 DataSourceFactory (org.apache.cayenne.configuration.server.DataSourceFactory)3 DbAdapterFactory (org.apache.cayenne.configuration.server.DbAdapterFactory)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 ClassGenerationAction (org.apache.cayenne.gen.ClassGenerationAction)3