Search in sources :

Example 1 with DriverDef

use of org.kie.workbench.common.screens.datasource.management.model.DriverDef 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;
    }
}
Also used : URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo) DriverDefEditorContent(org.kie.workbench.common.screens.datasource.management.model.DriverDefEditorContent) Connection(java.sql.Connection) TestResult(org.kie.workbench.common.screens.datasource.management.model.TestResult) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) Properties(java.util.Properties) URI(java.net.URI)

Example 2 with DriverDef

use of org.kie.workbench.common.screens.datasource.management.model.DriverDef in project kie-wb-common by kiegroup.

the class DefaultDriverInitializerImpl method initializeFromProperties.

private void initializeFromProperties(Properties properties) {
    final Set<String> driverCodes = new HashSet<>();
    DriverDef driverDef;
    final org.uberfire.java.nio.file.Path globalPath = Paths.convert(serviceHelper.getGlobalDataSourcesContext());
    org.uberfire.java.nio.file.Path targetPath;
    String source;
    for (String propertyName : properties.stringPropertyNames()) {
        if (propertyName.length() > UUID.length() && propertyName.startsWith(UUID + ".")) {
            driverCodes.add(propertyName.substring(UUID.length() + 1, propertyName.length()));
        }
    }
    for (String driverCode : driverCodes) {
        driverDef = new DriverDef();
        driverDef.setUuid(getDriverParam(properties, UUID, driverCode));
        driverDef.setName(getDriverParam(properties, NAME, driverCode));
        driverDef.setDriverClass(getDriverParam(properties, DRIVER_CLASS, driverCode));
        driverDef.setGroupId(getDriverParam(properties, GROUP_ID, driverCode));
        driverDef.setArtifactId(getDriverParam(properties, ARTIFACT_ID, driverCode));
        driverDef.setVersion(getDriverParam(properties, VERSION, driverCode));
        if (driverDefValidator.validate(driverDef)) {
            targetPath = globalPath.resolve(driverDef.getName() + ".driver");
            try {
                if (!ioService.exists(targetPath)) {
                    source = DriverDefSerializer.serialize(driverDef);
                    serviceHelper.getDefRegistry().setEntry(Paths.convert(targetPath), driverDef);
                    ioService.write(targetPath, source, optionsFactory.makeCommentedOption("system generated driver"));
                }
            } catch (Exception e) {
                serviceHelper.getDefRegistry().invalidateCache(Paths.convert(targetPath));
                logger.error("It was not possible to write driver definition {} in path {}. ", driverDef, targetPath);
            }
        } else {
            logger.warn("Driver will be skipped due to invalid or uncompleted properties {}.", driverCode);
        }
    }
}
Also used : DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) HashSet(java.util.HashSet)

Example 3 with DriverDef

use of org.kie.workbench.common.screens.datasource.management.model.DriverDef 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;
        }
    };
}
Also used : URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) Properties(java.util.Properties) URI(java.net.URI) Before(org.junit.Before)

Example 4 with DriverDef

use of org.kie.workbench.common.screens.datasource.management.model.DriverDef in project kie-wb-common by kiegroup.

the class NewDriverWizardTest method testCreate.

/**
 * Emulates a sequence of valid data entering and the wizard completion.
 */
private void testCreate(final Module module) {
    when(path.toString()).thenReturn("target_driver_path");
    when(translationService.format(eq(DataSourceManagementConstants.NewDriverDefWizard_DriverCreatedMessage), anyVararg())).thenReturn("OkMessage");
    if (module != null) {
        when(driverDefService.create(any(DriverDef.class), eq(module))).thenReturn(path);
        driverDefWizard.setModule(module);
    } else {
        when(driverDefService.createGlobal(any(DriverDef.class))).thenReturn(path);
    }
    driverDefWizard.start();
    completeValidDefPage();
    driverDefWizard.complete();
    DriverDef expectedDriverDef = new DriverDef();
    expectedDriverDef.setName(NAME);
    expectedDriverDef.setGroupId(GROUP_ID);
    expectedDriverDef.setArtifactId(ARTIFACT_ID);
    expectedDriverDef.setVersion(VERSION);
    expectedDriverDef.setDriverClass(DRIVER_CLASS);
    if (module != null) {
        verify(driverDefService, times(1)).create(expectedDriverDef, module);
    } else {
        verify(driverDefService, times(1)).createGlobal(expectedDriverDef);
    }
    verify(notificationEvent, times(1)).fire(new NotificationEvent("OkMessage"));
}
Also used : NotificationEvent(org.uberfire.workbench.events.NotificationEvent) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef)

Example 5 with DriverDef

use of org.kie.workbench.common.screens.datasource.management.model.DriverDef in project kie-wb-common by kiegroup.

the class DriverDefEditorHelperTest method setup.

@Before
public void setup() {
    clientValidationService = new ClientValidationServiceMock();
    editorHelper = new DriverDefEditorHelper(translationService, clientValidationService);
    editorHelper.setHandler(handler);
    editorHelper.init(mainPanel);
    driverDef = new DriverDef();
    editorHelper.setDriverDef(driverDef);
    verify(mainPanel, times(1)).clear();
    verify(mainPanel, times(1)).setName(driverDef.getName());
    verify(mainPanel, times(1)).setDriverClass(driverDef.getDriverClass());
    verify(mainPanel, times(1)).setGroupId(driverDef.getGroupId());
    verify(mainPanel, times(1)).setArtifactId(driverDef.getArtifactId());
    verify(mainPanel, times(1)).setVersion(driverDef.getVersion());
}
Also used : DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) ClientValidationServiceMock(org.kie.workbench.common.screens.datasource.management.client.util.ClientValidationServiceMock) Before(org.junit.Before)

Aggregations

DriverDef (org.kie.workbench.common.screens.datasource.management.model.DriverDef)19 Before (org.junit.Before)6 URI (java.net.URI)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 DriverDefEditorContent (org.kie.workbench.common.screens.datasource.management.model.DriverDefEditorContent)3 URLConnectionFactory (org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory)3 ClientValidationServiceMock (org.kie.workbench.common.screens.datasource.management.client.util.ClientValidationServiceMock)2 DataSourceDef (org.kie.workbench.common.screens.datasource.management.model.DataSourceDef)2 DataSourceDeploymentInfo (org.kie.workbench.common.screens.datasource.management.model.DataSourceDeploymentInfo)2 DriverDefInfo (org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo)2 DriverDeploymentInfo (org.kie.workbench.common.screens.datasource.management.model.DriverDeploymentInfo)2 URL (java.net.URL)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 ConnectionFactory (org.apache.commons.dbcp2.ConnectionFactory)1 PoolableConnection (org.apache.commons.dbcp2.PoolableConnection)1 PoolableConnectionFactory (org.apache.commons.dbcp2.PoolableConnectionFactory)1 PoolingDataSource (org.apache.commons.dbcp2.PoolingDataSource)1