com.ca.commons.cbutil
Class CBCache

java.lang.Object
  |
  +--com.ca.commons.cbutil.CBCache

public class CBCache
extends java.lang.Object

This class creates a cache directory called 'temp' where temporary files are stored. Currently it works for audio, jpegPhoto and odDocumentDOC attributes. The audio are stored in a sub dir called 'audio'. It also contains methods for decreasing the cache size, cleaning (or emptying the cache and sorting the temporary files according to their last modified date.


Nested Class Summary
static class CBCache.FileComparator
          This Comparator compares two Files by their lastModified() date.
 
Constructor Summary
CBCache()
           
 
Method Summary
static void cleanCache()
          Cleans the cache of all temporary entries by deleting them.
static void cleanCache(java.lang.String currentDN)
          Cleans the cache of entries that start with the dn of the entry being modified.
static void createAudioCache(java.lang.String currentDN, javax.naming.directory.Attributes entry, java.lang.String type, int size)
          Creates an audio cache directory named 'temp/audio' and adds temporary files to it currently naming them (for example) with the hash of their 'DN + a unique number + .wav'.
static void createCache(java.lang.String currentDN, javax.naming.directory.Attributes entry, java.lang.String type, int size)
          Creates a cache directory named 'temp' and adds temporary files to it currently naming them (for example) with the hash of their 'DN + a unique number + .jpg'.
static void decreaseAudioCacheSize()
          Decreases the size of the audio cache (usually if the cache contains 100 or more temporary files), by deleting the oldest files until there is only 50 temporary files left in the cache.
static void decreaseCacheSize()
          Decreases the size of the cache (usually if the cache contains 100 or more temporary files), by deleting the oldest files until there is only 50 temporary files left in the cache.
static void doAudioCache(java.lang.String currentDN, byte[] bytes)
          Creates the audio temporary files.
static void doNormalCache(java.lang.String currentDN, byte[] bytes, java.lang.String extension)
          Does the actual writing of the cache files.
static java.io.File getAudioCacheDirectory()
          Returns the directory that is being used for the audio caching.
static java.lang.String getAudioDirPath()
          Returns the absolute path of the audio temp directory.
static java.io.File getCacheDirectory()
          Returns the directory that is being used for the caching.
static java.lang.String getDirPath()
          Returns the absolute path of the temp directory.
static java.io.File[] sortFiles(java.io.File[] files)
          Sorts an array of files in order of lastModified (oldest to newest).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CBCache

public CBCache()
Method Detail

createCache

public static void createCache(java.lang.String currentDN,
                               javax.naming.directory.Attributes entry,
                               java.lang.String type,
                               int size)
Creates a cache directory named 'temp' and adds temporary files to it currently naming them (for example) with the hash of their 'DN + a unique number + .jpg'.

Parameters:
currentDN - the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
entry - the actual entry that is being displayed.
type - the attribute type for example: audio or jpegPhoto.
size - the size of the number of specific attribute values this entry contains (i.e. 3 jpegPhoto attributes).

createAudioCache

public static void createAudioCache(java.lang.String currentDN,
                                    javax.naming.directory.Attributes entry,
                                    java.lang.String type,
                                    int size)
Creates an audio cache directory named 'temp/audio' and adds temporary files to it currently naming them (for example) with the hash of their 'DN + a unique number + .wav'.

Parameters:
currentDN - the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
entry - the actual entry that is being displayed.
type - the attribute type for example: audio or jpegPhoto.
size - the size of the number of specific attribute values this entry contains (i.e. 3 audio attributes).

doNormalCache

public static void doNormalCache(java.lang.String currentDN,
                                 byte[] bytes,
                                 java.lang.String extension)
Does the actual writing of the cache files.

Parameters:
currentDN - the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
bytes - the byte[] of the audio attribute.
extension - the extensio of the cache file (currently .doc, .jpg).

doAudioCache

public static void doAudioCache(java.lang.String currentDN,
                                byte[] bytes)
Creates the audio temporary files. First, the type of audio file needs to be determined. This is done by converting the first 100 bytes of the audio file to hex then searching it for an identifier of the audio file (for example in a .wav file the header should contain the word 'WAVE' which in hex is '574156'). If the audio file type is determined the temporary file is created with the current DN + a unique number + the extension for example:

cn=Al,o=DEMOCORP,c=AU9.wav

This method can determine the following audio file types:

.wav .mp3 .rmi .ram .aiff .mid .au .stm .voc .xm .s3m .it

if an audio file type is unknown the temporary file is still created but with no extension.

Parameters:
currentDN - the dn of the entry being modified (will be used as part of the name of the temp file i.e: cn=Al,o=DEMOCORP,c=AU).
bytes - the byte[] of the audio attribute.

sortFiles

public static java.io.File[] sortFiles(java.io.File[] files)
Sorts an array of files in order of lastModified (oldest to newest).

Parameters:
files - the array of files to be sorted.
Returns:
the sorted file array.

getCacheDirectory

public static java.io.File getCacheDirectory()
Returns the directory that is being used for the caching.

Returns:
the directory.

getAudioCacheDirectory

public static java.io.File getAudioCacheDirectory()
Returns the directory that is being used for the audio caching.

Returns:
the directory.

decreaseCacheSize

public static void decreaseCacheSize()
Decreases the size of the cache (usually if the cache contains 100 or more temporary files), by deleting the oldest files until there is only 50 temporary files left in the cache. It also checks that it deletes all the files pertaining to one entry i.e. if an entry has four temporary files, this method will try to delete all four even if the minimum level of 50 is reached (this ensures that the templates refresh correctly).


decreaseAudioCacheSize

public static void decreaseAudioCacheSize()
Decreases the size of the audio cache (usually if the cache contains 100 or more temporary files), by deleting the oldest files until there is only 50 temporary files left in the cache. It also checks that it deletes all the files pertaining to one entry i.e. if an entry has four temporary files, this method will try to delete all four even if the minimum level of 50 is reached (this ensures that the templates refresh correctly).


getDirPath

public static java.lang.String getDirPath()
Returns the absolute path of the temp directory.

Returns:
the path of the temp directroy.

getAudioDirPath

public static java.lang.String getAudioDirPath()
Returns the absolute path of the audio temp directory.

Returns:
the path of the temp directroy.

cleanCache

public static void cleanCache(java.lang.String currentDN)
Cleans the cache of entries that start with the dn of the entry being modified.

Parameters:
currentDN - the dn of the entry being modified.

cleanCache

public static void cleanCache()
Cleans the cache of all temporary entries by deleting them.