Top Description Inners Fields Constructors Methods
org.python.core.packagecache

public abstract Class CachedJarsPackageManager

extends PackageManager
Class Inheritance
Known Direct Subclasses
org.python.core.packagecache.PathPackageManager
Imports
org.python.core.Options, .PyJavaPackage, org.python.core.util.FileUtil, org.python.util.Generic, java.io.BufferedInputStream, .BufferedOutputStream, .DataInputStream, .DataOutputStream, .EOFException, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, java.lang.reflect.Modifier, java.net.URL, .URLConnection, java.nio.file.FileVisitResult, .FileVisitor, .Files, .Path, .SimpleFileVisitor, .StandardOpenOption, java.nio.file.attribute.BasicFileAttributes, java.security.AccessControlException, java.util.ArrayList, .List, .Map, .Map.Entry, java.util.logging.Logger, java.util.zip.ZipEntry, .ZipInputStream

Abstract package manager that gathers info about statically known classes from a set of jars and the Java runtime. This info can be cached, eventually. Off-the-shelf this class offers a local file-system based cache implementation.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
CachedJarsPackageManager.ClassList

Used when representing the classes in a particular package.

public static class
CachedJarsPackageManager.JarXEntry

Class of object used to represent a cache file and last modification time, internally and to CachedJarsPackageManager#outCreateCacheFile.

Field Summary

Modifier and TypeField and Description
private File
cachedir

Directory in which cache files are stored.

private Map<String, CachedJarsPackageManager.JarXEntry>
index

Map from some source of class definitions to the name of a file used to cache lists of classes in that source.

private boolean
indexModified

Set true whenever the cache index is modified (needs ultimately to be re-written).

protected static Logger
Inherited from org.python.core.packagecache.PackageManager:
topLevelPackage

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addJarToPackages(URL jarurl)

Gathers classes info from jar specified by a URL.

public void
addJarToPackages(URL jarurl, boolean cache)

Gathers classes info from jar specified by URL.

public void
addJarToPackages(File jarfile)

Gathers classes info from jar specified by File jarfile.

public void
addJarToPackages(File jarfile, boolean cache)

Gathers package and class lists from a jar specified by a File.

private void
addJarToPackages(URL
identifying the JAR if jarfile is null
jarurl
,
File
identifying the JAR
jarfile
,
boolean
re-write the cache if it was out of date (and caching is active).
writeCache
)

Create (or ensure we have) a PyJavaPackage, descending from PackageManager#topLevelPackage in this PackageManager instance, for each package in a jar specified by a file or URL.

protected void
addModuleToPackages(Path modulePath)

Scan a Java module, creating package objects.

private void
addPackages(Map<String, String>
source of mappings
packageToClasses
,
String
becomes the __file__ attribute of the PyJavaPackage
jarfile
)

From a map of package name to comma/@-separated list of classes therein, relating to a particular JAR file, create a PyJavaPackage for each package.

private void
addZipEntry(Map<String, CachedJarsPackageManager.ClassList>
map (to update) from package name to class names by accessibility
zipPackages
,
ZipEntry
possibly representing a class
entry
,
ZipInputStream
the JAR or ZIP to which the entry belongs
zip
)

Process one entry from a JAR/ZIP, and if the entry is a (qualifying) Java class, add its name to that package's entry in the map passed in. A class is added only if filterByName(String, boolean) returns true.

protected void
comment(String
message template (see java.text.MessageFormat)
msg
,
Object...
parameters to insert
params
)

Comment log method - hook.

protected void
debug(String
message template (see java.text.MessageFormat)
msg
,
Object...
parameters to insert
params
)

Debug log method - hook.

protected void
deleteCacheFile(String cachefile)

Delete (invalidated) cache file from persistent storage - hook.

protected boolean

Returns:

true if name should be filtered out
filterByAccess
(String
class name
name
,
int
class access permissions as int
acc
)

Filter class by access perms helper method - hook.

protected boolean

Returns:

true if name should be filtered out
filterByName
(String
class/pkg name
name
,
boolean
if true, name refers to a pkg
pkg
)

Filter class/pkg by name helper method - hook.

private Map<String, String>

Returns:

map from packages to classes
getModularPackages
(Path
up to and including the name of the module
modulePath
)

Detect all of the packages in a single module, that contain any classes for which filterByName(String, boolean) returns true.

private Map<String, String>

Returns:

map from package name to comma/@-separated list of class names
getZipPackages
(InputStream
an open JAR/ZIP file
jarin
)

Detect all of the packages in an open JAR/ZIP file, that contain any classes for which filterByName(String, boolean) returns true.

protected void
initCache()

Initialise the cache by reading the index from storage, through inOpenIndex(), or by creating a new empty one.

