Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Refactoring TransactionTO for better generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 13, 2014
1 parent f796f9a commit 206698a
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
Expand All @@ -31,9 +32,9 @@
import org.syncany.plugins.StorageException;
import org.syncany.plugins.StorageTestResult;
import org.syncany.plugins.transfer.files.RemoteFile;
import org.syncany.plugins.transfer.files.TempRemoteFile;
import org.syncany.plugins.transfer.files.TransactionRemoteFile;
import org.syncany.plugins.transfer.files.TransactionTO;
import org.syncany.plugins.transfer.to.TransactionActionTO;
import org.syncany.plugins.transfer.to.TransactionTO;

/**
* Implements basic functionality of a {@link TransferManager} which
Expand Down Expand Up @@ -113,7 +114,7 @@ protected Set<RemoteFile> getFilesInTransactions() throws StorageException {
Map<String, TransactionRemoteFile> transactionFiles = list(TransactionRemoteFile.class);

for (TransactionRemoteFile transaction : transactionFiles.values()) {
Map<TempRemoteFile, RemoteFile> tempFileToTargetFileMap = null;
List<TransactionActionTO> transactionActions = null;

try {
File transactionFile = createTempFile("transaction");
Expand All @@ -127,15 +128,15 @@ protected Set<RemoteFile> getFilesInTransactions() throws StorageException {
TransactionTO transactionTO = serializer.read(TransactionTO.class, transactionFileStr);

// Extract final locations
tempFileToTargetFileMap = transactionTO.getTempToTargetFileMap();
transactionActions = transactionTO.getTransactionActions();
transactionFile.delete();
}
catch (Exception e) {
throw new StorageException("Failed to read transactionFile", e);
}

if (tempFileToTargetFileMap != null) {
filesInTransaction.addAll(tempFileToTargetFileMap.values());
for (TransactionActionTO transactionAction : transactionActions) {
filesInTransaction.add(transactionAction.getRemoteFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -33,7 +31,8 @@
import org.syncany.plugins.transfer.files.RemoteFile;
import org.syncany.plugins.transfer.files.TempRemoteFile;
import org.syncany.plugins.transfer.files.TransactionRemoteFile;
import org.syncany.plugins.transfer.files.TransactionTO;
import org.syncany.plugins.transfer.to.TransactionActionTO;
import org.syncany.plugins.transfer.to.TransactionTO;

/**
* This class represents a transaction in a remote system. It will keep track of
Expand All @@ -46,15 +45,13 @@ public class RemoteTransaction {

private TransferManager transferManager;
private Config config;
private Map<File, TempRemoteFile> localToTempRemoteFileMap;
private Map<TempRemoteFile, RemoteFile> tempToTargetRemoteFileMap;
private TransactionTO transactionTO;

public RemoteTransaction(Config config, TransferManager transferManager) {
this.config = config;
this.transferManager = transferManager;

this.localToTempRemoteFileMap = new HashMap<File, TempRemoteFile>();
this.tempToTargetRemoteFileMap = new HashMap<TempRemoteFile, RemoteFile>();

this.transactionTO = new TransactionTO(config.getMachineName());
}

/**
Expand All @@ -66,8 +63,12 @@ public void add(File localFile, RemoteFile remoteFile) throws StorageException {
logger.log(Level.INFO, "Adding file to transaction: " + localFile);
logger.log(Level.INFO, " -> Temp. remote file: " + temporaryRemoteFile + ", final location: " + remoteFile);

localToTempRemoteFileMap.put(localFile, temporaryRemoteFile);
tempToTargetRemoteFileMap.put(temporaryRemoteFile, remoteFile);
TransactionActionTO action = new TransactionActionTO();
action.setType(TransactionActionTO.TYPE_UPLOAD);
action.setLocalTempLocation(localFile);
action.setRemoteLocation(remoteFile);
action.setRemoteTempLocation(temporaryRemoteFile);
transactionTO.addTransactionAction(action);
}

/**
Expand All @@ -82,14 +83,18 @@ public void commit() throws StorageException {

transferManager.upload(localTransactionFile, remoteTransactionFile);

for (File localFile : localToTempRemoteFileMap.keySet()) {
logger.log(Level.INFO, "- Uploading {0} to temp. file {1} ...", new Object[] { localFile, localToTempRemoteFileMap.get(localFile) });
transferManager.upload(localFile, localToTempRemoteFileMap.get(localFile));
for (TransactionActionTO action : transactionTO.getTransactionActions()) {
File localFile = action.getLocalTempLocation();
RemoteFile tempRemoteFile = action.getTempRemoteFile();
logger.log(Level.INFO, "- Uploading {0} to temp. file {1} ...", new Object[] { localFile, tempRemoteFile });
transferManager.upload(localFile, tempRemoteFile);
}

for (RemoteFile temporaryFile : tempToTargetRemoteFileMap.keySet()) {
logger.log(Level.INFO, "- Moving temp. file {0} to final location {1} ...", new Object[] { temporaryFile, tempToTargetRemoteFileMap.get(temporaryFile) });
transferManager.move(temporaryFile, tempToTargetRemoteFileMap.get(temporaryFile));
for (TransactionActionTO action : transactionTO.getTransactionActions()) {
RemoteFile tempRemoteFile = action.getTempRemoteFile();
RemoteFile finalRemoteFile = action.getRemoteFile();
logger.log(Level.INFO, "- Moving temp. file {0} to final location {1} ...", new Object[] { tempRemoteFile, finalRemoteFile });
transferManager.move(tempRemoteFile, finalRemoteFile);
}

logger.log(Level.INFO, "- Deleting remote transaction file {0} ...", remoteTransactionFile);
Expand All @@ -105,7 +110,7 @@ private File writeLocalTransactionFile() throws StorageException {

try (PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(localTransactionFile), "UTF-8"))) {
Serializer serializer = new Persister();
serializer.write(new TransactionTO(config.getMachineName(), tempToTargetRemoteFileMap), out);
serializer.write(transactionTO, out);
}

logger.log(Level.INFO, "Wrote transaction manifest to temporary file: " + localTransactionFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.plugins.transfer.to;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
import org.syncany.plugins.StorageException;
import org.syncany.plugins.transfer.files.RemoteFile;
import org.syncany.plugins.transfer.files.TempRemoteFile;

/**
* A TransactionActionTO describes a single action on a file, which is to be bundled
* with other actions to describe a full Transaction.
* @author Pim Otte
*/

