blob: 7dfc8b0d45ffd45abd51c96d470c6e823092218f [file] [log] [blame]
Stuart McCullochf3173222012-06-07 21:57:32 +00001package aQute.bnd.component;
2
3import org.osgi.service.component.annotations.*;
4
5import aQute.lib.osgi.*;
6import aQute.lib.tag.*;
7import aQute.libg.version.*;
8
9/**
10 * Holds the information in the reference element.
11 */
12
13class ReferenceDef {
Stuart McCulloch4482c702012-06-15 13:27:53 +000014 Version version = AnnotationReader.V1_1;
Stuart McCullochf3173222012-06-07 21:57:32 +000015 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 McCulloch4482c702012-06-15 13:27:53 +000028 * @param analyzer
29 * the analyzer to report errors to.
30 * @throws Exception
Stuart McCullochf3173222012-06-07 21:57:32 +000031 */
32 public void prepare(Analyzer analyzer) throws Exception {
33 if (name == null)
34 analyzer.error("No name for a reference");
Stuart McCulloch4482c702012-06-15 13:27:53 +000035
36 if ((updated != null && !updated.equals("-")) || policyOption != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000037 version = max(version, AnnotationReader.V1_2);
38
39 if (target != null) {
40 String error = Verifier.validateFilter(target);
Stuart McCulloch4482c702012-06-15 13:27:53 +000041 if (error != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000042 analyzer.error("Invalid target filter %s for %s", target, name);
43 }
44
Stuart McCulloch4482c702012-06-15 13:27:53 +000045 if (service == null)
Stuart McCullochf3173222012-06-07 21:57:32 +000046 analyzer.error("No interface specified on %s", name);
Stuart McCulloch4482c702012-06-15 13:27:53 +000047
Stuart McCullochf3173222012-06-07 21:57:32 +000048 }
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 McCulloch4482c702012-06-15 13:27:53 +000075 if (updated != null && !"-".equals(updated))
Stuart McCullochf3173222012-06-07 21:57:32 +000076 ref.addAttribute("updated", updated);
77
Stuart McCulloch4482c702012-06-15 13:27:53 +000078 if (policyOption != null)
Stuart McCullochf3173222012-06-07 21:57:32 +000079 ref.addAttribute("policy-option", policyOption.toString());
Stuart McCulloch4482c702012-06-15 13:27:53 +000080
Stuart McCullochf3173222012-06-07 21:57:32 +000081 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}