src/org/ohmage/cache/XVideoDirectoryCache.java
private static synchronized void init(
			final int numFilesPerDirectory)
			throws DomainException {
		
		try {
			// If the current leaf directory has been set, we weren't the first
			// to call init(), so we can just back out.
			if(currVideoDirectory != null) {
				return;
			}
			
			// Get the root directory from the preference cache based on the
			// key.
			String rootFile;
			try {
				rootFile = 
					PreferenceCache.instance().lookup(
						PreferenceCache.KEY_VIDEO_DIRECTORY);
			}
			catch(CacheMissException e) {
				throw new DomainException(
					"Preference cache doesn't know about 'known' key: " + 
						PreferenceCache.KEY_VIDEO_DIRECTORY,
					e);
			}
			File rootDirectory = new File(rootFile);
			if(! rootDirectory.exists()) {
				throw new DomainException(
					"The root file doesn't exist suggesting an incomplete installation: " + 
						rootFile);
			}
			else if(! rootDirectory.isDirectory()) {
				throw new DomainException("The root file isn't a directory.");
			}
			
			// Get the number of folders deep that documents are stored.
			int fileDepth;
			try {
				fileDepth = 
					Integer.decode(
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_FILE_HIERARCHY_DEPTH));
			}
			catch(CacheMissException e) {
				throw new DomainException(
					"Preference cache doesn't know about 'known' key: " +
						PreferenceCache.KEY_FILE_HIERARCHY_DEPTH,
					e);
			}
			catch(NumberFormatException e) {
				throw new DomainException(
					"Stored value for key '" + 
						PreferenceCache.KEY_FILE_HIERARCHY_DEPTH + 
						"' is not decodable as a number.",
					e);
			}
			
			DirectoryFilter directoryFilter = new DirectoryFilter();
			File currDirectory = rootDirectory;
			for(int currDepth = 0; currDepth < fileDepth; currDepth++) {
				// Get the list of directories in the current directory.
				File[] currDirectories = 
					currDirectory.listFiles(directoryFilter);
				
				// If there aren't any, create the first sub-directory in this
				// directory.
				if(currDirectories.length == 0) {
					String newFolderName = 
						directoryNameBuilder(0, numFilesPerDirectory);
					currDirectory = 
						new File(
							currDirectory.getAbsolutePath() + 
							"/" + 
							newFolderName);
					currDirectory.mkdir();
				}
				// If the directory is overly full, step back up in the
				// structure. This should never happen, as it indicates that
				// there is an overflow in the structure.
				else if(currDirectories.length > numFilesPerDirectory) {
					// Take a step back in our depth.
					currDepth--;
					
					// If, while backing up the tree, we back out of the root
					// directory, we have filled up the space.
					if(currDepth < 0) {
						throw new DomainException(
							"Image directory structure full!");
					}

					// Get the next parent and the current directory to it.
					int nextDirectoryNumber = 
						Integer.decode(currDirectory.getName()) + 1;
					currDirectory = 
						new File(
							currDirectory.getParent() + 
							"/" + 
							nextDirectoryNumber);
					
					// If the directory already exists, then there is either a
					// concurrency issue or someone else is adding files.
					// Either way, this shouldn't happen.
					if(currDirectory.exists()) {
						throw new DomainException(
							"Somehow the 'new' directory already exists. This should be looked into: " + 
								currDirectory.getAbsolutePath());
					}
					// Otherwise, create the directory.
					else {
						currDirectory.mkdir();
					}
				}
				// Drill down to the directory with the largest, numeric value.
				else {
					currDirectory = getLargestSubfolder(currDirectories);
				}
			}
			
			// After we have found a suitable directory, set it.
			currVideoDirectory = currDirectory;
		}
		catch(SecurityException e) {
			throw new DomainException(
				"The current process doesn't have sufficient permiossions to create new directories.",
				e);
		}
	}
/**
	 * Initializes the directory structure by drilling down to the leaf
	 * directory with each step choosing the directory with the largest
	 * integer value.
	 */