@Root(name="transactionAction")
@Namespace(reference="http://syncany.org/transaction/action/1")
public class TransactionActionTO {
private static final Logger logger = Logger.getLogger(TransactionActionTO.class.getSimpleName());

public static final String TYPE_UPLOAD = "UPLOAD";
public static final String TYPE_DELETE = "DELETE";

@Element(name="type", required=true)
private String type;

@Element(name="remoteLocation", required=true)
private String remoteLocation;

@Element(name="remoteTempLocation", required=true)
private String remoteTempLocation;

@Element(name="localTempLocation", required=false)
private String localTempLocation;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public void setRemoteLocation(RemoteFile remoteFile) {
remoteLocation = remoteFile.getName();
}

public RemoteFile getRemoteFile() {
try {
return RemoteFile.createRemoteFile(remoteLocation);
}
catch (StorageException e) {
logger.log(Level.INFO, "Invalid remote temporary filename: " + remoteLocation);
return null;
}
}

public void setRemoteTempLocation(TempRemoteFile tempRemoteFile) {
remoteTempLocation = tempRemoteFile.getName();
}

public TempRemoteFile getTempRemoteFile() {
try {
return RemoteFile.createRemoteFile(remoteTempLocation, TempRemoteFile.class);
}
catch (StorageException e) {
logger.log(Level.INFO, "Invalid remote temporary filename: " + remoteTempLocation);
return null;
}
}

public void setLocalTempLocation(File file) {
localTempLocation = file.getAbsolutePath();
}

public File getLocalTempLocation() {
return new File(localTempLocation);
}

public void execute() {
if (TYPE_UPLOAD.equals(getType())) {

}
else if (TYPE_DELETE.equals(getType())) {
throw new RuntimeException("NOT IMPLEMENTED YET");
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.plugins.transfer.files;
package org.syncany.plugins.transfer.to;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Commit;
import org.simpleframework.xml.core.Persist;
import org.syncany.plugins.StorageException;

/**
* The Transaction transfer object exists to serialize a transaction,
Expand All @@ -49,48 +45,28 @@ public class TransactionTO {
@Element(name="machineName")
private String machineName;

@ElementMap(entry="file", key="tempLocation", value="finalLocation", attribute=false)
private Map<String, String> tempToTargetFileNamesMap;
@ElementList(entry="action")
private List<TransactionActionTO> transactionActionTOs;

private Map<TempRemoteFile, RemoteFile> tempToTargetFileMap;

public TransactionTO() {
// Nothing
}

public TransactionTO(String machineName, Map<TempRemoteFile, RemoteFile> finalLocations) {
public TransactionTO(String machineName) {
this.machineName = machineName;
this.tempToTargetFileMap = finalLocations;
transactionActionTOs = new ArrayList<TransactionActionTO>();
}

@Persist
public void prepare() {
tempToTargetFileNamesMap = new HashMap<String, String>();

for (TempRemoteFile tempFile : tempToTargetFileMap.keySet()) {
tempToTargetFileNamesMap.put(tempFile.getName(), tempToTargetFileMap.get(tempFile).getName());
}
}

@Commit
public void commit() {
tempToTargetFileMap = new HashMap<TempRemoteFile, RemoteFile>();

for (String tempFile : tempToTargetFileNamesMap.keySet()) {
try {
tempToTargetFileMap.put(new TempRemoteFile(tempFile), RemoteFile.createRemoteFile(tempToTargetFileNamesMap.get(tempFile)));
}
catch (StorageException e) {
logger.log(Level.INFO, "Invalid remote temporary filename: " + tempFile + " or " + tempToTargetFileNamesMap.get(tempFile));
}
}
public String getMachineName() {
return machineName;
}

public Map<TempRemoteFile, RemoteFile> getTempToTargetFileMap() {
return tempToTargetFileMap;
public List<TransactionActionTO> getTransactionActions() {
return transactionActionTOs;
}

public String getMachineName() {
return machineName;
public void addTransactionAction(TransactionActionTO transactionAction) {
transactionActionTOs.add(transactionAction);
}
}

0 comments on commit 206698a

Please sign in to comment.