Class TransactionAwareFeatureTransferManager
- java.lang.Object
-
- org.syncany.plugins.transfer.features.TransactionAwareFeatureTransferManager
-
- All Implemented Interfaces:
FeatureTransferManager
,TransferManager
public class TransactionAwareFeatureTransferManager extends java.lang.Object implements FeatureTransferManager
The TransactionAwareTransferManager adds all functionality regarding transactions to existing transfer managers.
-
-
Constructor Summary
Constructors Constructor Description TransactionAwareFeatureTransferManager(TransferManager originalTransferManager, TransferManager underlyingTransferManager, Config config, TransactionAware transactionAwareAnnotation)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T extends RemoteFile>
java.util.Map<java.lang.String,T>addAndFilterFilesInTransaction(java.lang.Class<T> remoteFileClass, java.util.Map<java.lang.String,T> remoteFiles)
Returns a list of remote files, excluding the files in transactions.void
cleanTransactions()
Checks if any transactions of the local machine were not completed and performs a rollback if any transactions were found.void
clearPendingTransactions()
clearPendingTransactions provides a way to delete the multiple transactions that might be queued after an interrupted up.void
clearResumableTransactions()
This methods deletes local copies of transactions that might be resumed.void
connect()
Establish a connection with the remote storage.protected java.io.File
createTempFile(java.lang.String name)
Creates a temporary file, either using the config (if initialized) or using the global temporary directory.boolean
delete(RemoteFile remoteFile)
Deletes an existing file from the remote storage permanently.void
disconnect()
Disconnect from the remote storage.void
download(RemoteFile remoteFile, java.io.File localFile)
Download an existing remote file to the local disk.protected java.util.Set<RemoteFile>
getFilesInTransactions(java.util.Set<TransactionTO> transactions)
Returns a Set of all files that are not temporary, but are listed in a transaction file.java.lang.String
getRemoteFilePath(java.lang.Class<? extends RemoteFile> remoteFileClass)
Return the path for a concreteRemoteFile
implementation as it is stored on the remote side *java.util.List<TransactionRemoteFile>
getTransactionsByClient(java.lang.String client)
This function returns a list of all remote transaction files that belong to the client.void
init(boolean createIfRequired)
Initialize remote storage.<T extends RemoteFile>
java.util.Map<java.lang.String,T>list(java.lang.Class<T> remoteFileClass)
Retrieves a list of all files in the remote repository, filtered by the type of the desired file, i.e.java.util.Collection<java.lang.Long>
loadPendingTransactionList()
Check if we have transactions from an Up operation that we can resume.void
move(RemoteFile sourceFile, RemoteFile targetFile)
Moves an existing file in the online storage.void
removeUnreferencedTemporaryFiles()
Removes temporary files on the offsite storage that are not listed in any of theTransactionRemoteFile
s available remotely.StorageTestResult
test(boolean testCreateTarget)
Tests whether the repository parameters are valid.boolean
testRepoFileExists()
Tests whether the repository file exists (seeSyncanyRemoteFile
).boolean
testTargetCanCreate()
Tests whether the target path/folder can be created (if it does not exist already).boolean
testTargetCanWrite()
Tests whether the target path/folder is writable by the application.boolean
testTargetExists()
Tests whether the target path/folder exists.void
upload(java.io.File localFile, RemoteFile remoteFile)
Update an existing local file to the online storage.
-
-
-
Constructor Detail
-
TransactionAwareFeatureTransferManager
public TransactionAwareFeatureTransferManager(TransferManager originalTransferManager, TransferManager underlyingTransferManager, Config config, TransactionAware transactionAwareAnnotation)
-
-
Method Detail
-
connect
public void connect() throws StorageException
Description copied from interface:TransferManager
Establish a connection with the remote storage.This method does not validate the correctness of the repository and it does not create any folders. The former is done by
TransferManager.test(boolean)
, the latter is done byTransferManager.init(boolean)
.- Specified by:
connect
in interfaceTransferManager
- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc.
-
disconnect
public void disconnect() throws StorageException
Description copied from interface:TransferManager
Disconnect from the remote storage.- Specified by:
disconnect
in interfaceTransferManager
- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc.
-
init
public void init(boolean createIfRequired) throws StorageException
Description copied from interface:TransferManager
Initialize remote storage. This method is called to set up a new repository.- Specified by:
init
in interfaceTransferManager
- Parameters:
createIfRequired
- true if the method should handle repo creation- Throws:
StorageException
- If the connection drops, or any other exception occurs.
-
download
public void download(RemoteFile remoteFile, java.io.File localFile) throws StorageException
Description copied from interface:TransferManager
Download an existing remote file to the local disk.The file is either downloaded completely or nothing at all. In the latter case, a
StorageException
is thrown.Implementations must make sure that if a file matches the specified name schema, it must be complete and consistent.
If remoteFile does not exist, a
StorageFileNotFoundException
is thrown.- Specified by:
download
in interfaceTransferManager
- Parameters:
remoteFile
- Existing source file on the remote storage. The only required property of the remote file is the name.localFile
- Not existing local file to which the remote file is going to be downloaded.- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc.
-
move
public void move(RemoteFile sourceFile, RemoteFile targetFile) throws StorageException
Description copied from interface:TransferManager
Moves an existing file in the online storage.If the sourceFile does not exists, a
StorageMoveException
is thrown.- Specified by:
move
in interfaceTransferManager
- Parameters:
sourceFile
- Existing remote file that is to be moved.targetFile
- Destination for the remote file.- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc.
-
upload
public void upload(java.io.File localFile, RemoteFile remoteFile) throws StorageException
Description copied from interface:TransferManager
Update an existing local file to the online storage.The file is either uploaded completely or nothing at all. In the latter case, a
StorageException
is thrown.Implementations must make sure that if a file matches the specified name schema, it must be complete and consistent.
Implementations must NOT upload a file if it already exists and has the same file size as the local file.
- Specified by:
upload
in interfaceTransferManager
- Parameters:
localFile
- Existing local file that is going to be uploaded.remoteFile
- Not existing destination file on the remote storage. The only required property of the remote file is the name.- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc.
-
delete
public boolean delete(RemoteFile remoteFile) throws StorageException
Description copied from interface:TransferManager
Deletes an existing file from the remote storage permanently.In case the remote file does not exist, it returns immediately without any notice. If the file cannot be deleted or the connection breaks, a
StorageException
is thrown.- Specified by:
delete
in interfaceTransferManager
- Parameters:
remoteFile
- Existing remote file to be deleted. The only required property of the remote file is the name.- Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc
-
list
public <T extends RemoteFile> java.util.Map<java.lang.String,T> list(java.lang.Class<T> remoteFileClass) throws StorageException
Description copied from interface:TransferManager
Retrieves a list of all files in the remote repository, filtered by the type of the desired file, i.e. by a sub-class ofRemoteFile
.- Specified by:
list
in interfaceTransferManager
- Parameters:
remoteFileClass
- Filter class:RemoteFile
or a sub-type thereof- Returns:
- Returns a list of remote files. In the map, the key is the file name,
the value the entire
RemoteFile
object. - Throws:
StorageException
- If the connection fails due to no Internet connection, authentication errors, etc
-
getRemoteFilePath
public java.lang.String getRemoteFilePath(java.lang.Class<? extends RemoteFile> remoteFileClass)
Description copied from interface:TransferManager
Return the path for a concreteRemoteFile
implementation as it is stored on the remote side *- Specified by:
getRemoteFilePath
in interfaceTransferManager
- Parameters:
remoteFileClass
- The class to provide the path for- Returns:
- A string pointing to the folder where a file is stored
-
cleanTransactions
public void cleanTransactions() throws StorageException, BlockingTransfersException
Checks if any transactions of the local machine were not completed and performs a rollback if any transactions were found. The rollback itself is performed in a transaction.The method uses
retrieveRemoteTransactions()
to download all transaction files and then rolls back the local machines's transactions:- Files in the transaction marked "UPLOAD" are deleted.
- Files in the transaction marked "DELETE" are moved back to their original place.
- Throws:
BlockingTransfersException
- if we cannot proceed (Deleting transaction by another client exists).StorageException
-
getTransactionsByClient
public java.util.List<TransactionRemoteFile> getTransactionsByClient(java.lang.String client) throws StorageException
This function returns a list of all remote transaction files that belong to the client. If blocking transactions exist, this methods returns null, because we are not allowed to proceed.- Throws:
StorageException
-
clearResumableTransactions
public void clearResumableTransactions()
This methods deletes local copies of transactions that might be resumed. This is done when a transaction is successfully resumed, or some other operations is performed, which implies that resuming is no longer an option.
-
clearPendingTransactions
public void clearPendingTransactions() throws java.io.IOException
clearPendingTransactions provides a way to delete the multiple transactions that might be queued after an interrupted up.- Throws:
java.io.IOException
-
loadPendingTransactionList
public java.util.Collection<java.lang.Long> loadPendingTransactionList() throws java.io.IOException
Check if we have transactions from an Up operation that we can resume.- Throws:
java.io.IOException
-
removeUnreferencedTemporaryFiles
public void removeUnreferencedTemporaryFiles() throws StorageException
Removes temporary files on the offsite storage that are not listed in any of theTransactionRemoteFile
s available remotely.Temporary files might be left over from unfinished transactions.
- Throws:
StorageException
-
test
public StorageTestResult test(boolean testCreateTarget)
Description copied from interface:TransferManager
Tests whether the repository parameters are valid. In particular, the method tests whether a target (folder, bucket, etc.) exists or, if not, whether it can be created. It furthermore tests whether a repository at the target already exists by checking if theSyncanyRemoteFile
exists.The relevant result is determined by the following methods:
TransferManager.testTargetExists()
: Tests whether the target exists.TransferManager.testTargetCanWrite()
: Tests whether the target is writable.TransferManager.testTargetCanCreate()
: Tests whether the target can be created if it does not exist already. This is only called iftestCreateTarget
is set.TransferManager.testRepoFileExists()
: Tests whether the repo file exists.
- Specified by:
test
in interfaceTransferManager
- Parameters:
testCreateTarget
- Iftrue
, the test will test if the target can be created in case it does not exist. Iffalse
, this test will be skipped.- Returns:
- Returns the result of testing the repository.
- See Also:
StorageTestResult
-
testTargetExists
public boolean testTargetExists() throws StorageException
Description copied from interface:TransferManager
Tests whether the target path/folder exists. This might be done by listing the parent path/folder or by retrieving metadata about the target. The method returnstrue
if the target exists,false
otherwise.This method is called by the
TransferManager.test(boolean)
method (only during repository initialization or initial connection).- Specified by:
testTargetExists
in interfaceTransferManager
- Returns:
- Returns
true
if the target exists,false
otherwise - Throws:
StorageException
- If the test cannot be performed, e.g. due to a connection failure
-
testTargetCanWrite
public boolean testTargetCanWrite() throws StorageException
Description copied from interface:TransferManager
Tests whether the target path/folder is writable by the application. This method may either check the write permissions of the target or actually write a test file to check write access. If the target does not exist,false
is returned. If the target exists and is writable,true
is returned.This method is called by the
TransferManager.test(boolean)
method (only during repository initialization or initial connection).- Specified by:
testTargetCanWrite
in interfaceTransferManager
- Returns:
- Returns
true
if the target can be written to,false
otherwise - Throws:
StorageException
- If the test cannot be performed, e.g. due to a connection failure
-
testTargetCanCreate
public boolean testTargetCanCreate() throws StorageException
Description copied from interface:TransferManager
Tests whether the target path/folder can be created (if it does not exist already). This method may either check the permissions of the parent path/folder or actually create and delete the target to determine create permissions.If the target already exists, the method returns
true
. If it does not, but it can be created (according to tests of this method), it also returnstrue
. In all other cases,false
is returned.This method is called by the
TransferManager.test(boolean)
method, but only if thetestCreateTarget
flag is set totrue
!- Specified by:
testTargetCanCreate
in interfaceTransferManager
- Returns:
- Returns
true
if the target can be created or already exists,false
otherwise - Throws:
StorageException
- If the test cannot be performed, e.g. due to a connection failure
-
testRepoFileExists
public boolean testRepoFileExists() throws StorageException
Description copied from interface:TransferManager
Tests whether the repository file exists (seeSyncanyRemoteFile
). This method is called by theTransferManager.test(boolean)
method (only during repository initialization (or initial connection).This method is called by the
TransferManager.test(boolean)
method (only during repository initialization or initial connection).- Specified by:
testRepoFileExists
in interfaceTransferManager
- Returns:
- Returns
true
if the repository is valid,false
otherwise - Throws:
StorageException
- If the test cannot be performed, e.g. due to a connection failure
-
addAndFilterFilesInTransaction
protected <T extends RemoteFile> java.util.Map<java.lang.String,T> addAndFilterFilesInTransaction(java.lang.Class<T> remoteFileClass, java.util.Map<java.lang.String,T> remoteFiles) throws StorageException
Returns a list of remote files, excluding the files in transactions. The method is used to hide unfinished transactions from other clients.- Throws:
StorageException
-
getFilesInTransactions
protected java.util.Set<RemoteFile> getFilesInTransactions(java.util.Set<TransactionTO> transactions) throws StorageException
Returns a Set of all files that are not temporary, but are listed in a transaction file. These belong to an unfinished transaction and should be ignored.- Throws:
StorageException
-
createTempFile
protected java.io.File createTempFile(java.lang.String name) throws java.io.IOException
Creates a temporary file, either using the config (if initialized) or using the global temporary directory.- Throws:
java.io.IOException
-
-