blob: bf7a31bcc4cd47726313421810cf04005f8724d9 [file] [log] [blame]
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00001package aQute.bnd.osgi;
Stuart McCullochbb014372012-06-07 21:57:32 +00002
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00003import static aQute.bnd.osgi.Constants.*;
Stuart McCullochbb014372012-06-07 21:57:32 +00004
5import java.util.*;
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00006import java.util.Map.Entry;
Stuart McCullochbb014372012-06-07 21:57:32 +00007import java.util.jar.*;
8
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +00009import aQute.bnd.header.*;
Stuart McCulloch6a046662012-07-19 13:11:20 +000010import aQute.bnd.version.*;
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +000011import aQute.lib.converter.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +000012import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000013
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 McCullochbb014372012-06-07 21:57:32 +000018 */
19public 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 McCulloch2286f232012-06-15 13:27:53 +000042 @Override
43 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000044 return attrs.getValue(key);
45 }
46
Stuart McCulloch2286f232012-06-15 13:27:53 +000047 @Override
48 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000049 attrs.putValue(key, value);
50 }
51
Stuart McCulloch2286f232012-06-15 13:27:53 +000052 @Override
53 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000054 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 McCulloch2286f232012-06-15 13:27:53 +000077 @Override
78 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000079 return processor.getProperty(key);
80 }
81
Stuart McCulloch2286f232012-06-15 13:27:53 +000082 @Override
83 public String get(String key, String deflt) {
Stuart McCullochbb014372012-06-07 21:57:32 +000084 return processor.getProperty(key, deflt);
85 }
86
Stuart McCulloch2286f232012-06-15 13:27:53 +000087 @Override
88 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000089 processor.setProperty(key, value);
90 }
91
Stuart McCulloch2286f232012-06-15 13:27:53 +000092 @Override
93 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000094 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 McCulloch2286f232012-06-15 13:27:53 +0000115 public static Domain domain(final Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000116 return new Domain() {
117
Stuart McCulloch2286f232012-06-15 13:27:53 +0000118 @Override
119 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000120 return map.get(key);
121 }
122
Stuart McCulloch2286f232012-06-15 13:27:53 +0000123 @Override
124 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000125 map.put(key, value);
126 }
127
Stuart McCulloch2286f232012-06-15 13:27:53 +0000128 @Override
129 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +0000130 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 McCulloch2286f232012-06-15 13:27:53 +0000169 ic.putAll(getParameters(INCLUDERESOURCE));
Stuart McCullochbb014372012-06-07 21:57:32 +0000170 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 McCulloch39cc9ac2012-07-16 13:43:38 +0000239 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 McCullochbb014372012-06-07 21:57:32 +0000244 }
245
246 public void setBundleSymbolicName(String s) {
247 set(BUNDLE_SYMBOLICNAME, s);
248 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000249
Stuart McCullochbb014372012-06-07 21:57:32 +0000250 public String getBundleVersion() {
251 return get(BUNDLE_VERSION);
252 }
253
254 public void setBundleVersion(String version) {
255 Version v = new Version(version);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000256 set(BUNDLE_VERSION, v.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000257 }
258
259 public void setBundleVersion(Version version) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000260 set(BUNDLE_VERSION, version.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000261 }
262
263 public void setFailOk(boolean b) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000264 set(FAIL_OK, b + "");
Stuart McCullochbb014372012-06-07 21:57:32 +0000265 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000266
Stuart McCullochbb014372012-06-07 21:57:32 +0000267 public boolean isFailOk() {
268 return Processor.isTrue(get(FAIL_OK));
269 }
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +0000270
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 McCullochbb014372012-06-07 21:57:32 +0000305}