Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.component; |
| 2 | |
| 3 | import org.osgi.service.component.annotations.*; |
| 4 | |
| 5 | import aQute.lib.osgi.*; |
| 6 | import aQute.lib.tag.*; |
| 7 | import aQute.libg.version.*; |
| 8 | |
| 9 | /** |
| 10 | * Holds the information in the reference element. |
| 11 | */ |
| 12 | |
| 13 | class ReferenceDef { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 14 | Version version = AnnotationReader.V1_1; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 15 | String name; |
| 16 | String service; |
| 17 | ReferenceCardinality cardinality; |
| 18 | ReferencePolicy policy; |
| 19 | ReferencePolicyOption policyOption; |
| 20 | String target; |
| 21 | String bind; |
| 22 | String unbind; |
| 23 | String updated; |
| 24 | |
| 25 | /** |
| 26 | * Prepare the reference, will check for any errors. |
| 27 | * |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | * @param analyzer |
| 29 | * the analyzer to report errors to. |
| 30 | * @throws Exception |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 31 | */ |
| 32 | public void prepare(Analyzer analyzer) throws Exception { |
| 33 | if (name == null) |
| 34 | analyzer.error("No name for a reference"); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 35 | |
| 36 | if ((updated != null && !updated.equals("-")) || policyOption != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 37 | version = max(version, AnnotationReader.V1_2); |
| 38 | |
| 39 | if (target != null) { |
| 40 | String error = Verifier.validateFilter(target); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | if (error != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 42 | analyzer.error("Invalid target filter %s for %s", target, name); |
| 43 | } |
| 44 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 45 | if (service == null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 46 | analyzer.error("No interface specified on %s", name); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 47 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Calculate the tag. |
| 52 | * |
| 53 | * @return a tag for the reference element. |
| 54 | */ |
| 55 | public Tag getTag() { |
| 56 | Tag ref = new Tag("reference"); |
| 57 | ref.addAttribute("name", name); |
| 58 | if (cardinality != null) |
| 59 | ref.addAttribute("cardinality", cardinality.toString()); |
| 60 | |
| 61 | if (policy != null) |
| 62 | ref.addAttribute("policy", policy.toString()); |
| 63 | |
| 64 | ref.addAttribute("interface", service); |
| 65 | |
| 66 | if (target != null) |
| 67 | ref.addAttribute("target", target); |
| 68 | |
| 69 | if (bind != null && !"-".equals(bind)) |
| 70 | ref.addAttribute("bind", bind); |
| 71 | |
| 72 | if (unbind != null && !"-".equals(unbind)) |
| 73 | ref.addAttribute("unbind", unbind); |
| 74 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 75 | if (updated != null && !"-".equals(updated)) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 76 | ref.addAttribute("updated", updated); |
| 77 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | if (policyOption != null) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | ref.addAttribute("policy-option", policyOption.toString()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 80 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 81 | return ref; |
| 82 | } |
| 83 | |
| 84 | static <T extends Comparable<T>> T max(T a, T b) { |
| 85 | int n = a.compareTo(b); |
| 86 | if (n >= 0) |
| 87 | return a; |
| 88 | else |
| 89 | return b; |
| 90 | } |
| 91 | |
| 92 | public String toString() { |
| 93 | return name; |
| 94 | } |
| 95 | |
| 96 | } |