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