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.directory;
017
018import org.xbib.elasticsearch.plugin.jdbc.classloader.AbstractResourceHandle;
019
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.MalformedURLException;
025import java.net.URL;
026import java.security.cert.Certificate;
027import java.util.jar.Attributes;
028import java.util.jar.Manifest;
029
030public class DirectoryResourceHandle extends AbstractResourceHandle {
031
032    private final String name;
033
034    private final File file;
035
036    private final Manifest manifest;
037
038    private final URL url;
039
040    private final URL codeSource;
041
042    public DirectoryResourceHandle(String name, File file, File codeSource, Manifest manifest) throws MalformedURLException {
043        this.name = name;
044        this.file = file;
045        this.codeSource = codeSource.toURI().toURL();
046        this.manifest = manifest;
047        url = file.toURI().toURL();
048    }
049
050    @Override
051    public String getName() {
052        return name;
053    }
054
055    @Override
056    public URL getUrl() {
057        return url;
058    }
059
060    @Override
061    public URL getCodeSourceUrl() {
062        return codeSource;
063    }
064
065    @Override
066    public boolean isDirectory() {
067        return file.isDirectory();
068    }
069
070    @Override
071    public InputStream getInputStream() throws IOException {
072        if (file.isDirectory()) {
073            return new EmptyInputStream();
074        }
075        return new FileInputStream(file);
076    }
077
078    @Override
079    public int getContentLength() {
080        if (file.isDirectory() || file.length() > Integer.MAX_VALUE) {
081            return -1;
082        } else {
083            return (int) file.length();
084        }
085    }
086
087    @Override
088    public Manifest getManifest() throws IOException {
089        return manifest;
090    }
091
092    @Override
093    public Attributes getAttributes() throws IOException {
094        if (manifest == null) {
095            return null;
096        }
097        return manifest.getAttributes(getName());
098    }
099
100    /**
101     * Always return null. This could be implementd by verifing the signatures
102     * in the manifest file against the actual file, but we don't need this
103     * right now.
104     *
105     * @return null
106     */
107    public Certificate[] getCertificates() {
108        return null;
109    }
110
111    static final class EmptyInputStream extends InputStream {
112
113        public int read() {
114            return -1;
115        }
116
117        public int read(byte b[]) {
118            return -1;
119        }
120
121        public int read(byte b[], int off, int len) {
122            return -1;
123        }
124
125        public long skip(long n) {
126            return 0;
127        }
128
129        public int available() {
130            return 0;
131        }
132
133        public void close() {
134        }
135
136        public synchronized void mark(int readlimit) {
137        }
138
139        public synchronized void reset() {
140        }
141
142        public boolean markSupported() {
143            return false;
144        }
145    }
146}