Package com.puppycrawl.tools.checkstyle
Class PropertyCacheFile
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.PropertyCacheFile
-
final class PropertyCacheFile extends java.lang.ObjectThis class maintains a persistent(on file-system) store of the files that have checked ok(no validation events) and their associated timestamp. It is used to optimize Checkstyle between few launches. It is mostly useful for plugin and extensions of Checkstyle. It uses a property file for storage. A hashcode of the Configuration is stored in the cache file to ensure the cache is invalidated when the configuration has changed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPropertyCacheFile.ExternalResourceClass which represents external resource.
-
Field Summary
Fields Modifier and Type Field Description private ConfigurationconfigConfiguration object.static java.lang.StringCONFIG_HASH_KEYThe property key to use for storing the hashcode of the configuration.private java.lang.StringconfigHashGenerated configuration hash.private java.util.PropertiesdetailsThe details on files.static java.lang.StringEXTERNAL_RESOURCE_KEY_PREFIXThe property prefix to use for storing the hashcode of an external resource.private java.lang.StringfileNameFile name of cache.
-
Constructor Summary
Constructors Constructor Description PropertyCacheFile(Configuration config, java.lang.String fileName)Creates a newPropertyCacheFileinstance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private booleanareExternalResourcesChanged(java.util.Set<PropertyCacheFile.ExternalResource> resources)Checks whether the contents of external configuration resources were changed.private voidfillCacheWithExternalResources(java.util.Set<PropertyCacheFile.ExternalResource> externalResources)Fills cache with a set ofPropertyCacheFile.ExternalResource.private static voidflushAndCloseOutStream(java.io.OutputStream stream)Flushes and closes output stream.java.lang.Stringget(java.lang.String name)Retrieves the hash of a specific file.private static java.lang.StringgetHashCodeBasedOnObjectContent(java.io.Serializable object)Calculates the hashcode for the serializable object based on its content.booleanisInCache(java.lang.String uncheckedFileName, long timestamp)Checks that file is in cache.private booleanisResourceLocationInCache(java.lang.String location)Checks whether resource location is in cache.voidload()Load cached values from file.private static byte[]loadExternalResource(java.lang.String location)Loads the content of external resource.private static java.util.Set<PropertyCacheFile.ExternalResource>loadExternalResources(java.util.Set<java.lang.String> resourceLocations)Loads a set ofPropertyCacheFile.ExternalResourcebased on their locations.voidpersist()Cleans up the object and updates the cache file.voidput(java.lang.String checkedFileName, long timestamp)Records that a file checked ok.voidputExternalResources(java.util.Set<java.lang.String> locations)Puts external resources in cache.voidremove(java.lang.String checkedFileName)Removed a specific file from the cache.voidreset()Resets the cache to be empty except for the configuration hash.
-
-
-
Field Detail
-
CONFIG_HASH_KEY
public static final java.lang.String CONFIG_HASH_KEY
The property key to use for storing the hashcode of the configuration. To avoid name clashes with the files that are checked the key is chosen in such a way that it cannot be a valid file name.- See Also:
- Constant Field Values
-
EXTERNAL_RESOURCE_KEY_PREFIX
public static final java.lang.String EXTERNAL_RESOURCE_KEY_PREFIX
The property prefix to use for storing the hashcode of an external resource. To avoid name clashes with the files that are checked the prefix is chosen in such a way that it cannot be a valid file name and makes it clear it is a resource.- See Also:
- Constant Field Values
-
details
private final java.util.Properties details
The details on files.
-
config
private final Configuration config
Configuration object.
-
fileName
private final java.lang.String fileName
File name of cache.
-
configHash
private java.lang.String configHash
Generated configuration hash.
-
-
Constructor Detail
-
PropertyCacheFile
PropertyCacheFile(Configuration config, java.lang.String fileName)
Creates a newPropertyCacheFileinstance.- Parameters:
config- the current configuration, not nullfileName- the cache file
-
-
Method Detail
-
load
public void load() throws java.io.IOExceptionLoad cached values from file.- Throws:
java.io.IOException- when there is a problems with file read
-
persist
public void persist() throws java.io.IOExceptionCleans up the object and updates the cache file.- Throws:
java.io.IOException- when there is a problems with file save
-
reset
public void reset()
Resets the cache to be empty except for the configuration hash.
-
flushAndCloseOutStream
private static void flushAndCloseOutStream(java.io.OutputStream stream) throws java.io.IOExceptionFlushes and closes output stream.- Parameters:
stream- the output stream- Throws:
java.io.IOException- when there is a problems with file flush and close
-
isInCache
public boolean isInCache(java.lang.String uncheckedFileName, long timestamp)Checks that file is in cache.- Parameters:
uncheckedFileName- the file to checktimestamp- the timestamp of the file to check- Returns:
- whether the specified file has already been checked ok
-
put
public void put(java.lang.String checkedFileName, long timestamp)Records that a file checked ok.- Parameters:
checkedFileName- name of the file that checked oktimestamp- the timestamp of the file
-
get
public java.lang.String get(java.lang.String name)
Retrieves the hash of a specific file.- Parameters:
name- The name of the file to retrieve.- Returns:
- The has of the file or
null.
-
remove
public void remove(java.lang.String checkedFileName)
Removed a specific file from the cache.- Parameters:
checkedFileName- The name of the file to remove.
-
getHashCodeBasedOnObjectContent
private static java.lang.String getHashCodeBasedOnObjectContent(java.io.Serializable object)
Calculates the hashcode for the serializable object based on its content.- Parameters:
object- serializable object.- Returns:
- the hashcode for serializable object.
-
putExternalResources
public void putExternalResources(java.util.Set<java.lang.String> locations)
Puts external resources in cache. If at least one external resource changed, clears the cache.- Parameters:
locations- locations of external resources.
-
loadExternalResources
private static java.util.Set<PropertyCacheFile.ExternalResource> loadExternalResources(java.util.Set<java.lang.String> resourceLocations)
Loads a set ofPropertyCacheFile.ExternalResourcebased on their locations.- Parameters:
resourceLocations- locations of external configuration resources.- Returns:
- a set of
PropertyCacheFile.ExternalResource.
-
loadExternalResource
private static byte[] loadExternalResource(java.lang.String location) throws CheckstyleExceptionLoads the content of external resource.- Parameters:
location- external resource location.- Returns:
- array of bytes which represents the content of external resource in binary form.
- Throws:
CheckstyleException- if error while loading occurs.
-
areExternalResourcesChanged
private boolean areExternalResourcesChanged(java.util.Set<PropertyCacheFile.ExternalResource> resources)
Checks whether the contents of external configuration resources were changed.- Parameters:
resources- a set ofPropertyCacheFile.ExternalResource.- Returns:
- true if the contents of external configuration resources were changed.
-
fillCacheWithExternalResources
private void fillCacheWithExternalResources(java.util.Set<PropertyCacheFile.ExternalResource> externalResources)
Fills cache with a set ofPropertyCacheFile.ExternalResource. If external resource from the set is already in cache, it will be skipped.- Parameters:
externalResources- a set ofPropertyCacheFile.ExternalResource.
-
isResourceLocationInCache
private boolean isResourceLocationInCache(java.lang.String location)
Checks whether resource location is in cache.- Parameters:
location- resource location.- Returns:
- true if resource location is in cache.
-
-