001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.jar;
018:
019: import org.apache.commons.vfs.FileName;
020: import org.apache.commons.vfs.FileSystemException;
021: import org.apache.commons.vfs.provider.zip.ZipFileObject;
022:
023: import java.io.IOException;
024: import java.security.cert.Certificate;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.jar.Attributes;
029: import java.util.jar.JarEntry;
030: import java.util.jar.JarFile;
031: import java.util.jar.Manifest;
032: import java.util.zip.ZipEntry;
033:
034: /**
035: * A file in a Jar file system.
036: *
037: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
038: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
039: */
040: public class JarFileObject extends ZipFileObject {
041: private Attributes attributes;
042:
043: final JarFileSystem fs;
044:
045: protected JarFileObject(final FileName name, final ZipEntry entry,
046: final JarFileSystem fs, final boolean zipExists)
047: throws FileSystemException {
048: super (name, entry, fs, zipExists);
049: this .fs = fs;
050:
051: try {
052: getAttributes(); // early get the attributes as the zip file might be closed
053: } catch (IOException e) {
054: throw new FileSystemException(e);
055: }
056: }
057:
058: /**
059: * Returns the Jar manifest.
060: */
061: Manifest getManifest() throws IOException {
062: if (fs.getZipFile() == null) {
063: return null;
064: }
065:
066: return ((JarFile) fs.getZipFile()).getManifest();
067: }
068:
069: /**
070: * Returns the attributes of this file.
071: */
072: Attributes getAttributes() throws IOException {
073: if (attributes == null) {
074: if (entry == null) {
075: attributes = new Attributes(1);
076: } else {
077: attributes = ((JarEntry) entry).getAttributes();
078: if (attributes == null) {
079: attributes = new Attributes(1);
080: }
081: }
082: }
083:
084: return attributes;
085: }
086:
087: /**
088: * Returns the value of an attribute.
089: */
090: protected Map doGetAttributes() throws Exception {
091: final Map attrs = new HashMap();
092:
093: // Add the file system's attributes first
094: final JarFileSystem fs = (JarFileSystem) getFileSystem();
095: addAll(fs.getAttributes(), attrs);
096:
097: // Add this file's attributes
098: addAll(getAttributes(), attrs);
099:
100: return attrs;
101: }
102:
103: /**
104: * Adds the source attributes to the destination map.
105: */
106: private void addAll(final Attributes src, final Map dest) {
107: for (Iterator iterator = src.entrySet().iterator(); iterator
108: .hasNext();) {
109: final Map.Entry entry = (Map.Entry) iterator.next();
110: final String name = entry.getKey().toString().toLowerCase();
111: dest.put(name, entry.getValue());
112: }
113: }
114:
115: /**
116: * Return the certificates of this JarEntry.
117: */
118: protected Certificate[] doGetCertificates() {
119: if (entry == null) {
120: return null;
121: }
122:
123: return ((JarEntry) entry).getCertificates();
124: }
125: }
|