Search in sources :

Example 26 with DatasourceServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException 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

DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)26 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)12 SQLException (java.sql.SQLException)10 ModelerException (org.pentaho.agilebi.modeler.ModelerException)10 Domain (org.pentaho.metadata.model.Domain)10 SerializedResultSet (org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet)10 MondrianCatalogServiceException (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException)9 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)8 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)8 SqlQueriesNotSupportedException (org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException)8 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)7 QueryValidationException (org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException)7 IOException (java.io.IOException)6 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)6 ArrayList (java.util.ArrayList)5 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)5 LogicalModel (org.pentaho.metadata.model.LogicalModel)5 SQLModelGeneratorException (org.pentaho.metadata.util.SQLModelGeneratorException)5 FileNotFoundException (java.io.FileNotFoundException)4 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)4