use of org.apache.cayenne.dbsync.DbSyncModule 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);
}
}
use of org.apache.cayenne.dbsync.DbSyncModule in project cayenne by apache.
the class Main method appendModules.
protected Collection<Module> appendModules(Collection<Module> modules) {
// TODO: this is dirty... ServerModule is out of place inside the Modeler...
// If we need ServerRuntime for certain operations, those should start their own stack...
modules.add(new ServerModule());
modules.add(new ProjectModule());
modules.add(new DbSyncModule());
modules.add(new CayenneModelerModule());
return modules;
}
use of org.apache.cayenne.dbsync.DbSyncModule 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();
}
}
use of org.apache.cayenne.dbsync.DbSyncModule in project cayenne by apache.
the class DbImporterTask method execute.
@Override
public void execute() {
config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).build());
validateAttributes();
Logger logger = new AntLogger(this);
config.setLogger(logger);
config.setSkipRelationshipsLoading(reverseEngineering.getSkipRelationshipsLoading());
config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
config.setTableTypes(reverseEngineering.getTableTypes());
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
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();
}
}
use of org.apache.cayenne.dbsync.DbSyncModule in project cayenne by apache.
the class DefaultDbImportActionTest method testSaveLoadedWithNonEmptyProject.
@Test
public void testSaveLoadedWithNonEmptyProject() throws Exception {
Logger log = mock(Logger.class);
Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
DbImportConfiguration params = mock(DbImportConfiguration.class);
URL projectURL = new URL(getPackageURL(), "dbimport/cayenne-testProject3.map.xml");
File projectFile = new File(projectURL.toURI());
projectFile.delete();
assertFalse(projectFile.exists());
Files.write(projectFile.toPath(), ("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<domain xmlns=\"http://cayenne.apache.org/schema/10/domain\"\n" + "\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + "\t xsi:schemaLocation=\"http://cayenne.apache.org/schema/10/domain http://cayenne.apache.org/schema/10/domain.xsd\"\n" + "\t project-version=\"10\">\n" + "</domain>").getBytes(Charset.forName("UTF-8")));
assertTrue(projectFile.isFile());
when(params.getCayenneProject()).thenReturn(projectFile);
DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
URL dataMapURL = new URL(getPackageURL(), "dbimport/testSaveLoaded3.map.xml");
File dataMapFile = new File(dataMapURL.toURI());
dataMapFile.delete();
assertFalse(dataMapFile.exists());
DataMap map = new DataMap("testSaveLoaded3");
map.setConfigurationSource(new URLResource(dataMapURL));
action.saveLoaded(map, params);
assertTrue(dataMapFile.isFile());
assertTrue(projectFile.isFile());
String dataMapContents = Util.stringFromFile(dataMapFile);
assertTrue("Has no project version saved", dataMapContents.contains("project-version=\""));
String projectContents = Util.stringFromFile(projectFile);
assertTrue("Has no project version saved", projectContents.contains("project-version=\""));
assertTrue("Has no datamap in project", projectContents.contains("<map name=\"testSaveLoaded3\"/>"));
}
Aggregations