package com.oxygencloud.sdk.util;
// 2013-02-19 johnpfeiffer requires: O2JavaSDK-0.0.345-jar-with-dependencies.jar (if using pom.xml rename the jar to O2JavaSDK-0.0.345.jar)
// Avoiding recursion in order to scale beyond the JVM stack
// TODO: validate how much more memory Breadth First Search uses than Depth First Search
// TODO: validate effect of moving a folder during the scan (Exception Handling)
// TODO: concurrency: review data structure choices LinkedBlockingQueue with timeout
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import com.oxygen.sdk.O2Agent;
import com.oxygen.sdk.O2AgentFactory;
import com.oxygen.sdk.O2FolderItem;
import com.oxygen.sdk.O2SessionInfo;
import com.oxygen.sdk.exception.O2NetworkException;
import com.oxygen.sdk.exception.O2RuleException;
import com.oxygen.sdk.exception.O2UnexpectedException;
public class SnapshotCLI {
protected static final int MINIMUMPARAMETERS = 1;
protected static final int MAXIMUMPARAMETERS = 3;
public static final String CORRECTUSAGE = "java -jar " + SnapshotCLI.class.getName() + ".jar https://API.URL apikey user password";
//Access to the data
private String apiURL;
private String apiKey;
private String oxygenUserId;
private String oxygenPassword;
private O2Agent agent = null;
//Maintain state
LinkedBlockingQueue<O2FolderItem> containerListUnvisited = null;
LinkedBlockingQueue<O2FolderItem> containerListVisited = null;
List<String> itemsByPath = null;
O2FolderItem currentContainer = null;
//Measure performance
private long startTimeInMilliseconds;
public static void main( String[] args ) throws O2NetworkException, O2RuleException, O2UnexpectedException {
if( args.length != 4 ) {
System.out.println( "ERROR: wrong number of parameters" );
System.out.println( CORRECTUSAGE );
System.exit( 1 );
}
SnapshotCLI main = new SnapshotCLI();
main.startTimeInMilliseconds = System.currentTimeMillis();
main.apiURL = args[0]; // TODO: input validation
main.apiKey = args[1];
main.oxygenUserId = args[2];
main.oxygenPassword = args[3];
main.agent = O2AgentFactory.createAndroidO2AgentCustomURL( main.apiURL );
O2SessionInfo session = main.agent.login( main.apiKey, main.oxygenUserId, main.oxygenPassword );
System.out.println( session.getUserID() + " has sessionId " + session.getSessionID() + " which expires "
+ session.getSessionExpirationTime() );
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Initial Seeding covering edge case of / root directory (User Volume)
main.containerListVisited = new LinkedBlockingQueue <O2FolderItem> ();
main.containerListUnvisited = new LinkedBlockingQueue <O2FolderItem> ();
main.itemsByPath = new ArrayList <String> ();
main.parseItemsList( main.agent.list() , "/" );
main.displayCurrentState( System.out );
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
main.currentContainer = main.containerListUnvisited.poll();
while( main.currentContainer != null )
{
main.containerListVisited.offer( main.currentContainer );
main.parseItemsList( main.agent.list( main.currentContainer ) , "/" + main.currentContainer.getName() + "/" );
main.currentContainer = main.containerListUnvisited.poll();
}
main.displayCurrentState( System.out ); //final state
} // end main
/* Helper Methods */
private void parseItemsList( List<O2FolderItem> containerList, String currentLevel ) {
for( O2FolderItem element : containerList ) {
O2FolderItem.Type currentType = element.getType();
if( currentType == O2FolderItem.Type.O2Space|| currentType == O2FolderItem.Type.O2Folder )
{
containerListUnvisited.offer( element );
}
if( currentType == O2FolderItem.Type.O2File )
{
itemsByPath.add( currentLevel + element.getName() );
}
}
}
private void displayItemsByPath( PrintStream out ) {
for( String item : itemsByPath ) {
out.println( item );
}
}
private void displayQueue( LinkedBlockingQueue<O2FolderItem> q, PrintStream out ) {
for( O2FolderItem element : q ) {
out.println( element.getName() );
}
}
private void displayCurrentState( PrintStream out ) {
if( itemsByPath == null || containerListVisited == null || containerListUnvisited == null ) {
throw new IllegalStateException( "ERROR: items, visited, or unvisited list is null" );
}
out.println( "\nITEMS (" + itemsByPath.size() + ")" );
displayItemsByPath( System.out );
out.println( "\nVISITED (" + containerListVisited.size() + ")" );
displayQueue( containerListVisited, System.out );
out.println( "\nUNVISITED (" + containerListUnvisited.size() + ")" );
displayQueue( containerListUnvisited, System.out );
out.println( "\n" + SnapshotCLI.class.getName() + " " + (System.currentTimeMillis() - startTimeInMilliseconds)
+ " milliseconds" );
}
} // end class