use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class ERDResourceHandler method createDiagram.
public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
if (folder == null) {
try {
folder = getDiagramsFolder(DBWorkbench.getPlatform().getWorkspace().getActiveProject(), true);
} catch (CoreException e) {
throw new DBException("Can't obtain folder for diagram", e);
}
}
if (folder == null) {
throw new DBException("Can't detect folder for diagram");
}
ContentUtils.checkFolderExists(folder, monitor);
final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
try {
DBRRunnableWithProgress runnable = monitor1 -> {
try {
EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>", new ERDContentProviderDecorated(), new ERDDecoratorDefault()) : copyFrom.copy();
newDiagram.setName(title);
newDiagram.setLayoutManualAllowed(true);
newDiagram.setLayoutManualDesired(true);
String diagramState = DiagramLoader.serializeDiagram(monitor1, null, newDiagram, false, false);
InputStream data = new ByteArrayInputStream(diagramState.getBytes(StandardCharsets.UTF_8));
file.create(data, true, RuntimeUtils.getNestedMonitor(monitor1));
} catch (Exception e) {
throw new InvocationTargetException(e);
}
};
if (monitor == null) {
UIUtils.runInProgressService(runnable);
} else {
runnable.run(monitor);
}
} catch (InvocationTargetException e) {
throw new DBException("Error creating diagram", e.getTargetException());
} catch (InterruptedException e) {
// interrupted
}
return file;
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class NavigatorHandlerObjectCreateCopy method pasteResource.
private void pasteResource(final File file, DBNResource toFolder) {
final IResource targetResource = toFolder.getResource();
assert targetResource != null;
final IContainer targetFolder = targetResource instanceof IContainer ? (IContainer) targetResource : targetResource.getParent();
try {
UIUtils.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
final IFile targetFile = targetFolder.getFile(new Path(file.getName()));
if (targetFile.exists()) {
throw new IOException("Target file '" + targetFile.getFullPath() + "' already exists");
}
try (InputStream is = new FileInputStream(file)) {
targetFile.create(is, true, monitor.getNestedMonitor());
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Copy error", "Error copying resource", e.getTargetException());
} catch (InterruptedException e) {
// ignore
}
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class JDBCDataSource method openConnection.
protected Connection openConnection(@NotNull DBRProgressMonitor monitor, @Nullable JDBCExecutionContext context, @NotNull String purpose) throws DBCException {
// It MUST be a JDBC driver
Driver driverInstance = null;
DBPDriver driver = getContainer().getDriver();
if (driver.isInstantiable() && !CommonUtils.isEmpty(driver.getDriverClassName())) {
try {
driverInstance = getDriverInstance(monitor);
} catch (DBException e) {
throw new DBCConnectException("Can't create driver instance", e, this);
}
} else {
if (!CommonUtils.isEmpty(driver.getDriverClassName())) {
try {
driver.loadDriver(monitor);
Class.forName(driver.getDriverClassName(), true, driver.getClassLoader());
} catch (Exception e) {
throw new DBCException("Driver class '" + driver.getDriverClassName() + "' not found", e);
}
}
}
DBPConnectionConfiguration connectionInfo = new DBPConnectionConfiguration(container.getActualConnectionConfiguration());
Properties connectProps = getAllConnectionProperties(monitor, context, purpose, connectionInfo);
final JDBCConnectionConfigurer connectionConfigurer = GeneralUtils.adapt(this, JDBCConnectionConfigurer.class);
DBAAuthModel authModel = connectionInfo.getAuthModel();
// Obtain connection
try {
if (connectionConfigurer != null) {
connectionConfigurer.beforeConnection(monitor, connectionInfo, connectProps);
}
final String url = getConnectionURL(connectionInfo);
if (driverInstance != null) {
try {
if (!driverInstance.acceptsURL(url)) {
// Just write a warning in log. Some drivers are poorly coded and always returns false here.
log.error("Bad URL: " + url);
}
} catch (Throwable e) {
log.debug("Error in " + driverInstance.getClass().getName() + ".acceptsURL() - " + url, e);
}
}
monitor.subTask("Connecting " + purpose);
Connection[] connection = new Connection[1];
Exception[] error = new Exception[1];
int openTimeout = getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT);
final Driver driverInstanceFinal = driverInstance;
try {
DBAAuthCredentials credentials = authModel.loadCredentials(getContainer(), connectionInfo);
authModel.initAuthentication(monitor, this, credentials, connectionInfo, connectProps);
} catch (DBException e) {
throw new DBCException("Authentication error: " + e.getMessage(), e);
}
DBRRunnableWithProgress connectTask = monitor1 -> {
try {
if (driverInstanceFinal == null) {
connection[0] = DriverManager.getConnection(url, connectProps);
} else {
connection[0] = driverInstanceFinal.connect(url, connectProps);
}
} catch (Exception e) {
error[0] = e;
} finally {
if (connectionConfigurer != null) {
try {
connectionConfigurer.afterConnection(monitor, connectionInfo, connectProps, connection[0], error[0]);
} catch (Exception e) {
log.debug(e);
}
}
}
};
boolean openTaskFinished;
try {
if (openTimeout <= 0) {
openTaskFinished = true;
connectTask.run(monitor);
} else {
openTaskFinished = RuntimeUtils.runTask(connectTask, "Opening database connection", openTimeout + 2000);
}
} finally {
authModel.endAuthentication(container, connectionInfo, connectProps);
}
if (error[0] != null) {
throw error[0];
}
if (!openTaskFinished) {
throw new DBCException("Connection has timed out");
}
if (connection[0] == null) {
throw new DBCException("Null connection returned");
}
// Set read-only flag
if (container.isConnectionReadOnly() && !isConnectionReadOnlyBroken()) {
connection[0].setReadOnly(true);
}
return connection[0];
} catch (SQLException ex) {
throw new DBCConnectException(ex.getMessage(), ex, this);
} catch (DBCException ex) {
throw ex;
} catch (Throwable e) {
throw new DBCConnectException("Unexpected driver error occurred while connecting to the database", e);
}
}
use of org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress in project dbeaver by serge-rider.
the class ResultSetViewer method restoreDataFilter.
private DBDDataFilter restoreDataFilter(final DBSDataContainer dataContainer) {
// Restore data filter
final DataFilterRegistry.SavedDataFilter savedConfig = DataFilterRegistry.getInstance().getSavedConfig(dataContainer);
if (savedConfig != null) {
final DBDDataFilter dataFilter = new DBDDataFilter();
DBRRunnableWithProgress restoreTask = monitor -> {
try {
savedConfig.restoreDataFilter(monitor, dataContainer, dataFilter);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
};
RuntimeUtils.runTask(restoreTask, "Restore data filter", 10000);
if (dataFilter.hasFilters()) {
return dataFilter;
}
}
return null;
}
Aggregations