001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 2000, 2001, 2002, 2003 Jesse Stockall. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution, if
019: * any, must include the following acknowlegement:
020: * "This product includes software developed by the
021: * Apache Software Foundation (http://www.apache.org/)."
022: * Alternately, this acknowlegement may appear in the software itself,
023: * if and wherever such third-party acknowlegements normally appear.
024: *
025: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
026: * Foundation" must not be used to endorse or promote products derived
027: * from this software without prior written permission. For written
028: * permission, please contact apache@apache.org.
029: *
030: * 5. Products derived from this software may not be called "Apache"
031: * nor may "Apache" appear in their names without prior written
032: * permission of the Apache Group.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
038: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: * SUCH DAMAGE.
046: * ====================================================================
047: *
048: */
049: package org.apache.tools.ant.taskdefs.optional.genjar;
050:
051: import java.io.File;
052: import java.io.IOException;
053: import java.io.InputStream;
054: import java.util.jar.Attributes;
055: import java.util.jar.JarEntry;
056: import java.util.jar.JarFile;
057:
058: /**
059: * Represents a jar file in the classpath.<p>
060: *
061: * When a Jar file is located in the classpath, a JarResolver is instantiated that remembers the
062: * path and performs searches in that jar. This class is used primarily to allow easy association of
063: * the <i>jar file</i> with the jar entry's attributes.<p>
064: *
065: * When a file is resolved from a JarEntrySpec, Attributes are added for the jar file and last
066: * modification time.<p>
067: *
068: * <p>
069: *
070: * TODO: copy all entry-attributes from the source jar into our manifest</p>
071: *
072: * @author Original Code: <a href="mailto:jake@riggshill.com">John W. Kohler </a>
073: * @author Jesse Stockall
074: * @version $Revision: 1.2 $ $Date: 2003/02/23 10:06:10 $
075: */
076: class JarResolver extends PathResolver {
077: File file = null;
078: JarFile jarFile = null;
079: String modified;
080:
081: /**
082: * Constructor for the JarResolver object
083: *
084: * @param file Description of the Parameter
085: * @param log Description of the Parameter
086: * @throws IOException Oops!
087: */
088: JarResolver(File file, Logger log) throws IOException {
089: super (log);
090:
091: this .file = file;
092: this .jarFile = new JarFile(file);
093: modified = formatDate(file.lastModified());
094: log.verbose("Resolver: " + file);
095: }
096:
097: /**
098: * Close the jar file.
099: *
100: * @throws IOException Oops!
101: */
102: public void close() throws IOException {
103: if (jarFile != null) {
104: jarFile.close();
105: }
106: }
107:
108: /**
109: * Description of the Method
110: *
111: * @param spec Description of the Parameter
112: * @return Description of the Return Value
113: * @throws IOException Oops!
114: */
115: public InputStream resolve(JarEntrySpec spec) throws IOException {
116: InputStream is = null;
117:
118: JarEntry je = jarFile.getJarEntry(spec.getJarName());
119: if (je != null) {
120: Attributes atts = spec.getAttributes();
121: atts.putValue(CONTENT_LOC, file.getAbsolutePath());
122: long modTime = je.getTime();
123: if (modTime > 0) {
124: atts.putValue(LAST_MOD, formatDate(modTime));
125: } else {
126: atts.putValue(LAST_MOD, modified);
127: }
128: spec.addAttributes(je.getAttributes());
129: is = jarFile.getInputStream(je);
130: log.debug(spec.getJarName() + "->" + file);
131: }
132: return is;
133: }
134:
135: /**
136: * Description of the Method
137: *
138: * @param fname Description of the Parameter
139: * @return Description of the Return Value
140: * @throws IOException Oops!
141: */
142: public InputStream resolve(String fname) throws IOException {
143: InputStream is = null;
144:
145: JarEntry je = jarFile.getJarEntry(fname);
146: if (je != null) {
147: is = jarFile.getInputStream(je);
148: log.debug(fname + "->" + file);
149: }
150: return is;
151: }
152: }
153: // vi:set ts=4 sw=4:
|