protected DataInputStream
inOpenCacheFile(String cachefile)

Open a particular cache file for reading from persistent storage.

protected DataInputStream
inOpenIndex()

Open cache index for reading from persistent storage – hook.

protected void
message(String
message template (see java.text.MessageFormat)
msg
,
Object...
parameters to insert
params
)

Message log method - hook.

protected DataOutputStream

Returns:

stream on which to represent the package to class-list textually
outCreateCacheFile
(CachedJarsPackageManager.JarXEntry
cache file description
entry
,
boolean
new or use existing file named in entry.cachefile
create
)

Create/open cache file for rewriting back to persistent storage – hook.

protected DataOutputStream
outOpenIndex()

Open cache index for writing back to persistent storage – hook.

private Map<String, String>
readCacheFile(CachedJarsPackageManager.JarXEntry entry, String jarcanon)

Read in cache file storing package info for a single jar.

public void
saveCache()

Write back cache index.

protected static String[]

Returns:

- An array of strings, each of which will not be larger than maxLength
splitString
(String
- The string to split up into chunks
str
,
int
- The max size a string should be
maxLength
)

Split up a string into several chunks based on a certain size The writeCacheFile method will use the writeUTF method on a DataOutputStream which only allows writing 64k chunks, so use this utility method to split it up

protected boolean
useCacheDir(File cachedir)

Initialize off-the-shelf (default) local file-system cache implementation.

protected void
warning(String
message template (see java.text.MessageFormat)
msg
,
Object...
parameters to insert
params
)

Warning log method - hook.

private void
writeCacheFile(CachedJarsPackageManager.JarXEntry
in index corresponding to the JAR
entry
,
String
canonical name of the JAR (used as key into index)
jarcanon
,
Map<String, String>
a map from package name to class names for that pacjkage in the JAR
zipPackages
,
boolean
if the cache file must be created (vs. being re-written)
brandNew
)

Write a cache file storing package info for a single JAR.

Inherited from org.python.core.packagecache.PackageManager:
addDirectoryaddJaraddJarDirbasicDoDircheckAccessdoDirfindClassfindClasslookupNamemakeJavaPackagemergenotifyPackageImportpackageExistssplitsplit

Field Detail

cachedirback to summary
private File cachedir

Directory in which cache files are stored.

indexback to summary
private Map<String, CachedJarsPackageManager.JarXEntry> index

Map from some source of class definitions to the name of a file used to cache lists of classes in that source. null if cache is not operating. (This source must have a time last modified so we may check that the cache is valid).

indexModifiedback to summary
private boolean indexModified

Set true whenever the cache index is modified (needs ultimately to be re-written).

loggerback to summary
protected static Logger logger

Constructor Detail

CachedJarsPackageManagerback to summary
public CachedJarsPackageManager()

Method Detail

addJarToPackagesback to summary
public void addJarToPackages(URL jarurl)

Gathers classes info from jar specified by a URL. Eventually just using previously cached info. Eventually updated info is not cached. Persistent cache storage access goes through inOpenCacheFile() and outCreateCacheFile().

addJarToPackagesback to summary
public void addJarToPackages(URL jarurl, boolean cache)

Gathers classes info from jar specified by URL. Eventually just using previously cached info. Eventually updated info is (re-)cached if param cache is true. Persistent cache storage access goes through inOpenCacheFile() and outCreateCacheFile().

addJarToPackagesback to summary
public void addJarToPackages(File jarfile)

Gathers classes info from jar specified by File jarfile. Eventually just using previously cached info. Eventually updated info is not cached. Persistent cache storage access goes through inOpenCacheFile() and outCreateCacheFile().

addJarToPackagesback to summary
public void addJarToPackages(File jarfile, boolean cache)

Gathers package and class lists from a jar specified by a File. Eventually just using previously cached info. Eventually updated info is (re-)cached if param cache is true. Persistent cache storage access goes through inOpenCacheFile() and outCreateCacheFile().

addJarToPackagesback to summary
private void addJarToPackages(URL jarurl, File jarfile, boolean writeCache)

Create (or ensure we have) a PyJavaPackage, descending from PackageManager#topLevelPackage in this PackageManager instance, for each package in a jar specified by a file or URL. Ensure that the class list in each package is updated with the classes this JAR supplies to it. The information concerning packages in the JAR and the classes they contain, may be read from from a previously cached account of the JAR, if the last-modified time of the JAR matches a cached value. If it is not read from a cache, it will be obtained by inspecting the JAR, and a new cache will be written (if requested). Access to persistent cache storage goes through inOpenCacheFile(String) and outCreateCacheFile(JarXEntry, boolean).

Parameters
jarurl:URL

identifying the JAR if jarfile is null

jarfile:File

identifying the JAR

