org.rhq.core.util.updater
Class FileHashcodeMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.TreeMap<String,String>
          extended by org.rhq.core.util.updater.FileHashcodeMap
All Implemented Interfaces:
Serializable, Cloneable, Map<String,String>, NavigableMap<String,String>, SortedMap<String,String>
Direct Known Subclasses:
ChangesFileHashcodeMap

public class FileHashcodeMap
extends TreeMap<String,String>

This provides a hashmap that maps a filename to its hashcode value (md5). The key to the map will be filenames; the values are unique hashcodes generated from the content of the files. Note that the keys can be either relative or absolute paths. If relative, some outside entity will need to know how to resolve those relative paths (i.e. some outside entity would need to know the top level root directory for all relative paths).

Author:
John Mazzitelli
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
static String DELETED_FILE_HASHCODE
           
static String UNKNOWN_DIR_HASHCODE
           
static String UNKNOWN_FILE_HASHCODE
           
 
Constructor Summary
FileHashcodeMap()
           
 
Method Summary
 boolean containsKey(Object key)
           
 String convertPath(String path)
          Converts the path to the form that will be stored internally.
static FileHashcodeMap generateFileHashcodeMap(File rootDir, Pattern ignoreRegex, Set<String> ignored)
          Given a directory, this will recursively traverse that directory's files/subdirectories and generate the hashcode map for all files it encounters and add that data to the given map.
 String get(Object key)
           
 Map<String,String> getUnknownContent()
          If a file was not readable or its hashcode could not be generated for some reason, its path will be returned as a key to the returned map.
static FileHashcodeMap loadFromFile(File file)
          Loads in the file that contains file/hashcode map data.
 String put(String key, String value)
           
 String remove(Object key)
           
 ChangesFileHashcodeMap rescan(File rootDir, Pattern ignoreRegex, boolean reportNewRootFilesAsNew)
          This rescans a set of files (found in this map) and returns a map with an updated, current set of hashcodes.
 void storeToFile(File file)
          Takes all map entries in this object and writes them to the given file such that it can later be loaded in via loadFromFile(File).
 
Methods inherited from class java.util.TreeMap
ceilingEntry, ceilingKey, clear, clone, comparator, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, putAll, size, subMap, subMap, tailMap, tailMap, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty
 

Field Detail

UNKNOWN_DIR_HASHCODE

public static final String UNKNOWN_DIR_HASHCODE
See Also:
Constant Field Values

UNKNOWN_FILE_HASHCODE

public static final String UNKNOWN_FILE_HASHCODE
See Also:
Constant Field Values

DELETED_FILE_HASHCODE

public static final String DELETED_FILE_HASHCODE
See Also:
Constant Field Values
Constructor Detail

FileHashcodeMap

public FileHashcodeMap()
Method Detail

generateFileHashcodeMap

public static FileHashcodeMap generateFileHashcodeMap(File rootDir,
                                                      Pattern ignoreRegex,
                                                      Set<String> ignored)
                                               throws Exception
Given a directory, this will recursively traverse that directory's files/subdirectories and generate the hashcode map for all files it encounters and add that data to the given map. If given a regular file, a single entry is added to the given map. Note that the returned map will have all relative paths as keys (relative to rootDir). Also note that ignoreRegex is matched against relative paths.

Parameters:
rootDir - existing directory to scan and generate hashcodes for all its files
ignoreRegex - a regular expression that indicates which files/directories should be ignored. If a relative file/directory path matches this regex, it will be skipped.
ignored - a set that will contain those files/directories that were ignored while scanning the root dir
Throws:
Exception - if failed to generate hashcode for the directory

loadFromFile

public static FileHashcodeMap loadFromFile(File file)
                                    throws Exception
Loads in the file that contains file/hashcode map data.

Parameters:
file - the file to load
Returns:
map of files/hashcodes found in the file
Throws:
Exception

storeToFile

public void storeToFile(File file)
                 throws Exception
Takes all map entries in this object and writes them to the given file such that it can later be loaded in via loadFromFile(File).

Parameters:
file - the file to store the entries to
Throws:
Exception - if failed to store the entries to the given file

rescan

public ChangesFileHashcodeMap rescan(File rootDir,
                                     Pattern ignoreRegex,
                                     boolean reportNewRootFilesAsNew)
                              throws Exception
This rescans a set of files (found in this map) and returns a map with an updated, current set of hashcodes. If this original map has relative paths, they will be considered relative to the given root directory. If a file is not found, it will still be in the returned map but its hashcode will be DELETED_FILE_HASHCODE. The root directory is also scanned for new files that are not in this original map - if new files are found (and they do not match the ignoreRegex), they are added to the returned map. Note that if reportNewRootFilesAsNew is false, and if new files are found in the top root directory and they are not related to the deployment fileset, they will not be added to the returned map.

Parameters:
rootDir - directory where the relative paths are expected to be
ignoreRegex - if relative paths of files under rootDir match this, they will be ignored. This will eliminate files/directories from being considered "new" because they aren't in original.
reportNewRootFilesAsNew - do not report as new any unrelated files found in the root dir
Returns:
a map with current files/hashcodes, including files that were not found in original. the returned object also has additional info such as those files that were added, deleted, changed from this original. It also indicates what was ignored during the rescan.
Throws:
Exception

getUnknownContent

public Map<String,String> getUnknownContent()
If a file was not readable or its hashcode could not be generated for some reason, its path will be returned as a key to the returned map. The value will be UNKNOWN_FILE_HASHCODE. If a directory was not readable or its list of files could not be retrieved for some reason, its path will be returned as a key to the returned map. The value will be UNKNOWN_DIR_HASHCODE.

Returns:
map of file or directories whose hashcodes could not be determined. Will be null if the map is fully complete and all content was able to have its hashcodes generated.

put

public String put(String key,
                  String value)
Specified by:
put in interface Map<String,String>
Overrides:
put in class TreeMap<String,String>

get

public String get(Object key)
Specified by:
get in interface Map<String,String>
Overrides:
get in class TreeMap<String,String>

remove

public String remove(Object key)
Specified by:
remove in interface Map<String,String>
Overrides:
remove in class TreeMap<String,String>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<String,String>
Overrides:
containsKey in class TreeMap<String,String>

convertPath

public String convertPath(String path)
Converts the path to the form that will be stored internally.

Parameters:
path - a filepath to be converted
Returns:
the converted path that is to be used to store in the internal sets.


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.