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.finder.archive;
018
019import org.apache.xbean.osgi.bundle.util.BundleResourceFinder;
020import org.apache.xbean.osgi.bundle.util.ResourceDiscoveryFilter;
021import org.osgi.framework.Bundle;
022import org.osgi.service.packageadmin.PackageAdmin;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.net.URL;
027import java.util.Collections;
028import java.util.Iterator;
029import java.util.zip.ZipEntry;
030
031/**
032 * TODO Unfinished
033 * @version $Rev$ $Date$
034 */
035public class BundleArchive implements Archive {
036
037    private final Bundle bundle;
038
039    public BundleArchive(PackageAdmin packageAdmin, Bundle bundle) throws Exception {
040        this(packageAdmin, bundle, BundleResourceFinder.FULL_DISCOVERY_FILTER);
041    }
042
043    public BundleArchive(PackageAdmin packageAdmin, Bundle bundle, ResourceDiscoveryFilter discoveryFilter) throws Exception {
044        this.bundle = bundle;
045        BundleResourceFinder bundleResourceFinder = new BundleResourceFinder(packageAdmin, bundle, "", ".class", discoveryFilter);
046        bundleResourceFinder.find(new AnnotationFindingCallback());
047    }
048
049    public Iterator<Entry> iterator() {
050        return Collections.EMPTY_LIST.iterator();
051    }
052
053    public InputStream getBytecode(String className) throws IOException, ClassNotFoundException {
054        int pos = className.indexOf("<");
055        if (pos > -1) {
056            className = className.substring(0, pos);
057        }
058        pos = className.indexOf(">");
059        if (pos > -1) {
060            className = className.substring(0, pos);
061        }
062        if (!className.endsWith(".class")) {
063            className = className.replace('.', '/') + ".class";
064        }
065
066        URL resource = bundle.getResource(className);
067        if (resource != null) return resource.openStream();
068
069        throw new ClassNotFoundException(className);
070    }
071
072    public Class<?> loadClass(String s) throws ClassNotFoundException {
073        return bundle.loadClass(s);
074    }
075
076    private class AnnotationFindingCallback implements BundleResourceFinder.ResourceFinderCallback {
077
078        public boolean foundInDirectory(Bundle bundle, String baseDir, URL url) throws Exception {
079            InputStream in = url.openStream();
080            try {
081                //TODO
082//                readClassDef(in);
083            } finally {
084                in.close();
085            }
086            return true;
087        }
088
089
090        public boolean foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream in) throws Exception {
091            //TODO
092//            readClassDef(in);
093            return true;
094        }
095    }
096
097
098}