Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions dropbox/src/main/java/com/sukanth/dropbox/DropBoxFileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import com.dropbox.core.v2.files.Metadata;
import com.sukanth.dropbox.pojo.DropBoxApplicationProperties;
import org.apache.log4j.Logger;

/** @author sukanthgunda */
Expand All @@ -29,48 +30,44 @@ public class DropBoxFileTransfer {
private static Logger logger = Logger.getLogger(DropBoxFileTransfer.class);

public static void main(String[] args) throws InterruptedException {
Properties properties;
DropBoxApplicationProperties properties;
ThreadPoolExecutor threadPoolExecutor = null;
ListFolderResult result;
LocalDateTime startTime = LocalDateTime.now();
DropBoxFileTransferJob dropBoxFileTransferJob = null;
DbxClientV2 dropboxClient = null;
properties = loadPropertiesFile();
logger.info("Loaded Properties");
String sourceLocation = properties.getProperty("SOURCE_LOCATION").trim();
boolean includedDeleted =
Boolean.parseBoolean(properties.getProperty("INCLUDE_DELETED").trim());
int threadPoolSize = Integer.parseInt(properties.getProperty("THREAD_POOL_SIZE"));
String accessToken = properties.getProperty("ACCESS_TOKEN").trim();
String clientIdentifier = properties.getProperty("CLIENT_IDENTIFIER").trim();
String destinationLocation = properties.getProperty("DESTINATION_LOCATION").trim();

List<Metadata> listFolderResultsMetadata = new ArrayList<>();
try {
logger.info("Transfer Start Time " + startTime);
logger.info("Started transferring files in " + sourceLocation);
dropboxClient = authenticate(accessToken, clientIdentifier);
logger.info("Started transferring files in " + properties.getSourceLocation());
dropboxClient = authenticate(properties.getAccessToken(), properties.getAccessToken());

logger.info(
"Authenticated to User "
+ dropboxClient.users().getCurrentAccount().getName().getDisplayName());
result =
dropboxClient
.files()
.listFolderBuilder(sourceLocation)
.withIncludeDeleted(includedDeleted)
.listFolderBuilder(loadPropertiesFile().getSourceLocation())
.withIncludeDeleted(properties.isIncludedDeleted())
.withRecursive(true)
.withIncludeMediaInfo(false)
.start();

threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadPoolSize);
threadPoolExecutor =
(ThreadPoolExecutor) Executors.newFixedThreadPool(properties.getThreadPooSize());
logger.info("Thread Pool Size " + threadPoolExecutor.getMaximumPoolSize());
while (true) {
if (Objects.nonNull(result)) {
List<Metadata> metadataList = result.getEntries();
for (Metadata resultMetaData : metadataList) {
listFolderResultsMetadata.add(resultMetaData);
dropBoxFileTransferJob =
new DropBoxFileTransferJob(destinationLocation, resultMetaData, dropboxClient);
new DropBoxFileTransferJob(
properties.getDestinationLocation(), resultMetaData, dropboxClient);
threadPoolExecutor.execute(dropBoxFileTransferJob);
}
if (!result.getHasMore()) {
Expand All @@ -92,7 +89,10 @@ public static void main(String[] args) throws InterruptedException {
logger.warn("RETRYING FAILED TRANSFER " + failedFile);
if (Objects.nonNull(dropBoxFileTransferJob)) {
dropBoxFileTransferJob.downloadFile(
dropboxClient, failedFile, destinationLocation.concat(failedFile), true);
dropboxClient,
failedFile,
loadPropertiesFile().getDestinationLocation().concat(failedFile),
true);
}
}
}
Expand Down Expand Up @@ -146,19 +146,32 @@ public static DbxClientV2 authenticate(String accessToken, String clientIdentifi
*
* @return prop
*/
public static Properties loadPropertiesFile() {
Properties prop = null;
public static DropBoxApplicationProperties loadPropertiesFile() {
Properties prop;
DropBoxApplicationProperties dropBoxApplicationProperties = new DropBoxApplicationProperties();
try (InputStream inputStream =
DropBoxFileTransfer.class.getClassLoader().getResourceAsStream("config.properties")) {
prop = new Properties();
if (Objects.isNull(inputStream)) {
throw new IOException("Unable to find config.properties");
}
prop.load(inputStream);

dropBoxApplicationProperties.setAccessToken(prop.getProperty("ACCESS_TOKEN").trim());
dropBoxApplicationProperties.setClientIdentifier(
prop.getProperty("CLIENT_IDENTIFIER").trim());
dropBoxApplicationProperties.setSourceLocation(prop.getProperty("SOURCE_LOCATION").trim());
dropBoxApplicationProperties.setIncludedDeleted(
Boolean.parseBoolean(prop.getProperty("INCLUDE_DELETED").trim()));
dropBoxApplicationProperties.setThreadPooSize(
Integer.parseInt(prop.getProperty("THREAD_POOL_SIZE")));
dropBoxApplicationProperties.setDestinationLocation(
prop.getProperty("DESTINATION_LOCATION").trim());

} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
System.exit(0);
}
return prop;
return dropBoxApplicationProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.sukanth.dropbox.pojo;

/**
* @apiNote pojo to hold application properties data
*/
public class DropBoxApplicationProperties {
private String sourceLocation;
private String destinationLocation;
private boolean isIncludedDeleted;
private int threadPooSize;
private String accessToken;
private String clientIdentifier;

public String getSourceLocation() {
return sourceLocation;
}

public void setSourceLocation(String sourceLocation) {
this.sourceLocation = sourceLocation;
}

public String getDestinationLocation() {
return destinationLocation;
}

public void setDestinationLocation(String destinationLocation) {
this.destinationLocation = destinationLocation;
}

public boolean isIncludedDeleted() {
return isIncludedDeleted;
}

public void setIncludedDeleted(boolean includedDeleted) {
isIncludedDeleted = includedDeleted;
}

public int getThreadPooSize() {
return threadPooSize;
}

public void setThreadPooSize(int threadPooSize) {
this.threadPooSize = threadPooSize;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public String getClientIdentifier() {
return clientIdentifier;
}

public void setClientIdentifier(String clientIdentifier) {
this.clientIdentifier = clientIdentifier;
}
}