blob: 6c64fe0ead5a29ee2545543ff07f0e5a827c7803 [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.component;
2
3import org.osgi.service.component.annotations.*;
4
Stuart McCulloch42151ee2012-07-16 13:43:38 +00005import aQute.bnd.osgi.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00006import aQute.lib.tag.*;
Stuart McCullochf3173222012-06-07 21:57:32 +00007
8/**
9 * Holds the information in the reference element.
10 */
11
12class ReferenceDef {
Stuart McCulloch4482c702012-06-15 13:27:53 +000013 Version version = AnnotationReader.V1_1;
Stuart McCullochf3173222012-06-07 21:57:32 +000014 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 McCulloch4482c702012-06-15 13:27:53 +000027 * @param analyzer
28 * the analyzer to report errors to.
29 * @throws Exception
Stuart McCullochf3173222012-06-07 21:57:32 +000030 */
31 public void prepare(Analyzer analyzer) throws Exception {
32 if (name == null)
33 analyzer.error("No name for a reference");
Stuart McCulloch4482c702012-06-15 13:27:53 +000034
35 if ((updated != null && !updated.equals("-")) || policyOption != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000036 version = max(version, AnnotationReader.V1_2);
37
38 if (target != null) {
39 String error = Verifier.validateFilter(target);
Stuart McCulloch4482c702012-06-15 13:27:53 +000040 if (error != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000041 analyzer.error("Invalid target filter %s for %s", target, name);
42 }
43
Stuart McCulloch4482c702012-06-15 13:27:53 +000044 if (service == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000045 analyzer.error("No interface specified on %s", name);
Stuart McCulloch4482c702012-06-15 13:27:53 +000046
Stuart McCullochf3173222012-06-07 21:57:32 +000047 }
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 McCulloch4482c702012-06-15 13:27:53 +000074 if (updated != null && !"-".equals(updated))
Stuart McCullochf3173222012-06-07 21:57:32 +000075 ref.addAttribute("updated", updated);
76
Stuart McCulloch4482c702012-06-15 13:27:53 +000077 if (policyOption != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000078 ref.addAttribute("policy-option", policyOption.toString());
Stuart McCulloch4482c702012-06-15 13:27:53 +000079
Stuart McCullochf3173222012-06-07 21:57:32 +000080 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 McCulloch669423b2012-06-26 16:34:24 +000087 return b;
Stuart McCullochf3173222012-06-07 21:57:32 +000088 }
89
90 public String toString() {
91 return name;
92 }
93
94}