FileDocCategorySizeDatePackage
JarAccess.javaAPI DocGlassfish v2 API12995Fri May 04 22:32:08 BST 2007com.sun.enterprise.util

JarAccess

public class JarAccess extends Object
This class implements a simple utility for creating and extracting JAR (Java Archive) files. The JAR format is based on the ZIP file format, with optional meta-information stored in a MANIFEST entry. To create an EJB JAR, use JarAccess.create(jarfile, baseDir, ejbNames, files). It borrows from the BeanBox and JDK code.

Fields Summary
static Logger
_logger
public static final String
MANIFEST
static final char
SEPARATOR
private File
jarName
private File
dirName
private String
beanName
private String[]
fileNames
private static LocalStringManagerImpl
localStrings
Constructors Summary
Methods Summary
static voidaddEntry(java.util.zip.ZipOutputStream zos, JarEntrySource source)

	String name = source.getName();
	if (name.equals("") || name.equals(".")) {
	    return;
	}

	//	long size = source.getLength();

	ZipEntry e = new ZipEntry(name);

	e.setTime(source.getTime());
	boolean markOnly = source.isMarkOnly();

	if (markOnly) {
	    e.setMethod(ZipEntry.STORED);
	    e.setSize(0);
	    e.setCrc(0);
	}
	zos.putNextEntry(e);
	if (! markOnly) {
	    byte[] buf = new byte[1024];
	    int len = 0;
	    InputStream is = new BufferedInputStream(source.getInputStream());

	    while (len != -1) {
		try{
	    	len = is.read(buf, 0, buf.length);
		}catch(EOFException eof){
			break;
		}

		if(len != -1)
			zos.write(buf, 0, len);
	    }

	    is.close();
	}
	zos.closeEntry();
    
public static voidcreate(java.io.OutputStream out, java.io.File baseDir, java.lang.String[] ejbNames, java.lang.String[] files)
Create a new JAR file; Given a base directory, ejb names, and a set of files names, these two relative to the base directory if baseDir is null, it means WD if beanFIle is null, it means generate no MANIFEST Generates a *non-signed* MANIFEST



                                                     
        
			       
			       
			       
	 
    
	int start = 0;
	if (ejbNames != null) {
	    start = 1;
	}
	JarEntrySource[] data = new JarEntrySource[files.length + start];
	if (ejbNames != null) {
	    data[0] = makeManifestEntry(ejbNames);
	}
	for (int i = 0; i<files.length; i++) {
	    data[i+start] = new JarEntrySource(entryName(files[i]),
					       new File(baseDir, files[i]));
	}
	create(out, data);
    
public static voidcreate(java.io.OutputStream out, java.lang.String[] files)
Creates a new ZIP file with a bunch of files

	ZipOutputStream zos = new ZipOutputStream(out);
	for (int i = 0; i < files.length; i++) {
	    addEntry(zos, new JarEntrySource(new File(files[i])));
	}
	zos.close();
    
public static voidcreate(java.io.OutputStream out, JarEntrySource[] entries)
Creates a new ZIP file with a bunch of entries

	ZipOutputStream zos = new ZipOutputStream(out);
	for (int i = 0; i < entries.length; i++) {
	    try {
	        addEntry(zos, entries[i]);
	    } catch ( Exception ex ) {
		/** IASRI 4660742 
    ex.printStackTrace(); 
    **/
		// START OF IASRI 4660742
		_logger.log(Level.SEVERE,"enterprise_util.excep_jaraccess_create",ex);
		// END OF IASRI 4660742
		throw new IOException("Invalid JAR entry: "
					+entries[i].getName());
	    }
	}
	zos.close();
    
