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 021package org.apache.xbean.finder; 022 023import java.io.InputStream; 024import java.net.URL; 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030import java.util.zip.ZipEntry; 031 032import org.apache.xbean.osgi.bundle.util.BundleResourceFinder; 033import org.apache.xbean.osgi.bundle.util.BundleUtils; 034import org.apache.xbean.osgi.bundle.util.ResourceDiscoveryFilter; 035import org.osgi.framework.Bundle; 036import org.osgi.service.packageadmin.PackageAdmin; 037 038/** 039 * @version $Rev: 1160131 $ $Date: 2011-08-22 09:07:20 +0200 (Mon, 22 Aug 2011) $ 040 */ 041public class BundleAnnotationFinder extends AbstractFinder { 042 private final Bundle bundle; 043 private final Set<String> paths; 044 045 public BundleAnnotationFinder(PackageAdmin packageAdmin, Bundle bundle) throws Exception { 046 this(packageAdmin, bundle, BundleResourceFinder.FULL_DISCOVERY_FILTER); 047 } 048 049 public BundleAnnotationFinder(PackageAdmin packageAdmin, Bundle bundle, ResourceDiscoveryFilter discoveryFilter) throws Exception { 050 this(packageAdmin, bundle, discoveryFilter, Collections.<String>emptySet()); 051 } 052 053 public BundleAnnotationFinder(PackageAdmin packageAdmin, Bundle bundle, ResourceDiscoveryFilter discoveryFilter, Set<String> paths) throws Exception { 054 this.bundle = BundleUtils.unwrapBundle(bundle); 055 BundleResourceFinder bundleResourceFinder = new BundleResourceFinder(packageAdmin, this.bundle, "", ".class", discoveryFilter); 056 bundleResourceFinder.find(new AnnotationFindingCallback()); 057 this.paths = paths; 058 } 059 060 @Override 061 protected URL getResource(String s) { 062 return bundle.getResource(s); 063 } 064 065 @Override 066 protected Class<?> loadClass(String s) throws ClassNotFoundException { 067 return bundle.loadClass(s); 068 } 069 070 @Override 071 public List<String> getAnnotatedClassNames() { 072 List<String> classNames = new ArrayList<String>(originalInfos.size()); 073 for (Map.Entry<String, ClassInfo> entry: originalInfos.entrySet()) { 074 if (paths.contains(entry.getValue().getPath())) { 075 classNames.add(entry.getKey()); 076 } 077 } 078 return classNames; 079 } 080 081 private class AnnotationFindingCallback implements BundleResourceFinder.ResourceFinderCallback { 082 083 public boolean foundInDirectory(Bundle bundle, String baseDir, URL url) throws Exception { 084 InputStream in = url.openStream(); 085 try { 086 readClassDef(in, baseDir); 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 readClassDef(in, jarName); 096 return true; 097 } 098 } 099 100}