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.geronimo.kernel.classloader;
018
019 import java.io.File;
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.net.JarURLConnection;
023 import java.net.MalformedURLException;
024 import java.net.URL;
025 import java.net.URLConnection;
026 import java.net.URLStreamHandler;
027 import java.security.Permission;
028 import java.security.cert.Certificate;
029 import java.util.jar.Attributes;
030 import java.util.jar.JarEntry;
031 import java.util.jar.JarFile;
032 import java.util.jar.Manifest;
033
034 /**
035 * @version $Rev: 811978 $ $Date: 2009-09-07 11:09:25 +0800 (Mon, 07 Sep 2009) $
036 */
037 public class JarFileUrlConnection extends JarURLConnection {
038 public static final URL DUMMY_JAR_URL;
039 static {
040 try {
041 DUMMY_JAR_URL = new URL("jar", "", -1, "file:dummy!/", new URLStreamHandler() {
042 protected URLConnection openConnection(URL u) {
043 throw new UnsupportedOperationException();
044 }
045 });
046 } catch (Exception e) {
047 throw new ExceptionInInitializerError(e);
048 }
049 }
050
051 private final URL url;
052 private final JarFile jarFile;
053 private final JarEntry jarEntry;
054 private final URL jarFileUrl;
055
056 public JarFileUrlConnection(URL url, JarFile jarFile, JarEntry jarEntry) throws MalformedURLException {
057 super(DUMMY_JAR_URL);
058
059 if (url == null) throw new NullPointerException("url is null");
060 if (jarFile == null) throw new NullPointerException("jarFile is null");
061 if (jarEntry == null) throw new NullPointerException("jarEntry is null");
062
063 this.url = url;
064 this.jarFile = jarFile;
065 this.jarEntry = jarEntry;
066 jarFileUrl = new File(jarFile.getName()).toURL();
067 }
068
069 public JarFile getJarFile() throws IOException {
070 if (getUseCaches()) {
071 return jarFile;
072 } else {
073 return new JarFile(jarFile.getName());
074 }
075 }
076
077 public synchronized void connect() {
078 }
079
080 public URL getJarFileURL() {
081 return jarFileUrl;
082 }
083
084 public String getEntryName() {
085 return getJarEntry().getName();
086 }
087
088 public Manifest getManifest() throws IOException {
089 return jarFile.getManifest();
090 }
091
092 public JarEntry getJarEntry() {
093 if (getUseCaches()) {
094 return jarEntry;
095 } else {
096 //return (JarEntry) jarEntry.clone();
097 // There is a clone method, but the below way might be safer.
098 return jarFile.getJarEntry(jarEntry.getName());
099 }
100 }
101
102 public Attributes getAttributes() throws IOException {
103 return getJarEntry().getAttributes();
104 }
105
106 public Attributes getMainAttributes() throws IOException {
107 return getManifest().getMainAttributes();
108 }
109
110 public Certificate[] getCertificates() throws IOException {
111 return getJarEntry().getCertificates();
112 }
113
114 public URL getURL() {
115 return url;
116 }
117
118 public int getContentLength() {
119 long size = getJarEntry().getSize();
120 if (size > Integer.MAX_VALUE) {
121 return -1;
122 }
123 return (int) size;
124 }
125
126 public long getLastModified() {
127 return getJarEntry().getTime();
128 }
129
130 public synchronized InputStream getInputStream() throws IOException {
131 return jarFile.getInputStream(jarEntry);
132 }
133
134 public Permission getPermission() throws IOException {
135 URL jarFileUrl = new File(jarFile.getName()).toURI().toURL();
136 return jarFileUrl.openConnection().getPermission();
137 }
138
139 public String toString() {
140 return JarFileUrlConnection.class.getName() + ":" + url;
141 }
142 }