private static java.lang.StringentryName(java.lang.String name)

	name = name.replace(File.separatorChar, '/");
	if (name.startsWith("/")) {
	    name = name.substring(1);
	} else if (name.startsWith("./")) {
	    name = name.substring(2);
	}
	return name;
    
protected static voiderror(java.lang.String s)
Print an error message

	/** IASRI 4660742
	System.err.println(s);
	*/
	// START OF IASRI 4660742
	_logger.log(Level.SEVERE,"enterprise_util.some_error",s);
	// END OF IASRI 4660742
    
public static voidextract(java.io.InputStream in, java.lang.String[] files)

	ZipInputStream zis = new ZipInputStream(in);
	ZipEntry e;
	while ((e = zis.getNextEntry()) != null) {
	    if (files == null) {
		extractFile(zis, e);
	    } else {
		String name = e.getName().replace('/", File.separatorChar);
		for (int i = 0; i < files.length; i++) {
		    if (name.startsWith(files[i])) {
			extractFile(zis, e);
			break;
		    }
		}
	    }
	}
    
public static java.util.Vectorextract(java.io.InputStream in, java.lang.String[] files, java.lang.String directory)

	Vector extractedFiles = new Vector();
	ZipInputStream zis = new ZipInputStream(in);
	ZipEntry e;
	while ((e = zis.getNextEntry()) != null) {
	    if (files == null) {
		File extractedFile = extractFile(zis, e, directory);
		extractedFiles.addElement(extractedFile);
	    } else {
		String name = e.getName().replace('/", File.separatorChar);
		for (int i = 0; i < files.length; i++) {
		    if (name.startsWith(files[i])) {
			File extractedFile = extractFile(zis, e, directory);
			extractedFiles.addElement(extractedFile);
			break;
		    }
		}
	    }
	}
	return extractedFiles;
    
private static java.io.FileextractFile(java.util.zip.ZipInputStream zis, java.util.zip.ZipEntry e, java.lang.String dir)

	File f = new File(dir+File.separatorChar+e.getName().replace('/", File.separatorChar));
	if (e.isDirectory()) {
	    if (!f.exists() && !f.mkdirs() || !f.isDirectory()) {
		throw new IOException(f + ": could not create directory");
	    }
	} else {
	    if (f.getParent() != null) {
		File d = new File(f.getParent());
		if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
		    throw new IOException(d + ": could not create directory");
		}
	    }
	    OutputStream os = new FileOutputStream(f);
	    byte[] b = new byte[512];
	    int len;
	    while ((len = zis.read(b, 0, b.length)) != -1) {
		os.write(b, 0, len);
	    }
	    zis.closeEntry();
	    os.close();
	}
	return f;
    
private static voidextractFile(java.util.zip.ZipInputStream zis, java.util.zip.ZipEntry e)

	File f = new File(e.getName().replace('/", File.separatorChar));
	if (e.isDirectory()) {
	    if (!f.exists() && !f.mkdirs() || !f.isDirectory()) {
		throw new IOException(f + ": could not create directory");
	    }
	} else {
	    if (f.getParent() != null) {
		File d = new File(f.getParent());
		if (!d.exists() && !d.mkdirs() || !d.isDirectory()) {
		    throw new IOException(d + ": could not create directory");
		}
	    }
	    OutputStream os = new FileOutputStream(f);
	    byte[] b = new byte[512];
	    int len;
	    while ((len = zis.read(b, 0, b.length)) != -1) {
		os.write(b, 0, len);
	    }
	    zis.closeEntry();
	    os.close();
	}
    
private static voidlist(java.io.InputStream in, java.lang.String[] files)

	ZipInputStream zis = new ZipInputStream(in);
	ZipEntry e;
	while ((e = zis.getNextEntry()) != null) {
	    String name = e.getName().replace('/", File.separatorChar);
	    /*
	     * In the case of a compressed (deflated) entry, the entry size
	     * is stored immediately following the entry data and cannot be
	     * determined until the entry is fully read. Therefore, we close
	     * the entry first before printing out its attributes.
	     */
	    zis.closeEntry();
	    if (files == null) {
		printEntry(e);
	    } else {
		for (int i = 0; i < files.length; i++) {
		    if (name.startsWith(files[i])) {
			printEntry(e);
			break;
		    }
		}
	    }
	}
    
public static JarEntrySourcemakeManifestEntry(java.lang.String[] ejbNames)
An InputStream with the data about the Manifest

	StringBuffer s = new StringBuffer("Manifest-Version: 1.0\n");
	s.append("\n");
	for ( int i=0; i<ejbNames.length; i++ ) {
	    s.append("Name: "+ejbNames[i]+"\n");
	    s.append("Enterprise-Bean: True\n");
	    s.append("\n");
	}
	return new JarEntrySource(MANIFEST,
	    new ByteArrayInputStream(s.toString().getBytes()));
    
protected static voidoutput(java.lang.String s)
Print an output message

	/** IASRI 4660742
	System.err.println(s);
	**/
	// START OF IASRI 4660742
	_logger.log(Level.FINE,s);
	// END OF IASRI 4660742
    
private booleanparseArgs(java.lang.String[] args)
Parse the arguments

	int l = args.length;
	int i;
	for (i = 0; i<l; i++) {
	    if (args[i].equals("-bean")) {
		if (i+1 >= l) {
		    error(localStrings.getLocalString("jaraccess.bean.option",
                                                      ""));
		    return false;
		}
		beanName = args[i+1];
		i += 1;
	    } else if (args[i].equals("-dir")) {
		if (i+1 >= l) {
		    error(localStrings.getLocalString("jaraccess.dir.option",
                                                      ""));
		    return false;
		}
		dirName = new File(args[i+1]);
		i += 1;
	    } else {
		break;
	    }
	}
	if (i+1 >= l) {
	    error(localStrings.getLocalString("jaraccess.num.args", ""));
	    return false;
	}
	jarName = new File(args[i]);
	i += 1;
	fileNames = new String[l-i];
	for (int j=0; j<l-i ; j++) {
	    fileNames[j] = args[j+i];
	}
	// printArgs();
	return true;
    
private voidprintArgs()
Print the argumens read, for debugging

	/** IASRI 4660742
	System.err.println("jarName: "+jarName);
	System.err.println("dirName: "+dirName);
	System.err.println("beanName: "+beanName);
	System.err.println("fileNames: "+fileNames);
	**/
	// START OF IASRI 4660742
  if (_logger.isLoggable(Level.FINE)) {
	    _logger.log(Level.FINE,"jarName: "+jarName);
	    _logger.log(Level.FINE,"dirName: "+dirName);
	    _logger.log(Level.FINE,"beanName: "+beanName);
	    _logger.log(Level.FINE,"fileNames: "+fileNames);
  }
	// END OF IASRI 4660742
	if (fileNames != null) {
	    for (int i=0; i<fileNames.length; i++) {
		/** IASRI 4660742
		System.err.println("fileNames["+i+"]: "+fileNames[i]);
		**/
		// START OF IASRI 4660742
    if (_logger.isLoggable(Level.FINE)) {
		    _logger.log(Level.FINE,"fileNames["+i+"]: "+fileNames[i]);
    }
		// END OF IASRI 4660742
	    }
	}
    
private static voidprintEntry(java.util.zip.ZipEntry e)

	output(e.getName());