|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object dsto.dfc.tools.JarX
public class JarX
A JAR file generator tool that operates by taking a one or more 'root' resource patterns (eg the main class of an application or a set of library package names) and generates a JAR containing the root resources plus any referenced resources.
NOTE: All resources must be named using the resource syntax: ie most importantly classes are NOT referenced by their logical class name. For example, the logical class name com.frobsoft.stuff.Widget should be referred to by the resource name "com/frobsoft/stuff/Widget.class".
TODO:
for a Ant build task that uses JarX.
Constructor Summary | |
---|---|
JarX(File archiveFile)
Create an instance of JarX writing to a ZIP archive. |
|
JarX(File archiveFile,
boolean useJarFormat)
Create an instance of JarX writing to a ZIP or JAR archive. |
Method Summary | |
---|---|
void |
addExcludeMatchers(List newExcludes)
Add a list of ResourceMatcher's to the excludes list. |
void |
addExcludeMatchers(ResourceMatcher[] newExcludes)
Convenience version of addExcludeMatchers. |
void |
addExcludeRegexps(List newExcludes)
Add a set of regular expression rules to the exclude set. |
void |
addExcludeRegexps(String[] newExcludes)
Convenience version of addExcludes. |
void |
addExcludes(List newExcludes)
Add simple string or wildcard rules to the exclude set. |
void |
addExcludes(String[] newExcludes)
Convenience version of addExcludes. |
protected void |
addResource(String resourceName,
byte[] resourceData)
Add a resource, and all the resources it references in the case of classes, to the archive. |
protected void |
addResourceFromClasspath(String resourceName)
Add a resource (via addResource()) found by searching the classpath. |
protected void |
addResourceFromFile(String resourceName,
File file)
Add a resource stored in a file (via addResource()). |
protected void |
addResourceFromZip(String resourceName,
ZipFile zipFile)
Add a resource stored in a ZIP archive (via addResource()). |
void |
addRootMatchers(List newResourceMatchers)
Add a list of ResourceMatcher's to the root resources list. |
void |
addRootMatchers(ResourceMatcher[] newResourceMatchers)
Convenience version of addRootMatchers. |
void |
addRootRegexps(List regexps)
Add a list of regular expressions to the root resources list. |
void |
addRootRegexps(String[] regexps)
Convenience version of addRootRegexps. |
void |
addRoots(List newRoots)
Add a list of resource names as roots of the resource tree to archive. |
void |
addRoots(String[] newRoots)
Convenience version of addRoots. |
static String |
classToResourceName(String className)
Generate the resource name for a logical class name. |
void |
clearExcludes()
Clear the exclude rule set. |
protected void |
closeOpenZips()
Close any ZIP archives opened via findOrOpenZip(). |
void |
dispose()
|
protected static void |
error(String message)
Log an error. |
protected String[] |
findClassResources(String className,
byte[] classData)
Return a list of resources referenced by a class. |
protected ZipFile |
findOrOpenZip(String zipFileName)
Find an already opened ZIP archive (cached in openZips) or open it. |
static String[] |
getSystemClassPath()
Get the JVM system class path as an array of String's. |
protected boolean |
isResourceExcludedByMatch(String resourceName)
Test if a resource is matched by the excludes set. |
protected boolean |
isResourceIncludedByMatch(String resourceName)
Tests if a resource is in the root include set by matching a pettern. |
protected boolean |
isResourceIncludedByName(String resourceName)
Tests if a resource is in the root include set directly by name. |
protected boolean |
isResourceStored(String resourceName)
Test if a resource has been stored in the archive. |
protected static boolean |
isSystemClass(String className)
Test if a given class name is a 'system' class, ie a class provided by the JRE or a core extension. |
protected boolean |
isZipArchive(String path)
Test if a path name refers to a ZIP archive. |
protected static void |
log(String message)
Log a message. |
protected byte[] |
readResourceData(String path,
String resourceName)
Read the data associated with a resource from either a ZIP archive or a file. |
protected byte[] |
readResourceDataFromFile(String resourceName,
File resourceFile)
Read resource data from a file. |
protected byte[] |
readResourceDataFromZip(String resourceName,
ZipFile zipFile)
Read resource data from a ZIP archive. |
void |
run()
Start archive creation. |
protected void |
runDirect()
Run the archive operation in 'direct' mode: scan each root resource in roots and add it via addResourceFromClasspath(). |
protected void |
runFullScan()
Run the archive operation in 'full scan' mode: scan all resources in the classpath looking for resource names that match those in roots and rootMatchers. |
protected void |
scanArchive(String archivePath)
Scan a ZIP archive for resources that match those in the root set. |
protected void |
scanDirectory(File directory,
String resourcePath)
Scan a directory and all its subdirectories for resources that match those in the root set. |
void |
setAutoFindResources(boolean newValue)
Set to true if JarX should include resources guessed by looking at calls to Class.getResource(). |
void |
setClassPath(String[] newClassPath)
Set the path used to find resources. |
void |
setMainClassName(String mainClassName)
Set the name (not the resource name) of the class to specify as the main application in the JAR manifest (the "Main-Class" attribute). |
void |
setManifestFile(File newManifestFile)
Set the file to use as the base of the JAR manifest. |
protected void |
setupDefaultExcludes()
|
void |
setUseJarFormat(boolean useJarFormat)
Set whether to generate a JAR-compatible archive (generate a manifest and use a java.util.JarOutputStream). |
protected boolean |
shouldIncludeImportedResource(String resourceName)
Test if a resource imported by another resource should be included using the rules: (a) resource must not be a system class and (b) resource must either be included directly by name OR not be excluded by a pattern. |
protected boolean |
shouldIncludeResource(String resourceName)
Test if a resource found by scanning the classpath should be included using the rules: (a) resource must not be a system class and (b) resource must EITHER be included directly by name OR matched by a pattern and not excluded by a pattern (ie exclude patterns override include patterns, but not inclusion directly by name). |
protected boolean |
shouldStoreResource(String resourceName)
Test if a resource should be stored ie (a) is not already stored (b) is included in the root resource set and (c) is not in the excludes set. |
protected void |
storeResource(String resourceName,
byte[] resourceData)
Store a resource in the output archive. |
protected static void |
warn(String message)
Log a warning. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JarX(File archiveFile)
archiveFile
- The file to write the ZIP archive to.public JarX(File archiveFile, boolean useJarFormat)
archiveFile
- The file to write the ZIP archive to.useJarFormat
- True if the archive should be JAR-compatible.setUseJarFormat(boolean)
Method Detail |
---|
public static String[] getSystemClassPath()
public static String classToResourceName(String className)
className
- The name of a class eg "com.frobsoft.Widget".
public void setUseJarFormat(boolean useJarFormat)
setManifestFile(java.io.File)
,
setMainClassName(java.lang.String)
public void setManifestFile(File newManifestFile)
setUseJarFormat(boolean)
public void setMainClassName(String mainClassName)
Example: setMainClassName ("com.frobsoft.Main").
setUseJarFormat(boolean)
public void setClassPath(String[] newClassPath)
newClassPath
- The new class path. Must contain either
directory paths or ZIP/JAR file names.public void clearExcludes()
addExcludes(java.util.List)
public void addExcludes(String[] newExcludes)
newExcludes
- An array of exclude expressions.for full description.
public void addExcludes(List newExcludes)
newExcludes
- A list of strings to used as exclude wildcard
patterns, where "*" can be used as a kleene star operator.- you can use regular
expressions too!
public void addExcludeRegexps(String[] newExcludes) throws IllegalArgumentException
newExcludes
- A list of regular expressions to add to the
exclude set.
IllegalArgumentException
addExcludeRegexps(java.util.List)
public void addExcludeRegexps(List newExcludes) throws IllegalArgumentException
newExcludes
- A list of regular expression strings.
IllegalArgumentException
- if one of the regular
expressions has invalid syntax.public void addExcludeMatchers(ResourceMatcher[] newExcludes)
addExcludeMatchers(java.util.List)
public void addExcludeMatchers(List newExcludes)
newExcludes
- A list of ResourceMatcher instances.ResourceMatcher
,
RegexpResourceMatcher
,
WildcardResourceMatcher
public void addRoots(String[] newRoots)
addRoots(java.util.List)
public void addRoots(List newRoots)
newRoots
- A list of resource names.addRootRegexps(java.util.List)
,
WildcardResourceMatcher
public void addRootRegexps(String[] regexps)
addRootRegexps(java.util.List)
public void addRootRegexps(List regexps)
regexps
- a value of type 'List'public void addRootMatchers(ResourceMatcher[] newResourceMatchers)
addRootMatchers(java.util.List)
public void addRootMatchers(List newResourceMatchers)
newResourceMatchers
- A list of ResourceMatcher instances.ResourceMatcher
,
RegexpResourceMatcher
,
WildcardResourceMatcher
public void setAutoFindResources(boolean newValue)
public void run() throws IOException
IOException
- if an error ocurred while creating the
archive.public void dispose()
protected void setupDefaultExcludes()
protected boolean isResourceIncludedByName(String resourceName)
protected boolean isResourceIncludedByMatch(String resourceName)
protected boolean isResourceExcludedByMatch(String resourceName)
protected boolean isResourceStored(String resourceName)
protected boolean shouldStoreResource(String resourceName)
resourceName
- a value of type 'String'
return true;#shouldIncludeResource
protected boolean shouldIncludeResource(String resourceName)
resourceName
- a value of type 'String'
protected boolean shouldIncludeImportedResource(String resourceName)
resourceName
- a value of type 'String'
protected boolean isZipArchive(String path)
protected void runDirect()
runFullScan()
,
addResourceFromClasspath(java.lang.String)
protected void runFullScan()
runDirect()
,
scanArchive(java.lang.String)
,
scanDirectory(java.io.File, java.lang.String)
protected void scanArchive(String archivePath)
archivePath
- The path to a ZIP-format archive file.protected void scanDirectory(File directory, String resourcePath)
directory
- The directory to scan.resourcePath
- The resource name of the directory (ie its
relative path from the root of the scan).protected void addResourceFromZip(String resourceName, ZipFile zipFile) throws IOException
resourceName
- The name of the resource.zipFile
- The ZIP file to read from.
IOException
- if an IO error occurs.addResource(java.lang.String, byte[])
protected void addResourceFromFile(String resourceName, File file) throws IOException
resourceName
- The name of the resource.file
- The file to read from.
IOException
- if an IO error occurs.addResource(java.lang.String, byte[])
protected void addResourceFromClasspath(String resourceName) throws IOException
resourceName
- The resource name.
IOException
- if an IO error occurs, including if the
resource is not found.setClassPath(java.lang.String[])
,
addResource(java.lang.String, byte[])
protected void addResource(String resourceName, byte[] resourceData) throws IOException
resourceName
- The name of the resource.resourceData
- The resource data.
IOException
- if an IO exception occurs while writing
the resource.findClassResources(java.lang.String, byte[])
protected byte[] readResourceData(String path, String resourceName) throws IOException
path
- The path to a ZIP archive or a file.resourceName
- The name of the resource.
IOException
- if the data could not be read.readResourceDataFromFile(java.lang.String, java.io.File)
,
readResourceDataFromZip(java.lang.String, java.util.zip.ZipFile)
protected byte[] readResourceDataFromFile(String resourceName, File resourceFile) throws IOException
resourceName
- The name of the resource.resourceFile
- The file to read from.
IOException
- if the data could not be read.protected byte[] readResourceDataFromZip(String resourceName, ZipFile zipFile) throws IOException
resourceName
- The name of the resource.zipFile
- The ZIP archive to read from.
IOException
- if the data could not be read.protected void storeResource(String resourceName, byte[] resourceData) throws IOException
resourceName
- The resource name.resourceData
- The resource data.
IOException
- if an IO error occurred.protected ZipFile findOrOpenZip(String zipFileName) throws IOException
zipFileName
- The file name of the ZIP archive.
IOException
- if an IO error occurred.closeOpenZips()
protected void closeOpenZips()
findOrOpenZip(java.lang.String)
protected static void error(String message)
protected static void warn(String message)
protected static void log(String message)
protected String[] findClassResources(String className, byte[] classData) throws IOException
The logic in this method is shamelessly copied from the original in ZipLock: see class doc for author acknowledgements and contact info.
className
- The class resource name.classData
- The class resource data.
IOException
- if an IO error occurs.protected static boolean isSystemClass(String className)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |