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.IOException;
020import java.io.InputStream;
021import java.io.File;
022import java.io.FileInputStream;
023import java.net.URL;
024import java.net.MalformedURLException;
025import java.security.cert.Certificate;
026import java.util.jar.Attributes;
027import java.util.jar.Manifest;
028
029/**
030 * @version $Rev: 776705 $ $Date: 2009-05-20 16:09:47 +0200 (Wed, 20 May 2009) $
031 */
032public class DirectoryResourceHandle extends AbstractResourceHandle {
033    private final String name;
034    private final File file;
035    private final Manifest manifest;
036    private final URL url;
037    private final URL codeSource;
038
039    public DirectoryResourceHandle(String name, File file, File codeSource, Manifest manifest) throws MalformedURLException {
040        this.name = name;
041        this.file = file;
042        this.codeSource = codeSource.toURI().toURL();
043        this.manifest = manifest;
044        url = file.toURI().toURL();
045    }
046
047    public String getName() {
048        return name;
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 file.isDirectory();
061    }
062
063    public InputStream getInputStream() throws IOException {
064        if (file.isDirectory()) {
065            return new IoUtil.EmptyInputStream();
066        }
067        return new FileInputStream(file);
068    }
069
070    public int getContentLength() {
071        if (file.isDirectory() || file.length() > Integer.MAX_VALUE) {
072            return -1;
073        } else {
074            return (int) file.length();
075        }
076    }
077
078    public Manifest getManifest() throws IOException {
079        return manifest;
080    }
081
082    public Attributes getAttributes() throws IOException {
083        if (manifest == null) {
084            return null;
085        }
086        return manifest.getAttributes(getName());
087    }
088
089    /**
090     * Always return null.  This could be implementd by verifing the signatures
091     * in the manifest file against the actual file, but we don't need this right now.
092     * @return null
093     */
094    public Certificate[] getCertificates() {
095        return null;
096    }
097}