edu.sdsc.inca.agent
Class RepositoryCache

java.lang.Object
  extended by java.lang.Thread
      extended by edu.sdsc.inca.agent.RepositoryCache
All Implemented Interfaces:
java.lang.Runnable

public class RepositoryCache
extends java.lang.Thread

The Agent will cache packages (reporter or library) locally when a suite uses them and send them to the reporter managers. This class tracks the packages cached on disk and will periodically look for updates and download them.

Author:
Shava Smallen <ssmallen@sdsc.edu>

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
RepositoryCache(java.lang.String cacheLocation, int updatePeriod)
          Create a new RepositoryCache object using cacheLocation as the directory to cache packages.
RepositoryCache(java.lang.String cacheLocation, edu.sdsc.inca.repository.Repositories repositories)
          Create a new RepositoryCache object using cacheLocation as the directory to cache packages.
 
Method Summary
 boolean checkForPackageUpdate(edu.sdsc.inca.dataModel.catalog.PackageType aPackage)
          Checks for a package update using the following techniques: 1) If package's latestVersion field is true, checks for package update.
 java.lang.String[] checkForPackageUpdates()
          Check all packages contained in the cache and send any package updates to the reporter managers.
 boolean existsLocally(java.lang.String name, java.lang.String version)
          Check the local repository cache for the specified package and version.
 boolean fetchPackage(java.lang.String name, java.lang.String version)
          Fetch the specified package from the available repositories and store locally on disk.
 java.lang.String getCacheLocation()
          Retrieve the location of the repository cache.
 edu.sdsc.inca.dataModel.catalog.CatalogDocument getCatalog()
          Return the repository cache catalog.
 java.lang.String[] getDependencies(java.lang.String uri)
          Return the package dependencies for this particular package.
 java.lang.String getFilename(java.lang.String name, java.lang.String version)
          Return the filename to use on the reporter manager when installation a parcticular package name and version.
 java.lang.String getInstallPath(java.lang.String name, java.lang.String version)
          Return the installation path to use on the reporter manager when installing for a particular package name and version.
 edu.sdsc.inca.dataModel.catalog.PackageType getPackage(java.lang.String name, java.lang.String version)
           
 edu.sdsc.inca.dataModel.catalog.PackageType getPackageByUri(java.lang.String uri)
           
 byte[] getPackageContent(java.lang.String name, java.lang.String version)
          Return the specified package.
 java.lang.String getPackageNameByUri(java.lang.String uri)
           
 java.lang.String getPermissions(java.lang.String name, java.lang.String version)
          Look up a specify package and version in the repository cache and return the permissions needed for installation.
 edu.sdsc.inca.repository.Repositories getRepositories()
          Return the repositories that the repository cache references when looking for reporter packages.
 int getUpdatePeriod()
          Return the current update period (i.e., amount of time used to sleep in between updates).
 java.lang.String getUri(java.lang.String name, java.lang.String version)
           
protected static boolean isPythonModule(java.lang.String filename)
          Determine whether package file is a python module
 byte[] readPackageFromDisk(java.lang.String name, java.lang.String version)
          Return the contents of the specified package from our local cache.
 boolean removePackage(java.lang.String name)
          Remove the specified package from the repository cache.
 void resolveReporters(edu.sdsc.inca.util.SuiteWrapper suite)
          Resolve all reporter names in the provided suite document to uris using information provided in the repository cache.
 void run()
          Starts a thread to check for package updates from the available repositories.
 void savePackageToDisk(byte[] fileContent, java.lang.String name, java.lang.String version)
          Save the specified package to disk (i.e., cache it).
 void setCacheLocation(java.lang.String cacheLocation)
          Set the location of the where the agent will cache repository packages.
 void setRepositories(edu.sdsc.inca.repository.Repositories repositories)
          Set the repositories the repository cache will reference when looking for packages.
 void setUpdatePeriod(int updatePeriod)
          Set the current update period (i.e., amount of time used to sleep in between updates).
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

