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;
}
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);
}
}
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;
}
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();
}
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();
}
Aggregations