Merge "Address @XmlSeeAlso limitation. Provide the ability to inject the JAXB types...
[controller.git] / opendaylight / northbound / bundlescanner / implementation / src / main / java / org / opendaylight / controller / northbound / bundlescanner / internal / BundleInfo.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.northbound.bundlescanner.internal;
10
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;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.regex.Pattern;
20
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.Constants;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * BundleInfo holds information related to the bundle obtained during the
28  * bundle scan process.
29  */
30 /*package*/ class BundleInfo {
31     private static final Logger LOGGER = LoggerFactory.getLogger(BundleInfo.class);
32
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;
37
38     public BundleInfo(Bundle bundle, Map<String, Set<String>> classes) {
39         this.bundle = bundle;
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));
44     }
45
46     @Override
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("}");
54         return sb.toString();
55     }
56
57     public Bundle getBundle() {
58         return bundle;
59     }
60
61     public long getId() {
62         return bundle.getBundleId();
63     }
64
65     public List<Class<?>> getAnnotatedClasses(Pattern pattern) {
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());
70             }
71         }
72         return BundleScanner.loadClasses(bundle, result);
73     }
74
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;
80         }
81         return false;
82     }
83
84     public List<Class<?>> getAnnotatedClasses(
85             Collection<BundleInfo> allbundles,
86             Pattern pattern)
87     {
88         List<Class<?>> classes = getAnnotatedClasses(pattern);
89         processAnnotatedClassesInternal(this, allbundles, pattern,
90                 new HashSet<BundleInfo>(), classes);
91         return classes;
92     }
93
94     private List<String> getExportedAnnotatedClasses(Pattern pattern) {
95         List<String> classes = new ArrayList<String>();
96         for (Map.Entry<String, Set<String>> entry : annotatedClasses.entrySet()) {
97             String cls = entry.getKey();
98             int idx = cls.lastIndexOf(".");
99             String pkg = (idx == -1 ? "" : cls.substring(0, idx));
100             // for a class to match, the package has to be exported and
101             // annotations should match the given pattern
102             if (exportPkgs.contains(pkg) && matches(pattern, entry.getValue())) {
103                 classes.add(cls);
104             }
105         }
106         if (LOGGER.isDebugEnabled()) {
107             LOGGER.debug("Found in bundle:{} exported classes:[{}]",
108                     getBundle().getSymbolicName(), classes);
109         }
110         return classes;
111     }
112
113     private static void processAnnotatedClassesInternal(
114             BundleInfo target,
115             Collection<BundleInfo> bundlesToScan,
116             Pattern pattern,
117             Collection<BundleInfo> visited,
118             List<Class<?>> classes)
119     {
120         for (BundleInfo other : bundlesToScan) {
121             if (other.getId() == target.getId()) continue;
122             if (target.isDependantOn(other)) {
123                 if (!visited.contains(other)) {
124                     classes.addAll(BundleScanner.loadClasses(other.getBundle(),
125                             other.getExportedAnnotatedClasses(pattern)));
126                     visited.add(other);
127                     processAnnotatedClassesInternal(other, bundlesToScan,
128                             pattern, visited, classes);
129                 }
130             }
131         }
132     }
133
134     private boolean isDependantOn(BundleInfo other) {
135         for (String pkg : importPkgs) {
136             if (other.exportPkgs.contains(pkg)) return true;
137         }
138         return false;
139     }
140
141     public List<BundleInfo> getDependencies(Collection<BundleInfo> bundles) {
142         List<BundleInfo> result = new ArrayList<BundleInfo>();
143         for(BundleInfo bundle : bundles) {
144             if (isDependantOn(bundle)) result.add(bundle);
145         }
146         return result;
147     }
148
149
150     private static Set<String> parsePackages(String packageString) {
151         if (packageString == null) return Collections.emptySet();
152         String[] packages = packageString.split(",");
153         Set<String> result = new HashSet<String>();
154         for (int i=0; i<packages.length; i++) {
155             String[] nameAndAttrs = packages[i].split(";");
156             String packageName = nameAndAttrs[0].trim();
157             result.add(packageName);
158         }
159         return result;
160     }
161
162 }