RepositoryCache

public RepositoryCache(java.lang.String cacheLocation,
                       edu.sdsc.inca.repository.Repositories repositories)
Create a new RepositoryCache object using cacheLocation as the directory to cache packages. Will be able to read in any existing cached packages. By default, the repository contents will be checked for updates every 4 hours. Use RepositoryCache(cacheLocation,updatePeriod) or setUpdatePeriod to change frequency.

Parameters:
cacheLocation - A directory where cached packages will be stored.
repositories - The available repositories where packages can be downloaded from.

RepositoryCache

public RepositoryCache(java.lang.String cacheLocation,
                       int updatePeriod)
Create a new RepositoryCache object using cacheLocation as the directory to cache packages. Will be able to read in any existing cached packages. The repository contents will be checked for updates every updatePeriod milliseconds.

Parameters:
cacheLocation - A directory where cached packages will be stored.
updatePeriod - Number of milliseconds to sleep before checking the repositories for updates.
Method Detail

checkForPackageUpdate

public boolean checkForPackageUpdate(edu.sdsc.inca.dataModel.catalog.PackageType aPackage)
Checks for a package update using the following techniques: 1) If package's latestVersion field is true, checks for package update. Otherwise, returns false meaning no package update (since we have the version we need). 2) Checks the repository it was fetched from and fetches the package information to see if the available version is newer than the cached version. If so, it will fetch the new version of the package. 3) If the repository no longer has the package or the repository is no longer registered with the Agent, then we check all available repositories to see if the package exists there. If we find a repository that has the package we fetch it if its version is newer than our cached version. 4) If the package is not available from any repositories, then we keep the cached version and return false.

Parameters:
aPackage - A package that will be checked for an update.
Returns:
True if the package was updated and false if it was not.

checkForPackageUpdates

public java.lang.String[] checkForPackageUpdates()
Check all packages contained in the cache and send any package updates to the reporter managers.

Returns:
An array of strings containing package names that need to be updated

getCacheLocation

public java.lang.String getCacheLocation()
Retrieve the location of the repository cache.

Returns:
A string containing the path to the repository cache.

existsLocally

public boolean existsLocally(java.lang.String name,
                             java.lang.String version)
Check the local repository cache for the specified package and version.

Parameters:
name - Name of the package to look for.
version - Version of the package to look for or null if latest version is desired.
Returns:
True if reporter package exists in repository cache and false otherwise.

fetchPackage

public boolean fetchPackage(java.lang.String name,
                            java.lang.String version)
Fetch the specified package from the available repositories and store locally on disk. This function is synchronized so that multiple processes can use this class without stepping on each other.

Parameters:
name - The name of the repository package to fetch.
version - The version of the package to fetch or null for latest
Returns:
True if the repository was successfully fetched and false otherwise.

getCatalog

public edu.sdsc.inca.dataModel.catalog.CatalogDocument getCatalog()
Return the repository cache catalog.

Returns:
the repository cache catalog (XMLbeans document)

getDependencies

public java.lang.String[] getDependencies(java.lang.String uri)
Return the package dependencies for this particular package.

Parameters:
uri - The uri of the package to return the dependencies on. *
Returns:
The dependencies on this particular package.

getFilename

public java.lang.String getFilename(java.lang.String name,
                                    java.lang.String version)
Return the filename to use on the reporter manager when installation a parcticular package name and version.

Parameters:
name - Name of the package to retrieve the install path for.
version - Version of the package to retrieve the install path for.
Returns:
The filename to use when storing the package on the remote reporter manager (in the installpath).

getInstallPath

public java.lang.String getInstallPath(java.lang.String name,
                                       java.lang.String version)
Return the installation path to use on the reporter manager when installing for a particular package name and version.

Parameters:
name - Name of the package to retrieve the install path for.
version - Version of the package to retrieve the install path for.
Returns:
The relative install path to use on the reporter manager when installing this package.

