Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 1 | package aQute.bnd.osgi; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 2 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 3 | import static aQute.bnd.osgi.Constants.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 4 | |
| 5 | import java.util.*; |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 6 | import java.util.Map.Entry; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 7 | import java.util.jar.*; |
| 8 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 9 | import aQute.bnd.header.*; |
Stuart McCulloch | 6a04666 | 2012-07-19 13:11:20 +0000 | [diff] [blame^] | 10 | import aQute.bnd.version.*; |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 11 | import aQute.lib.converter.*; |
Stuart McCulloch | 81d48de | 2012-06-29 19:23:09 +0000 | [diff] [blame] | 12 | import aQute.service.reporter.*; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * This class abstracts domains that have properties holding OSGi meta data. It |
| 16 | * provides access to the keys, the set method and the get method. It then |
| 17 | * provides convenient methods to access these properties via semantic methods. |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 18 | */ |
| 19 | public abstract class Domain implements Iterable<String> { |
| 20 | |
| 21 | public abstract String get(String key); |
| 22 | |
| 23 | public String get(String key, String deflt) { |
| 24 | String result = get(key); |
| 25 | if (result != null) |
| 26 | return result; |
| 27 | return deflt; |
| 28 | } |
| 29 | |
| 30 | public abstract void set(String key, String value); |
| 31 | |
| 32 | public abstract Iterator<String> iterator(); |
| 33 | |
| 34 | public static Domain domain(final Manifest manifest) { |
| 35 | Attributes attrs = manifest.getMainAttributes(); |
| 36 | return domain(attrs); |
| 37 | } |
| 38 | |
| 39 | public static Domain domain(final Attributes attrs) { |
| 40 | return new Domain() { |
| 41 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 42 | @Override |
| 43 | public String get(String key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 44 | return attrs.getValue(key); |
| 45 | } |
| 46 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 47 | @Override |
| 48 | public void set(String key, String value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 49 | attrs.putValue(key, value); |
| 50 | } |
| 51 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 52 | @Override |
| 53 | public Iterator<String> iterator() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 54 | final Iterator<Object> it = attrs.keySet().iterator(); |
| 55 | |
| 56 | return new Iterator<String>() { |
| 57 | |
| 58 | public boolean hasNext() { |
| 59 | return it.hasNext(); |
| 60 | } |
| 61 | |
| 62 | public String next() { |
| 63 | return it.next().toString(); |
| 64 | } |
| 65 | |
| 66 | public void remove() { |
| 67 | it.remove(); |
| 68 | } |
| 69 | }; |
| 70 | } |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | public static Domain domain(final Processor processor) { |
| 75 | return new Domain() { |
| 76 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 77 | @Override |
| 78 | public String get(String key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 79 | return processor.getProperty(key); |
| 80 | } |
| 81 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 82 | @Override |
| 83 | public String get(String key, String deflt) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 84 | return processor.getProperty(key, deflt); |
| 85 | } |
| 86 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 87 | @Override |
| 88 | public void set(String key, String value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 89 | processor.setProperty(key, value); |
| 90 | } |
| 91 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 92 | @Override |
| 93 | public Iterator<String> iterator() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 94 | final Iterator<String> it = processor.getPropertyKeys(true).iterator(); |
| 95 | |
| 96 | return new Iterator<String>() { |
| 97 | String current; |
| 98 | |
| 99 | public boolean hasNext() { |
| 100 | return it.hasNext(); |
| 101 | } |
| 102 | |
| 103 | public String next() { |
| 104 | return current = it.next().toString(); |
| 105 | } |
| 106 | |
| 107 | public void remove() { |
| 108 | processor.getProperties().remove(current); |
| 109 | } |
| 110 | }; |
| 111 | } |
| 112 | }; |
| 113 | } |
| 114 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 115 | public static Domain domain(final Map<String,String> map) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 116 | return new Domain() { |
| 117 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 118 | @Override |
| 119 | public String get(String key) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 120 | return map.get(key); |
| 121 | } |
| 122 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 123 | @Override |
| 124 | public void set(String key, String value) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 125 | map.put(key, value); |
| 126 | } |
| 127 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 128 | @Override |
| 129 | public Iterator<String> iterator() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 130 | return map.keySet().iterator(); |
| 131 | } |
| 132 | }; |
| 133 | } |
| 134 | |
| 135 | public Parameters getParameters(String key, Reporter reporter) { |
| 136 | return new Parameters(get(key), reporter); |
| 137 | } |
| 138 | |
| 139 | public Parameters getParameters(String key) { |
| 140 | return new Parameters(get(key)); |
| 141 | } |
| 142 | |
| 143 | public Parameters getParameters(String key, String deflt) { |
| 144 | return new Parameters(get(key, deflt)); |
| 145 | } |
| 146 | |
| 147 | public Parameters getParameters(String key, String deflt, Reporter reporter) { |
| 148 | return new Parameters(get(key, deflt), reporter); |
| 149 | } |
| 150 | |
| 151 | public Parameters getImportPackage() { |
| 152 | return getParameters(IMPORT_PACKAGE); |
| 153 | } |
| 154 | |
| 155 | public Parameters getExportPackage() { |
| 156 | return getParameters(EXPORT_PACKAGE); |
| 157 | } |
| 158 | |
| 159 | public Parameters getBundleClassPath() { |
| 160 | return getParameters(BUNDLE_CLASSPATH); |
| 161 | } |
| 162 | |
| 163 | public Parameters getPrivatePackage() { |
| 164 | return getParameters(PRIVATE_PACKAGE); |
| 165 | } |
| 166 | |
| 167 | public Parameters getIncludeResource() { |
| 168 | Parameters ic = getParameters(INCLUDE_RESOURCE); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 169 | ic.putAll(getParameters(INCLUDERESOURCE)); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 170 | return ic; |
| 171 | } |
| 172 | |
| 173 | public Parameters getDynamicImportPackage() { |
| 174 | return getParameters(DYNAMICIMPORT_PACKAGE); |
| 175 | } |
| 176 | |
| 177 | public Parameters getExportContents() { |
| 178 | return getParameters(EXPORT_CONTENTS); |
| 179 | } |
| 180 | |
| 181 | public String getBundleActivator() { |
| 182 | return get(BUNDLE_ACTIVATOR); |
| 183 | } |
| 184 | |
| 185 | public void setPrivatePackage(String s) { |
| 186 | if (s != null) |
| 187 | set(PRIVATE_PACKAGE, s); |
| 188 | } |
| 189 | |
| 190 | public void setIncludeResource(String s) { |
| 191 | if (s != null) |
| 192 | set(INCLUDE_RESOURCE, s); |
| 193 | } |
| 194 | |
| 195 | public void setBundleActivator(String s) { |
| 196 | if (s != null) |
| 197 | set(BUNDLE_ACTIVATOR, s); |
| 198 | } |
| 199 | |
| 200 | public void setExportPackage(String s) { |
| 201 | if (s != null) |
| 202 | set(EXPORT_PACKAGE, s); |
| 203 | } |
| 204 | |
| 205 | public void setImportPackage(String s) { |
| 206 | if (s != null) |
| 207 | set(IMPORT_PACKAGE, s); |
| 208 | } |
| 209 | |
| 210 | public void setBundleClasspath(String s) { |
| 211 | if (s != null) |
| 212 | set(BUNDLE_CLASSPATH, s); |
| 213 | } |
| 214 | |
| 215 | public Parameters getBundleClasspath() { |
| 216 | return getParameters(BUNDLE_CLASSPATH); |
| 217 | } |
| 218 | |
| 219 | public void setBundleRequiredExecutionEnvironment(String s) { |
| 220 | if (s != null) |
| 221 | set(BUNDLE_REQUIREDEXECUTIONENVIRONMENT, s); |
| 222 | } |
| 223 | |
| 224 | public Parameters getBundleRequiredExecutionEnvironment() { |
| 225 | return getParameters(BUNDLE_REQUIREDEXECUTIONENVIRONMENT); |
| 226 | } |
| 227 | |
| 228 | public void setSources(boolean b) { |
| 229 | if (b) |
| 230 | set(SOURCES, "true"); |
| 231 | else |
| 232 | set(SOURCES, "false"); |
| 233 | } |
| 234 | |
| 235 | public boolean isSources() { |
| 236 | return Processor.isTrue(get(SOURCES)); |
| 237 | } |
| 238 | |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 239 | public Map.Entry<String,Attrs> getBundleSymbolicName() { |
| 240 | Parameters p = getParameters(BUNDLE_SYMBOLICNAME); |
| 241 | if (p.isEmpty()) |
| 242 | return null; |
| 243 | return p.entrySet().iterator().next(); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | public void setBundleSymbolicName(String s) { |
| 247 | set(BUNDLE_SYMBOLICNAME, s); |
| 248 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 249 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 250 | public String getBundleVersion() { |
| 251 | return get(BUNDLE_VERSION); |
| 252 | } |
| 253 | |
| 254 | public void setBundleVersion(String version) { |
| 255 | Version v = new Version(version); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 256 | set(BUNDLE_VERSION, v.toString()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | public void setBundleVersion(Version version) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 260 | set(BUNDLE_VERSION, version.toString()); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | public void setFailOk(boolean b) { |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 264 | set(FAIL_OK, b + ""); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 265 | } |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 266 | |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 267 | public boolean isFailOk() { |
| 268 | return Processor.isTrue(get(FAIL_OK)); |
| 269 | } |
Stuart McCulloch | 39cc9ac | 2012-07-16 13:43:38 +0000 | [diff] [blame] | 270 | |
| 271 | /** |
| 272 | * Find an icon with the requested size in the list of icons. |
| 273 | * |
| 274 | * @param requestedSize |
| 275 | * the number of pixels desired |
| 276 | * @return null or a the selected URI (which may be relative) |
| 277 | */ |
| 278 | public String getIcon(int requestedSize) throws Exception { |
| 279 | String spec = get(Constants.BUNDLE_ICON); |
| 280 | if (spec == null) |
| 281 | return null; |
| 282 | |
| 283 | Parameters p = OSGiHeader.parseHeader(spec); |
| 284 | int dist = Integer.MAX_VALUE; |
| 285 | String selected = null; |
| 286 | |
| 287 | for (Entry<String,Attrs> e : p.entrySet()) { |
| 288 | String url = e.getKey(); |
| 289 | if (selected == null) |
| 290 | selected = url; |
| 291 | |
| 292 | int size = Converter.cnv(Integer.class, e.getValue().get("size")); |
| 293 | if (size != 0 && Math.abs(requestedSize - size) < dist) { |
| 294 | dist = Math.abs(requestedSize - size); |
| 295 | selected = url; |
| 296 | } |
| 297 | } |
| 298 | return selected; |
| 299 | } |
| 300 | |
| 301 | public void setConditionalPackage(String string) { |
| 302 | set(CONDITIONAL_PACKAGE, string); |
| 303 | |
| 304 | } |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 305 | } |