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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
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            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()).toURL();
126            return jarFileUrl.openConnection().getPermission();
127        }
128    
129        public String toString() {
130            return JarFileUrlConnection.class.getName() + ":" + url;
131        }
132    }