Search in sources :

Example 1 with ConnectJob

use of org.jkiss.dbeaver.runtime.jobs.ConnectJob in project dbeaver by serge-rider.

the class DataSourceHandler method connectToDataSource.

/**
 * Connects datasource
 * @param monitor progress monitor or null. If nul then new job will be started
 * @param dataSourceContainer    container to connect
 * @param onFinish               finish handler
 */
public static void connectToDataSource(@Nullable DBRProgressMonitor monitor, @NotNull DBPDataSourceContainer dataSourceContainer, @Nullable final DBRProgressListener onFinish) {
    if (dataSourceContainer instanceof DataSourceDescriptor && !dataSourceContainer.isConnected()) {
        final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) dataSourceContainer;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(dataSourceDescriptor))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final ConnectJob connectJob = new ConnectJob(dataSourceDescriptor);
        final JobChangeAdapter jobChangeAdapter = new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                IStatus result = connectJob.getConnectStatus();
                if (result.isOK()) {
                    if (!dataSourceDescriptor.isSavePassword()) {
                    // Rest password back to null
                    // TODO: to be correct we need to reset password info.
                    // but we need a password to open isolated contexts (e.g. for data export)
                    // Currently it is not possible to ask for password from isolation context opening
                    // procedure. We need to do something here...
                    // dataSourceDescriptor.getConnectionConfiguration().setUserName(oldName);
                    // dataSourceDescriptor.getConnectionConfiguration().setUserPassword(oldPassword);
                    }
                }
                if (onFinish != null) {
                    onFinish.onTaskFinished(result);
                } else if (!result.isOK()) {
                    UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError(connectJob.getName(), // NLS.bind(CoreMessages.runtime_jobs_connect_status_error, dataSourceContainer.getName()),
                    null, result));
                }
            }
        };
        if (monitor != null) {
            connectJob.runSync(monitor);
            jobChangeAdapter.done(new IJobChangeEvent() {

                @Override
                public long getDelay() {
                    return 0;
                }

                @Override
                public Job getJob() {
                    return connectJob;
                }

                @Override
                public IStatus getResult() {
                    return connectJob.getConnectStatus();
                }

                public IStatus getJobGroupResult() {
                    return null;
                }
            });
        } else {
            connectJob.addJobChangeListener(jobChangeAdapter);
            // Schedule in UI because connect may be initiated during application startup
            // and UI is still not initiated. In this case no progress dialog will appear
            // to be sure run in UI async
            UIUtils.asyncExec(new Runnable() {

                @Override
                public void run() {
                    connectJob.schedule();
                }
            });
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) Job(org.eclipse.core.runtime.jobs.Job) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Example 2 with ConnectJob

use of org.jkiss.dbeaver.runtime.jobs.ConnectJob in project dbeaver by dbeaver.

the class DataSourceHandler method connectToDataSource.

/**
 * Connects datasource
 * @param monitor progress monitor or null. If nul then new job will be started
 * @param dataSourceContainer    container to connect
 * @param onFinish               finish handler
 */
public static void connectToDataSource(@Nullable DBRProgressMonitor monitor, @NotNull DBPDataSourceContainer dataSourceContainer, @Nullable final DBRProgressListener onFinish) {
    if (dataSourceContainer instanceof DataSourceDescriptor && !dataSourceContainer.isConnected()) {
        final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) dataSourceContainer;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(dataSourceDescriptor))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final ConnectJob connectJob = new ConnectJob(dataSourceDescriptor);
        final JobChangeAdapter jobChangeAdapter = new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                IStatus result = connectJob.getConnectStatus();
                if (result.isOK()) {
                    if (!dataSourceDescriptor.isSavePassword()) {
                    // Rest password back to null
                    // TODO: to be correct we need to reset password info.
                    // but we need a password to open isolated contexts (e.g. for data export)
                    // Currently it is not possible to ask for password from isolation context opening
                    // procedure. We need to do something here...
                    // dataSourceDescriptor.getConnectionConfiguration().setUserName(oldName);
                    // dataSourceDescriptor.getConnectionConfiguration().setUserPassword(oldPassword);
                    }
                }
                if (onFinish != null) {
                    onFinish.onTaskFinished(result);
                } else if (!result.isOK()) {
                    DBUserInterface.getInstance().showError(connectJob.getName(), // NLS.bind(CoreMessages.runtime_jobs_connect_status_error, dataSourceContainer.getName()),
                    null, result);
                }
            }
        };
        if (monitor != null) {
            connectJob.runSync(monitor);
            jobChangeAdapter.done(new IJobChangeEvent() {

                @Override
                public long getDelay() {
                    return 0;
                }

                @Override
                public Job getJob() {
                    return connectJob;
                }

                @Override
                public IStatus getResult() {
                    return connectJob.getConnectStatus();
                }

                public IStatus getJobGroupResult() {
                    return null;
                }
            });
        } else {
            connectJob.addJobChangeListener(jobChangeAdapter);
            // Schedule in UI because connect may be initiated during application startup
            // and UI is still not initiated. In this case no progress dialog will appear
            // to be sure run in UI async
            DBeaverUI.asyncExec(new Runnable() {

                @Override
                public void run() {
                    connectJob.schedule();
                }
            });
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) ConnectJob(org.jkiss.dbeaver.runtime.jobs.ConnectJob) Job(org.eclipse.core.runtime.jobs.Job) DataSourceDescriptor(org.jkiss.dbeaver.registry.DataSourceDescriptor)

Aggregations

IStatus (org.eclipse.core.runtime.IStatus)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)2 Job (org.eclipse.core.runtime.jobs.Job)2 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)2 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)2 ConnectJob (org.jkiss.dbeaver.runtime.jobs.ConnectJob)2 DisconnectJob (org.jkiss.dbeaver.runtime.jobs.DisconnectJob)2