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.xbean.classloader;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.JarURLConnection;
023import java.net.MalformedURLException;
024import java.net.URL;
025import java.net.URLConnection;
026import java.net.URLStreamHandler;
027import java.security.Permission;
028import java.security.cert.Certificate;
029import java.util.jar.Attributes;
030import java.util.jar.JarEntry;
031import java.util.jar.JarFile;
032import java.util.jar.Manifest;
033
034/**
035 * @version $Rev: 776705 $ $Date: 2009-05-20 16:09:47 +0200 (Wed, 20 May 2009) $
036 */
037public 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()).toURI().toURL();
067    }
068
069    public JarFile getJarFile() throws IOException {
070        return jarFile;
071    }
072
073    public synchronized void connect() {
074    }
075
076    public URL getJarFileURL() {
077        return jarFileUrl;
078    }
079
080    public String getEntryName() {
081        return getJarEntry().getName();
082    }
083
084    public Manifest getManifest() throws IOException {
085        return jarFile.getManifest();
086    }
087
088    public JarEntry getJarEntry() {
089        return jarEntry;
090    }
091
092    public Attributes getAttributes() throws IOException {
093        return getJarEntry().getAttributes();
094    }
095
096    public Attributes getMainAttributes() throws IOException {
097        return getManifest().getMainAttributes();
098    }
099
100    public Certificate[] getCertificates() throws IOException {
101        return getJarEntry().getCertificates();
102    }
103
104    public URL getURL() {
105        return url;
106    }
107
108    public int getContentLength() {
109        long size = getJarEntry().getSize();
110        if (size > Integer.MAX_VALUE) {
111            return -1;
112        }
113        return (int) size;
114    }
115
116    public long getLastModified() {
117        return getJarEntry().getTime();
118    }
119
120    public synchronized InputStream getInputStream() throws IOException {
121        return jarFile.getInputStream(jarEntry);
122    }
123
124    public Permission getPermission() throws IOException {
125        URL jarFileUrl = new File(jarFile.getName()).toURI().toURL();
126        return jarFileUrl.openConnection().getPermission();
127    }
128
129    public String toString() {
130        return JarFileUrlConnection.class.getName() + ":" + url;
131    }
132}