Search in sources :

Example 6 with IDatabaseDialect

use of org.pentaho.database.IDatabaseDialect in project data-access by pentaho.

the class ConnectionService method createDatabaseConnection.

/**
 * Create a database connection
 *
 * @param driver
 *          String name of the driver to use
 * @param url
 *          String name of the url used to create the connection.
 *
 * @return IDatabaseConnection for the given parameters
 */
@GET
@Path("/createDatabaseConnection")
@Produces({ APPLICATION_JSON })
@Facet(name = "Unsupported")
public IDatabaseConnection createDatabaseConnection(@QueryParam("driver") String driver, @QueryParam("url") String url) {
    for (IDatabaseDialect dialect : dialectService.getDatabaseDialects()) {
        if (dialect.getNativeDriver() != null && dialect.getNativeDriver().equals(driver)) {
            if (dialect.getNativeJdbcPre() != null && url.startsWith(dialect.getNativeJdbcPre())) {
                return dialect.createNativeConnection(url);
            }
        }
    }
    // if no native driver was found, create a custom dialect object.
    IDatabaseConnection conn = genericDialect.createNativeConnection(url);
    conn.getAttributes().put(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS, driver);
    return conn;
}
Also used : IDatabaseDialect(org.pentaho.database.IDatabaseDialect) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Facet(org.codehaus.enunciate.Facet)

Example 7 with IDatabaseDialect

use of org.pentaho.database.IDatabaseDialect in project data-access by pentaho.

the class ConnectionServiceImpl method testConnection.

public boolean testConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    ensureDataAccessPermission();
    if (connection != null) {
        sanitizer.sanitizeConnectionParameters(connection);
        if (connection.getPassword() == null) {
            // Can have an empty password but not a null one
            // $NON-NLS-1$
            connection.setPassword("");
        }
        IDatabaseDialect dialect = dialectService.getDialect(connection);
        String driverClass = null;
        if (connection.getDatabaseType().getShortName().equals("GENERIC")) {
            driverClass = connection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS);
        } else {
            driverClass = dialect.getNativeDriver();
        }
        IPentahoConnection pentahoConnection = null;
        try {
            if (connection.getAccessType().equals(DatabaseAccessType.JNDI)) {
                pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connection.getDatabaseName(), null, this);
            } else {
                if (connection.isUsingConnectionPool()) {
                    Properties props = new Properties();
                    props.put(IPentahoConnection.CONNECTION_NAME, connection.getName());
                    props.put(IPentahoConnection.CONNECTION, connection);
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, props, null, this);
                } else {
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, driverClass, dialect.getURLWithExtraOptions(connection), connection.getUsername(), getConnectionPassword(connection.getName(), connection.getPassword()), null, this);
                }
            }
        } catch (DatabaseDialectException e) {
            throw new ConnectionServiceException(e);
        }
        if (pentahoConnection != null) {
            // make sure we have a native connection behind the SQLConnection object
            // if the native connection is null, the test of the connection failed
            boolean testedOk = ((SQLConnection) pentahoConnection).getNativeConnection() != null;
            pentahoConnection.close();
            return testedOk;
        } else {
            return false;
        }
    } else {
        // $NON-NLS-1$
        String message = Messages.getErrorString("ConnectionServiceImpl.ERROR_0008_UNABLE_TO_TEST_NULL_CONNECTION");
        logger.error(message);
        // $NON-NLS-1$
        throw new ConnectionServiceException(Response.SC_BAD_REQUEST, message);
    }
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) Properties(java.util.Properties)

Example 8 with IDatabaseDialect

use of org.pentaho.database.IDatabaseDialect in project data-access by pentaho.

the class InMemoryConnectionServiceImpl method getConnection.

/**
 * NOTE: caller is responsible for closing connection
 *
 * @param ds
 * @return
 * @throws DataSourceManagementException
 */
private static java.sql.Connection getConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    java.sql.Connection conn = null;
    String driverClass = connection.getAccessType().getClass().toString();
    if (StringUtils.isEmpty(driverClass)) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0020_CONNECTION_ATTEMPT_FAILED"));
        throw new ConnectionServiceException(Messages.getErrorString(// $NON-NLS-1$
        "ConnectionServiceInMemoryDelegate.ERROR_0020_CONNECTION_ATTEMPT_FAILED"));
    }
    Class<?> driverC = null;
    try {
        driverC = Class.forName(driverClass);
    } catch (ClassNotFoundException e) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0021_DRIVER_NOT_FOUND_IN_CLASSPATH", driverClass), e);
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0021_DRIVER_NOT_FOUND_IN_CLASSPATH"), // $NON-NLS-1$
        e);
    }
    if (!Driver.class.isAssignableFrom(driverC)) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0021_DRIVER_NOT_FOUND_IN_CLASSPATH", driverClass));
        throw new ConnectionServiceException(Messages.getErrorString(// $NON-NLS-1$
        "ConnectionServiceInMemoryDelegate.ERROR_0021_DRIVER_NOT_FOUND_IN_CLASSPATH"));
    }
    Driver driver = null;
    try {
        driver = driverC.asSubclass(Driver.class).newInstance();
    } catch (InstantiationException e) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0022_UNABLE_TO_INSTANCE_DRIVER", driverClass), e);
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0022_UNABLE_TO_INSTANCE_DRIVER"), // $NON-NLS-1$
        e);
    } catch (IllegalAccessException e) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0022_UNABLE_TO_INSTANCE_DRIVER", driverClass), e);
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0022_UNABLE_TO_INSTANCE_DRIVER"), // $NON-NLS-1$
        e);
    }
    try {
        DriverManager.registerDriver(driver);
        DatabaseDialectService dialectService = new DatabaseDialectService();
        IDatabaseDialect dialect = dialectService.getDialect(connection);
        conn = DriverManager.getConnection(dialect.getURLWithExtraOptions(connection), connection.getUsername(), connection.getPassword());
        return conn;
    } catch (SQLException e) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0023_UNABLE_TO_CONNECT"), e);
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0023_UNABLE_TO_CONNECT"), // $NON-NLS-1$
        e);
    } catch (DatabaseDialectException e) {
        logger.error(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0023_UNABLE_TO_CONNECT"), e);
        throw new ConnectionServiceException(Messages.getErrorString("ConnectionServiceInMemoryDelegate.ERROR_0023_UNABLE_TO_CONNECT"), // $NON-NLS-1$
        e);
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) SQLException(java.sql.SQLException) Driver(java.sql.Driver) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService)

