Search in sources :

Example 21 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class JdbcDatasourceResourceTest method testGetConnectionIDsError.

@Test
public void testGetConnectionIDsError() throws Exception {
    ConnectionServiceException mockConnectionServiceException = mock(ConnectionServiceException.class);
    doThrow(mockConnectionServiceException).when(jdbcDatasourceResource.service).getConnections();
    try {
        JaxbList<String> connections = jdbcDatasourceResource.getConnectionIDs();
        fail("Should get WebApplicationException");
    } catch (WebApplicationException e) {
    // good
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 22 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class MultitableDatasourceService method getTableFields.

public List<String> getTableFields(String table, IDatabaseConnection connection) throws DatasourceServiceException {
    try {
        DatabaseMeta databaseMeta = this.getDatabaseMeta(connection);
        Database database = new Database(null, databaseMeta);
        database.connect();
        String query = databaseMeta.getSQLQueryFields(table);
        // Setting the query limit to 1 before executing the query
        database.setQueryLimit(1);
        database.getRows(query, 1);
        String[] tableFields = database.getReturnRowMeta().getFieldNames();
        List<String> fields = Arrays.asList(tableFields);
        database.disconnect();
        return fields;
    } catch (KettleDatabaseException e) {
        logger.error(e);
        throw new DatasourceServiceException(e);
    } catch (ConnectionServiceException e) {
        logger.error(e);
        throw new DatasourceServiceException(e);
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 23 with ConnectionServiceException

use of org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException in project data-access by pentaho.

the class MultitableDatasourceService method getDatabaseTables.

public List<String> getDatabaseTables(IDatabaseConnection connection, String schema) throws DatasourceServiceException {
    try {
        DatabaseMeta databaseMeta = this.getDatabaseMeta(connection);
        Database database = new Database(null, databaseMeta);
        database.connect();
        String[] tableNames = database.getTablenames(schema, true, this.isDataServicesConnection(connection) ? new HashMap<String, String>() {

            {
                put("STREAMING", "N");
            }
        } : null);
        List<String> tables = new ArrayList<String>();
        tables.addAll(Arrays.asList(tableNames));
        tables.addAll(Arrays.asList(database.getViews(schema, true)));
        database.disconnect();
        return tables;
    } catch (KettleDatabaseException e) {
        logger.error("Error creating database object", e);
        throw new DatasourceServiceException(e);
    } catch (ConnectionServiceException e) {
        logger.error("Error getting database meta", e);
        throw new DatasourceServiceException(e);
    }
}
Also used : ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) HashMap(java.util.HashMap) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) ArrayList(java.util.ArrayList) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 24 with ConnectionServiceException

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

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

ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)26 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)9 Test (org.junit.Test)8 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)8 DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)8 PentahoAccessControlException (org.pentaho.platform.api.engine.PentahoAccessControlException)6 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)6 Path (javax.ws.rs.Path)4 IDatabaseDialect (org.pentaho.database.IDatabaseDialect)4 DuplicateDatasourceException (org.pentaho.platform.api.repository.datasource.DuplicateDatasourceException)4 NonExistingDatasourceException (org.pentaho.platform.api.repository.datasource.NonExistingDatasourceException)4 ArrayList (java.util.ArrayList)3 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 Response (javax.ws.rs.core.Response)3 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)3 Database (org.pentaho.di.core.database.Database)3 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)3