1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.jar;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Collection;
22 import java.util.jar.Attributes;
23 import java.util.jar.Attributes.Name;
24 import java.util.jar.JarFile;
25 import java.util.jar.Manifest;
26 import java.util.zip.ZipEntry;
27 import java.util.zip.ZipFile;
28
29 import org.apache.commons.vfs2.Capability;
30 import org.apache.commons.vfs2.FileObject;
31 import org.apache.commons.vfs2.FileSystemException;
32 import org.apache.commons.vfs2.FileSystemOptions;
33 import org.apache.commons.vfs2.provider.AbstractFileName;
34 import org.apache.commons.vfs2.provider.zip.ZipFileObject;
35 import org.apache.commons.vfs2.provider.zip.ZipFileSystem;
36
37
38
39
40 public class JarFileSystem extends ZipFileSystem {
41 private Attributes attributes;
42
43
44
45
46
47
48
49
50
51 protected JarFileSystem(final AbstractFileName rootFileName, final FileObject parentLayer, final FileSystemOptions fileSystemOptions)
52 throws FileSystemException {
53 super(rootFileName, parentLayer, fileSystemOptions);
54 }
55
56
57
58
59
60
61
62
63
64
65 @Override
66 protected void addCapabilities(final Collection<Capability> caps) {
67
68 caps.addAll(JarFileProvider.CAPABILITIES);
69 }
70
71 @Override
72 protected ZipFile createZipFile(final File file) throws FileSystemException {
73 try {
74 return new JarFile(file);
75 } catch (final IOException ioe) {
76 throw new FileSystemException("vfs.provider.jar/open-jar-file.error", file, ioe);
77 }
78 }
79
80 @Override
81 protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry)
82 throws FileSystemException {
83 return new JarFileObject(name, entry, this, true);
84 }
85
86 Object getAttribute(final Name attrName) throws FileSystemException {
87 try {
88 final Attributes attr = getAttributes();
89 return attr.getValue(attrName);
90 } catch (final IOException ioe) {
91 throw new FileSystemException(attrName.toString(), ioe);
92 }
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public Object getAttribute(final String attrName) throws FileSystemException {
104 final Name name = lookupName(attrName);
105 return getAttribute(name);
106 }
107
108 Attributes getAttributes() throws IOException {
109 if (attributes == null) {
110 final Manifest man = ((JarFile) getZipFile()).getManifest();
111 if (man == null) {
112 attributes = new Attributes(1);
113 } else {
114 attributes = man.getMainAttributes();
115 if (attributes == null) {
116 attributes = new Attributes(1);
117 }
118 }
119 }
120
121 return attributes;
122 }
123
124 @Override
125 protected ZipFile getZipFile() throws FileSystemException {
126
127 return super.getZipFile();
128 }
129
130 Name lookupName(final String attrName) {
131 if (Name.CLASS_PATH.toString().equals(attrName)) {
132 return Name.CLASS_PATH;
133 }
134 if (Name.CONTENT_TYPE.toString().equals(attrName)) {
135 return Name.CONTENT_TYPE;
136 }
137 if (Name.EXTENSION_INSTALLATION.toString().equals(attrName)) {
138 return Name.EXTENSION_INSTALLATION;
139 }
140 if (Name.EXTENSION_LIST.toString().equals(attrName)) {
141 return Name.EXTENSION_LIST;
142 }
143 if (Name.EXTENSION_NAME.toString().equals(attrName)) {
144 return Name.EXTENSION_NAME;
145 }
146 if (Name.IMPLEMENTATION_TITLE.toString().equals(attrName)) {
147 return Name.IMPLEMENTATION_TITLE;
148 }
149 if (Name.IMPLEMENTATION_URL.toString().equals(attrName)) {
150 return Name.IMPLEMENTATION_URL;
151 }
152 if (Name.IMPLEMENTATION_VENDOR.toString().equals(attrName)) {
153 return Name.IMPLEMENTATION_VENDOR;
154 }
155 if (Name.IMPLEMENTATION_VENDOR_ID.toString().equals(attrName)) {
156 return Name.IMPLEMENTATION_VENDOR_ID;
157 }
158 if (Name.IMPLEMENTATION_VERSION.toString().equals(attrName)) {
159 return Name.IMPLEMENTATION_VENDOR;
160 }
161 if (Name.MAIN_CLASS.toString().equals(attrName)) {
162 return Name.MAIN_CLASS;
163 }
164 if (Name.MANIFEST_VERSION.toString().equals(attrName)) {
165 return Name.MANIFEST_VERSION;
166 }
167 if (Name.SEALED.toString().equals(attrName)) {
168 return Name.SEALED;
169 }
170 if (Name.SIGNATURE_VERSION.toString().equals(attrName)) {
171 return Name.SIGNATURE_VERSION;
172 }
173 if (Name.SPECIFICATION_TITLE.toString().equals(attrName)) {
174 return Name.SPECIFICATION_TITLE;
175 }
176 if (Name.SPECIFICATION_VENDOR.toString().equals(attrName)) {
177 return Name.SPECIFICATION_VENDOR;
178 }
179 if (Name.SPECIFICATION_VERSION.toString().equals(attrName)) {
180 return Name.SPECIFICATION_VERSION;
181 }
182 return new Name(attrName);
183 }
184
185 }