use of org.apache.cayenne.datasource.DriverDataSource in project cayenne by apache.
the class DbGenerator method runGenerator.
/**
* Creates a temporary DataSource out of DataSourceInfo and invokes
* <code>public void runGenerator(DataSource ds)</code>.
*/
public void runGenerator(DataSourceInfo dsi) throws Exception {
this.failures = null;
// and therefore no need to create a connection
if (isEmpty(true)) {
return;
}
Driver driver = (Driver) Class.forName(dsi.getJdbcDriver()).newInstance();
DataSource dataSource = new DriverDataSource(driver, dsi.getDataSourceUrl(), dsi.getUserName(), dsi.getPassword());
runGenerator(dataSource);
}
use of org.apache.cayenne.datasource.DriverDataSource in project cayenne by apache.
the class DataSourcePreferences method testDataSourceAction.
/**
* Tries to establish a DB connection, reporting the status of this
* operation.
*/
public void testDataSourceAction() {
DBConnectionInfo currentDataSource = getConnectionInfo();
if (currentDataSource == null) {
return;
}
if (currentDataSource.getJdbcDriver() == null) {
JOptionPane.showMessageDialog(null, "No JDBC Driver specified", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
if (currentDataSource.getUrl() == null) {
JOptionPane.showMessageDialog(null, "No Database URL specified", "Warning", JOptionPane.WARNING_MESSAGE);
return;
}
try {
FileClassLoadingService classLoader = new FileClassLoadingService();
List<File> oldPathFiles = ((FileClassLoadingService) getApplication().getClassLoadingService()).getPathFiles();
Collection<String> details = new ArrayList<>();
for (File oldPathFile : oldPathFiles) {
details.add(oldPathFile.getAbsolutePath());
}
Preferences classPathPreferences = getApplication().getPreferencesNode(ClasspathPreferences.class, "");
if (editor.getChangedPreferences().containsKey(classPathPreferences)) {
Map<String, String> map = editor.getChangedPreferences().get(classPathPreferences);
for (Map.Entry<String, String> en : map.entrySet()) {
String key = en.getKey();
if (!details.contains(key)) {
details.add(key);
}
}
}
if (editor.getRemovedPreferences().containsKey(classPathPreferences)) {
Map<String, String> map = editor.getRemovedPreferences().get(classPathPreferences);
for (Map.Entry<String, String> en : map.entrySet()) {
String key = en.getKey();
if (details.contains(key)) {
details.remove(key);
}
}
}
if (details.size() > 0) {
classLoader.setPathFiles(details.stream().map(File::new).collect(Collectors.toList()));
}
Class<Driver> driverClass = classLoader.loadClass(Driver.class, currentDataSource.getJdbcDriver());
Driver driver = driverClass.newInstance();
// connect via Cayenne DriverDataSource - it addresses some driver
// issues...
// can't use try with resource here as we can loose meaningful exception
Connection c = new DriverDataSource(driver, currentDataSource.getUrl(), currentDataSource.getUserName(), currentDataSource.getPassword()).getConnection();
try {
c.close();
} catch (SQLException ignored) {
// i guess we can ignore this...
}
JOptionPane.showMessageDialog(null, "Connected Successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
} catch (Throwable th) {
th = Util.unwindException(th);
String message = "Error connecting to DB: " + th.getLocalizedMessage();
StringTokenizer st = new StringTokenizer(message);
StringBuilder sbMessage = new StringBuilder();
int len = 0;
String tempString;
while (st.hasMoreTokens()) {
tempString = st.nextElement().toString();
if (len < 110) {
len = len + tempString.length() + 1;
} else {
sbMessage.append("\n");
len = 0;
}
sbMessage.append(tempString).append(" ");
}
JOptionPane.showMessageDialog(null, sbMessage.toString(), "Warning", JOptionPane.WARNING_MESSAGE);
}
}
use of org.apache.cayenne.datasource.DriverDataSource 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.datasource.DriverDataSource 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.datasource.DriverDataSource in project cayenne by apache.
the class DriverDataSourceFactory method getDataSource.
public DataSource getDataSource(DataNodeDescriptor nodeDescriptor) throws Exception {
DataSourceInfo properties = nodeDescriptor.getDataSourceDescriptor();
if (properties == null) {
throw new IllegalArgumentException("'nodeDescriptor' contains no datasource descriptor");
}
Driver driver = objectFactory.newInstance(Driver.class, properties.getJdbcDriver());
return new DriverDataSource(driver, properties.getDataSourceUrl(), properties.getUserName(), properties.getPassword());
}
Aggregations