- dbgriddemo.zip
- GridDemo
- .classpath
- .compatibility
- .contentsettings
- .exadelproject
- .j2ee
- .project
- .runtime
- .websettings
- .zipCreation
- bin
- com
- freeware
- masters
- actions
- ClientAction.class
- model
- Client.class
- dbscripts
- GridDemo.xml
- log4j.properties
- src
- web
- css
- images
- Edit.gif
- Edit_Old.gif
- First.gif
- ImgAsc.gif
- ImgDesc.gif
- Last.gif
- Next.gif
- Previous.gif
- View.gif
- WEB-INF
- datagrid.tld
- lib
- commons-beanutils.jar
- commons-collections.jar
- commons-digester.jar
- commons-fileupload.jar
- commons-logging.jar
- commons-logging-api.jar
- commons-validator.jar
- dbgrid-1.0.4.jar
- jakarta-oro.jar
- log4j-1.2.9.jar
- struts.jar
- pages
- struts-bean.tld
- struts-config.xml
- struts-config_1_2.dtd
- struts-html.tld
- struts-logic.tld
- struts-nested.tld
- struts-tiles.tld
- tiles-config_1_1.dtd
- web.xml
- web-app_2_3.dtd
- dbgridsrc.zip
|
/*------------------------------------------------------------------------------
* PACKAGE : com.freeware.anttasks
* FILE : ManifestReader.java
* CREATED : Mar 2, 2006 12:40:53 PM
*------------------------------------------------------------------------------
* ChangeLog:
*-----------------------------------------------------------------------------*/
package com.freeware.anttasks;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Manifest.Attribute;
import org.apache.tools.ant.taskdefs.Manifest.Section;
/**
* An ant task for printing the manifest from a file. This task is configured in
* manifest as shown below.
* <pre>
* <target name="printDetails">
* <manifestreader srcfile="${basedir}/myproduct.jar">
* <attribute name="Built-Date" value="Built On"/>
* <section name="com/freeware">
* <attribute name="Implementation-Title" value="Product Name"/>
* <attribute name="Implementation-Version" value="Product Version"/>
* <attribute name="Implementation-Vendor" value="Copyright"/>
* </section>
* </manifestreader>
* </target>
* </pre>
* In order to use this task you must instruct ant about this task this can be
* done as shown below.
* <pre>
* <typedef resource="com/freeware/anttasks/antlib.xml">
* <classpath>
* <pathelement location="${ant.contrib}/antfreeware-1.0.0.jar" />
* </classpath>
* </typedef>
* </pre>
* @author Prasad P. Khandekar
* @version 1.0.0
*/
public class ManifestReader
extends Task
{
/**
* the name of the jar file from which information needs to be extracted
*/
private String mstrSrcFile = null;
/**
* A list containing attributes from main section of manifest
*/
private Map mainAttrMap = null;
/**
* A list containing various sections defined in manifest
*/
private Map sectionMap = null;
public ManifestReader()
{
super();
}
/**
* Sets the name of the jar file from which manifest needs to be read and
* printed.
* @param jarFile The name of the jar file.
*/
public void setSrcfile(String jarFile)
{
mstrSrcFile = jarFile;
}
/**
* Returns the name of the jar file from which manifest needs to be read and
* printed.
* @return The name of the jar file.
*/
public String getSrcfile()
{
return mstrSrcFile;
}
/**
* Method which allows ant to supply our task the details about main manifest
* attributes to be be printed from the jar file. These attributes are
* configured in build.xml as nested elements of our task as shown below.
* <pre>
* <target name="printDetails">
* <manifestreader srcfile="${basedir}/myproduct.jar">
* <attribute name="Built-Date" value="Built On"/>
* <attribute name="Implementation-Title" value="Product Name"/>
* <attribute name="Implementation-Version" value="Product Version"/>
* <attribute name="Implementation-Vendor" value="Copyright"/>
* </manifestreader>
* </target>
* </pre>
* @param attr The instance of {@link org.apache.tools.ant.taskdefs.Manifest.Attribute}
* class holding information about the manifest attribute.
*/
public void addAttribute(Attribute attr)
{
if (null == mainAttrMap)
mainAttrMap = new HashMap();
if (attr != null)
mainAttrMap.put(attr.getKey(), attr);
}
/**
* Method which allows ant to supply our task details about one ore more
* sections in manifest files from which the attributes are to be printed.
* These attributes are configured in build.xml as nested elements of our
* task as shown below.
* <pre>
* <target name="printDetails">
* <manifestreader srcfile="${basedir}/myproduct.jar">
* <attribute name="Built-Date" value="Built On"/>
* <section name="com/freeware">
* <attribute name="Implementation-Title" value="Product Name"/>
* <attribute name="Implementation-Version" value="Product Version"/>
* <attribute name="Implementation-Vendor" value="Copyright"/>
* </section>
* </manifestreader>
* </target>
* </pre>
* @param attr The instance of {@link org.apache.tools.ant.taskdefs.Manifest.Attribute}
* class holding information about the manifest attribute.
*/
public void addSection(Section section)
{
if (null == sectionMap)
sectionMap = new HashMap();
if (section != null)
sectionMap.put(section.getName(), section);
}
/* (non-Javadoc)
* @see org.apache.tools.ant.Task#execute()
*/
public void execute() throws BuildException
{
Map.Entry entry = null;
Section section = null;
JarFile jarFile = null;
Iterator iter = null;
Manifest manifest = null;
if (!checkFile(mstrSrcFile)) return;
if (null == mainAttrMap && null == sectionMap) return;
try
{
jarFile = new JarFile(mstrSrcFile);
manifest = jarFile.getManifest();
if (mainAttrMap != null)
printAttributes(mainAttrMap, manifest.getMainAttributes());
if (sectionMap != null)
{
iter = sectionMap.entrySet().iterator();
while (iter.hasNext())
{
entry = (Map.Entry) iter.next();
section = (Section) entry.getValue();
printAttributes(section, manifest.getAttributes(section.getName()));
}
}
}
catch (IOException Ex)
{
}
finally
{
if (manifest != null) manifest = null;
try
{
if (jarFile != null)
jarFile.close();
}
catch (IOException ex)
{
}
jarFile = null;
}
}
/**
* helper method to determines whether the jar file from which manifest is
* to be read exists and is a valid jar file.
* @param pstrFile The jar file path and name.
* @return true if jar file exists and is a valid jar, false otherwise
*/
private boolean checkFile(String pstrFile)
{
boolean blnRet = false;
File file = null;
JarFile jarFile = null;
if (null == pstrFile) return blnRet;
file = new File(pstrFile);
blnRet = file.exists();
try
{
if (blnRet)
{
blnRet = false;
jarFile = new JarFile(file);
jarFile.close();
blnRet = true;
}
}
catch (IOException Ex)
{
blnRet = false;
}
finally
{
try
{
if (jarFile != null)
jarFile.close();
}
catch (IOException Ex)
{
}
if (file != null) file = null;
}
return blnRet;
}
/**
* Helper method to enumerate the attribute map and print corrosponding
* attribute values from manifest file's main attribues
* @param infoMap The map of attributes whose value are to be read from manifest
* @param attrs The map of attributes from manifest.
*/
private void printAttributes(Map infoMap, Map attrs)
{
String key = null;
String attrVal = null;
Iterator iter = null;
Map.Entry entry = null;
Attribute srcAttr = null;
if (null == infoMap || null == attrs) return;
iter = infoMap.entrySet().iterator();
while (iter.hasNext())
{
entry = (Map.Entry) iter.next();
srcAttr = (Attribute) entry.getValue();
key = srcAttr.getName();
attrVal = (String) attrs.get(new Attributes.Name(key));
if (attrVal != null)
System.out.println(srcAttr.getValue() + " " + attrVal);
}
}
/**
* Helper method to enumerate the manifest section and print the attribute
* values.
* @param section The manifest section.
* @param attrMap The map of attributes from a manifest section.
*/
private void printAttributes(Section section, Map attrs)
{
String key = null;
String attrName = null;
String attrVal = null;
Attribute srcAttr = null;
Enumeration enm = null;
if (null == section || null == attrs) return;
enm = section.getAttributeKeys();
while (enm.hasMoreElements())
{
key = (String) enm.nextElement();
srcAttr = section.getAttribute(key);
attrName = srcAttr.getName();
attrVal = (String) attrs.get(new Attributes.Name(attrName));
if (attrVal != null)
System.out.println(srcAttr.getValue() + " " + attrVal);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am a software professional with over 20 years of commercial business applications design and development experience.
My programming experience includes Java, Spring, .NET, Classic VB & ASP, Scripting, Power Builder, PHP, Magic & far far ago FoxPro, C, Assembly and COBOL.
From last 11 years I am mostly working with Java Technology. I am currently available to take up new assignments.