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.FileObject;
021: import org.apache.commons.vfs.FileSystemException;
022: import org.apache.commons.vfs.FileSystemOptions;
023: import org.apache.commons.vfs.provider.zip.ZipFileObject;
024: import org.apache.commons.vfs.provider.zip.ZipFileSystem;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.Collection;
029: import java.util.jar.Attributes;
030: import java.util.jar.Attributes.Name;
031: import java.util.jar.JarFile;
032: import java.util.jar.Manifest;
033: import java.util.zip.ZipEntry;
034: import java.util.zip.ZipFile;
035:
036: /**
037: * A read-only file system for Jar files.
038: *
039: * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
040: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
041: */
042: public class JarFileSystem extends ZipFileSystem {
043: private Attributes attributes;
044:
045: protected JarFileSystem(final FileName rootName,
046: final FileObject file,
047: final FileSystemOptions fileSystemOptions)
048: throws FileSystemException {
049: super (rootName, file, fileSystemOptions);
050: }
051:
052: protected ZipFile createZipFile(File file)
053: throws FileSystemException {
054: try {
055: return new JarFile(file);
056: } catch (IOException ioe) {
057: throw new FileSystemException(
058: "vfs.provider.jar/open-jar-file.error", file, ioe);
059: }
060: }
061:
062: protected ZipFileObject createZipFileObject(FileName name,
063: ZipEntry entry) throws FileSystemException {
064: return new JarFileObject(name, entry, this , true);
065: }
066:
067: /**
068: * Returns the capabilities of this file system.
069: */
070: protected void addCapabilities(final Collection caps) {
071: // super.addCapabilities(caps);
072: caps.addAll(JarFileProvider.capabilities);
073: }
074:
075: Attributes getAttributes() throws IOException {
076: if (attributes == null) {
077: final Manifest man = ((JarFile) getZipFile()).getManifest();
078: if (man == null) {
079: attributes = new Attributes(1);
080: } else {
081: attributes = man.getMainAttributes();
082: if (attributes == null) {
083: attributes = new Attributes(1);
084: }
085: }
086: }
087:
088: return attributes;
089: }
090:
091: Object getAttribute(Name attrName) throws FileSystemException {
092: try {
093: final Attributes attr = getAttributes();
094: final String value = attr.getValue(attrName);
095: return value;
096: } catch (IOException ioe) {
097: throw new FileSystemException(attrName.toString(), ioe);
098: }
099: }
100:
101: Name lookupName(String attrName) {
102: if (Name.CLASS_PATH.equals(attrName)) {
103: return Name.CLASS_PATH;
104: } else if (Name.CONTENT_TYPE.equals(attrName)) {
105: return Name.CONTENT_TYPE;
106: } else if (Name.EXTENSION_INSTALLATION.equals(attrName)) {
107: return Name.EXTENSION_INSTALLATION;
108: } else if (Name.EXTENSION_LIST.equals(attrName)) {
109: return Name.EXTENSION_LIST;
110: } else if (Name.EXTENSION_NAME.equals(attrName)) {
111: return Name.EXTENSION_NAME;
112: } else if (Name.IMPLEMENTATION_TITLE.equals(attrName)) {
113: return Name.IMPLEMENTATION_TITLE;
114: } else if (Name.IMPLEMENTATION_URL.equals(attrName)) {
115: return Name.IMPLEMENTATION_URL;
116: } else if (Name.IMPLEMENTATION_VENDOR.equals(attrName)) {
117: return Name.IMPLEMENTATION_VENDOR;
118: } else if (Name.IMPLEMENTATION_VENDOR_ID.equals(attrName)) {
119: return Name.IMPLEMENTATION_VENDOR_ID;
120: } else if (Name.IMPLEMENTATION_VERSION.equals(attrName)) {
121: return Name.IMPLEMENTATION_VENDOR;
122: } else if (Name.MAIN_CLASS.equals(attrName)) {
123: return Name.MAIN_CLASS;
124: } else if (Name.MANIFEST_VERSION.equals(attrName)) {
125: return Name.MANIFEST_VERSION;
126: } else if (Name.SEALED.equals(attrName)) {
127: return Name.SEALED;
128: } else if (Name.SIGNATURE_VERSION.equals(attrName)) {
129: return Name.SIGNATURE_VERSION;
130: } else if (Name.SPECIFICATION_TITLE.equals(attrName)) {
131: return Name.SPECIFICATION_TITLE;
132: } else if (Name.SPECIFICATION_VENDOR.equals(attrName)) {
133: return Name.SPECIFICATION_VENDOR;
134: } else if (Name.SPECIFICATION_VERSION.equals(attrName)) {
135: return Name.SPECIFICATION_VERSION;
136: } else {
137: return new Name(attrName);
138: }
139: }
140:
141: /**
142: * Retrives the attribute with the specified name. The default
143: * implementation simply throws an exception.
144: */
145: public Object getAttribute(String attrName)
146: throws FileSystemException {
147: final Name name = lookupName(attrName);
148: return getAttribute(name);
149: }
150:
151: protected ZipFile getZipFile() throws FileSystemException {
152: return super.getZipFile();
153: }
154: }
|