writeCache:boolean

re-write the cache if it was out of date (and caching is active).

addModuleToPackagesback to summary
protected void addModuleToPackages(Path modulePath)

Scan a Java module, creating package objects.

addPackagesback to summary
private void addPackages(Map<String, String> packageToClasses, String jarfile)

From a map of package name to comma/@-separated list of classes therein, relating to a particular JAR file, create a PyJavaPackage for each package.

Parameters
packageToClasses:Map<String, String>

source of mappings

jarfile:String

becomes the __file__ attribute of the PyJavaPackage

addZipEntryback to summary
private void addZipEntry(Map<String, CachedJarsPackageManager.ClassList> zipPackages, ZipEntry entry, ZipInputStream zip) throws IOException

Process one entry from a JAR/ZIP, and if the entry is a (qualifying) Java class, add its name to that package's entry in the map passed in. A class is added only if filterByName(String, boolean) returns true. We add it to the accessible or inaccessible list according to whether filterByAccess(String, int) returns true for the name and permissions of that class.

Parameters
zipPackages:Map<String, CachedJarsPackageManager.ClassList>

map (to update) from package name to class names by accessibility

entry:ZipEntry

possibly representing a class

zip:ZipInputStream

the JAR or ZIP to which the entry belongs

commentback to summary
protected void comment(String msg, Object... params)

Comment log method - hook. This default implementation does nothing.

Parameters
msg:String

message template (see java.text.MessageFormat)

params:Object[]

parameters to insert

debugback to summary
protected void debug(String msg, Object... params)

Debug log method - hook. This default implementation does nothing.

Parameters
msg:String

message template (see java.text.MessageFormat)

params:Object[]

parameters to insert

deleteCacheFileback to summary
protected void deleteCacheFile(String cachefile)

Delete (invalidated) cache file from persistent storage - hook. This default implementation is part of the off-the-shelf local file-system cache implementation. Can be overridden.

filterByAccessback to summary
protected boolean filterByAccess(String name, int acc)

Filter class by access perms helper method - hook. The default implementation is used by addJarToPackages in order to filter out non-public classes. Should be used or overridden by derived classes too. Also to be used in doDir. Access permissions can be read with checkAccess.

Parameters
name:String

class name

acc:int

class access permissions as int

Returns:boolean

true if name should be filtered out

filterByNameback to summary
protected boolean filterByName(String name, boolean pkg)

Filter class/pkg by name helper method - hook. The default implementation is used by addJarToPackages in order to filter out classes whose name contains '$' (e.g. inner classes). Should be used or overridden by derived classes too. Also to be used in doDir.

Parameters
name:String

class/pkg name

pkg:boolean

if true, name refers to a pkg

Returns:boolean

true if name should be filtered out

getModularPackagesback to summary
private Map<String, String> getModularPackages(Path modulePath) throws IOException

Detect all of the packages in a single module, that contain any classes for which filterByName(String, boolean) returns true. For each such package, list the (surviving) classes in two lists: accessible and inaccessible, which is judged according to filterByAccess(String, int). The returned map is from package to these two lists, now comma-separated lists, with an '@' between them.

Parameters
modulePath:Path

up to and including the name of the module

Returns:Map<String, String>

map from packages to classes

getZipPackagesback to summary
private Map<String, String> getZipPackages(InputStream jarin) throws IOException

Detect all of the packages in an open JAR/ZIP file, that contain any classes for which filterByName(String, boolean) returns true. For each such package, list the (surviving) classes in two lists: accessible and inaccessible, which is judged according to filterByAccess(String, int). The returned map is from package to these two lists, now comma-separated lists, with an '@' between them.

Parameters
jarin:InputStream

an open JAR/ZIP file

Returns:Map<String, String>

map from package name to comma/@-separated list of class names

initCacheback to summary
protected void initCache()

Initialise the cache by reading the index from storage, through inOpenIndex(), or by creating a new empty one.

inOpenCacheFileback to summary
protected DataInputStream inOpenCacheFile(String cachefile) throws IOException

Open a particular cache file for reading from persistent storage. This default implementation is part of the off-the-shelf local file-system cache implementation. Can be overridden.

inOpenIndexback to summary
protected DataInputStream inOpenIndex() throws IOException

Open cache index for reading from persistent storage – hook. Must Return null if this is absent. This default implementation is part of the off-the-shelf local file-system cache implementation. Can be overridden.

messageback to summary
protected void message(String msg, Object... params)

Message log method - hook. This default implementation does nothing.

Parameters
msg:String

message template (see java.text.MessageFormat)

params:Object[]

parameters to insert

outCreateCacheFileback to summary
protected DataOutputStream outCreateCacheFile(CachedJarsPackageManager.JarXEntry entry, boolean create) throws IOException

