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