blob: f520378c1d22bcc74f6d2f5b806b6a4d10629e13 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.lib.osgi;
2
3import static aQute.lib.osgi.Constants.*;
4
5import java.util.*;
6import java.util.jar.*;
7
8import aQute.libg.header.*;
9import aQute.libg.reporter.*;
10import 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 McCullochbb014372012-06-07 21:57:32 +000016 */
17public 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 McCulloch2286f232012-06-15 13:27:53 +000040 @Override
41 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000042 return attrs.getValue(key);
43 }
44
Stuart McCulloch2286f232012-06-15 13:27:53 +000045 @Override
46 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000047 attrs.putValue(key, value);
48 }
49
Stuart McCulloch2286f232012-06-15 13:27:53 +000050 @Override
51 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000052 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 McCulloch2286f232012-06-15 13:27:53 +000075 @Override
76 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +000077 return processor.getProperty(key);
78 }
79
Stuart McCulloch2286f232012-06-15 13:27:53 +000080 @Override
81 public String get(String key, String deflt) {
Stuart McCullochbb014372012-06-07 21:57:32 +000082 return processor.getProperty(key, deflt);
83 }
84
Stuart McCulloch2286f232012-06-15 13:27:53 +000085 @Override
86 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +000087 processor.setProperty(key, value);
88 }
89
Stuart McCulloch2286f232012-06-15 13:27:53 +000090 @Override
91 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +000092 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 McCulloch2286f232012-06-15 13:27:53 +0000113 public static Domain domain(final Map<String,String> map) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000114 return new Domain() {
115
Stuart McCulloch2286f232012-06-15 13:27:53 +0000116 @Override
117 public String get(String key) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000118 return map.get(key);
119 }
120
Stuart McCulloch2286f232012-06-15 13:27:53 +0000121 @Override
122 public void set(String key, String value) {
Stuart McCullochbb014372012-06-07 21:57:32 +0000123 map.put(key, value);
124 }
125
Stuart McCulloch2286f232012-06-15 13:27:53 +0000126 @Override
127 public Iterator<String> iterator() {
Stuart McCullochbb014372012-06-07 21:57:32 +0000128 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 McCulloch2286f232012-06-15 13:27:53 +0000167 ic.putAll(getParameters(INCLUDERESOURCE));
Stuart McCullochbb014372012-06-07 21:57:32 +0000168 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 McCulloch2286f232012-06-15 13:27:53 +0000244
Stuart McCullochbb014372012-06-07 21:57:32 +0000245 public String getBundleVersion() {
246 return get(BUNDLE_VERSION);
247 }
248
249 public void setBundleVersion(String version) {
250 Version v = new Version(version);
Stuart McCulloch2286f232012-06-15 13:27:53 +0000251 set(BUNDLE_VERSION, v.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000252 }
253
254 public void setBundleVersion(Version version) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000255 set(BUNDLE_VERSION, version.toString());
Stuart McCullochbb014372012-06-07 21:57:32 +0000256 }
257
258 public void setFailOk(boolean b) {
Stuart McCulloch2286f232012-06-15 13:27:53 +0000259 set(FAIL_OK, b + "");
Stuart McCullochbb014372012-06-07 21:57:32 +0000260 }
Stuart McCulloch2286f232012-06-15 13:27:53 +0000261
Stuart McCullochbb014372012-06-07 21:57:32 +0000262 public boolean isFailOk() {
263 return Processor.isTrue(get(FAIL_OK));
264 }
265}