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