Search in sources :

Example 1 with AdhocObjectFactory

use of org.apache.cayenne.di.AdhocObjectFactory in project cayenne by apache.

the class DbGeneratorMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    Logger logger = new MavenLogger(this);
    // check missing data source parameters
    dataSource.validate();
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
    AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
    logger.info(String.format("connection settings - [driver: %s, url: %s, username: %s]", dataSource.getDriver(), dataSource.getUrl(), dataSource.getUsername()));
    logger.info(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK));
    try {
        final DbAdapter adapterInst = (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
        // Load the data map and run the db generator.
        DataMap dataMap = loadDataMap(injector);
        DbGenerator generator = new DbGenerator(adapterInst, dataMap, NoopJdbcEventLogger.getInstance());
        generator.setShouldCreateFKConstraints(createFK);
        generator.setShouldCreatePKSupport(createPK);
        generator.setShouldCreateTables(createTables);
        generator.setShouldDropPKSupport(dropPK);
        generator.setShouldDropTables(dropTables);
        // load driver taking custom CLASSPATH into account...
        DriverDataSource driverDataSource = new DriverDataSource((Driver) Class.forName(dataSource.getDriver()).newInstance(), dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
        generator.runGenerator(driverDataSource);
    } catch (Exception ex) {
        Throwable th = Util.unwindException(ex);
        String message = "Error generating database";
        if (th.getLocalizedMessage() != null) {
            message += ": " + th.getLocalizedMessage();
        }
        logger.error(message);
        throw new MojoExecutionException(message, th);
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Logger(org.slf4j.Logger) NoopJdbcEventLogger(org.apache.cayenne.log.NoopJdbcEventLogger) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DataMap(org.apache.cayenne.map.DataMap) DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) DbGenerator(org.apache.cayenne.access.DbGenerator)

Example 2 with AdhocObjectFactory

use of org.apache.cayenne.di.AdhocObjectFactory in project cayenne by apache.

the class DbGenerateTask method createDbAdapter.

DbAdapter createDbAdapter() {
    Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()));
    AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
    return (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Injector(org.apache.cayenne.di.Injector) DbSyncModule(org.apache.cayenne.dbsync.DbSyncModule) AdhocObjectFactory(org.apache.cayenne.di.AdhocObjectFactory) ToolsModule(org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)

Aggregations

DbAdapter (org.apache.cayenne.dba.DbAdapter)2 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)2 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)2 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)2 Injector (org.apache.cayenne.di.Injector)2 DbGenerator (org.apache.cayenne.access.DbGenerator)1 DriverDataSource (org.apache.cayenne.datasource.DriverDataSource)1 NoopJdbcEventLogger (org.apache.cayenne.log.NoopJdbcEventLogger)1 DataMap (org.apache.cayenne.map.DataMap)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 Logger (org.slf4j.Logger)1