getPackage

public edu.sdsc.inca.dataModel.catalog.PackageType getPackage(java.lang.String name,
                                                              java.lang.String version)

getPackageByUri

public edu.sdsc.inca.dataModel.catalog.PackageType getPackageByUri(java.lang.String uri)

getPackageContent

public byte[] getPackageContent(java.lang.String name,
                                java.lang.String version)
                         throws java.io.IOException
Return the specified package. If the package is already cached on the machine, it will be retrieved from the cache. Otherwise, it will be searched for in the available repositories and if it exists, will be returned.

Parameters:
name - The name of the package to retrieve.
version - The version of the package to fetch or null to get the latest.
Returns:
The contents of the specified package.
Throws:
java.io.IOException - If there is an error retrieving the package.

getPackageNameByUri

public java.lang.String getPackageNameByUri(java.lang.String uri)

getPermissions

public java.lang.String getPermissions(java.lang.String name,
                                       java.lang.String version)
Look up a specify package and version in the repository cache and return the permissions needed for installation.

Parameters:
name - Name of the package to look up permissions on.
version - Version of the package to look up permissions on.
Returns:
Permissions for reporter manager to use on package when installing

getRepositories

public edu.sdsc.inca.repository.Repositories getRepositories()
Return the repositories that the repository cache references when looking for reporter packages.

Returns:
The repositories object used in the repository cache.

getUpdatePeriod

public int getUpdatePeriod()
Return the current update period (i.e., amount of time used to sleep in between updates).

Returns:
The update period in milliseconds.

getUri

public java.lang.String getUri(java.lang.String name,
                               java.lang.String version)

readPackageFromDisk

public byte[] readPackageFromDisk(java.lang.String name,
                                  java.lang.String version)
                           throws java.io.IOException
Return the contents of the specified package from our local cache.

Parameters:
name - Name of the package to retrieve.
version - Version of the package to retrieve
Returns:
The contents of the specified package.
Throws:
java.io.IOException - If there is an error reading the package from disk.

removePackage

public boolean removePackage(java.lang.String name)
Remove the specified package from the repository cache.

Parameters:
name - Name of the package to remove
Returns:
True upon successful deletion and false if error.

run

public void run()
Starts a thread to check for package updates from the available repositories.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

resolveReporters

public void resolveReporters(edu.sdsc.inca.util.SuiteWrapper suite)
                      throws edu.sdsc.inca.ConfigurationException
Resolve all reporter names in the provided suite document to uris using information provided in the repository cache.

Parameters:
suite - A suite containing reporter names and possibly versions
Throws:
edu.sdsc.inca.ConfigurationException - if unable to locate reporter in cache

savePackageToDisk

public void savePackageToDisk(byte[] fileContent,
                              java.lang.String name,
                              java.lang.String version)
                       throws java.io.IOException
Save the specified package to disk (i.e., cache it).

Parameters:
fileContent - The package content as a text string.
name - The name of the package
version - The version of the package to save
Throws:
java.io.IOException - If there is an error saving the package to disk.

setCacheLocation

public void setCacheLocation(java.lang.String cacheLocation)
Set the location of the where the agent will cache repository packages.

Parameters:
cacheLocation - A string containing the path to a directory where the repository cache can store files.

setRepositories

public void setRepositories(edu.sdsc.inca.repository.Repositories repositories)
Set the repositories the repository cache will reference when looking for packages.

Parameters:
repositories - A set of repositories for which reporter packages can be downloaded from.

setUpdatePeriod

public void setUpdatePeriod(int updatePeriod)
Set the current update period (i.e., amount of time used to sleep in between updates).

Parameters:
updatePeriod - The update period in milliseconds.

isPythonModule

protected static boolean isPythonModule(java.lang.String filename)
Determine whether package file is a python module

Parameters:
filename - the name of the file in the repository
Returns:
True if appears to be a python module and false otherwise