001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.xbean.finder;
022    
023    import java.io.InputStream;
024    import java.net.URL;
025    import java.util.zip.ZipEntry;
026    
027    import org.apache.xbean.osgi.bundle.util.BundleResourceFinder;
028    import org.apache.xbean.osgi.bundle.util.ResourceDiscoveryFilter;
029    import org.osgi.framework.Bundle;
030    import org.osgi.service.packageadmin.PackageAdmin;
031    
032    /**
033     * @version $Rev: 942659 $ $Date: 2010-05-10 07:14:17 +0200 (Mon, 10 May 2010) $
034     */
035    public class BundleAnnotationFinder extends AbstractFinder {
036        private final Bundle bundle;
037    
038        public BundleAnnotationFinder(PackageAdmin packageAdmin, Bundle bundle) throws Exception {
039            this(packageAdmin, bundle, BundleResourceFinder.FULL_DISCOVERY_FILTER);
040        }
041    
042        public BundleAnnotationFinder(PackageAdmin packageAdmin, Bundle bundle, ResourceDiscoveryFilter discoveryFilter) throws Exception {
043            this.bundle = bundle;
044            BundleResourceFinder bundleResourceFinder = new BundleResourceFinder(packageAdmin, bundle, "", ".class", discoveryFilter);
045            bundleResourceFinder.find(new AnnotationFindingCallback());
046        }
047    
048        @Override
049        protected URL getResource(String s) {
050            return bundle.getResource(s);
051        }
052    
053        @Override
054        protected Class<?> loadClass(String s) throws ClassNotFoundException {
055            return bundle.loadClass(s);
056        }
057    
058        private class AnnotationFindingCallback implements BundleResourceFinder.ResourceFinderCallback {
059    
060          
061            public void foundInDirectory(Bundle bundle, String baseDir, URL url) throws Exception {
062                InputStream in = url.openStream();
063                try {
064                    readClassDef(in);
065                } finally {
066                    in.close();
067                }
068            }
069    
070           
071            public void foundInJar(Bundle bundle, String jarName, ZipEntry entry, InputStream in) throws Exception {
072                readClassDef(in);
073            }
074        }
075    
076    }