001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.plugin.jdbc.classloader.jar;
017
018import org.xbib.elasticsearch.plugin.jdbc.classloader.AbstractResourceHandle;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.MalformedURLException;
023import java.net.URL;
024import java.security.cert.Certificate;
025import java.util.jar.Attributes;
026import java.util.jar.JarEntry;
027import java.util.jar.JarFile;
028import java.util.jar.Manifest;
029
030public class JarResourceHandle extends AbstractResourceHandle {
031
032    private final JarFile jarFile;
033
034    private final JarEntry jarEntry;
035
036    private final URL url;
037
038    private final URL codeSource;
039
040    public JarResourceHandle(JarFile jarFile, JarEntry jarEntry, URL codeSource) throws MalformedURLException {
041        this.jarFile = jarFile;
042        this.jarEntry = jarEntry;
043        this.url = JarFileUrlStreamHandler.createURL(jarFile, jarEntry, codeSource);
044        this.codeSource = codeSource;
045    }
046
047    public String getName() {
048        return jarEntry.getName();
049    }
050
051    public URL getUrl() {
052        return url;
053    }
054
055    public URL getCodeSourceUrl() {
056        return codeSource;
057    }
058
059    public boolean isDirectory() {
060        return jarEntry.isDirectory();
061    }
062
063    public InputStream getInputStream() throws IOException {
064        return jarFile.getInputStream(jarEntry);
065    }
066
067    public int getContentLength() {
068        return (int) jarEntry.getSize();
069    }
070
071    @Override
072    public Manifest getManifest() throws IOException {
073        return jarFile.getManifest();
074    }
075
076    @Override
077    public Attributes getAttributes() throws IOException {
078        return jarEntry.getAttributes();
079    }
080
081    @Override
082    public Certificate[] getCertificates() {
083        return jarEntry.getCertificates();
084    }
085}