Search in sources :

Example 1 with DBDatasourceServiceException

use of org.pentaho.platform.api.data.DBDatasourceServiceException in project pdi-platform-plugin by pentaho.

the class PlatformKettleDataSourceProvider method getNamedDataSourceFromService.

protected <T extends IDBDatasourceService> DataSource getNamedDataSourceFromService(Class<T> dataSourceServiceInterface, String dataSourceName) throws DataSourceNamingException {
    T datasourceService = PentahoSystem.get(dataSourceServiceInterface, null);
    IDBDatasourceService service = (datasourceService == null) ? PentahoSystem.get(IDBDatasourceService.class, null) : datasourceService;
    if (service != null) {
        try {
            return service.getDataSource(dataSourceName);
        } catch (DBDatasourceServiceException ex) {
            throw new DataSourceNamingException(ex);
        }
    }
    return null;
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DataSourceNamingException(org.pentaho.di.core.database.DataSourceNamingException) IDBDatasourceService(org.pentaho.platform.api.data.IDBDatasourceService)

Example 2 with DBDatasourceServiceException

use of org.pentaho.platform.api.data.DBDatasourceServiceException in project pentaho-platform by pentaho.

the class PooledDatasourceHelper method getJndiDataSource.

public static DataSource getJndiDataSource(final String dsName) throws DBDatasourceServiceException {
    try {
        InitialContext ctx = new InitialContext();
        Object lkup = null;
        DataSource rtn = null;
        NamingException firstNe = null;
        // First, try what they ask for...
        try {
            lkup = ctx.lookup(dsName);
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
            firstNe = ignored;
        }
        try {
            // Needed this for Jboss
            // $NON-NLS-1$
            lkup = ctx.lookup("java:" + dsName);
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
        // ignored
        }
        try {
            // Tomcat
            // $NON-NLS-1$
            lkup = ctx.lookup("java:comp/env/jdbc/" + dsName);
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
        // ignored
        }
        try {
            // Others?
            // $NON-NLS-1$
            lkup = ctx.lookup("jdbc/" + dsName);
            if (lkup != null) {
                rtn = (DataSource) lkup;
                return rtn;
            }
        } catch (NamingException ignored) {
        // ignored
        }
        if (firstNe != null) {
            throw new DBDatasourceServiceException(firstNe);
        }
        throw new DBDatasourceServiceException(dsName);
    } catch (NamingException ne) {
        throw new DBDatasourceServiceException(ne);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) DataSource(javax.sql.DataSource) PoolingDataSource(org.apache.commons.dbcp.PoolingDataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource)

Example 3 with DBDatasourceServiceException

use of org.pentaho.platform.api.data.DBDatasourceServiceException in project pentaho-platform by pentaho.

the class PooledDatasourceHelper method convert.

@VisibleForTesting
static DataSource convert(IDatabaseConnection databaseConnection, Supplier<IDatabaseDialectService> dialectSupplier) throws DBDatasourceServiceException {
    // From Spring
    DriverManagerDataSource basicDatasource = new DriverManagerDataSource();
    IDatabaseDialect dialect = Optional.ofNullable(dialectSupplier.get()).orElseThrow(() -> new DBDatasourceServiceException(Messages.getInstance().getErrorString("PooledDatasourceHelper.ERROR_0001_DATASOURCE_CANNOT_LOAD_DIALECT_SVC"))).getDialect(databaseConnection);
    if (databaseConnection.getDatabaseType() == null && dialect == null) {
        // We do not have enough information to create a DataSource. Throwing exception
        throw new DBDatasourceServiceException(Messages.getInstance().getErrorString("PooledDatasourceHelper.ERROR_0001_DATASOURCE_CREATE_ERROR_NO_DIALECT", databaseConnection.getName()));
    }
    if (databaseConnection.getDatabaseType().getShortName().equals("GENERIC")) {
        // $NON-NLS-1$
        String driverClassName = databaseConnection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS);
        if (!StringUtils.isEmpty(driverClassName)) {
            initDriverClass(basicDatasource, dialect, driverClassName, databaseConnection.getName());
        } else {
            // We do not have enough information to create a DataSource. Throwing exception
            throw new DBDatasourceServiceException(Messages.getInstance().getErrorString("PooledDatasourceHelper.ERROR_0002_DATASOURCE_CREATE_ERROR_NO_CLASSNAME", databaseConnection.getName()));
        }
    } else {
        if (!StringUtils.isEmpty(dialect.getNativeDriver())) {
            initDriverClass(basicDatasource, dialect, dialect.getNativeDriver(), databaseConnection.getName());
        } else {
            // We do not have enough information to create a DataSource. Throwing exception
            throw new DBDatasourceServiceException(Messages.getInstance().getErrorString("PooledDatasourceHelper.ERROR_0003_DATASOURCE_CREATE_ERROR_NO_DRIVER", databaseConnection.getName()));
        }
    }
    try {
        basicDatasource.setUrl(dialect.getURLWithExtraOptions(databaseConnection));
    } catch (DatabaseDialectException e) {
        basicDatasource.setUrl(null);
    }
    basicDatasource.setUsername(databaseConnection.getUsername());
    basicDatasource.setPassword(databaseConnection.getPassword());
    return basicDatasource;
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 4 with DBDatasourceServiceException

use of org.pentaho.platform.api.data.DBDatasourceServiceException in project pentaho-platform by pentaho.

the class DynamicConnectionDatasourceServiceTest method setUp.

@Before
public void setUp() {
    mockConnection = mock(IDatabaseConnection.class);
    // Set it up - this is a NATIVE connection
    when(mockConnection.getAccessType()).thenReturn(DatabaseAccessType.NATIVE);
    when(mockConnection.getDatabaseName()).thenReturn(dsName);
    DataSource mockDs = mock(DataSource.class);
    IDatasourceMgmtService mockMgmtService = mock(IDatasourceMgmtService.class);
    DynamicallyPooledOrJndiDatasourceService dynamic = new DynamicallyPooledOrJndiDatasourceService();
    dynamic.setNonPooledDatasourceService(nonPooledSpyService);
    dynamic.setPooledDatasourceService(pooledSpyService);
    spyService = spy(dynamic);
    try {
        when(mockMgmtService.getDatasourceByName(dsName)).thenReturn(mockConnection);
    } catch (DatasourceMgmtServiceException e) {
        e.printStackTrace();
    }
    try {
        doReturn(mockDs).when(nonPooledSpyService).resolveDatabaseConnection(mockConnection);
        doReturn(mockDs).when(pooledSpyService).resolveDatabaseConnection(mockConnection);
    } catch (DBDatasourceServiceException e) {
        e.printStackTrace();
    }
    doReturn(mockMgmtService).when(nonPooledSpyService).getDatasourceMgmtService();
    doReturn(mockMgmtService).when(pooledSpyService).getDatasourceMgmtService();
    doReturn(mockMgmtService).when(spyService).getDatasourceMgmtService();
    spyService.clearCache();
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DataSource(javax.sql.DataSource) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Before(org.junit.Before)

Example 5 with DBDatasourceServiceException

use of org.pentaho.platform.api.data.DBDatasourceServiceException in project pentaho-platform by pentaho.

the class JNDIDatasourceServiceTest method setUp.

@Before
public void setUp() {
    IDatabaseConnection mockConnection = mock(IDatabaseConnection.class);
    // Set it up - this is a JNDI connection
    when(mockConnection.getAccessType()).thenReturn(DatabaseAccessType.JNDI);
    when(mockConnection.getDatabaseName()).thenReturn(dsName);
    DataSource mockDs = mock(DataSource.class);
    IDatasourceMgmtService mockMgmtService = mock(IDatasourceMgmtService.class);
    spyService = spy(service);
    try {
        when(mockMgmtService.getDatasourceByName(dsName)).thenReturn(mockConnection);
    } catch (DatasourceMgmtServiceException e) {
        e.printStackTrace();
    }
    try {
        doReturn(mockDs).when(spyService).getJndiDataSource(dsName);
    } catch (DBDatasourceServiceException e) {
        e.printStackTrace();
    }
    doReturn(mockMgmtService).when(spyService).getDatasourceMgmtService();
    service.clearCache();
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) DataSource(javax.sql.DataSource) IDatasourceMgmtService(org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService) DatasourceMgmtServiceException(org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException) Before(org.junit.Before)

Aggregations

DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)23 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)11 DataSource (javax.sql.DataSource)10 Test (org.junit.Test)7 IDatabaseDialectService (org.pentaho.database.service.IDatabaseDialectService)6 IDBDatasourceService (org.pentaho.platform.api.data.IDBDatasourceService)6 HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 DatabaseConnection (org.pentaho.database.model.DatabaseConnection)5 DatabaseDialectService (org.pentaho.database.service.DatabaseDialectService)5 DatasourceMgmtServiceException (org.pentaho.platform.api.repository.datasource.DatasourceMgmtServiceException)5 IDatasourceMgmtService (org.pentaho.platform.api.repository.datasource.IDatasourceMgmtService)5 NamingException (javax.naming.NamingException)3 Before (org.junit.Before)3 DatabaseTypeHelper (org.pentaho.database.util.DatabaseTypeHelper)3 DataSourceNamingException (org.pentaho.di.core.database.DataSourceNamingException)3 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)3 SQLException (java.sql.SQLException)2 InitialContext (javax.naming.InitialContext)2 PoolingDataSource (org.apache.commons.dbcp.PoolingDataSource)2