提交 56d0eef1 编写于 作者: S Serge Rider

#104 Get rid of temp project

上级 e59e8b2e
......@@ -18,8 +18,13 @@
package org.jkiss.dbeaver.core;
import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
......@@ -44,6 +49,7 @@ import org.jkiss.dbeaver.runtime.net.GlobalProxyAuthenticator;
import org.jkiss.dbeaver.runtime.net.GlobalProxySelector;
import org.jkiss.dbeaver.runtime.qm.QMControllerImpl;
import org.jkiss.dbeaver.runtime.qm.QMLogFileWriter;
import org.jkiss.utils.CommonUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.Version;
......@@ -51,7 +57,7 @@ import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.ProxySelector;
import java.text.MessageFormat;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
......@@ -71,8 +77,8 @@ public class DBeaverCore implements DBPApplication {
private static boolean standalone = false;
private static volatile boolean isClosing = false;
private File tempFolder;
private IWorkspace workspace;
private IProject tempProject;
private OSDescriptor localSystem;
private DBNModel navigatorModel;
......@@ -170,6 +176,33 @@ public class DBeaverCore implements DBPApplication {
{
long startTime = System.currentTimeMillis();
log.debug("Initialize Core...");
// Temp folder
{
try {
final java.nio.file.Path tempDirectory = Files.createTempDirectory(TEMP_PROJECT_NAME);
tempFolder = tempDirectory.toFile();
} catch (IOException e) {
final String sysTempFolder = System.getProperty("java.io.tmpdir");
if (!CommonUtils.isEmpty(sysTempFolder)) {
tempFolder = new File(sysTempFolder, TEMP_PROJECT_NAME);
if (!tempFolder.mkdirs()){
final String sysUserFolder = System.getProperty("user.home");
if (!CommonUtils.isEmpty(sysUserFolder)) {
tempFolder = new File(sysUserFolder, TEMP_PROJECT_NAME);
if (!tempFolder.mkdirs()){
tempFolder = new File(TEMP_PROJECT_NAME);
if (!tempFolder.mkdirs()){
log.error("Can't create temp directory!");
}
}
}
}
}
}
}
// Register properties adapter
this.workspace = ResourcesPlugin.getWorkspace();
......@@ -215,30 +248,6 @@ public class DBeaverCore implements DBPApplication {
} catch (DBException e) {
log.error("Error loading projects", e);
}
try {
// Temp project
tempProject = workspace.getRoot().getProject(TEMP_PROJECT_NAME);
if (tempProject.exists()) {
try {
tempProject.delete(true, true, monitor);
} catch (CoreException e) {
log.error("Can't delete temp project", e);
}
}
IProjectDescription description = workspace.newProjectDescription(TEMP_PROJECT_NAME);
description.setName(TEMP_PROJECT_NAME);
description.setComment("Project for DBeaver temporary content");
try {
tempProject.create(description, IProject.HIDDEN, monitor);
} catch (CoreException e) {
log.error("Can't create temp project", e);
}
tempProject.open(monitor);
} catch (Throwable e) {
log.error("Cannot open temp project", e); //$NON-NLS-1$
}
}
public synchronized void dispose()
......@@ -272,18 +281,6 @@ public class DBeaverCore implements DBPApplication {
this.projectRegistry = null;
}
// Cleanup temp project
IProgressMonitor monitor = new NullProgressMonitor();
if (workspace != null) {
if (tempProject != null && tempProject.exists()) {
try {
tempProject.delete(true, true, monitor);
} catch (CoreException e) {
log.warn("Can't cleanup temp project", e);
}
}
}
if (this.qmLogWriter != null) {
this.queryManager.unregisterMetaListener(qmLogWriter);
this.qmLogWriter.dispose();
......@@ -297,6 +294,7 @@ public class DBeaverCore implements DBPApplication {
if (isStandalone() && workspace != null) {
try {
IProgressMonitor monitor = new NullProgressMonitor();
workspace.save(true, monitor);
} catch (CoreException ex) {
log.error("Can't save workspace", ex); //$NON-NLS-1$
......@@ -375,27 +373,8 @@ public class DBeaverCore implements DBPApplication {
return projectRegistry;
}
public IProject getTempProject() {
return tempProject;
}
@NotNull
public IFolder getTempFolder(DBRProgressMonitor monitor, String name)
throws IOException
{
if (tempProject == null) {
throw new IOException("Temp project wasn't initialized properly");
}
IPath tempPath = tempProject.getProjectRelativePath().append(name);
IFolder tempFolder = tempProject.getFolder(tempPath);
if (!tempFolder.exists()) {
try {
tempFolder.create(true, true, monitor.getNestedMonitor());
tempFolder.setHidden(true);
} catch (CoreException ex) {
throw new IOException(MessageFormat.format(CoreMessages.DBeaverCore_error_can_create_temp_dir, tempFolder.toString()), ex);
}
}
public File getTempFolder(DBRProgressMonitor monitor, String name) {
return tempFolder;
}
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2015 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;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.jkiss.dbeaver.ui.editors.IPersistentStorage;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import java.io.*;
/**
* LocalFileStorage
*/
public class LocalFileStorage implements IStorage, IPersistentStorage {
private final File file;
public LocalFileStorage(File file) {
this.file = file;
}
@Override
public InputStream getContents() throws CoreException {
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new CoreException(GeneralUtils.makeExceptionStatus(e));
}
}
@Override
public IPath getFullPath() {
return new Path(file.getAbsolutePath());
}
@Override
public String getName() {
return file.getName();
}
@Override
public boolean isReadOnly() {
return !file.canWrite();
}
@Override
public void setContents(IProgressMonitor monitor, InputStream stream) throws CoreException {
try (OutputStream os = new FileOutputStream(file)) {
ContentUtils.copyStreams(stream, 0, os, new DefaultProgressMonitor(monitor));
} catch (IOException e) {
throw new CoreException(GeneralUtils.makeExceptionStatus(e));
}
}
@Override
public <T> T getAdapter(Class<T> adapter) {
return null;
}
}
\ No newline at end of file
......@@ -309,7 +309,7 @@ public class ContentEditor extends MultiPageAbstractEditor implements IValueEdit
} catch (Exception e) {
log.warn("Can't determine value content length", e);
// Get file length
contentLength = getEditorInput().getFile().getFullPath().toFile().length();
contentLength = getEditorInput().getContentFile().length();
}
MimeType mimeType = ContentUtils.getMimeType(contentType);
IEditorPart defaultPage = null, preferredPage = null;
......
......@@ -17,7 +17,6 @@
*/
package org.jkiss.dbeaver.ui.editors.content;
import org.jkiss.dbeaver.Log;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.*;
......@@ -31,11 +30,13 @@ import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.*;
import org.eclipse.ui.part.MultiPageEditorActionBarContributor;
import org.eclipse.ui.texteditor.BasicTextEditorActionContributor;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.UIIcon;
import org.jkiss.dbeaver.ui.dialogs.DialogUtils;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.DialogUtils;
import org.jkiss.dbeaver.utils.ContentUtils;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
......@@ -118,17 +119,13 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
if (this.activeEditor != null) {
if (encodingCombo != null && !encodingCombo.isDisposed()) {
try {
String curCharset = this.activeEditor.getEditorInput().getFile().getCharset();
int charsetCount = encodingCombo.getItemCount();
for (int i = 0; i < charsetCount; i++) {
if (encodingCombo.getItem(i).equals(curCharset)) {
encodingCombo.select(i);
break;
}
String curCharset = ContentUtils.DEFAULT_CHARSET;
int charsetCount = encodingCombo.getItemCount();
for (int i = 0; i < charsetCount; i++) {
if (encodingCombo.getItem(i).equals(curCharset)) {
encodingCombo.select(i);
break;
}
} catch (CoreException e) {
log.error(e);
}
}
applyAction.setEnabled(activeEditor.isDirty());
......@@ -189,11 +186,7 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
{
String curCharset = null;
if (getEditor() != null) {
try {
curCharset = getEditor().getEditorInput().getFile().getCharset();
} catch (CoreException e) {
log.error(e);
}
curCharset = getEditor().getEditorInput().getFileCharset();
}
encodingCombo = UIUtils.createEncodingCombo(parent, curCharset);
encodingCombo.setToolTipText("Content Encoding");
......@@ -205,22 +198,7 @@ public class ContentEditorContributor extends MultiPageEditorActionBarContributo
final ContentEditorInput contentEditorInput = contentEditor.getEditorInput();
Combo combo = (Combo) e.widget;
final String charset = combo.getItem(combo.getSelectionIndex());
try {
contentEditor.getSite().getWorkbenchWindow().run(false, false, new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
contentEditorInput.getFile().setCharset(charset, monitor);
} catch (CoreException e1) {
throw new InvocationTargetException(e1);
}
}
});
} catch (InvocationTargetException e1) {
log.error(e1.getTargetException());
} catch (InterruptedException e1) {
// do nothing
}
contentEditorInput.setFileCharset(charset);
}
}
......
......@@ -17,12 +17,11 @@
*/
package org.jkiss.dbeaver.ui.editors.content;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourceAttributes;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IPathEditorInput;
import org.eclipse.ui.IPersistableElement;
......@@ -30,14 +29,17 @@ import org.jkiss.code.Nullable;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.core.DBeaverCore;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.DBPContextProvider;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.data.DBDContentStorageLocal;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCExecutionContext;
import org.jkiss.dbeaver.model.impl.TemporaryContentStorage;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.LocalFileStorage;
import org.jkiss.dbeaver.runtime.RuntimeUtils;
import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.ui.DBeaverIcons;
import org.jkiss.dbeaver.ui.data.IAttributeController;
import org.jkiss.dbeaver.ui.data.IValueController;
......@@ -55,8 +57,9 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
private IValueController valueController;
private ContentEditorPart[] editorParts;
private IFile contentFile;
private File contentFile;
private boolean contentDetached = false;
private String fileCharset = ContentUtils.DEFAULT_CHARSET;
public ContentEditorInput(
IValueController valueController,
......@@ -69,6 +72,10 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
this.prepareContent(monitor);
}
public File getContentFile() {
return contentFile;
}
public IValueController getValueController()
{
return valueController;
......@@ -129,6 +136,9 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
@Override
public Object getAdapter(Class adapter)
{
if (adapter == IStorage.class) {
return new LocalFileStorage(contentFile);
}
return null;
}
......@@ -178,11 +188,8 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
catch (IOException e) {
// Delete temp file
if (contentFile != null && contentFile.exists()) {
try {
contentFile.delete(true, false, monitor.getNestedMonitor());
}
catch (CoreException e1) {
log.warn("Can't delete temporary content file", e);
if (!contentFile.delete()) {
log.warn("Can't delete temporary content file '" + contentFile.getAbsolutePath() + "'");
}
}
throw new DBException("Can't delete content file", e);
......@@ -197,41 +204,26 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
private void markReadOnly(boolean readOnly) throws DBException
{
ResourceAttributes attributes = contentFile.getResourceAttributes();
if (attributes != null && attributes.isReadOnly() != readOnly) {
attributes.setReadOnly(readOnly);
try {
contentFile.setResourceAttributes(attributes);
}
catch (CoreException e) {
throw new DBException("Can't set content read-only", e);
}
if (!contentFile.setWritable(!readOnly)) {
throw new DBException("Can't set content read-only");
}
}
public void release(DBRProgressMonitor monitor)
{
if (contentFile != null && !contentDetached) {
ContentUtils.deleteTempFile(monitor, contentFile);
if (!contentFile.delete()) {
log.warn("Can't delete temp file '" + contentFile.getAbsolutePath() + "'");
}
contentDetached = true;
}
}
public IFile getFile() {
return contentFile;
}
public IStorage getStorage()
throws CoreException
{
return contentFile;
}
@Nullable
@Override
public IPath getPath()
{
return contentFile == null ? null : contentFile.getLocation();
return contentFile == null ? null : new Path(contentFile.getAbsolutePath());
}
public boolean isReadOnly() {
......@@ -241,9 +233,9 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
void saveToExternalFile(File file, IProgressMonitor monitor)
throws CoreException
{
try {
try (InputStream is = new FileInputStream(contentFile)) {
ContentUtils.saveContentToFile(
contentFile.getContents(true),
is,
file,
RuntimeUtils.makeMonitor(monitor));
}
......@@ -257,23 +249,9 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
{
try {
try (InputStream inputStream = new FileInputStream(extFile)) {
/*
ResourceAttributes atts = contentFile.getResourceAttributes();
atts.setReadOnly(false);
contentFile.setResourceAttributes(atts);
*/
File intFile = contentFile.getLocation().toFile();
try (OutputStream outputStream = new FileOutputStream(intFile)) {
try (OutputStream outputStream = new FileOutputStream(contentFile)) {
ContentUtils.copyStreams(inputStream, extFile.length(), outputStream, RuntimeUtils.makeMonitor(monitor));
}
// Append zero empty content to trigger content refresh
contentFile.appendContents(
new ByteArrayInputStream(new byte[0]),
true,
false,
monitor);
}
}
catch (Throwable e) {
......@@ -288,17 +266,15 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
markReadOnly(false);
if (contents.isNull()) {
ContentUtils.copyStreamToFile(monitor, new ByteArrayInputStream(new byte[0]), 0, contentFile);
} else {
if (storage == null) {
log.warn("Can't get data from null storage");
return;
}
if (ContentUtils.isTextContent(contents)) {
ContentUtils.copyReaderToFile(monitor, storage.getContentReader(), storage.getContentLength(), storage.getCharset(), contentFile);
try (OutputStream os = new FileOutputStream(contentFile)) {
if (contents.isNull()) {
ContentUtils.copyStreams(new ByteArrayInputStream(new byte[0]), 0, os, monitor);
} else {
ContentUtils.copyStreamToFile(monitor, storage.getContentStream(), storage.getContentLength(), contentFile);
if (storage == null) {
log.warn("Can't get data from null storage");
return;
}
ContentUtils.copyStreams(storage.getContentStream(), storage.getContentLength(), os, monitor);
}
}
......@@ -330,4 +306,12 @@ public class ContentEditorInput implements IPathEditorInput, DBPContextProvider
public DBCExecutionContext getExecutionContext() {
return valueController.getExecutionContext();
}
public String getFileCharset() {
return fileCharset;
}
public void setFileCharset(String fileCharset) {
this.fileCharset = fileCharset;
}
}
\ No newline at end of file
......@@ -18,7 +18,6 @@
package org.jkiss.dbeaver.model;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.jkiss.code.NotNull;
......@@ -29,6 +28,7 @@ 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;
import java.util.Collection;
......@@ -60,7 +60,7 @@ public interface DBPApplication
DBPPreferenceStore getPreferenceStore();
@NotNull
IFolder getTempFolder(DBRProgressMonitor monitor, String name) throws IOException;
File getTempFolder(DBRProgressMonitor monitor, String name) throws IOException;
@NotNull
DBRRunnableContext getRunnableContext();
......
......@@ -18,7 +18,7 @@
package org.jkiss.dbeaver.model.data;
import org.eclipse.core.resources.IFile;
import java.io.File;
/**
* Local content storage.
......@@ -28,6 +28,6 @@ import org.eclipse.core.resources.IFile;
*/
public interface DBDContentStorageLocal extends DBDContentStorage {
IFile getDataFile();
File getDataFile();
}
\ No newline at end of file
......@@ -17,9 +17,6 @@
*/
package org.jkiss.dbeaver.model.impl;
import org.jkiss.dbeaver.Log;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
......@@ -32,8 +29,6 @@ import java.io.*;
*/
public class ExternalContentStorage implements DBDContentStorage {
static final Log log = Log.getLog(ExternalContentStorage.class);
private final DBPApplication application;
private File file;
private String charset;
......@@ -81,17 +76,15 @@ public class ExternalContentStorage implements DBDContentStorage {
throws IOException
{
// Create new local storage
IFile tempFile = ContentUtils.createTempContentFile(monitor, application, "copy" + this.hashCode());
File tempFile = ContentUtils.createTempContentFile(monitor, application, "copy" + this.hashCode());
try {
InputStream is = new FileInputStream(file);
try {
tempFile.setContents(is, true, false, monitor.getNestedMonitor());
}
finally {
ContentUtils.close(is);
try (InputStream is = new FileInputStream(file)) {
try (OutputStream os = new FileOutputStream(tempFile)) {
ContentUtils.copyStreams(is, file.length(), os, monitor);
}
}
} catch (CoreException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
} catch (IOException e) {
ContentUtils.deleteTempFile(tempFile);
throw new IOException(e);
}
return new TemporaryContentStorage(application, tempFile);
......
......@@ -17,32 +17,23 @@
*/
package org.jkiss.dbeaver.model.impl;
import org.jkiss.dbeaver.Log;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.data.DBDContentStorageLocal;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.utils.ContentUtils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.*;
/**
* File content storage
*/
public class TemporaryContentStorage implements DBDContentStorageLocal {
static final Log log = Log.getLog(TemporaryContentStorage.class);
private final DBPApplication application;
private IFile file;
private File file;
public TemporaryContentStorage(DBPApplication application, IFile file)
public TemporaryContentStorage(DBPApplication application, File file)
{
this.application = application;
this.file = file;
......@@ -52,44 +43,26 @@ public class TemporaryContentStorage implements DBDContentStorageLocal {
public InputStream getContentStream()
throws IOException
{
try {
return file.getContents();
}
catch (CoreException e) {
throw new IOException(e);
}
return new FileInputStream(file);
}
@Override
public Reader getContentReader()
throws IOException
{
try {
return new InputStreamReader(
file.getContents(),
file.getCharset());
}
catch (CoreException e) {
throw new IOException(e);
}
return new FileReader(file);
}
@Override
public long getContentLength()
{
return file.getLocation().toFile().length();
return file.length();
}
@Override
public String getCharset()
{
try {
return file.getCharset();
}
catch (CoreException e) {
log.warn(e);
return null;
}
return ContentUtils.DEFAULT_CHARSET;
}
@Override
......@@ -97,17 +70,15 @@ public class TemporaryContentStorage implements DBDContentStorageLocal {
throws IOException
{
// Create new local storage
IFile tempFile = ContentUtils.createTempContentFile(monitor, application, "copy" + this.hashCode());
File tempFile = ContentUtils.createTempContentFile(monitor, application, "copy" + this.hashCode());
try {
InputStream is = file.getContents(true);
try {
tempFile.setContents(is, true, false, monitor.getNestedMonitor());
try (InputStream is = new FileInputStream(file)) {
try (OutputStream os = new FileOutputStream(tempFile)) {
ContentUtils.copyStreams(is, file.length(), os, monitor);
}
}
finally {
ContentUtils.close(is);
}
} catch (CoreException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
} catch (IOException e) {
ContentUtils.deleteTempFile(tempFile);
throw new IOException(e);
}
return new TemporaryContentStorage(application, tempFile);
......@@ -116,16 +87,11 @@ public class TemporaryContentStorage implements DBDContentStorageLocal {
@Override
public void release()
{
try {
file.delete(true, false, new NullProgressMonitor());
}
catch (CoreException e) {
log.warn(e);
}
ContentUtils.deleteTempFile(file);
}
@Override
public IFile getDataFile()
public File getDataFile()
{
return file;
}
......
......@@ -17,10 +17,9 @@
*/
package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.eclipse.core.resources.IFile;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
......@@ -34,11 +33,8 @@ import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.MimeTypes;
import org.jkiss.utils.IOUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
......@@ -100,20 +96,20 @@ public class JDBCContentBLOB extends JDBCContentLOB {
}
} else {
// Create new local storage
IFile tempFile;
File tempFile;
try {
tempFile = ContentUtils.createTempContentFile(monitor, application, "blob" + blob.hashCode());
}
catch (IOException e) {
throw new DBCException("Can't create temporary file", e);
}
try {
ContentUtils.copyStreamToFile(monitor, blob.getBinaryStream(), contentLength, tempFile);
try (OutputStream os = new FileOutputStream(tempFile)) {
ContentUtils.copyStreams(blob.getBinaryStream(), contentLength, os, monitor);
} catch (IOException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
throw new DBCException("IO error whle copying stream", e);
ContentUtils.deleteTempFile(tempFile);
throw new DBCException("IO error while copying stream", e);
} catch (SQLException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
ContentUtils.deleteTempFile(tempFile);
throw new DBCException(e, dataSource);
}
this.storage = new TemporaryContentStorage(application, tempFile);
......
......@@ -17,13 +17,15 @@
*/
package org.jkiss.dbeaver.model.impl.jdbc.data;
import org.eclipse.core.resources.IFile;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.ModelPreferences;
import org.jkiss.dbeaver.model.DBPApplication;
import org.jkiss.dbeaver.model.DBPDataSource;
import org.jkiss.dbeaver.model.data.*;
import org.jkiss.dbeaver.model.data.DBDContent;
import org.jkiss.dbeaver.model.data.DBDContentCached;
import org.jkiss.dbeaver.model.data.DBDContentStorage;
import org.jkiss.dbeaver.model.data.DBDDisplayFormat;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement;
import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession;
......@@ -32,12 +34,10 @@ import org.jkiss.dbeaver.model.impl.TemporaryContentStorage;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.struct.DBSTypedObject;
import org.jkiss.dbeaver.utils.ContentUtils;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.dbeaver.utils.MimeTypes;
import org.jkiss.utils.CommonUtils;
import java.io.IOException;
import java.io.Reader;
import java.io.*;
import java.sql.Clob;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
......@@ -96,20 +96,20 @@ public class JDBCContentCLOB extends JDBCContentLOB implements DBDContent {
}
} else {
// Create new local storage
IFile tempFile;
File tempFile;
try {
tempFile = ContentUtils.createTempContentFile(monitor, application, "clob" + clob.hashCode());
}
catch (IOException e) {
throw new DBCException("Can't create temp file", e);
}
try {
ContentUtils.copyReaderToFile(monitor, clob.getCharacterStream(), contentLength, GeneralUtils.DEFAULT_FILE_CHARSET_NAME, tempFile);
try (Writer os = new FileWriter(tempFile)) {
ContentUtils.copyStreams(clob.getCharacterStream(), contentLength, os, monitor);
} catch (IOException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
ContentUtils.deleteTempFile(tempFile);
throw new DBCException("IO error while copying content", e);
} catch (SQLException e) {
ContentUtils.deleteTempFile(monitor, tempFile);
ContentUtils.deleteTempFile(tempFile);
throw new DBCException(e, dataSource);
}
this.storage = new TemporaryContentStorage(application, tempFile);
......
......@@ -60,20 +60,21 @@ public class ContentUtils {
}
public static IFolder getLobFolder(DBRProgressMonitor monitor, DBPApplication application)
public static File getLobFolder(DBRProgressMonitor monitor, DBPApplication application)
throws IOException
{
return application.getTempFolder(monitor, LOB_DIR);
}
public static IFile createTempContentFile(DBRProgressMonitor monitor, DBPApplication application, String fileName)
public static File createTempContentFile(DBRProgressMonitor monitor, DBPApplication application, String fileName)
throws IOException
{
IFile file = makeTempFile(
return makeTempFile(
monitor,
getLobFolder(monitor, application),
fileName,
"data");
/*
try {
String charset = application.getPreferenceStore().getString(ModelPreferences.CONTENT_HEX_ENCODING);
file.setCharset(charset, monitor.getNestedMonitor());
......@@ -81,17 +82,15 @@ public class ContentUtils {
log.error("Can't set file charset", e);
}
return file;
*/
}
public static IFile makeTempFile(DBRProgressMonitor monitor, IFolder folder, String name, String extension)
public static File makeTempFile(DBRProgressMonitor monitor, File folder, String name, String extension)
throws IOException
{
IFile tempFile = folder.getFile(name + "-" + System.currentTimeMillis() + "." + extension); //$NON-NLS-1$ //$NON-NLS-2$
try {
InputStream contents = new ByteArrayInputStream(new byte[0]);
tempFile.create(contents, true, monitor.getNestedMonitor());
} catch (CoreException ex) {
throw new IOException(MessageFormat.format(ModelMessages.DBeaverCore_error_can_create_temp_file, tempFile.toString(), folder.toString()), ex);
File tempFile = new File(folder, name + "-" + System.currentTimeMillis() + "." + extension); //$NON-NLS-1$ //$NON-NLS-2$
if (!tempFile.createNewFile()){
throw new IOException(MessageFormat.format(ModelMessages.DBeaverCore_error_can_create_temp_file, tempFile.getAbsolutePath(), folder.getAbsoluteFile()));
}
return tempFile;
}
......@@ -430,4 +429,10 @@ public class ContentUtils {
return MimeTypes.TEXT_XML.equalsIgnoreCase(content.getContentType());
}
public static void deleteTempFile(File tempFile) {
if (!tempFile.delete()) {
log.warn("Can't delete temp file '" + tempFile.getAbsolutePath() + "'");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册