下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.framework.namespace.PackageNamespace 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void getExportedPackages0(final Bundle bundle, final String name, final ArrayList<ExportedPackage> result) {
if (bundle == null) {
throw new IllegalArgumentException("bundle==null");
}
if (result == null) {
throw new IllegalArgumentException("result==null");
}
final List<BundleRevision> revs = bundle.adapt(BundleRevisions.class).getRevisions();
if (revs.isEmpty()) {
return;
}
for (final BundleRevision r : revs) {
final BundleWiring wiring = r.getWiring();
if (wiring != null && wiring.isInUse()) {
for (final Capability cap : wiring.getCapabilities(PackageNamespace.PACKAGE_NAMESPACE)) {
if (name == null || name.equals(cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE))) {
result.add(new ExportedPackageImpl((BundleCapability) cap));
}
}
}
}
}
/**
* @see org.osgi.service.packageadmin.ExportedPackage#getImportingBundles()
*/
public Bundle[] getImportingBundles() {
final ArrayList<Bundle> result = new ArrayList<Bundle>();
final BundleWiring wiring = cap.getResource().getWiring();
if (wiring != null) {
final List<BundleWire> wires = wiring.getProvidedWires(PackageNamespace.PACKAGE_NAMESPACE);
for (final BundleWire wire : wires) {
if (wire.getCapability().equals(cap)) {
final Bundle b = wire.getRequirer().getBundle();
if (b != cap.getResource().getBundle()) {
result.add(wire.getRequirer().getBundle());
}
}
}
addRequiringBundles(wiring, result);
}
return toArrayOrNull(result, Bundle.class);
}
private void exportSystemBundlePackages(final String[] pkgs)
throws BundleException {
for (final String pkg : pkgs) {
final String[] literals = Utils.splitString(pkg, ';');
if (literals.length > 0) {
final ParseResult parseResult = Utils.parseLiterals(literals,
1);
final HashMap<String, Object> attrs = parseResult
.getAttributes();
attrs.put(PackageNamespace.PACKAGE_NAMESPACE,
literals[0].trim());
systemBundleCapabilities.add(new BundleCapabilityImpl(this,
PackageNamespace.PACKAGE_NAMESPACE,
parseResult.getDirectives(), attrs,
Constants.EXPORT_PACKAGE + ' ' + pkg));
}
}
}
BundleCapabilityImpl(final BundleRevision revision, final String str)
throws BundleException {
super(str);
this.revision = revision;
this.prettyPrint = null;
final String excludeStr = getDirectives().get(
PackageNamespace.CAPABILITY_EXCLUDE_DIRECTIVE);
if (excludeStr == null) {
// TODO: couldn't we stop here???
excludes = new String[1];
excludes[0] = "";
hasExcludes = false;
} else {
excludes = Utils.splitString(Utils.unQuote(excludeStr), ',');
hasExcludes = true;
}
final String includeStr = getDirectives().get(
PackageNamespace.CAPABILITY_INCLUDE_DIRECTIVE);
if (includeStr == null) {
includes = new String[1];
includes[0] = "*";
} else {
includes = Utils.splitString(Utils.unQuote(excludeStr), ',');
}
}
public BundleCapabilityImpl(final BundleRevision revision,
final String namespace, final Map<String, String> directives,
final Map<String, Object> attributes, final String prettyPrint) {
super(namespace, directives, attributes);
this.revision = revision;
this.prettyPrint = prettyPrint;
final String excludeStr = getDirectives().get(
PackageNamespace.CAPABILITY_EXCLUDE_DIRECTIVE);
if (excludeStr == null) {
// TODO: couldn't we stop here???
excludes = new String[1];
excludes[0] = "";
hasExcludes = false;
} else {
excludes = Utils.splitString(Utils.unQuote(excludeStr), ',');
hasExcludes = true;
}
final String includeStr = getDirectives().get(
PackageNamespace.CAPABILITY_INCLUDE_DIRECTIVE);
if (includeStr == null) {
includes = new String[1];
includes[0] = "*";
} else {
includes = Utils.splitString(Utils.unQuote(includeStr), ',');
}
if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace)) {
if (attributes
.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE) == null) {
attributes.put(
PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE,
Version.emptyVersion);
}
}
}
HashMap<String, BundleWire> getPackageImportWires() {
final List<BundleWire> list = getRequiredWires(PackageNamespace.PACKAGE_NAMESPACE);
final HashMap<String, BundleWire> result = new HashMap<String, BundleWire>();
if (list != null) {
for (final BundleWire wire : list) {
result.put((String) wire.getCapability().getAttributes()
.get(PackageNamespace.PACKAGE_NAMESPACE), wire);
}
}
return result;
}
void refresh() {
// iterate over old and current revisions
for (final BundleRevision brev : revisions) {
final Revision rev = (Revision) brev;
if (rev.wiring != null) {
rev.wiring.cleanup();
rev.wiring = null;
}
}
revisions.clear();
if (currentRevision != null) {
revisions.add(currentRevision);
// detach fragments (if any) and reset classloader
currentRevision.refresh();
// remove from framework wirings
framework.wirings.remove(currentRevision);
// clear and restore dynamic imports
currentRevision.dynamicImports.clear();
for (final BundleRequirement req : currentRevision.requirements
.lookup(PackageNamespace.PACKAGE_NAMESPACE)) {
if (PackageNamespace.RESOLUTION_DYNAMIC
.equals(req.getDirectives().get(
Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE))) {
currentRevision.dynamicImports.add(req);
}
}
}
}
private HashSet<String> createExportIndex() {
final List<BundleCapability> packageReqs = capabilities
.get(PackageNamespace.PACKAGE_NAMESPACE);
final HashSet<String> index = new HashSet<String>();
if (packageReqs != null) {
for (final BundleCapability req : packageReqs) {
index.add((String) req.getAttributes()
.get(PackageNamespace.PACKAGE_NAMESPACE));
}
}
return index;
}
public int compare(final Capability c1, final Capability c2) {
if (!(c1 instanceof BundleCapability
&& c2 instanceof BundleCapability)) {
return 0;
}
final BundleCapability cap1 = (BundleCapability) c1;
final BundleCapability cap2 = (BundleCapability) c2;
final int cap1Resolved = cap1.getResource().getWiring() == null ? 0
: 1;
final int cap2Resolved = cap2.getResource().getWiring() == null ? 0
: 1;
int score = cap2Resolved - cap1Resolved;
if (score != 0) {
return score;
}
final Version cap1Version = (Version) cap1.getAttributes()
.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
final Version cap2Version = (Version) cap2.getAttributes()
.get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
score = cap2Version.compareTo(cap1Version);
if (score != 0) {
return score;
}
final long cap1BundleId = cap1.getRevision().getBundle()
.getBundleId();
final long cap2BundleId = cap2.getRevision().getBundle()
.getBundleId();
return (int) (cap1BundleId - cap2BundleId);
}
private void hostFragment(final ResolveContext context,
final BundleRevision fragment, final BundleRevision host,
final MultiMap<Resource, Wire> solution) {
// host the capabilities
for (final Capability cap : fragment.getCapabilities(null)) {
if (!IdentityNamespace.IDENTITY_NAMESPACE
.equals(cap.getNamespace())) {
final HostedBundleCapability hostedCap = new HostedBundleCapability(
host, cap);
context.insertHostedCapability(
new ArrayList<Capability>(host.getCapabilities(
PackageNamespace.PACKAGE_NAMESPACE)),
hostedCap);
}
}
// create host wire
final Capability hostCapability = host
.getCapabilities(HostNamespace.HOST_NAMESPACE).get(0);
final Requirement hostRequirement = fragment
.getRequirements(HostNamespace.HOST_NAMESPACE).get(0);
final Wire wire = Resources.createWire(hostCapability,
hostRequirement);
solution.insert(fragment, wire);
solution.insert(host, wire);
}
/**
* @see org.osgi.service.packageadmin.ExportedPackage#getName()
*/
public String getName() {
return (String) cap.getAttributes().get(PackageNamespace.PACKAGE_NAMESPACE);
}
/**
* @see org.osgi.service.packageadmin.ExportedPackage#getVersion()
*/
public Version getVersion() {
return (Version) cap.getAttributes().get(PackageNamespace.CAPABILITY_VERSION_ATTRIBUTE);
}
WovenClassImpl(final String clazzName, final byte[] bytes,
final Revision revision, final ProtectionDomain domain) {
this.bytes = bytes;
this.clazzName = clazzName;
this.dynamicImportRequirements = new ArrayList<BundleRequirement>();
this.dynamicImports = new ArrayList<String>() {
/**
*
*/
private static final long serialVersionUID = 975783807443126126L;
@Override
public boolean add(final String dynImport) {
checkDynamicImport(dynImport);
return super.add(dynImport);
}
@Override
public boolean addAll(
final Collection<? extends String> c) {
for (final String dynImport : c) {
checkDynamicImport(dynImport);
}
return super.addAll(c);
}
private void checkDynamicImport(final String dynImport)
throws IllegalArgumentException {
try {
final String[] literals = Utils
.splitString(dynImport, ';');
if (literals[0].contains(";")) {
throw new IllegalArgumentException(dynImport);
}
final ParseResult parseResult = Utils
.parseLiterals(literals, 1);
final HashMap<String, String> dirs = parseResult
.getDirectives();
dirs.put(Namespace.REQUIREMENT_FILTER_DIRECTIVE,
Utils.createFilter(
PackageNamespace.PACKAGE_NAMESPACE,
literals[0],
parseResult.getLatter()));
dirs.put(Namespace.REQUIREMENT_RESOLUTION_DIRECTIVE,
PackageNamespace.RESOLUTION_DYNAMIC);
dirs.put(Concierge.DIR_INTERNAL, literals[0]);
final BundleRequirement req = new BundleRequirementImpl(
revision,
PackageNamespace.PACKAGE_NAMESPACE, dirs,
null, Constants.DYNAMICIMPORT_PACKAGE + ' '
+ dynImport);
dynamicImportRequirements.add(req);
} catch (final BundleException be) {
throw new IllegalArgumentException(
"Unvalid dynamic import " + dynImport);
}
}
};
this.domain = domain;
}
public static String createFilter(final String namespace, final String req,
final Map<String, Object> attributes) throws BundleException {
final Object version = attributes.get(Constants.VERSION_ATTRIBUTE);
if (PackageNamespace.PACKAGE_NAMESPACE.equals(namespace)) {
if (version != null
&& attributes.containsKey(SPECIFICATION_VERSION)) {
if (!new Version(Utils.unQuote((String) attributes
.get(SPECIFICATION_VERSION))).equals(version)) {
throw new BundleException(
"both version and specification-version are given for the import "
+ req);
} else {
attributes.remove(SPECIFICATION_VERSION);
}
}
}
final StringBuffer buffer = new StringBuffer();
buffer.append('(');
buffer.append(namespace);
buffer.append('=');
buffer.append(req);
buffer.append(')');
if (attributes.size() == 0) {
return buffer.toString();
}
buffer.insert(0, "(&");
for (final Map.Entry<String, Object> attribute : attributes.entrySet()) {
final String key = attribute.getKey();
final Object value = attribute.getValue();
if (Constants.VERSION_ATTRIBUTE.equals(key)
|| Constants.BUNDLE_VERSION_ATTRIBUTE.equals(key)) {
if (value instanceof String) {
final VersionRange range = new VersionRange(
Utils.unQuote((String) value));
if (range.getRight() == null) {
buffer.append('(');
buffer.append(key);
buffer.append(">=");
buffer.append(range.getLeft());
buffer.append(')');
} else {
boolean open = range.getLeftType() == VersionRange.LEFT_OPEN;
buffer.append(open ? "(!(" : "(");
buffer.append(key);
buffer.append(open ? "<=" : ">=");
buffer.append(range.getLeft());
buffer.append(open ? "))" : ")");
open = range.getRightType() == VersionRange.RIGHT_OPEN;
buffer.append(open ? "(!(" : "(");
buffer.append(key);
buffer.append(open ? ">=" : "<=");
buffer.append(range.getRight());
buffer.append(open ? "))" : ")");
}
} else {
buffer.append('(');
buffer.append(key);
buffer.append(">=");
buffer.append(value);
buffer.append(')');
}
continue;
}
buffer.append("(");
buffer.append(key);
buffer.append("=");
buffer.append(value);
buffer.append(")");
}
buffer.append(")");
return buffer.toString();
}