提交 700ba023 编写于 作者: S Serge Rider

Navigator: object create redesign (run in non-ui job). Application model refactoring

上级 58ca5ee9
......@@ -37,7 +37,6 @@ import org.jkiss.dbeaver.model.navigator.DBNModel;
import org.jkiss.dbeaver.model.qm.QMController;
import org.jkiss.dbeaver.model.qm.QMUtils;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.runtime.OSDescriptor;
import org.jkiss.dbeaver.registry.DataSourceProviderRegistry;
import org.jkiss.dbeaver.registry.PluginServiceRegistry;
......@@ -87,7 +86,7 @@ public class DBeaverCore implements DBPApplication {
private QMControllerImpl queryManager;
private QMLogFileWriter qmLogWriter;
private ProjectRegistry projectRegistry;
private DefaultCertificateStorage certificateStorage;
private DBACertificateStorage certificateStorage;
private final List<IPluginService> activatedServices = new ArrayList<>();
......@@ -394,12 +393,6 @@ public class DBeaverCore implements DBPApplication {
return tempFolder;
}
@NotNull
@Override
public DBRRunnableContext getRunnableContext() {
return DBeaverUI.getDefaultRunnableContext();
}
@NotNull
public List<IProject> getLiveProjects()
{
......
......@@ -23,6 +23,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
......@@ -76,6 +77,8 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
public static final String OLD_CONFIG_FILE_NAME = "data-sources.xml"; //$NON-NLS-1$
private static PasswordEncrypter ENCRYPTOR = new SimpleStringEncrypter();
private final DBPApplication application;
private final IProject project;
......@@ -347,7 +350,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
if (file.exists()) {
File dsFile = file.getLocation().toFile();
if (dsFile.exists()) {
loadDataSources(dsFile, new SimpleStringEncrypter(), refresh, parseResults);
loadDataSources(dsFile, refresh, parseResults);
}
}
}
......@@ -380,14 +383,14 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
}
}
private void loadDataSources(File fromFile, PasswordEncrypter encrypter, boolean refresh, ParseResults parseResults)
private void loadDataSources(File fromFile, boolean refresh, ParseResults parseResults)
{
if (!fromFile.exists()) {
return;
}
boolean extraConfig = !fromFile.getName().equalsIgnoreCase(CONFIG_FILE_NAME);
try (InputStream is = new FileInputStream(fromFile)) {
loadDataSources(is, encrypter, extraConfig, refresh, parseResults);
loadDataSources(is, extraConfig, refresh, parseResults);
} catch (DBException ex) {
log.warn("Error loading datasource config from " + fromFile.getAbsolutePath(), ex);
} catch (IOException ex) {
......@@ -395,12 +398,12 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
}
}
private void loadDataSources(InputStream is, PasswordEncrypter encrypter, boolean extraConfig, boolean refresh, ParseResults parseResults)
private void loadDataSources(InputStream is, boolean extraConfig, boolean refresh, ParseResults parseResults)
throws DBException, IOException
{
SAXReader parser = new SAXReader(is);
try {
final DataSourcesParser dsp = new DataSourcesParser(extraConfig, refresh, parseResults, encrypter);
final DataSourcesParser dsp = new DataSourcesParser(extraConfig, refresh, parseResults);
parser.parse(dsp);
}
catch (XMLException ex) {
......@@ -417,7 +420,6 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
}
updateProjectNature();
final IProgressMonitor progressMonitor = new NullProgressMonitor();
PasswordEncrypter encrypter = new SimpleStringEncrypter();
IFile configFile = getProject().getFile(CONFIG_FILE_NAME);
saveInProgress = true;
try {
......@@ -432,7 +434,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
xml.startElement("data-sources");
for (DataSourceDescriptor dataSource : localDataSources) {
if (!dataSource.isProvided()) {
saveDataSource(xml, dataSource, encrypter);
saveDataSource(xml, dataSource);
}
}
xml.endElement();
......@@ -490,7 +492,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
}
}
private void saveDataSource(XMLBuilder xml, DataSourceDescriptor dataSource, PasswordEncrypter encrypter)
private void saveDataSource(XMLBuilder xml, DataSourceDescriptor dataSource)
throws IOException
{
xml.startElement(RegistryConstants.TAG_DATA_SOURCE);
......@@ -535,29 +537,16 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
xml.addAttribute(RegistryConstants.ATTR_DATABASE, CommonUtils.notEmpty(connectionInfo.getDatabaseName()));
xml.addAttribute(RegistryConstants.ATTR_URL, CommonUtils.notEmpty(connectionInfo.getUrl()));
xml.addAttribute(RegistryConstants.ATTR_USER, CommonUtils.notEmpty(connectionInfo.getUserName()));
clearSecuredPassword(dataSource, null);
if (dataSource.isSavePassword() && !CommonUtils.isEmpty(connectionInfo.getUserPassword())) {
/*
try {
final ISecurePreferences dsNode = dataSource.getSecurePreferences();
if (!CommonUtils.isEmpty(connectionInfo.getUserPassword())) {
dsNode.put(RegistryConstants.ATTR_PASSWORD, connectionInfo.getUserPassword(), true);
} else {
dsNode.remove(RegistryConstants.ATTR_PASSWORD);
}
} catch (StorageException e) {
log.error("Can't save password in secure storage", e);
}
*/
String encPassword = connectionInfo.getUserPassword();
if (!CommonUtils.isEmpty(encPassword)) {
if (!saveSecuredPassword(dataSource, null, connectionInfo.getUserPassword())) {
try {
encPassword = encrypter.encrypt(encPassword);
}
catch (EncryptionException e) {
log.error("Can't encrypt password. Save it as is", e);
xml.addAttribute(RegistryConstants.ATTR_PASSWORD, ENCRYPTOR.encrypt(connectionInfo.getUserPassword()));
} catch (EncryptionException e) {
log.error("Error encrypting password", e);
}
}
xml.addAttribute(RegistryConstants.ATTR_PASSWORD, encPassword);
}
if (!CommonUtils.isEmpty(connectionInfo.getClientHomeId())) {
xml.addAttribute(RegistryConstants.ATTR_HOME, connectionInfo.getClientHomeId());
......@@ -604,7 +593,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
String encPassword = configuration.getPassword();
if (!CommonUtils.isEmpty(encPassword)) {
try {
encPassword = encrypter.encrypt(encPassword);
encPassword = ENCRYPTOR.encrypt(encPassword);
}
catch (EncryptionException e) {
log.error("Can't encrypt password. Save it as is", e);
......@@ -696,6 +685,28 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
xml.endElement();
}
private boolean saveSecuredPassword(DataSourceDescriptor dataSource, String subNode, String password) {
try {
ISecurePreferences prefNode = dataSource.getSecurePreferences();
if (subNode != null) {
prefNode = prefNode.node(subNode);
}
if (!CommonUtils.isEmpty(password)) {
prefNode.put(RegistryConstants.ATTR_PASSWORD, password, true);
} else {
prefNode.remove(RegistryConstants.ATTR_PASSWORD);
}
} catch (StorageException e) {
log.error("Can't save password in secure storage", e);
}
return false;
}
private void clearSecuredPassword(DataSourceDescriptor dataSource, String subNode) {
}
private void saveObjectFiler(XMLBuilder xml, String typeName, String objectID, DBSObjectFilter filter) throws IOException
{
xml.startElement(RegistryConstants.TAG_FILTER);
......@@ -741,7 +752,6 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
DataSourceDescriptor curDataSource;
boolean extraConfig;
boolean refresh;
PasswordEncrypter encrypter;
boolean isDescription = false;
DBRShellCommand curCommand = null;
private DBWHandlerConfiguration curNetworkHandler;
......@@ -749,12 +759,11 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
private StringBuilder curQuery;
private ParseResults parseResults;
private DataSourcesParser(boolean extraConfig, boolean refresh, ParseResults parseResults, PasswordEncrypter encrypter)
private DataSourcesParser(boolean extraConfig, boolean refresh, ParseResults parseResults)
{
this.extraConfig = extraConfig;
this.refresh = refresh;
this.parseResults = parseResults;
this.encrypter = encrypter;
}
@Override
......@@ -1049,7 +1058,7 @@ public class DataSourceRegistry implements DBPDataSourceRegistry
{
if (!CommonUtils.isEmpty(encPassword)) {
try {
encPassword = encrypter.decrypt(encPassword);
encPassword = ENCRYPTOR.decrypt(encPassword);
}
catch (Throwable e) {
// could not decrypt - use as is
......
......@@ -24,7 +24,6 @@ import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.core.DBeaverNature;
import org.jkiss.dbeaver.core.DBeaverUI;
import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.DBPExternalFileManager;
import org.jkiss.dbeaver.model.DBPProjectManager;
import org.jkiss.dbeaver.model.project.DBPProjectListener;
......@@ -174,11 +173,6 @@ public class ProjectRegistry implements DBPProjectManager, DBPExternalFileManage
}
}
@Override
public DBPApplication geApplication() {
return DBeaverCore.getInstance();
}
@Override
public DBPResourceHandler getResourceHandler(IResource resource)
{
......
......@@ -30,10 +30,10 @@ import org.jkiss.dbeaver.model.edit.DBEObjectManager;
import org.jkiss.dbeaver.model.navigator.DBNContainer;
import org.jkiss.dbeaver.model.navigator.DBNDatabaseNode;
import org.jkiss.dbeaver.model.navigator.DBNNode;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry;
import org.jkiss.dbeaver.ui.AbstractUIJob;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.editors.IDatabaseEditor;
import org.jkiss.dbeaver.ui.editors.entity.EntityEditor;
......@@ -106,7 +106,7 @@ public abstract class NavigatorHandlerObjectCreateBase extends NavigatorHandlerO
job.schedule();
}
static class CreateJob<OBJECT_TYPE extends DBSObject, CONTAINER_TYPE> extends AbstractUIJob {
static class CreateJob<OBJECT_TYPE extends DBSObject, CONTAINER_TYPE> extends AbstractJob {
private final CommandTarget commandTarget;
private final DBEObjectMaker<OBJECT_TYPE, CONTAINER_TYPE> objectMaker;
private final CONTAINER_TYPE parentObject;
......@@ -122,7 +122,7 @@ public abstract class NavigatorHandlerObjectCreateBase extends NavigatorHandlerO
}
@Override
protected IStatus runInUIThread(DBRProgressMonitor monitor) {
protected IStatus run(DBRProgressMonitor monitor) {
try {
newObject = objectMaker.createNewObject(monitor, commandTarget.getContext(), parentObject, sourceObject);
if (newObject == null) {
......@@ -141,35 +141,13 @@ public abstract class NavigatorHandlerObjectCreateBase extends NavigatorHandlerO
}
}
IWorkbenchWindow workbenchWindow = DBeaverUI.getActiveWorkbenchWindow();
try {
final boolean openEditor = (objectMaker.getMakerOptions() & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
final DBNDatabaseNode newChild = DBeaverCore.getInstance().getNavigatorModel().findNode(newObject);
if (newChild != null) {
DatabaseNavigatorView view = UIUtils.findView(workbenchWindow, DatabaseNavigatorView.class);
if (view != null) {
view.showNode(newChild);
}
IDatabaseEditor editor = commandTarget.getEditor();
if (editor != null) {
// Just activate existing editor
workbenchWindow.getActivePage().activate(editor);
} else if (openEditor) {
// Open new one with existing context
EntityEditorInput editorInput = new EntityEditorInput(
newChild,
commandTarget.getContext());
workbenchWindow.getActivePage().openEditor(
editorInput,
EntityEditor.class.getName());
}
} else {
throw new DBException("Can't find node corresponding to new object");
// Open object in UI thread
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
openNewObject();
}
} catch (Throwable e) {
UIUtils.showErrorDialog(workbenchWindow.getShell(), "Create object", null, e);
}
});
return Status.OK_STATUS;
} catch (Exception e) {
......@@ -177,6 +155,38 @@ public abstract class NavigatorHandlerObjectCreateBase extends NavigatorHandlerO
}
}
private void openNewObject() {
IWorkbenchWindow workbenchWindow = DBeaverUI.getActiveWorkbenchWindow();
try {
final boolean openEditor = (objectMaker.getMakerOptions() & DBEObjectMaker.FEATURE_EDITOR_ON_CREATE) != 0;
final DBNDatabaseNode newChild = DBeaverCore.getInstance().getNavigatorModel().findNode(newObject);
if (newChild != null) {
DatabaseNavigatorView view = UIUtils.findView(workbenchWindow, DatabaseNavigatorView.class);
if (view != null) {
view.showNode(newChild);
}
IDatabaseEditor editor = commandTarget.getEditor();
if (editor != null) {
// Just activate existing editor
workbenchWindow.getActivePage().activate(editor);
} else if (openEditor) {
// Open new one with existing context
EntityEditorInput editorInput = new EntityEditorInput(
newChild,
commandTarget.getContext());
workbenchWindow.getActivePage().openEditor(
editorInput,
EntityEditor.class.getName());
}
} else {
throw new DBException("Can't find node corresponding to new object");
}
} catch (Throwable e) {
UIUtils.showErrorDialog(workbenchWindow.getShell(), "Create object", null, e);
}
}
}
}
\ No newline at end of file
......@@ -417,7 +417,12 @@ public class EntityEditor extends MultiPageDatabaseEditor
@Override
public void onCommandChange(DBECommand command)
{
firePropertyChange(IEditorPart.PROP_DIRTY);
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
firePropertyChange(IEditorPart.PROP_DIRTY);
}
});
}
};
DBECommandContext commandContext = getCommandContext();
......
......@@ -36,6 +36,7 @@ import org.jkiss.dbeaver.model.impl.sql.edit.struct.SQLTableColumnManager;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSDataType;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.ui.UITask;
import org.jkiss.dbeaver.ui.editors.object.struct.AttributeEditPage;
import org.jkiss.utils.CommonUtils;
......@@ -91,23 +92,28 @@ public class MySQLTableColumnManager extends SQLTableColumnManager<MySQLTableCol
}
@Override
protected MySQLTableColumn createDatabaseObject(DBRProgressMonitor monitor, DBECommandContext context, MySQLTableBase parent, Object copyFrom)
protected MySQLTableColumn createDatabaseObject(final DBRProgressMonitor monitor, final DBECommandContext context, final MySQLTableBase parent, Object copyFrom)
{
DBSDataType columnType = findBestDataType(parent.getDataSource(), "varchar"); //$NON-NLS-1$
final MySQLTableColumn column = new MySQLTableColumn(parent);
column.setName(getNewColumnName(monitor, context, parent));
final String typeName = columnType == null ? "integer" : columnType.getName().toLowerCase();
column.setTypeName(typeName); //$NON-NLS-1$
column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
column.setValueType(columnType == null ? Types.INTEGER : columnType.getTypeID());
column.setOrdinalPosition(-1);
column.setFullTypeName(DBUtils.getFullTypeName(column));
AttributeEditPage page = new AttributeEditPage(null, column);
if (!page.edit()) {
return null;
}
return column;
return new UITask<MySQLTableColumn>() {
@Override
protected MySQLTableColumn runTask() {
DBSDataType columnType = findBestDataType(parent.getDataSource(), "varchar"); //$NON-NLS-1$
final MySQLTableColumn column = new MySQLTableColumn(parent);
column.setName(getNewColumnName(monitor, context, parent));
final String typeName = columnType == null ? "integer" : columnType.getName().toLowerCase();
column.setTypeName(typeName); //$NON-NLS-1$
column.setMaxLength(columnType != null && columnType.getDataKind() == DBPDataKind.STRING ? 100 : 0);
column.setValueType(columnType == null ? Types.INTEGER : columnType.getTypeID());
column.setOrdinalPosition(-1);
column.setFullTypeName(DBUtils.getFullTypeName(column));
AttributeEditPage page = new AttributeEditPage(null, column);
if (!page.edit()) {
return null;
}
return column;
}
}.execute();
}
@Override
......
......@@ -27,7 +27,6 @@ import org.jkiss.dbeaver.model.edit.DBERegistry;
import org.jkiss.dbeaver.model.navigator.DBNModel;
import org.jkiss.dbeaver.model.qm.QMController;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import java.io.File;
import java.io.IOException;
......@@ -46,6 +45,7 @@ public interface DBPApplication
@NotNull
DBPProjectManager getProjectManager();
@NotNull
Collection<IProject> getLiveProjects();
......@@ -67,6 +67,4 @@ public interface DBPApplication
@NotNull
File getTempFolder(DBRProgressMonitor monitor, String name) throws IOException;
@NotNull
DBRRunnableContext getRunnableContext();
}
......@@ -28,8 +28,6 @@ import org.jkiss.dbeaver.model.project.DBPResourceHandler;
*/
public interface DBPProjectManager
{
DBPApplication geApplication();
DBPResourceHandler getResourceHandler(IResource resource);
IProject getActiveProject();
......
......@@ -18,11 +18,15 @@
package org.jkiss.dbeaver.model.admin;
import org.eclipse.equinox.security.storage.ISecurePreferences;
/**
* Secure preferences.
* Used to store passwords.
* Client security manager
*/
public interface DBASecurePreferences
public interface DBAClientSecurity
{
boolean useSecurePreferences();
ISecurePreferences getSecurePreferences();
}
......@@ -17,14 +17,16 @@
*/
package org.jkiss.dbeaver.model.navigator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.*;
import org.jkiss.dbeaver.model.messages.ModelMessages;
import org.jkiss.dbeaver.model.runtime.AbstractJob;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
......@@ -245,22 +247,25 @@ public class DBNProjectDatabases extends DBNNode implements DBNContainer, DBPEve
if (parentNode.getChildNodes() == null && parentNode.allowsChildren()) {
final DBNDatabaseNode nodeToLoad = parentNode;
// We have to load children here
try {
model.getApplication().getRunnableContext().run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException
{
try {
nodeToLoad.getChildren(monitor);
} catch (Exception e) {
throw new InvocationTargetException(e);
}
final AbstractJob loaderJob = new AbstractJob("Load sibling nodes of new database object") {
{
setUser(true);
}
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try {
nodeToLoad.getChildren(monitor);
} catch (Exception e) {
return GeneralUtils.makeExceptionStatus(e);
}
});
} catch (InvocationTargetException e) {
log.error(e.getTargetException());
return Status.OK_STATUS;
}
};
loaderJob.schedule();
try {
loaderJob.join();
} catch (InterruptedException e) {
// do nothing
// That's ok
}
}
if (!parentFound) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册