Package com.puppycrawl.tools.checkstyle
Class PropertyCacheFile
java.lang.Object
com.puppycrawl.tools.checkstyle.PropertyCacheFile
This 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 ClassesModifier and TypeClassDescriptionprivate static final class
Class which represents external resource. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
Default number for base 16 encoding.private static final byte[]
Default buffer for reading from streams.private static final int
Size of default byte array for buffer.private final Configuration
Configuration object.static final String
The property key to use for storing the hashcode of the configuration.private String
Generated configuration hash.private final Properties
The details on files.static final String
The property prefix to use for storing the hashcode of an external resource.private final String
File name of cache. -
Constructor Summary
ConstructorsConstructorDescriptionPropertyCacheFile
(Configuration config, String fileName) Creates a newPropertyCacheFile
instance. -
Method Summary
Modifier and TypeMethodDescriptionprivate boolean
Checks whether the contents of external configuration resources were changed.private void
fillCacheWithExternalResources
(Set<PropertyCacheFile.ExternalResource> externalResources) Fills cache with a set ofPropertyCacheFile.ExternalResource
.Retrieves the hash of a specific file.private static String
Calculates the hashcode for the serializable object based on its content.boolean
Checks that file is in cache.private boolean
Checks whether the resource is changed.private boolean
isResourceLocationInCache
(String location) Checks whether resource location is in cache.void
load()
Load cached values from file.private static byte[]
loadExternalResource
(String location) Loads the content of external resource.private static Set<PropertyCacheFile.ExternalResource>
loadExternalResources
(Set<String> resourceLocations) Loads a set ofPropertyCacheFile.ExternalResource
based on their locations.void
persist()
Cleans up the object and updates the cache file.void
Records that a file checked ok.void
putExternalResources
(Set<String> locations) Puts external resources in cache.void
Removed a specific file from the cache.void
reset()
Resets the cache to be empty except for the configuration hash.private static void
serialize
(Serializable object, OutputStream outputStream) Serializes object to output stream.private static byte[]
toByteArray
(InputStream stream) Reads all the contents of an input stream and returns it as a byte array.
-
Field Details
-
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:
-
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:
-
BUFFER_SIZE
Size of default byte array for buffer.- See Also:
-
BUFFER
Default buffer for reading from streams. -
BASE_16
Default number for base 16 encoding.- See Also:
-
details
The details on files. -
config
Configuration object. -
fileName
File name of cache. -
configHash
Generated configuration hash.
-
-
Constructor Details
-
PropertyCacheFile
Creates a newPropertyCacheFile
instance.- Parameters:
config
- the current configuration, not nullfileName
- the cache file- Throws:
IllegalArgumentException
- when either arguments are null
-
-
Method Details
-
load
Load cached values from file.- Throws:
IOException
- when there is a problems with file read
-
persist
Cleans up the object and updates the cache file.- Throws:
IOException
- when there is a problems with file save
-
reset
Resets the cache to be empty except for the configuration hash. -
isInCache
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
Records that a file checked ok.- Parameters:
checkedFileName
- name of the file that checked oktimestamp
- the timestamp of the file
-
get
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
Removed a specific file from the cache.- Parameters:
checkedFileName
- The name of the file to remove.
-
getHashCodeBasedOnObjectContent
Calculates the hashcode for the serializable object based on its content.- Parameters:
object
- serializable object.- Returns:
- the hashcode for serializable object.
- Throws:
IllegalStateException
- when some unexpected happened.
-
serialize
Serializes object to output stream.- Parameters:
object
- object to be serializedoutputStream
- serialization stream- Throws:
IOException
- if an error occurs
-
putExternalResources
Puts external resources in cache. If at least one external resource changed, clears the cache.- Parameters:
locations
- locations of external resources.
-
loadExternalResources
private static Set<PropertyCacheFile.ExternalResource> loadExternalResources(Set<String> resourceLocations) Loads a set ofPropertyCacheFile.ExternalResource
based on their locations.- Parameters:
resourceLocations
- locations of external configuration resources.- Returns:
- a set of
PropertyCacheFile.ExternalResource
.
-
loadExternalResource
Loads 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:
IOException
- if error while loading occurs.CheckstyleException
- if error while loading occurs.
-
toByteArray
Reads all the contents of an input stream and returns it as a byte array.- Parameters:
stream
- The input stream to read from.- Returns:
- The resulting byte array of the stream.
- Throws:
IOException
- if there is an error reading the input stream.
-
areExternalResourcesChanged
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.
-
isResourceChanged
Checks whether the resource is changed.- Parameters:
resource
- resource to check.- Returns:
- true if resource is changed.
-
fillCacheWithExternalResources
private void fillCacheWithExternalResources(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
Checks whether resource location is in cache.- Parameters:
location
- resource location.- Returns:
- true if resource location is in cache.
-