blob: 1054896cc16694dc39e215842057c88b44b99a26 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.bnd.component;
2
3import java.lang.reflect.*;
4import java.util.*;
5
6import org.osgi.service.component.annotations.*;
7
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00008import aQute.bnd.osgi.*;
9import aQute.bnd.osgi.Descriptors.TypeRef;
Stuart McCulloch6a046662012-07-19 13:11:20 +000010import aQute.bnd.version.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000011import aQute.lib.collections.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000012import aQute.lib.tag.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000013
14/**
15 * This class just holds the information for the component, implementation, and
16 * service/provide elements. The {@link #prepare(Analyzer)} method will check if
17 * things are ok and the {@link #getTag()} method returns a tag if the prepare
18 * method returns without any errors. The class uses {@link ReferenceDef} to
19 * hold the references.
20 */
21class ComponentDef {
22 final static String NAMESPACE_STEM = "http://www.osgi.org/xmlns/scr";
23 final List<String> properties = new ArrayList<String>();
Stuart McCulloch2286f232012-06-15 13:27:53 +000024 final MultiMap<String,String> property = new MultiMap<String,String>();
25 final Map<String,ReferenceDef> references = new TreeMap<String,ReferenceDef>();
Stuart McCullochbb014372012-06-07 21:57:32 +000026
27 Version version = AnnotationReader.V1_1;
28 String name;
29 String factory;
30 Boolean immediate;
31 Boolean servicefactory;
32 ConfigurationPolicy configurationPolicy;
33 TypeRef implementation;
34 TypeRef service[];
35 String activate;
36 String deactivate;
37 String modified;
38 Boolean enabled;
39 String xmlns;
40 String configurationPid;
41 List<Tag> propertyTags = new ArrayList<Tag>();
42
43 /**
44 * Called to prepare. If will look for any errors or inconsistencies in the
45 * setup.
46 *
47 * @param analyzer
48 * the analyzer to report errors and create references
49 * @throws Exception
50 */
51 void prepare(Analyzer analyzer) throws Exception {
52
53 for (ReferenceDef ref : references.values()) {
54 ref.prepare(analyzer);
55 if (ref.version.compareTo(version) > 0)
56 version = ref.version;
57 }
58
59 if (implementation == null) {
60 analyzer.error("No Implementation defined for component " + name);
61 return;
62 }
63
64 analyzer.referTo(implementation);
65
66 if (name == null)
67 name = implementation.getFQN();
68
69 if (service != null && service.length > 0) {
70 for (TypeRef interfaceName : service)
71 analyzer.referTo(interfaceName);
72 } else if (servicefactory != null && servicefactory)
73 analyzer.warning("The servicefactory:=true directive is set but no service is provided, ignoring it");
74
75 if (configurationPid != null)
76 version = ReferenceDef.max(version, AnnotationReader.V1_2);
77
Stuart McCulloch2286f232012-06-15 13:27:53 +000078 for (Map.Entry<String,List<String>> kvs : property.entrySet()) {
Stuart McCullochbb014372012-06-07 21:57:32 +000079 Tag property = new Tag("property");
80 String name = kvs.getKey();
81 String type = null;
82 int n = name.indexOf(':');
83 if (n > 0) {
84 type = name.substring(n + 1);
85 name = name.substring(0, n);
86 }
87
88 property.addAttribute("name", name);
89 if (type != null) {
90 property.addAttribute("type", type);
91 }
92 if (kvs.getValue().size() == 1) {
93 String value = kvs.getValue().get(0);
94 value = check(type, value, analyzer);
95 property.addAttribute("value", value);
96 } else {
97 StringBuilder sb = new StringBuilder();
98
99 String del = "";
100 for (String v : kvs.getValue()) {
101 sb.append(del);
102 v = check(type, v, analyzer);
103 sb.append(v);
104 del = "\n";
105 }
106 property.addContent(sb.toString());
107 }
108 propertyTags.add(property);
109 }
110 }
111
112 /**
113 * Returns a tag describing the component element.
114 *
115 * @return a component element
116 */
117 Tag getTag() {
118 Tag component = new Tag("scr:component");
119 if (xmlns != null)
120 component.addAttribute("xmlns:scr", xmlns);
121 else
122 component.addAttribute("xmlns:scr", NAMESPACE_STEM + "/v" + version);
123
124 component.addAttribute("name", name);
125
126 if (servicefactory != null)
127 component.addAttribute("servicefactory", servicefactory);
128
129 if (configurationPolicy != null)
Stuart McCulloch2286f232012-06-15 13:27:53 +0000130 component.addAttribute("configuration-policy", configurationPolicy.toString().toLowerCase());
Stuart McCullochbb014372012-06-07 21:57:32 +0000131
132 if (enabled != null)
133 component.addAttribute("enabled", enabled);
134
135 if (immediate != null)
136 component.addAttribute("immediate", immediate);
137
138 if (factory != null)
139 component.addAttribute("factory", factory);
140
141 if (activate != null)
142 component.addAttribute("activate", activate);
143
144 if (deactivate != null)
145 component.addAttribute("deactivate", deactivate);
146
147 if (modified != null)
148 component.addAttribute("modified", modified);
149
150 if (configurationPid != null)
151 component.addAttribute("configuration-pid", configurationPid);
152
153 Tag impl = new Tag(component, "implementation");
154 impl.addAttribute("class", implementation.getFQN());
155
156 if (service != null && service.length != 0) {
157 Tag s = new Tag(component, "service");
158 if (servicefactory != null && servicefactory)
159 s.addAttribute("servicefactory", true);
160
161 for (TypeRef ss : service) {
162 Tag provide = new Tag(s, "provide");
163 provide.addAttribute("interface", ss.getFQN());
164 }
165 }
166
167 for (ReferenceDef ref : references.values()) {
168 Tag refTag = ref.getTag();
169 component.addContent(refTag);
170 }
171
172 for (Tag tag : propertyTags)
173 component.addContent(tag);
174
175 for (String entry : properties) {
176 Tag properties = new Tag(component, "properties");
177 properties.addAttribute("entry", entry);
178 }
179 return component;
180 }
181
182 private String check(String type, String v, Analyzer analyzer) {
183 if (type == null)
184 return v;
185
186 try {
Stuart McCulloch6a046662012-07-19 13:11:20 +0000187 if ( type.equals("Char"))
188 type = "Character";
189
Stuart McCulloch2286f232012-06-15 13:27:53 +0000190 Class< ? > c = Class.forName("java.lang." + type);
Stuart McCullochbb014372012-06-07 21:57:32 +0000191 if (c == String.class)
192 return v;
193
194 v = v.trim();
195 if (c == Character.class)
196 c = Integer.class;
197 Method m = c.getMethod("valueOf", String.class);
198 m.invoke(null, v);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000199 }
200 catch (ClassNotFoundException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000201 analyzer.error("Invalid data type %s", type);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000202 }
203 catch (NoSuchMethodException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000204 analyzer.error("Cannot convert data %s to type %s", v, type);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000205 }
206 catch (NumberFormatException e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000207 analyzer.error("Not a valid number %s for %s, %s", v, type, e.getMessage());
Stuart McCulloch2286f232012-06-15 13:27:53 +0000208 }
209 catch (Exception e) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000210 analyzer.error("Cannot convert data %s to type %s", v, type);
211 }
212 return v;
213 }
214}