2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.northbound.bundlescanner.internal;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Dictionary;
15 import java.util.HashSet;
16 import java.util.List;
19 import java.util.regex.Pattern;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.Constants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * BundleInfo holds information related to the bundle obtained during the
28 * bundle scan process.
30 /*package*/ class BundleInfo {
31 private static final Logger LOGGER = LoggerFactory.getLogger(BundleInfo.class);
33 private final Bundle bundle;
34 private final Map<String, Set<String>> annotatedClasses;
35 private final Set<String> exportPkgs;
36 private final Set<String> importPkgs;
38 public BundleInfo(Bundle bundle, Map<String, Set<String>> classes) {
40 this.annotatedClasses = classes;
41 Dictionary<String, String> dict = bundle.getHeaders();
42 this.importPkgs = parsePackages(dict.get(Constants.IMPORT_PACKAGE));
43 this.exportPkgs = parsePackages(dict.get(Constants.EXPORT_PACKAGE));
47 public String toString() {
48 StringBuilder sb = new StringBuilder(super.toString());
49 sb.append("{name:").append(bundle.getSymbolicName())
50 .append(" id:").append(getId())
51 .append(" annotated-classes:").append(annotatedClasses)
52 .append(" imports:").append(importPkgs)
53 .append(" exports:").append(exportPkgs).append("}");
57 public Bundle getBundle() {
62 return bundle.getBundleId();
65 public List<Class<?>> getAnnotatedClasses(Pattern pattern, Set<String> excludes) {
66 List<String> result = new ArrayList<String>();
67 for (Map.Entry<String, Set<String>> entry : annotatedClasses.entrySet()) {
68 if (matches(pattern, entry.getValue())) {
69 result.add(entry.getKey());
72 return BundleScanner.loadClasses(result, bundle, excludes);
75 private boolean matches(Pattern pattern, Set<String> values) {
76 if (pattern == null) return true;
77 //LOGGER.debug("Matching: {} {}", pattern.toString(), values);
78 for (String s : values) {
79 if (pattern.matcher(s).find()) return true;
85 * Get classes with annotations matching a pattern
87 * @param allbundles - all bundles
88 * @param pattern - annotation pattern to match
89 * @param initBundle - the bundle which initiated this call
90 * @param excludes - set of class names to be excluded
92 * @return list of annotated classes matching the pattern
94 public List<Class<?>> getAnnotatedClasses(
95 Collection<BundleInfo> allbundles,
96 Pattern pattern, Bundle initBundle,
99 List<Class<?>> classes = getAnnotatedClasses(pattern, excludes);
100 processAnnotatedClassesInternal(this, allbundles, pattern,
101 new HashSet<BundleInfo>(), classes, initBundle, excludes);
105 private List<String> getExportedAnnotatedClasses(Pattern pattern) {
106 List<String> classes = new ArrayList<String>();
107 for (Map.Entry<String, Set<String>> entry : annotatedClasses.entrySet()) {
108 String cls = entry.getKey();
109 int idx = cls.lastIndexOf(".");
110 String pkg = (idx == -1 ? "" : cls.substring(0, idx));
111 // for a class to match, the package has to be exported and
112 // annotations should match the given pattern
113 if (exportPkgs.contains(pkg) && matches(pattern, entry.getValue())) {
117 if (LOGGER.isDebugEnabled()) {
118 LOGGER.debug("Found in bundle:{} exported classes:[{}]",
119 getBundle().getSymbolicName(), classes);
124 private static void processAnnotatedClassesInternal(
126 Collection<BundleInfo> bundlesToScan,
128 Collection<BundleInfo> visited,
129 List<Class<?>> classes,
130 Bundle initBundle, Set<String> excludes)
132 for (BundleInfo other : bundlesToScan) {
133 if (other.getId() == target.getId()) continue;
134 if (target.isDependantOn(other)) {
135 if (!visited.contains(other)) {
136 classes.addAll(BundleScanner.loadClasses(
137 other.getExportedAnnotatedClasses(pattern),
138 initBundle, excludes));
140 processAnnotatedClassesInternal(other, bundlesToScan,
141 pattern, visited, classes, initBundle, excludes);
147 private boolean isDependantOn(BundleInfo other) {
148 for (String pkg : importPkgs) {
149 if (other.exportPkgs.contains(pkg)) return true;
154 public List<BundleInfo> getDependencies(Collection<BundleInfo> bundles) {
155 List<BundleInfo> result = new ArrayList<BundleInfo>();
156 for(BundleInfo bundle : bundles) {
157 if (isDependantOn(bundle)) result.add(bundle);
163 private static Set<String> parsePackages(String packageString) {
164 if (packageString == null) return Collections.emptySet();
165 String[] packages = packageString.split(",");
166 Set<String> result = new HashSet<String>();
167 for (int i=0; i<packages.length; i++) {
168 String[] nameAndAttrs = packages[i].split(";");
169 String packageName = nameAndAttrs[0].trim();
170 result.add(packageName);