Search in sources :

Example 6 with DBDatasourceServiceException

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

the class PooledDatasourceHelperTest method testCreateDatasourceNoClassName.

@Test
public void testCreateDatasourceNoClassName() throws Exception {
    DatabaseDialectService dialectService = new DatabaseDialectService(false);
    final DatabaseTypeHelper databaseTypeHelper = new DatabaseTypeHelper(dialectService.getDatabaseTypes());
    final DatabaseConnection con = new DatabaseConnection();
    con.setId("Postgres");
    con.setName("Postgres");
    con.setAccessType(DatabaseAccessType.NATIVE);
    con.setDatabaseType(databaseTypeHelper.getDatabaseTypeByShortName("GENERIC"));
    con.setUsername("pentaho_user");
    con.setPassword("password");
    final HashMap<String, String> attrs = new HashMap<>();
    attrs.put(DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "");
    attrs.put(DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate");
    con.setAttributes(attrs);
    try {
        PooledDatasourceHelper.convert(con, () -> dialectService);
        fail("Expecting the exception to be thrown");
    } catch (DBDatasourceServiceException ex) {
        assertNotNull(ex);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) DatabaseTypeHelper(org.pentaho.database.util.DatabaseTypeHelper) HashMap(java.util.HashMap) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService) IDatabaseDialectService(org.pentaho.database.service.IDatabaseDialectService) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with DBDatasourceServiceException

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

the class PooledDatasourceHelperTest method testCreateDatasourceNoDialect.

@Test
public void testCreateDatasourceNoDialect() throws Exception {
    DatabaseDialectService dialectService = new DatabaseDialectService(false);
    final DatabaseConnection con = new DatabaseConnection();
    con.setId("Postgres");
    con.setName("Postgres");
    con.setAccessType(DatabaseAccessType.NATIVE);
    con.setUsername("pentaho_user");
    con.setPassword("password");
    final HashMap<String, String> attrs = new HashMap<>();
    attrs.put(DatabaseConnection.ATTRIBUTE_CUSTOM_DRIVER_CLASS, "org.postgresql.Driver");
    attrs.put(DatabaseConnection.ATTRIBUTE_CUSTOM_URL, "jdbc:postgresql://localhost:5432/hibernate");
    con.setAttributes(attrs);
    try {
        PooledDatasourceHelper.convert(con, () -> dialectService);
        fail("Expecting the exception to be thrown");
    } catch (DBDatasourceServiceException ex) {
        assertNotNull(ex);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) HashMap(java.util.HashMap) DatabaseDialectService(org.pentaho.database.service.DatabaseDialectService) IDatabaseDialectService(org.pentaho.database.service.IDatabaseDialectService) DatabaseConnection(org.pentaho.database.model.DatabaseConnection) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 8 with DBDatasourceServiceException

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

the class PooledDatasourceHelperTest method testSetupPooledDataSourceForJNDI.

@Test
public void testSetupPooledDataSourceForJNDI() {
    try {
        IDatabaseConnection databaseConnection = mock(IDatabaseConnection.class);
        when(databaseConnection.getAccessType()).thenReturn(DatabaseAccessType.JNDI);
        PooledDatasourceHelper.setupPooledDataSource(databaseConnection);
        fail("Expecting the exception to be thrown");
    } catch (DBDatasourceServiceException ex) {
        assertNotNull(ex);
    }
}
Also used : DBDatasourceServiceException(org.pentaho.platform.api.data.DBDatasourceServiceException) IDatabaseConnection(org.pentaho.database.model.IDatabaseConnection) Test(org.junit.Test)

Example 9 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 10 with DBDatasourceServiceException

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

the class BaseDatasourceService method getDSBoundName.

/**
 * Since JNDI is supported different ways in different app servers, it's nearly impossible to have a ubiquitous
 * way to look up a datasource. This method is intended to hide all the lookups that may be required to find a
 * jndi name, and return the actual bound name.
 *
 * @param dsName
 *          The Datasource name (like SampleData)
 * @return The bound DS name if it is bound in JNDI (like "jdbc/SampleData")
 * @throws DBDatasourceServiceException
 */
public String getDSBoundName(final String dsName) throws DBDatasourceServiceException {
    try {
        InitialContext ctx = new InitialContext();
        Object lkup = null;
        NamingException firstNe = null;
        String rtn = dsName;
        // First, try what they ask for...
        try {
            lkup = ctx.lookup(rtn);
            if (lkup != null) {
                return rtn;
            }
        } catch (NamingException ignored) {
            firstNe = ignored;
        }
        try {
            // Needed this for Jboss
            // $NON-NLS-1$
            rtn = "java:" + dsName;
            lkup = ctx.lookup(rtn);
            if (lkup != null) {
                return rtn;
            }
        } catch (NamingException ignored) {
        // ignored
        }
        try {
            // Tomcat
            // $NON-NLS-1$
            rtn = "java:comp/env/jdbc/" + dsName;
            lkup = ctx.lookup(rtn);
            if (lkup != null) {
                return rtn;
            }
        } catch (NamingException ignored) {
        // ignored
        }
        try {
            // Others?
            // $NON-NLS-1$
            rtn = "jdbc/" + dsName;
            lkup = ctx.lookup(rtn);
            if (lkup != null) {
                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)

Aggregations

DBDatasourceServiceException (org.pentaho.platform.api.data.DBDatasourceServiceException)22 IDatabaseConnection (org.pentaho.database.model.IDatabaseConnection)11 DataSource (javax.sql.DataSource)9 Test (org.junit.Test)7 IDatabaseDialectService (org.pentaho.database.service.IDatabaseDialectService)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 IDBDatasourceService (org.pentaho.platform.api.data.IDBDatasourceService)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 ObjectFactoryException (org.pentaho.platform.api.engine.ObjectFactoryException)3 SQLException (java.sql.SQLException)2 Properties (java.util.Properties)2 InitialContext (javax.naming.InitialContext)2 PoolingDataSource (org.apache.commons.dbcp.PoolingDataSource)2