提交 70f9d032 编写于 作者: S Serge Rider

Datasource jobs refactoring (events processing)

上级 0d0d455b
......@@ -29,6 +29,7 @@ import org.jkiss.dbeaver.model.app.DBPDataSourceRegistry;
import org.jkiss.dbeaver.model.app.DBPPlatform;
import org.jkiss.dbeaver.model.connection.DBPClientHome;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.model.connection.DBPConnectionEventType;
import org.jkiss.dbeaver.model.data.DBDDataFormatterProfile;
import org.jkiss.dbeaver.model.data.DBDPreferences;
import org.jkiss.dbeaver.model.data.DBDValueHandler;
......@@ -42,16 +43,14 @@ import org.jkiss.dbeaver.model.net.DBWHandlerType;
import org.jkiss.dbeaver.model.net.DBWNetworkHandler;
import org.jkiss.dbeaver.model.net.DBWTunnel;
import org.jkiss.dbeaver.model.preferences.DBPPropertySource;
import org.jkiss.dbeaver.model.runtime.DBRProcessDescriptor;
import org.jkiss.dbeaver.model.runtime.DBRProgressListener;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.model.runtime.*;
import org.jkiss.dbeaver.model.struct.*;
import org.jkiss.dbeaver.model.virtual.DBVModel;
import org.jkiss.dbeaver.registry.driver.DriverDescriptor;
import org.jkiss.dbeaver.registry.formatter.DataFormatterProfile;
import org.jkiss.dbeaver.runtime.TasksJob;
import org.jkiss.dbeaver.runtime.properties.PropertyCollector;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.dbeaver.ui.actions.datasource.DataSourceHandler;
import org.jkiss.utils.CommonUtils;
......@@ -651,6 +650,8 @@ public class DataSourceDescriptor
}
}
processEvents(monitor, DBPConnectionEventType.BEFORE_CONNECT);
tunnelConnectionInfo = null;
try {
// Handle tunnel
......@@ -695,6 +696,8 @@ public class DataSourceDescriptor
connectTime = new Date();
loginDate = connectTime;
processEvents(monitor, DBPConnectionEventType.AFTER_CONNECT);
if (reflect) {
getRegistry().notifyDataSourceListeners(new DBPEvent(
DBPEvent.Action.OBJECT_UPDATE,
......@@ -748,6 +751,33 @@ public class DataSourceDescriptor
}
private void processEvents(DBRProgressMonitor monitor, DBPConnectionEventType eventType)
{
DBPConnectionConfiguration info = getActualConnectionConfiguration();
DBRShellCommand command = info.getEvent(eventType);
if (command != null && command.isEnabled()) {
Map<String, Object> variables = new HashMap<>();
for (Map.Entry<Object, Object> entry : info.getProperties().entrySet()) {
variables.put(CommonUtils.toString(entry.getKey()), entry.getValue());
}
variables.put(RegistryConstants.VARIABLE_HOST, info.getHostName());
variables.put(RegistryConstants.VARIABLE_PORT, info.getHostPort());
variables.put(RegistryConstants.VARIABLE_SERVER, info.getServerName());
variables.put(RegistryConstants.VARIABLE_DATABASE, info.getDatabaseName());
variables.put(RegistryConstants.VARIABLE_USER, info.getUserName());
variables.put(RegistryConstants.VARIABLE_PASSWORD, info.getUserPassword());
variables.put(RegistryConstants.VARIABLE_URL, info.getUrl());
final DBRProcessDescriptor processDescriptor = new DBRProcessDescriptor(command, variables);
monitor.subTask("Execute process " + processDescriptor.getName());
DBUserInterface.getInstance().executeProcess(processDescriptor);
if (command.isWaitProcessFinish()) {
processDescriptor.waitFor();
}
addChildProcess(processDescriptor);
}
}
@Override
public boolean disconnect(final DBRProgressMonitor monitor)
throws DBException
......@@ -812,7 +842,11 @@ public class DataSourceDescriptor
}
}
monitor.beginTask("Disconnect from '" + getName() + "'", 4);
monitor.beginTask("Disconnect from '" + getName() + "'", 6);
processEvents(monitor, DBPConnectionEventType.BEFORE_DISCONNECT);
monitor.worked(1);
// Close datasource
monitor.subTask("Close connection");
......@@ -834,6 +868,10 @@ public class DataSourceDescriptor
}
monitor.worked(1);
processEvents(monitor, DBPConnectionEventType.AFTER_DISCONNECT);
monitor.worked(1);
monitor.done();
// Terminate child processes
......
......@@ -22,6 +22,16 @@ package org.jkiss.dbeaver.registry;
*/
public class RegistryConstants {
// Process parameters constants
public static final String VARIABLE_HOST = "host";
public static final String VARIABLE_PORT = "port";
public static final String VARIABLE_SERVER = "server";
public static final String VARIABLE_DATABASE = "database";
public static final String VARIABLE_USER = "user";
public static final String VARIABLE_PASSWORD = "password";
public static final String VARIABLE_URL = "url";
public static final String DRIVERS_FILE_NAME = "drivers.xml"; //$NON-NLS-1$
public static final String CONNECTION_TYPES_FILE_NAME = "connection-types.xml"; //$NON-NLS-1$
......
......@@ -22,10 +22,10 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.connection.DBPConnectionEventType;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.utils.GeneralUtils;
/**
......@@ -33,7 +33,7 @@ import org.jkiss.dbeaver.utils.GeneralUtils;
* Always returns OK status.
* To get real status use getConectStatus.
*/
public class ConnectJob extends EventProcessorJob
public class ConnectJob extends AbstractJob
{
private static final Log log = Log.getLog(ConnectJob.class);
......@@ -42,12 +42,14 @@ public class ConnectJob extends EventProcessorJob
protected boolean reflect = true;
protected Throwable connectError;
protected IStatus connectStatus;
protected final DBPDataSourceContainer container;
public ConnectJob(
DataSourceDescriptor container)
{
super(NLS.bind(CoreMessages.runtime_jobs_connect_name, container.getName()), container);
super(NLS.bind(CoreMessages.runtime_jobs_connect_name, container.getName()));
setUser(true);
this.container = container;
}
public IStatus getConnectStatus() {
......@@ -68,8 +70,6 @@ public class ConnectJob extends EventProcessorJob
connectThread.setName(NLS.bind(CoreMessages.runtime_jobs_connect_thread_name, container.getName()));
}
processEvents(DBPConnectionEventType.BEFORE_CONNECT);
try {
container.connect(monitor, initialize, reflect);
} finally {
......@@ -79,8 +79,6 @@ public class ConnectJob extends EventProcessorJob
}
}
processEvents(DBPConnectionEventType.AFTER_CONNECT);
connectStatus = Status.OK_STATUS;
}
catch (Throwable ex) {
......
......@@ -22,7 +22,7 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.osgi.util.NLS;
import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.connection.DBPConnectionEventType;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.utils.GeneralUtils;
......@@ -31,14 +31,16 @@ import org.jkiss.dbeaver.utils.GeneralUtils;
* Always returns OK status.
* To get real status use getConectStatus.
*/
public class DisconnectJob extends EventProcessorJob
public class DisconnectJob extends AbstractJob
{
private IStatus connectStatus;
protected final DBPDataSourceContainer container;
public DisconnectJob(DBPDataSourceContainer container)
{
super(NLS.bind(CoreMessages.runtime_jobs_disconnect_name, container.getName()), container);
super(NLS.bind(CoreMessages.runtime_jobs_disconnect_name, container.getName()));
setUser(true);
this.container = container;
}
public IStatus getConnectStatus() {
......@@ -49,9 +51,7 @@ public class DisconnectJob extends EventProcessorJob
protected IStatus run(DBRProgressMonitor monitor)
{
try {
processEvents(DBPConnectionEventType.BEFORE_DISCONNECT);
container.disconnect(monitor);
processEvents(DBPConnectionEventType.AFTER_DISCONNECT);
connectStatus = Status.OK_STATUS;
}
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2016 Serge Rieder (serge@jkiss.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License (version 2)
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jkiss.dbeaver.runtime.jobs;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.connection.DBPConnectionConfiguration;
import org.jkiss.dbeaver.model.connection.DBPConnectionEventType;
import org.jkiss.dbeaver.model.runtime.DBRProcessDescriptor;
import org.jkiss.dbeaver.model.runtime.DBRShellCommand;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.runtime.ui.DBUserInterface;
import org.jkiss.utils.CommonUtils;
import java.util.HashMap;
import java.util.Map;
/**
* Event processor job
*/
public abstract class EventProcessorJob extends AbstractJob {
private static final Log log = Log.getLog(EventProcessorJob.class);
public static final String VARIABLE_HOST = "host";
public static final String VARIABLE_PORT = "port";
public static final String VARIABLE_SERVER = "server";
public static final String VARIABLE_DATABASE = "database";
public static final String VARIABLE_USER = "user";
public static final String VARIABLE_PASSWORD = "password";
public static final String VARIABLE_URL = "url";
protected final DBPDataSourceContainer container;
protected EventProcessorJob(String name, DBPDataSourceContainer container)
{
super(name);
this.container = container;
}
protected void processEvents(DBPConnectionEventType eventType)
{
DBPConnectionConfiguration info = container.getActualConnectionConfiguration();
DBRShellCommand command = info.getEvent(eventType);
if (command != null && command.isEnabled()) {
Map<String, Object> variables = new HashMap<>();
for (Map.Entry<Object, Object> entry : info.getProperties().entrySet()) {
variables.put(CommonUtils.toString(entry.getKey()), entry.getValue());
}
variables.put(VARIABLE_HOST, info.getHostName());
variables.put(VARIABLE_PORT, info.getHostPort());
variables.put(VARIABLE_SERVER, info.getServerName());
variables.put(VARIABLE_DATABASE, info.getDatabaseName());
variables.put(VARIABLE_USER, info.getUserName());
variables.put(VARIABLE_PASSWORD, info.getUserPassword());
variables.put(VARIABLE_URL, info.getUrl());
final DBRProcessDescriptor processDescriptor = new DBRProcessDescriptor(command, variables);
DBUserInterface.getInstance().executeProcess(processDescriptor);
if (command.isWaitProcessFinish()) {
processDescriptor.waitFor();
}
if (container instanceof DataSourceDescriptor) {
((DataSourceDescriptor)container).addChildProcess(processDescriptor);
}
}
}
}
......@@ -231,9 +231,6 @@ public abstract class ConnectionWizard extends Wizard implements INewWizard {
if (dataSource == null) {
throw new DBException(CoreMessages.editors_sql_status_not_connected_to_database);
}
// monitor.subTask("Initialize connection");
// dataSource.initialize(monitor);
// monitor.worked(1);
monitor.subTask(CoreMessages.dialog_connection_wizard_start_connection_monitor_subtask_test);
DBPDataSourceInfo info = dataSource.getInfo();
......
......@@ -29,7 +29,7 @@ import org.jkiss.dbeaver.core.CoreMessages;
import org.jkiss.dbeaver.model.connection.DBPConnectionEventType;
import org.jkiss.dbeaver.model.runtime.DBRShellCommand;
import org.jkiss.dbeaver.registry.DataSourceDescriptor;
import org.jkiss.dbeaver.runtime.jobs.EventProcessorJob;
import org.jkiss.dbeaver.registry.RegistryConstants;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.UIUtils;
......@@ -155,13 +155,13 @@ public class EditShellCommandsDialogPage extends ActiveWizardPage<ConnectionWiza
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 2;
infoLabel.setLayoutData(gd);
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_HOST, "target host");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_PORT, "target port");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_SERVER, "target server name");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_DATABASE, "target database");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_USER, "user name");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_PASSWORD, "password (plain)");
addVariableLegend(helpGroup, EventProcessorJob.VARIABLE_URL, "JDBC URL");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_HOST, "target host");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_PORT, "target port");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_SERVER, "target server name");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_DATABASE, "target database");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_USER, "user name");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_PASSWORD, "password (plain)");
addVariableLegend(helpGroup, RegistryConstants.VARIABLE_URL, "JDBC URL");
}
selectEventType(null);
......@@ -209,7 +209,9 @@ public class EditShellCommandsDialogPage extends ActiveWizardPage<ConnectionWiza
command.setCommand(commandText.getText());
} else {
TableItem item = getEventItem(eventType);
command.setEnabled(item.getChecked());
if (item != null) {
command.setEnabled(item.getChecked());
}
command.setShowProcessPanel(showProcessCheck.getSelection());
command.setWaitProcessFinish(waitFinishCheck.getSelection());
command.setTerminateAtDisconnect(terminateCheck.getSelection());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册