Search in sources :

Example 1 with DriverDataSource

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);
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Driver(java.sql.Driver) DataSource(javax.sql.DataSource) DriverDataSource(org.apache.cayenne.datasource.DriverDataSource)

Example 2 with DriverDataSource

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);
    }
}
Also used : DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) Driver(java.sql.Driver) DBConnectionInfo(org.apache.cayenne.modeler.pref.DBConnectionInfo) StringTokenizer(java.util.StringTokenizer) Preferences(java.util.prefs.Preferences) File(java.io.File) Map(java.util.Map) FileClassLoadingService(org.apache.cayenne.modeler.FileClassLoadingService)

Example 3 with DriverDataSource

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);
    }
}
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 4 with DriverDataSource

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();
    }
}
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 DriverDataSource

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());
}
Also used : DataSourceInfo(org.apache.cayenne.conn.DataSourceInfo) DriverDataSource(org.apache.cayenne.datasource.DriverDataSource) Driver(java.sql.Driver)

Aggregations

DriverDataSource (org.apache.cayenne.datasource.DriverDataSource)5 Driver (java.sql.Driver)3 DbGenerator (org.apache.cayenne.access.DbGenerator)2 DbAdapter (org.apache.cayenne.dba.DbAdapter)2 DbSyncModule (org.apache.cayenne.dbsync.DbSyncModule)2 ToolsModule (org.apache.cayenne.dbsync.reverse.configuration.ToolsModule)2 Injector (org.apache.cayenne.di.Injector)2 NoopJdbcEventLogger (org.apache.cayenne.log.NoopJdbcEventLogger)2 DataMap (org.apache.cayenne.map.DataMap)2 Logger (org.slf4j.Logger)2 File (java.io.File)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 Preferences (java.util.prefs.Preferences)1 DataSource (javax.sql.DataSource)1 DataSourceInfo (org.apache.cayenne.conn.DataSourceInfo)1 AdhocObjectFactory (org.apache.cayenne.di.AdhocObjectFactory)1