Create/open cache file for rewriting back to persistent storage – hook. If create is false, the cache file is supposed to exist at entry.cachefile and will be opened for rewriting. If create is true, entry.cachefile is the base name (e.g. JAR or module name) for a cache to be created, and the full name will be put in entry.cachefile on exit. This default implementation is part of the off-the-shelf local file-system cache implementation. It may be overridden to provide a different cache medium and use of entry.cachefile.

Parameters
entry:CachedJarsPackageManager.JarXEntry

cache file description

create:boolean

new or use existing file named in entry.cachefile

Returns:DataOutputStream

stream on which to represent the package to class-list textually

outOpenIndexback to summary
protected DataOutputStream outOpenIndex() throws IOException

Open cache index for writing back to persistent storage – hook. This default implementation is part of the off-the-shelf local file-system cache implementation. Can be overridden.

readCacheFileback to summary
private Map<String, String> readCacheFile(CachedJarsPackageManager.JarXEntry entry, String jarcanon)

Read in cache file storing package info for a single jar. Return null and delete this cache file if it is invalid.

Annotations
@SuppressWarnings:empty-statement
saveCacheback to summary
public void saveCache()

Write back cache index. The index is a mapping from each source of class definitions to the file where the cache for that source is held. This list is accessed through outOpenIndex().

splitStringback to summary
protected static String[] splitString(String str, int maxLength)

Split up a string into several chunks based on a certain size The writeCacheFile method will use the writeUTF method on a DataOutputStream which only allows writing 64k chunks, so use this utility method to split it up

Parameters
str:String

- The string to split up into chunks

maxLength:int

- The max size a string should be

Returns:String[]

- An array of strings, each of which will not be larger than maxLength

useCacheDirback to summary
protected boolean useCacheDir(File cachedir)

Initialize off-the-shelf (default) local file-system cache implementation. Must be called before initCache. cachedir is the cache repository directory, this is eventually created. Returns true if dir works.

warningback to summary
protected void warning(String msg, Object... params)

Warning log method - hook. This default implementation does nothing.

Parameters
msg:String

message template (see java.text.MessageFormat)

params:Object[]

parameters to insert

writeCacheFileback to summary
private void writeCacheFile(CachedJarsPackageManager.JarXEntry entry, String jarcanon, Map<String, String> zipPackages, boolean brandNew)

Write a cache file storing package info for a single JAR. *

Parameters
entry:CachedJarsPackageManager.JarXEntry

in index corresponding to the JAR

jarcanon:String

canonical name of the JAR (used as key into index)

zipPackages:Map<String, String>

a map from package name to class names for that pacjkage in the JAR

brandNew:boolean

if the cache file must be created (vs. being re-written)

org.python.core.packagecache back to summary

private Class CachedJarsPackageManager.ClassList

extends Object
Class Inheritance
  • java.lang.Object
  • org.python.core.packagecache.CachedJarsPackageManager.ClassList

Used when representing the classes in a particular package.

Field Summary

Modifier and TypeField and Description
pack-priv List<String>
accessible

Class names of accessible classes

pack-priv List<String>
inaccessible

Class names of inaccessible classes

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static void
public String
toString()

Overrides java.lang.Object.toString.

Retrieve the two lists in "cache file" format A,B,C[@D,E].

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

accessibleback to summary
pack-priv List<String> accessible

Class names of accessible classes

inaccessibleback to summary
pack-priv List<String> inaccessible

Class names of inaccessible classes

Constructor Detail

ClassListback to summary
private ClassList()

Method Detail

appendListback to summary
private static void appendList(StringBuilder buf, List<String> names)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Retrieve the two lists in "cache file" format A,B,C[@D,E].

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override

org.python.core.packagecache back to summary

public Class CachedJarsPackageManager.JarXEntry

extends Object
Class Inheritance
  • java.lang.Object
  • org.python.core.packagecache.CachedJarsPackageManager.JarXEntry

Class of object used to represent a cache file and last modification time, internally and to CachedJarsPackageManager#outCreateCacheFile. When caching, a JarXEntry is created for each JAR processed for classes, and corresponds to a file in the package cache directory. The name is based on the name of the JAR.

Field Summary

Modifier and TypeField and Description
public String
cachefile

Specifies the actual cache file once that is created or opened.

public long

Constructor Summary

AccessConstructor and Description
public
JarXEntry(String cachefile)

public
JarXEntry(String cachefile, long mtime)

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

cachefileback to summary
public String cachefile

Specifies the actual cache file once that is created or opened.

mtimeback to summary
public long mtime

Constructor Detail

JarXEntryback to summary
public JarXEntry(String cachefile)
JarXEntryback to summary
public JarXEntry(String cachefile, long mtime)