use of org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory in project kie-wb-common by kiegroup.
the class DBCPDataSourceProviderTest method setup.
@Before
public void setup() throws Exception {
super.setup();
driverDef1 = new DriverDef();
driverDef1.setUuid(DRIVER1_UUID);
driverDef1.setName(DRIVER1_NAME);
driverDef1.setDriverClass(DRIVER1_CLASS);
driverDef1.setArtifactId(ARTIFACT_ID);
driverDef1.setGroupId(GROUP_ID);
driverDef1.setVersion(VERSION);
dbcpDrivers = new ArrayList<>();
dbcpDrivers.add(driverDef1);
driver1Uri = new URI("file:///maven_dir/driver1_file.jar");
when(artifactResolver.resolve(driverDef1.getGroupId(), driverDef1.getArtifactId(), driverDef1.getVersion())).thenReturn(driver1Uri);
driverProvider = dbcpDriverProvider;
dataSourceProvider = new DBCPDataSourceProvider(dbcpDriverProvider, artifactResolver) {
@Override
protected URLConnectionFactory buildConnectionFactory(URI uri, String driverClass, String connectionURL, Properties connectionProperties) throws Exception {
return urlConnectionFactory;
}
};
}
use of org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory in project kie-wb-common by kiegroup.
the class DataSourceDefEditorServiceImpl method testConnection.
@Override
public TestResult testConnection(DataSourceDef dataSourceDef, Module module) {
TestResult result = new TestResult(false);
if (isEmpty(dataSourceDef.getConnectionURL())) {
result.setMessage("A valid connection url is required");
return result;
}
if (isEmpty(dataSourceDef.getUser()) || isEmpty(dataSourceDef.getPassword())) {
result.setMessage("A valid user and password are required");
return result;
}
DriverDefInfo driverDefInfo = null;
if (isEmpty(dataSourceDef.getDriverUuid())) {
result.setMessage("A valid driver is required");
return result;
}
if (module != null) {
driverDefInfo = dataSourceDefQueryService.findModuleDriver(dataSourceDef.getDriverUuid(), module.getRootPath());
} else {
driverDefInfo = dataSourceDefQueryService.findGlobalDriver(dataSourceDef.getDriverUuid());
}
if (driverDefInfo == null) {
result.setMessage("Data source driver: " + dataSourceDef.getUuid() + " was not found");
return result;
}
DriverDefEditorContent driverDefEditorContent = driverDefService.loadContent(driverDefInfo.getPath());
DriverDef driverDef = driverDefEditorContent.getDef();
URI uri;
try {
uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(), driverDef.getVersion());
} catch (Exception e) {
result.setMessage("Connection could not be tested due to the following error: " + e.getMessage());
return result;
}
if (uri == null) {
result.setMessage("Driver artifact: " + driverDef.getGroupId() + ":" + driverDef.getArtifactId() + ":" + driverDef.getVersion() + " was not found");
return result;
}
try {
Properties properties = new Properties();
properties.put("user", dataSourceDef.getUser());
properties.put("password", dataSourceDef.getPassword());
URLConnectionFactory connectionFactory = new URLConnectionFactory(uri.toURL(), driverDef.getDriverClass(), dataSourceDef.getConnectionURL(), properties);
Connection conn = connectionFactory.createConnection();
if (conn == null) {
result.setMessage("It was not possible to open connection");
} else {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Connection was successfully obtained: " + conn);
stringBuilder.append("\n");
stringBuilder.append("*** DatabaseProductName: " + conn.getMetaData().getDatabaseProductName());
stringBuilder.append("\n");
stringBuilder.append("*** DatabaseProductVersion: " + conn.getMetaData().getDatabaseProductVersion());
stringBuilder.append("\n");
stringBuilder.append("*** DriverName: " + conn.getMetaData().getDriverName());
stringBuilder.append("\n");
stringBuilder.append("*** DriverVersion: " + conn.getMetaData().getDriverVersion());
stringBuilder.append("\n");
conn.close();
stringBuilder.append("Connection was successfully released.");
stringBuilder.append("\n");
result.setTestPassed(true);
result.setMessage(stringBuilder.toString());
}
return result;
} catch (Exception e) {
result.setMessage(e.getMessage());
return result;
}
}
use of org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory in project kie-wb-common by kiegroup.
the class DBCPDataSourceProvider method deploy.
@Override
public DataSourceDeploymentInfo deploy(DataSourceDef dataSourceDef) throws Exception {
DriverDef driverDef = null;
for (DriverDef _driverDef : driverProvider.getDeployments()) {
if (_driverDef.getUuid().equals(dataSourceDef.getDriverUuid())) {
driverDef = _driverDef;
break;
}
}
if (driverDef == null) {
throw new Exception("Required driver: " + dataSourceDef.getDriverUuid() + " is not deployed");
}
final URI uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(), driverDef.getVersion());
if (uri == null) {
throw new Exception("Unable to get driver library artifact for driver: " + driverDef);
}
final Properties properties = new Properties();
properties.setProperty("user", dataSourceDef.getUser());
properties.setProperty("password", dataSourceDef.getPassword());
final URLConnectionFactory urlConnectionFactory = buildConnectionFactory(uri, driverDef.getDriverClass(), dataSourceDef.getConnectionURL(), properties);
// Connection Factory that the pool will use for creating connections.
ConnectionFactory connectionFactory = new DBCPConnectionFactory(urlConnectionFactory);
// Poolable connection factory
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
// The pool to be used by the ConnectionFactory
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
// Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool);
// Finally create DataSource
PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);
DataSourceDeploymentInfo deploymentInfo = new DataSourceDeploymentInfo(dataSourceDef.getUuid(), true, dataSourceDef.getUuid(), false);
deploymentRegistry.put(deploymentInfo.getDeploymentId(), new DBCPDataSource(dataSource));
deploymentInfos.put(deploymentInfo.getDeploymentId(), deploymentInfo);
deployedDataSources.put(deploymentInfo.getDeploymentId(), dataSourceDef);
return deploymentInfo;
}
Aggregations