blob: f520378c1d22bcc74f6d2f5b806b6a4d10629e13 [file] [log] [blame]
package aQute.lib.osgi;
import static aQute.lib.osgi.Constants.*;
import java.util.*;
import java.util.jar.*;
import aQute.libg.header.*;
import aQute.libg.reporter.*;
import aQute.libg.version.*;
/**
* This class abstracts domains that have properties holding OSGi meta data. It
* provides access to the keys, the set method and the get method. It then
* provides convenient methods to access these properties via semantic methods.
*/
public abstract class Domain implements Iterable<String> {
public abstract String get(String key);
public String get(String key, String deflt) {
String result = get(key);
if (result != null)
return result;
return deflt;
}
public abstract void set(String key, String value);
public abstract Iterator<String> iterator();
public static Domain domain(final Manifest manifest) {
Attributes attrs = manifest.getMainAttributes();
return domain(attrs);
}
public static Domain domain(final Attributes attrs) {
return new Domain() {
@Override
public String get(String key) {
return attrs.getValue(key);
}
@Override
public void set(String key, String value) {
attrs.putValue(key, value);
}
@Override
public Iterator<String> iterator() {
final Iterator<Object> it = attrs.keySet().iterator();
return new Iterator<String>() {
public boolean hasNext() {
return it.hasNext();
}
public String next() {
return it.next().toString();
}
public void remove() {
it.remove();
}
};
}
};
}
public static Domain domain(final Processor processor) {
return new Domain() {
@Override
public String get(String key) {
return processor.getProperty(key);
}
@Override
public String get(String key, String deflt) {
return processor.getProperty(key, deflt);
}
@Override
public void set(String key, String value) {
processor.setProperty(key, value);
}
@Override
public Iterator<String> iterator() {
final Iterator<String> it = processor.getPropertyKeys(true).iterator();
return new Iterator<String>() {
String current;
public boolean hasNext() {
return it.hasNext();
}
public String next() {
return current = it.next().toString();
}
public void remove() {
processor.getProperties().remove(current);
}
};
}
};
}
public static Domain domain(final Map<String,String> map) {
return new Domain() {
@Override
public String get(String key) {
return map.get(key);
}
@Override
public void set(String key, String value) {
map.put(key, value);
}
@Override
public Iterator<String> iterator() {
return map.keySet().iterator();
}
};
}
public Parameters getParameters(String key, Reporter reporter) {
return new Parameters(get(key), reporter);
}
public Parameters getParameters(String key) {
return new Parameters(get(key));
}
public Parameters getParameters(String key, String deflt) {
return new Parameters(get(key, deflt));
}
public Parameters getParameters(String key, String deflt, Reporter reporter) {
return new Parameters(get(key, deflt), reporter);
}
public Parameters getImportPackage() {
return getParameters(IMPORT_PACKAGE);
}
public Parameters getExportPackage() {
return getParameters(EXPORT_PACKAGE);
}
public Parameters getBundleClassPath() {
return getParameters(BUNDLE_CLASSPATH);
}
public Parameters getPrivatePackage() {
return getParameters(PRIVATE_PACKAGE);
}
public Parameters getIncludeResource() {
Parameters ic = getParameters(INCLUDE_RESOURCE);
ic.putAll(getParameters(INCLUDERESOURCE));
return ic;
}
public Parameters getDynamicImportPackage() {
return getParameters(DYNAMICIMPORT_PACKAGE);
}
public Parameters getExportContents() {
return getParameters(EXPORT_CONTENTS);
}
public String getBundleActivator() {
return get(BUNDLE_ACTIVATOR);
}
public void setPrivatePackage(String s) {
if (s != null)
set(PRIVATE_PACKAGE, s);
}
public void setIncludeResource(String s) {
if (s != null)
set(INCLUDE_RESOURCE, s);
}
public void setBundleActivator(String s) {
if (s != null)
set(BUNDLE_ACTIVATOR, s);
}
public void setExportPackage(String s) {
if (s != null)
set(EXPORT_PACKAGE, s);
}
public void setImportPackage(String s) {
if (s != null)
set(IMPORT_PACKAGE, s);
}
public void setBundleClasspath(String s) {
if (s != null)
set(BUNDLE_CLASSPATH, s);
}
public Parameters getBundleClasspath() {
return getParameters(BUNDLE_CLASSPATH);
}
public void setBundleRequiredExecutionEnvironment(String s) {
if (s != null)
set(BUNDLE_REQUIREDEXECUTIONENVIRONMENT, s);
}
public Parameters getBundleRequiredExecutionEnvironment() {
return getParameters(BUNDLE_REQUIREDEXECUTIONENVIRONMENT);
}
public void setSources(boolean b) {
if (b)
set(SOURCES, "true");
else
set(SOURCES, "false");
}
public boolean isSources() {
return Processor.isTrue(get(SOURCES));
}
public String getBundleSymbolicName() {
return get(BUNDLE_SYMBOLICNAME);
}
public void setBundleSymbolicName(String s) {
set(BUNDLE_SYMBOLICNAME, s);
}
public String getBundleVersion() {
return get(BUNDLE_VERSION);
}
public void setBundleVersion(String version) {
Version v = new Version(version);
set(BUNDLE_VERSION, v.toString());
}
public void setBundleVersion(Version version) {
set(BUNDLE_VERSION, version.toString());
}
public void setFailOk(boolean b) {
set(FAIL_OK, b + "");
}
public boolean isFailOk() {
return Processor.isTrue(get(FAIL_OK));
}
}