diff options
author | Stefan Seelmann <mail@stefan-seelmann.de> | 2021-02-06 18:09:19 +0300 |
---|---|---|
committer | Stefan Seelmann <mail@stefan-seelmann.de> | 2021-02-06 18:09:19 +0300 |
commit | 020b4a00090b3ec088f29e9aea9c21b1cf7e673c (patch) | |
tree | 38b426e6fb130e7800f31e1dd9b497924aae1fba /plugins | |
parent | b8b7d52e0bc29de327da2c7e6b89d0c3c016721c (diff) |
Cleanup save/load of connections and folders, add connection view tests
Diffstat (limited to 'plugins')
2 files changed, 33 insertions, 55 deletions
diff --git a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionFolderManager.java b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionFolderManager.java index 84f4216a2..942ebad42 100644 --- a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionFolderManager.java +++ b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionFolderManager.java @@ -23,7 +23,6 @@ package org.apache.directory.studio.connection.core; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -35,7 +34,8 @@ import org.apache.directory.api.util.FileUtils; import org.apache.directory.studio.connection.core.event.ConnectionEventRegistry; import org.apache.directory.studio.connection.core.event.ConnectionUpdateListener; import org.apache.directory.studio.connection.core.io.ConnectionIO; -import org.apache.directory.studio.connection.core.io.ConnectionIOException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; /** @@ -365,21 +365,23 @@ public class ConnectionFolderManager implements ConnectionUpdateListener */ private synchronized void saveConnectionFolders() { + File file = new File( getConnectionFolderStoreFileName() ); + File tempFile = new File( getConnectionFolderStoreFileName() + ConnectionManager.TEMP_SUFFIX ); + // To avoid a corrupt file, save object to a temp file first - try + try ( FileOutputStream fileOutputStream = new FileOutputStream( tempFile ) ) { - ConnectionIO.saveConnectionFolders( folderList, new FileOutputStream( getConnectionFolderStoreFileName() - + ConnectionManager.TEMP_SUFFIX ) ); + ConnectionIO.saveConnectionFolders( folderList, fileOutputStream ); } catch ( IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); + Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, + Messages.error__saving_connections + e.getMessage(), e ); + ConnectionCorePlugin.getDefault().getLog().log( status ); + return; } // move temp file to good file - File file = new File( getConnectionFolderStoreFileName() ); - File tempFile = new File( getConnectionFolderStoreFileName() + ConnectionManager.TEMP_SUFFIX ); if ( file.exists() ) { file.delete(); @@ -392,8 +394,10 @@ public class ConnectionFolderManager implements ConnectionUpdateListener } catch ( IOException e ) { - // TODO Auto-generated catch block - e.printStackTrace(); + Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, + Messages.error__saving_connections + e.getMessage(), e ); + ConnectionCorePlugin.getDefault().getLog().log( status ); + return; } } @@ -405,26 +409,15 @@ public class ConnectionFolderManager implements ConnectionUpdateListener { ConnectionEventRegistry.suspendEventFiringInCurrentThread(); - try + try ( FileInputStream fileInputStream = new FileInputStream( getConnectionFolderStoreFileName() ) ) { - folderList = ConnectionIO.loadConnectionFolders( new FileInputStream( getConnectionFolderStoreFileName() ) ); + folderList = ConnectionIO.loadConnectionFolders( fileInputStream ); } catch ( Exception e ) { - // If loading failed, try with temp file - try - { - folderList = ConnectionIO.loadConnectionFolders( new FileInputStream( - getConnectionFolderStoreFileName() + ConnectionManager.TEMP_SUFFIX ) ); - } - catch ( FileNotFoundException e1 ) - { - // TODO Auto-generated catch block - } - catch ( ConnectionIOException e1 ) - { - // TODO Auto-generated catch block - } + Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, + Messages.error__saving_connections + e.getMessage(), e ); + ConnectionCorePlugin.getDefault().getLog().log( status ); } if ( !folderList.isEmpty() ) diff --git a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionManager.java b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionManager.java index af283625e..a1fddb173 100644 --- a/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionManager.java +++ b/plugins/connection.core/src/main/java/org/apache/directory/studio/connection/core/ConnectionManager.java @@ -23,7 +23,6 @@ package org.apache.directory.studio.connection.core; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; @@ -33,7 +32,6 @@ import org.apache.directory.api.util.FileUtils; import org.apache.directory.studio.connection.core.event.ConnectionEventRegistry; import org.apache.directory.studio.connection.core.event.ConnectionUpdateListener; import org.apache.directory.studio.connection.core.io.ConnectionIO; -import org.apache.directory.studio.connection.core.io.ConnectionIOException; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtensionPoint; @@ -360,29 +358,29 @@ public class ConnectionManager implements ConnectionUpdateListener public synchronized void saveConnections() { Set<ConnectionParameter> connectionParameters = new HashSet<>(); - + for ( Connection connection : connectionList ) { connectionParameters.add( connection.getConnectionParameter() ); } + File file = new File( getConnectionStoreFileName() ); + File tempFile = new File( getConnectionStoreFileName() + TEMP_SUFFIX ); + // To avoid a corrupt file, save object to a temp file first - try + try ( FileOutputStream fileOutputStream = new FileOutputStream( tempFile ) ) { - ConnectionIO - .save( connectionParameters, new FileOutputStream( getConnectionStoreFileName() + TEMP_SUFFIX ) ); + ConnectionIO.save( connectionParameters, fileOutputStream ); } catch ( IOException e ) { Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, Messages.error__saving_connections + e.getMessage(), e ); ConnectionCorePlugin.getDefault().getLog().log( status ); + return; } // move temp file to good file - File file = new File( getConnectionStoreFileName() ); - File tempFile = new File( getConnectionStoreFileName() + TEMP_SUFFIX ); - if ( file.exists() ) { file.delete(); @@ -398,6 +396,7 @@ public class ConnectionManager implements ConnectionUpdateListener Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, Messages.error__saving_connections + e.getMessage(), e ); ConnectionCorePlugin.getDefault().getLog().log( status ); + return; } } @@ -409,29 +408,15 @@ public class ConnectionManager implements ConnectionUpdateListener { Set<ConnectionParameter> connectionParameters = null; - try + try ( FileInputStream fileInputStream = new FileInputStream( getConnectionStoreFileName() ) ) { - connectionParameters = ConnectionIO.load( new FileInputStream( getConnectionStoreFileName() ) ); + connectionParameters = ConnectionIO.load( fileInputStream ); } catch ( Exception e ) { - // If loading failed, try with temp file - try - { - connectionParameters = ConnectionIO.load( new FileInputStream( getConnectionStoreFileName() - + TEMP_SUFFIX ) ); - } - catch ( FileNotFoundException e1 ) - { - // ignore, this is a fresh workspace - return; - } - catch ( ConnectionIOException e1 ) - { - Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, - Messages.error__loading_connections + e.getMessage(), e ); - ConnectionCorePlugin.getDefault().getLog().log( status ); - } + Status status = new Status( IStatus.ERROR, ConnectionCoreConstants.PLUGIN_ID, + Messages.error__loading_connections + e.getMessage(), e ); + ConnectionCorePlugin.getDefault().getLog().log( status ); } if ( connectionParameters != null ) |