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 */ 017package org.apache.commons.vfs2.provider.jar; 018 019import java.io.IOException; 020import java.security.cert.Certificate; 021import java.util.HashMap; 022import java.util.Map; 023import java.util.Map.Entry; 024import java.util.jar.Attributes; 025import java.util.jar.JarEntry; 026import java.util.jar.JarFile; 027import java.util.jar.Manifest; 028import java.util.zip.ZipEntry; 029 030import org.apache.commons.vfs2.FileSystemException; 031import org.apache.commons.vfs2.provider.AbstractFileName; 032import org.apache.commons.vfs2.provider.zip.ZipFileObject; 033 034/** 035 * A file in a Jar file system. 036 */ 037public class JarFileObject extends ZipFileObject { 038 private final JarFileSystem fs; 039 040 private Attributes attributes; 041 042 protected JarFileObject(final AbstractFileName name, final ZipEntry entry, final JarFileSystem fs, 043 final boolean zipExists) throws FileSystemException { 044 super(name, entry, fs, zipExists); 045 this.fs = fs; 046 047 try { 048 getAttributes(); // early get the attributes as the zip file might be closed 049 } catch (final IOException e) { 050 throw new FileSystemException(e); 051 } 052 } 053 054 /** 055 * Returns the Jar manifest. 056 */ 057 Manifest getManifest() throws IOException { 058 if (fs.getZipFile() == null) { 059 return null; 060 } 061 062 return ((JarFile) fs.getZipFile()).getManifest(); 063 } 064 065 /** 066 * Returns the attributes of this file. 067 */ 068 Attributes getAttributes() throws IOException { 069 if (attributes == null) { 070 if (entry == null) { 071 attributes = new Attributes(1); 072 } else { 073 attributes = ((JarEntry) entry).getAttributes(); 074 if (attributes == null) { 075 attributes = new Attributes(1); 076 } 077 } 078 } 079 080 return attributes; 081 } 082 083 /** 084 * Returns the value of an attribute. 085 */ 086 @Override 087 protected Map<String, Object> doGetAttributes() throws Exception { 088 final Map<String, Object> attrs = new HashMap<>(); 089 090 // Add the file system's attributes first 091 final JarFileSystem fs = (JarFileSystem) getFileSystem(); 092 addAll(fs.getAttributes(), attrs); 093 094 // Add this file's attributes 095 addAll(getAttributes(), attrs); 096 097 return attrs; 098 } 099 100 /** 101 * Adds the source attributes to the destination map. 102 */ 103 private void addAll(final Attributes src, final Map<String, Object> dest) { 104 for (final Entry<Object, Object> entry : src.entrySet()) { 105 // final String name = entry.getKey().toString().toLowerCase(); 106 final String name = entry.getKey().toString(); 107 dest.put(name, entry.getValue()); 108 } 109 } 110 111 /** 112 * Return the certificates of this JarEntry. 113 */ 114 @Override 115 protected Certificate[] doGetCertificates() { 116 if (entry == null) { 117 return null; 118 } 119 120 return ((JarEntry) entry).getCertificates(); 121 } 122}