Initializes the directory structure by drilling down to the leaf
directory with each step choosing the directory with the largest
integer value.
Initializes the directory structure by drilling down to the leaf directory with each step choosing the directory with the largest integer value .
private static synchronized void init(
			final int numFilesPerDirectory)
			throws DomainException {
		try {
			if(currVideoDirectory != null) {
				return;
			}
			String rootFile;
			try {
				rootFile = 
					PreferenceCache.instance().lookup(
						PreferenceCache.KEY_VIDEO_DIRECTORY);
			}
			catch(CacheMissException e) {
				throw new DomainException(
					"Preference cache doesn't know about 'known' key: " + 
						PreferenceCache.KEY_VIDEO_DIRECTORY,
					e);
			}
			File rootDirectory = new File(rootFile);
			if(! rootDirectory.exists()) {
				throw new DomainException(
					"The root file doesn't exist suggesting an incomplete installation: " + 
						rootFile);
			}
			else if(! rootDirectory.isDirectory()) {
				throw new DomainException("The root file isn't a directory.");
			}
			int fileDepth;
			try {
				fileDepth = 
					Integer.decode(
						PreferenceCache.instance().lookup(
							PreferenceCache.KEY_FILE_HIERARCHY_DEPTH));
			}
			catch(CacheMissException e) {
				throw new DomainException(
					"Preference cache doesn't know about 'known' key: " +
						PreferenceCache.KEY_FILE_HIERARCHY_DEPTH,
					e);
			}
			catch(NumberFormatException e) {
				throw new DomainException(
					"Stored value for key '" + 
						PreferenceCache.KEY_FILE_HIERARCHY_DEPTH + 
						"' is not decodable as a number.",
					e);
			}
			DirectoryFilter directoryFilter = new DirectoryFilter();
			File currDirectory = rootDirectory;
			for(int currDepth = 0; currDepth < fileDepth; currDepth++) {
				File[] currDirectories = 
					currDirectory.listFiles(directoryFilter);
				if(currDirectories.length == 0) {
					String newFolderName = 
						directoryNameBuilder(0, numFilesPerDirectory);
					currDirectory = 
						new File(
							currDirectory.getAbsolutePath() + 
							"/" + 
							newFolderName);
					currDirectory.mkdir();
				}
				else if(currDirectories.length > numFilesPerDirectory) {
					currDepth--;
					if(currDepth < 0) {
						throw new DomainException(
							"Image directory structure full!");
					}
					int nextDirectoryNumber = 
						Integer.decode(currDirectory.getName()) + 1;
					currDirectory = 
						new File(
							currDirectory.getParent() + 
							"/" + 
							nextDirectoryNumber);
					if(currDirectory.exists()) {
						throw new DomainException(
							"Somehow the 'new' directory already exists. This should be looked into: " + 
								currDirectory.getAbsolutePath());
					}
					else {
						currDirectory.mkdir();
					}
				}
				else {
					currDirectory = getLargestSubfolder(currDirectories);
				}
			}
			currVideoDirectory = currDirectory;
		}
		catch(SecurityException e) {
			throw new DomainException(
				"The current process doesn't have sufficient permiossions to create new directories.",
				e);
		}
	}
private static synchronized void init ( final int numFilesPerDirectory ) throws DomainException { try { if ( currVideoDirectory != null ) { return ; } String rootFile ; try { rootFile = PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_VIDEO_DIRECTORY ) ; } catch ( CacheMissException e ) { throw new DomainException ( "Preference cache doesn't know about 'known' key: " + PreferenceCache . KEY_VIDEO_DIRECTORY , e ) ; } File rootDirectory = new File ( rootFile ) ; if ( ! rootDirectory . exists ( ) ) { throw new DomainException ( "The root file doesn't exist suggesting an incomplete installation: " + rootFile ) ; } else if ( ! rootDirectory . isDirectory ( ) ) { throw new DomainException ( "The root file isn't a directory." ) ; } int fileDepth ; try { fileDepth = Integer . decode ( PreferenceCache . instance ( ) . lookup ( PreferenceCache . KEY_FILE_HIERARCHY_DEPTH ) ) ; } catch ( CacheMissException e ) { throw new DomainException ( "Preference cache doesn't know about 'known' key: " + PreferenceCache . KEY_FILE_HIERARCHY_DEPTH , e ) ; } catch ( NumberFormatException e ) { throw new DomainException ( "Stored value for key '" + PreferenceCache . KEY_FILE_HIERARCHY_DEPTH + "' is not decodable as a number." , e ) ; } DirectoryFilter directoryFilter = new DirectoryFilter ( ) ; File currDirectory = rootDirectory ; for ( int currDepth = 0 ; currDepth < fileDepth ; currDepth ++ ) { File [ ] currDirectories = currDirectory . listFiles ( directoryFilter ) ; if ( currDirectories . length == 0 ) { String newFolderName = directoryNameBuilder ( 0 , numFilesPerDirectory ) ; currDirectory = new File ( currDirectory . getAbsolutePath ( ) + "/" + newFolderName ) ; currDirectory . mkdir ( ) ; } else if ( currDirectories . length > numFilesPerDirectory ) { currDepth -- ; if ( currDepth < 0 ) { throw new DomainException ( "Image directory structure full!" ) ; } int nextDirectoryNumber = Integer . decode ( currDirectory . getName ( ) ) + 1 ; currDirectory = new File ( currDirectory . getParent ( ) + "/" + nextDirectoryNumber ) ; if ( currDirectory . exists ( ) ) { throw new DomainException ( "Somehow the 'new' directory already exists. This should be looked into: " + currDirectory . getAbsolutePath ( ) ) ; } else { currDirectory . mkdir ( ) ; } } else { currDirectory = getLargestSubfolder ( currDirectories ) ; } } currVideoDirectory = currDirectory ; } catch ( SecurityException e ) { throw new DomainException ( "The current process doesn't have sufficient permiossions to create new directories." , e ) ; } }
Initializes the directory structure by drilling down to the leaf
 directory with each step choosing the directory with the largest
 integer value.
Initializes the directory structure by drilling down to the leaf directory with each step choosing the directory with the largest integer value .
// If the current leaf directory has been set, we weren't the first // to call init(), so we can just back out. // Get the root directory from the preference cache based on the // key. // Get the number of folders deep that documents are stored. // Get the list of directories in the current directory. // If there aren't any, create the first sub-directory in this // directory. // If the directory is overly full, step back up in the // structure. This should never happen, as it indicates that // there is an overflow in the structure. // Take a step back in our depth. // If, while backing up the tree, we back out of the root // directory, we have filled up the space. // Get the next parent and the current directory to it. // If the directory already exists, then there is either a // concurrency issue or someone else is adding files. // Either way, this shouldn't happen. // Otherwise, create the directory. // Drill down to the directory with the largest, numeric value. // After we have found a suitable directory, set it.