use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.
the class ObjectListControl method loadData.
public void loadData(boolean lazy) {
if (this.loadingJob != null) {
int dataLoadUpdatePeriod = 200;
int dataLoadTimes = getDataLoadTimeout() / dataLoadUpdatePeriod;
try {
for (int i = 0; i < dataLoadTimes; i++) {
Thread.sleep(dataLoadUpdatePeriod);
if (this.loadingJob == null) {
break;
}
}
} catch (InterruptedException e) {
// interrupted
}
if (loadingJob != null) {
UIUtils.showMessageBox(getShell(), "Load", "Service is busy", SWT.ICON_WARNING);
return;
}
return;
}
getListPropertySource();
clearLazyCache();
this.lazyLoadCanceled = false;
if (lazy) {
// start loading service
synchronized (this) {
this.loadingJob = createLoadService();
if (this.loadingJob != null) {
this.loadingJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
loadingJob = null;
}
});
this.loadingJob.schedule(LAZY_LOAD_DELAY);
}
}
} else {
// Load data synchronously
final LoadingJob<Collection<OBJECT_TYPE>> loadService = createLoadService();
if (loadService != null) {
loadService.syncRun();
}
}
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.
the class ObjectListControl method addLazyObject.
private synchronized void addLazyObject(OBJECT_TYPE object, ObjectColumn column) {
if (lazyObjects == null) {
lazyObjects = new LinkedHashMap<>();
}
List<ObjectColumn> objectColumns = lazyObjects.get(object);
if (objectColumns == null) {
objectColumns = new ArrayList<>();
lazyObjects.put(object, objectColumns);
}
if (!objectColumns.contains(column)) {
objectColumns.add(column);
}
if (lazyLoadingJob == null) {
lazyLoadingJob = new LazyLoaderJob();
lazyLoadingJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
synchronized (ObjectListControl.this) {
if (lazyObjects == null || lazyObjects.isEmpty()) {
lazyLoadingJob = null;
} else {
lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
}
}
}
});
lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
}
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.
the class GenerateMultiSQLDialog method executeSQL.
@Override
protected void executeSQL() {
final String jobName = getShell().getText();
final SQLScriptProgressListener<T> scriptListener = getScriptListener();
final List<T> objects = getCheckedObjects();
final Map<T, List<String>> objectsSQL = new LinkedHashMap<>();
for (T object : objects) {
final List<String> lines = new ArrayList<>();
generateObjectCommand(lines, object);
objectsSQL.put(object, lines);
}
final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {
Exception objectProcessingError;
@Override
protected IStatus run(final DBRProgressMonitor monitor) {
final DataSourceJob curJob = this;
UIUtils.asyncExec(() -> scriptListener.beginScriptProcessing(curJob, objects));
monitor.beginTask(jobName, objects.size());
try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
if (isRunInSeparateTransaction()) {
commitChanges(session);
}
for (int i = 0; i < objects.size(); i++) {
if (monitor.isCanceled()) {
break;
}
final int objectNumber = i;
final T object = objects.get(i);
monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
objectProcessingError = null;
UIUtils.asyncExec(() -> scriptListener.beginObjectProcessing(object, objectNumber));
try {
final List<String> lines = objectsSQL.get(object);
for (String line : lines) {
try (final DBCStatement statement = DBUtils.makeStatement(session, line, false)) {
if (statement.executeStatement()) {
try (DBCResultSet resultSet = statement.openResultSet()) {
// Run in sync because we need result set
UIUtils.syncExec(() -> {
try {
scriptListener.processObjectResults(object, statement, resultSet);
} catch (DBCException e) {
objectProcessingError = e;
}
});
}
if (objectProcessingError != null) {
break;
}
} else {
UIUtils.syncExec(() -> {
try {
scriptListener.processObjectResults(object, statement, null);
} catch (DBCException e) {
objectProcessingError = e;
}
});
}
}
}
} catch (Exception e) {
objectProcessingError = e;
} finally {
UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
}
monitor.worked(1);
}
if (isRunInSeparateTransaction()) {
commitChanges(session);
}
} finally {
monitor.done();
UIUtils.asyncExec(scriptListener::endScriptProcessing);
}
return Status.OK_STATUS;
}
};
job.setUser(false);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (needsRefreshOnFinish()) {
List<T> objectToRefresh = new ArrayList<>(selectedObjects);
UIUtils.asyncExec(() -> {
try {
UIUtils.runInProgressDialog(monitor -> {
monitor.beginTask("Refresh objects", objectToRefresh.size());
for (T object : objectToRefresh) {
try {
DBNDatabaseNode objectNode = DBNUtils.getNodeByObject(object);
if (objectNode != null) {
objectNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
}
} catch (Exception e) {
log.error("Error refreshing object '" + object.getName() + "'", e);
}
}
monitor.done();
});
} catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Objects refresh", "Error refreshing navigator objects", e);
}
});
}
}
});
job.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.
the class GenerateSQLDialog method executeSQL.
protected void executeSQL() {
final String jobName = getShell().getText();
final String[] scriptLines = generateSQLScript();
DataSourceJob job = new DataSourceJob(jobName, executionContext) {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
for (String line : scriptLines) {
DBCStatement statement = DBUtils.makeStatement(session, line, false);
try {
statement.executeStatement();
} finally {
statement.close();
}
}
} catch (DBCException e) {
return GeneralUtils.makeExceptionStatus(e);
}
return Status.OK_STATUS;
}
};
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
if (onSuccess != null) {
onSuccess.run();
}
}
}
});
job.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent 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();
}
});
}
}
}
Aggregations