Example 9 with IDatabaseDialect

use of org.pentaho.database.IDatabaseDialect in project data-access by pentaho.

the class DatasourceInMemoryServiceHelper method getDataSourceConnection.

/**
 * NOTE: caller is responsible for closing connection
 *
 * @param connectionName
 * @return
 * @throws DatasourceServiceException
 */
public static java.sql.Connection getDataSourceConnection(String connectionName) throws DatasourceServiceException {
    IDatabaseConnection connection = null;
    try {
        ConnectionServiceImpl service = new ConnectionServiceImpl();
        connection = service.getConnectionByName(connectionName);
    } catch (ConnectionServiceException e1) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0008_CONNECTION_SERVICE_EXCEPTION"));
        // we should return null because we do not able to use connection
        return null;
    }
    java.sql.Connection conn = null;
    DatabaseDialectService dialectService = new DatabaseDialectService();
    IDatabaseDialect dialect = dialectService.getDialect(connection);
    String driverClass = null;
    if (connection.getDatabaseType().getShortName().equals("GENERIC")) {
        driverClass = connection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS);
    } else {
        driverClass = dialect.getNativeDriver();
    }
    if (StringUtils.isEmpty(driverClass)) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0001_CONNECTION_ATTEMPT_FAILED"));
        throw new DatasourceServiceException(Messages.getErrorString(// $NON-NLS-1$
        "DatasourceInMemoryServiceHelper.ERROR_0001_CONNECTION_ATTEMPT_FAILED"));
    }
    Class<?> driverC = null;
    try {
        driverC = Class.forName(driverClass);
    } catch (ClassNotFoundException e) {
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0002_DRIVER_NOT_FOUND_IN_CLASSPATH", driverClass), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0002_DRIVER_NOT_FOUND_IN_CLASSPATH"), // $NON-NLS-1$
        e);
    }
    if (!Driver.class.isAssignableFrom(driverC)) {
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0002_DRIVER_NOT_FOUND_IN_CLASSPATH", // $NON-NLS-1$
        driverClass));
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0002_DRIVER_NOT_FOUND_IN_CLASSPATH", // $NON-NLS-1$
        driverClass));
    }
    Driver driver = null;
    try {
        driver = driverC.asSubclass(Driver.class).newInstance();
    } catch (InstantiationException e) {
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0003_UNABLE_TO_INSTANCE_DRIVER", driverClass), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0003_UNABLE_TO_INSTANCE_DRIVER"), // $NON-NLS-1$
        e);
    } catch (IllegalAccessException e) {
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0003_UNABLE_TO_INSTANCE_DRIVER", driverClass), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0003_UNABLE_TO_INSTANCE_DRIVER"), // $NON-NLS-1$
        e);
    }
    try {
        DriverManager.registerDriver(driver);
        conn = DriverManager.getConnection(dialect.getURLWithExtraOptions(connection), connection.getUsername(), connection.getPassword());
        return conn;
    } catch (SQLException e) {
        logger.error(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0004_UNABLE_TO_CONNECT"), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0004_UNABLE_TO_CONNECT"), // $NON-NLS-1$
        e);
    } catch (DatabaseDialectException e) {
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0004_UNABLE_TO_CONNECT"), // $NON-NLS-1$
        e);
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) SQLException(java.sql.SQLException) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) Driver(java.sql.Driver) ConnectionServiceImpl(org.pentaho.platform.dataaccess.datasource.wizard.service.impl.ConnectionServiceImpl) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Aggregations

IDatabaseDialect (org.pentaho.database.IDatabaseDialect)9 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)6 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)4 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)4 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)4 Driver (java.sql.Driver)2 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)2 ConnectionServiceImpl (org.pentaho.platform.dataaccess.datasource.wizard.service.impl.ConnectionServiceImpl)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 NamingException (javax.naming.NamingException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 AbandonedConfig (org.apache.commons.dbcp.AbandonedConfig)1 AbandonedObjectPool (org.apache.commons.dbcp.AbandonedObjectPool)1 ConnectionFactory (org.apache.commons.dbcp.ConnectionFactory)1 DriverManagerConnectionFactory (org.apache.commons.dbcp.DriverManagerConnectionFactory)1 PoolableConnectionFactory (org.apache.commons.dbcp.PoolableConnectionFactory)1