Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.bnd.component; |
| 2 | |
| 3 | import java.lang.reflect.*; |
| 4 | import java.util.*; |
| 5 | |
| 6 | import org.osgi.service.component.annotations.*; |
| 7 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 8 | import aQute.bnd.osgi.*; |
| 9 | import aQute.bnd.osgi.Descriptors.TypeRef; |
Stuart McCulloch | 6a04666 | 2012-07-19 13:11:20 +0000 | [diff] [blame^] | 10 | import aQute.bnd.version.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 11 | import aQute.lib.collections.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 12 | import aQute.lib.tag.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | |
| 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 | */ |
| 21 | class ComponentDef { |
| 22 | final static String NAMESPACE_STEM = "http://www.osgi.org/xmlns/scr"; |
| 23 | final List<String> properties = new ArrayList<String>(); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 24 | final MultiMap<String,String> property = new MultiMap<String,String>(); |
| 25 | final Map<String,ReferenceDef> references = new TreeMap<String,ReferenceDef>(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 26 | |
| 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 78 | for (Map.Entry<String,List<String>> kvs : property.entrySet()) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 130 | component.addAttribute("configuration-policy", configurationPolicy.toString().toLowerCase()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 131 | |
| 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 McCulloch | 6a04666 | 2012-07-19 13:11:20 +0000 | [diff] [blame^] | 187 | if ( type.equals("Char")) |
| 188 | type = "Character"; |
| 189 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 190 | Class< ? > c = Class.forName("java.lang." + type); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 191 | 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 McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 199 | } |
| 200 | catch (ClassNotFoundException e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 201 | analyzer.error("Invalid data type %s", type); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 202 | } |
| 203 | catch (NoSuchMethodException e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 204 | analyzer.error("Cannot convert data %s to type %s", v, type); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 205 | } |
| 206 | catch (NumberFormatException e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 207 | analyzer.error("Not a valid number %s for %s, %s", v, type, e.getMessage()); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 208 | } |
| 209 | catch (Exception e) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 210 | analyzer.error("Cannot convert data %s to type %s", v, type); |
| 211 | } |
| 212 | return v; |
| 213 | } |
| 214 | } |