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