blob: 828f1cf5f0892c520ed1663c9367a8dd683012f1 [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.*;
10import aQute.lib.converter.*;
Stuart McCulloch81d48de2012-06-29 19:23:09 +000011import aQute.service.reporter.*;
Stuart McCullochbb014372012-06-07 21:57:32 +000012
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 McCullochbb014372012-06-07 21:57:32 +000017 */
18public 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 McCulloch2286f232012-06-15 13:27:53 +000041 @Override
42 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000043 return attrs.getValue(key);
44 }
45
Stuart McCulloch2286f232012-06-15 13:27:53 +000046 @Override
47 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000048 attrs.putValue(key, value);
49 }
50
Stuart McCulloch2286f232012-06-15 13:27:53 +000051 @Override
52 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000053 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 McCulloch2286f232012-06-15 13:27:53 +000076 @Override
77 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000078 return processor.getProperty(key);
79 }
80
Stuart McCulloch2286f232012-06-15 13:27:53 +000081 @Override
82 public String get(String key, String deflt) {
Stuart McCullochbb014372012-06-07 21:57:32 +000083 return processor.getProperty(key, deflt);
84 }
85
Stuart McCulloch2286f232012-06-15 13:27:53 +000086 @Override
87 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000088 processor.setProperty(key, value);
89 }
90
Stuart McCulloch2286f232012-06-15 13:27:53 +000091 @Override
92 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000093 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 McCulloch2286f232012-06-15 13:27:53 +0000114 public static Domain domain(final Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000115 return new Domain() {
116
Stuart McCulloch2286f232012-06-15 13:27:53 +0000117 @Override
118 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000119 return map.get(key);
120 }
121
Stuart McCulloch2286f232012-06-15 13:27:53 +0000122 @Override
123 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000124 map.put(key, value);
125 }
126
Stuart McCulloch2286f232012-06-15 13:27:53 +0000127 @Override
128 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +0000129 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 McCulloch2286f232012-06-15 13:27:53 +0000168 ic.putAll(getParameters(INCLUDERESOURCE));
Stuart McCullochbb014372012-06-07 21:57:32 +0000169 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 McCulloch39cc9ac2012-07-16 13:43:38 +0000238 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 McCullochbb014372012-06-07 21:57:32 +0000243 }
244
245 public void setBundleSymbolicName(String s) {
246 set(BUNDLE_SYMBOLICNAME, s);
247 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000248
Stuart McCullochbb014372012-06-07 21:57:32 +0000249 public String getBundleVersion() {
250 return get(BUNDLE_VERSION);
251 }
252
253 public void setBundleVersion(String version) {
254 Version v = new Version(version);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000255 set(BUNDLE_VERSION, v.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000256 }
257
258 public void setBundleVersion(Version version) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000259 set(BUNDLE_VERSION, version.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000260 }
261
262 public void setFailOk(boolean b) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000263 set(FAIL_OK, b + "");
Stuart McCullochbb014372012-06-07 21:57:32 +0000264 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000265
Stuart McCullochbb014372012-06-07 21:57:32 +0000266 public boolean isFailOk() {
267 return Processor.isTrue(get(FAIL_OK));
268 }
Stuart McCulloch39cc9ac2012-07-16 13:43:38 +0000269
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 McCullochbb014372012-06-07 21:57:32 +0000304}