Search in sources :

Example 1 with ConnectionLostDialog

use of org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog in project dbeaver by serge-rider.

the class DatabaseLazyEditorInput method initializeRealInput.

public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
    final DBNModel navigatorModel = DBeaverCore.getInstance().getNavigatorModel();
    while (!dataSource.isConnected()) {
        boolean connected;
        try {
            connected = dataSource.connect(monitor, true, true);
        } catch (final DBException e) {
            // Connection error
            final Integer result = new UITask<Integer>() {

                @Override
                protected Integer runTask() {
                    ConnectionLostDialog clDialog = new ConnectionLostDialog(DBeaverUI.getActiveWorkbenchShell(), dataSource, e, "Close");
                    return clDialog.open();
                }
            }.execute();
            if (result == IDialogConstants.STOP_ID) {
                // Close editor
                return null;
            } else if (result == IDialogConstants.RETRY_ID) {
                continue;
            } else {
                return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSource));
            }
        }
        if (!connected) {
            throw new DBException("Connection to '" + dataSource.getName() + "' canceled");
        }
        break;
    }
    try {
        DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, dataSource, true);
        if (dsNode == null) {
            throw new DBException("Datasource '" + dataSource.getName() + "' navigator node not found");
        }
        dsNode.initializeNode(monitor, null);
        final DBNNode node = navigatorModel.getNodeByPath(monitor, project, nodePath);
        if (node == null) {
            throw new DBException("Navigator node '" + nodePath + "' not found");
        }
        if (node instanceof DBNDatabaseNode) {
            EntityEditorInput realInput = new EntityEditorInput((DBNDatabaseNode) node);
            realInput.setDefaultFolderId(activeFolderId);
            realInput.setDefaultPageId(activePageId);
            return realInput;
        } else {
            throw new DBException("Database node has bad type: " + node.getClass().getName());
        }
    } catch (DBException e) {
        return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSource));
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) EntityEditorInput(org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel)

Example 2 with ConnectionLostDialog

use of org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog in project dbeaver by serge-rider.

the class DataSourceInvalidateHandler method execute.

public static void execute(final Shell shell, final DBCExecutionContext context) {
    if (context != null) {
        //final DataSourceDescriptor dataSourceDescriptor = (DataSourceDescriptor) context;
        if (!ArrayUtils.isEmpty(Job.getJobManager().find(context.getDataSource().getContainer()))) {
            // Already connecting/disconnecting - just return
            return;
        }
        final InvalidateJob invalidateJob = new InvalidateJob(context);
        invalidateJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                StringBuilder message = new StringBuilder();
                Throwable error = null;
                int totalNum = 0, connectedNum = 0, aliveNum = 0;
                for (InvalidateJob.ContextInvalidateResult result : invalidateJob.getInvalidateResults()) {
                    totalNum++;
                    if (result.error != null) {
                        error = result.error;
                    }
                    switch(result.result) {
                        case CONNECTED:
                        case RECONNECTED:
                            connectedNum++;
                            break;
                        case ALIVE:
                            aliveNum++;
                            break;
                        default:
                            break;
                    }
                }
                if (connectedNum > 0) {
                    message.insert(0, "Connections reopened: " + connectedNum + " (of " + totalNum + ")");
                } else if (message.length() == 0) {
                    message.insert(0, "All connections (" + totalNum + ") are alive!");
                }
                if (error != null) {
                    //                        UIUtils.showErrorDialog(
                    //                            shell,
                    //                            "Invalidate data source [" + context.getDataSource().getContainer().getName() + "]",
                    //                            "Error while connecting to the datasource",// + "\nTime spent: " + RuntimeUtils.formatExecutionTime(invalidateJob.getTimeSpent()),
                    //                            error);
                    DBeaverUI.syncExec(new Runnable() {

                        @Override
                        public void run() {
                        }
                    });
                    final DBPDataSourceContainer container = context.getDataSource().getContainer();
                    final Throwable dialogError = error;
                    final Integer result = new UITask<Integer>() {

                        @Override
                        protected Integer runTask() {
                            ConnectionLostDialog clDialog = new ConnectionLostDialog(shell, container, dialogError, "Disconnect");
                            return clDialog.open();
                        }
                    }.execute();
                    if (result == IDialogConstants.STOP_ID) {
                        // Disconnect - to notify UI and reflect model changes
                        new DisconnectJob(container).schedule();
                    } else if (result == IDialogConstants.RETRY_ID) {
                        execute(shell, context);
                    }
                } else {
                    log.info(message);
                }
            }
        });
        invalidateJob.schedule();
    }
}
Also used : UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) DisconnectJob(org.jkiss.dbeaver.runtime.jobs.DisconnectJob) InvalidateJob(org.jkiss.dbeaver.runtime.jobs.InvalidateJob) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer)

Aggregations

UITask (org.jkiss.dbeaver.ui.UITask)2 ConnectionLostDialog (org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog)2 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)1 DBException (org.jkiss.dbeaver.DBException)1 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)1 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)1 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)1 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)1 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)1 DisconnectJob (org.jkiss.dbeaver.runtime.jobs.DisconnectJob)1 InvalidateJob (org.jkiss.dbeaver.runtime.jobs.InvalidateJob)1 EntityEditorInput (org.jkiss.dbeaver.ui.editors.entity.EntityEditorInput)1