下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.framework.namespace.IdentityNamespace 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static Bundle[] getBundles(String symbolicName, FrameworkWiring fwkWiring) {
BundleContext context = fwkWiring.getBundle().getBundleContext();
if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName)) {
symbolicName = context.getBundle(Constants.SYSTEM_BUNDLE_LOCATION).getSymbolicName();
}
StringBuilder filter = new StringBuilder();
filter.append('(')
.append(IdentityNamespace.IDENTITY_NAMESPACE)
.append('=')
.append(symbolicName)
.append(')');
Map<String, String> directives = Collections.singletonMap(Namespace.REQUIREMENT_FILTER_DIRECTIVE, filter.toString());
Collection<BundleCapability> matchingBundleCapabilities = fwkWiring.findProviders(ModuleContainer.createRequirement(IdentityNamespace.IDENTITY_NAMESPACE, directives, Collections.emptyMap()));
if (matchingBundleCapabilities.isEmpty()) {
return null;
}
Bundle[] results = matchingBundleCapabilities.stream().map(c -> c.getRevision().getBundle())
// Remove all the bundles that are uninstalled
.filter(bundle -> (bundle.getState() & (Bundle.UNINSTALLED)) == 0)
.sorted((b1, b2) -> b2.getVersion().compareTo(b1.getVersion())) // highest version first
.toArray(Bundle[]::new);
return results.length > 0 ? results : null;
}
private void printBundleResources(PrintWriter out, List<Resource> resources, boolean verbose) {
out.println("I Bundle resource");
out.println("- --------------------");
for (Resource r : resources) {
Map<String, Object> identity = r.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).iterator().next().getAttributes();
out.print(isInstalled(r) ? "* " : " ");
out.print(identity.get(IdentityNamespace.IDENTITY_NAMESPACE));
out.print(", version=");
out.println(identity.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE));
if (verbose) {
Map<String, Object> content = r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).iterator().next().getAttributes();
out.println(" Type: " + identity.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE));
out.println(" URL: " + content.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE));
out.println(" Size: " + content.get(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE));
}
}
}
private boolean isInstalled(Resource r) {
Map<String, Object> identity = r.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE).iterator().next().getAttributes();
String name = (String) identity.get(IdentityNamespace.IDENTITY_NAMESPACE);
Version version = (Version) identity.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
Map<String, Object> content = r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE).iterator().next().getAttributes();
Bundle lb = bc.getBundle((String)content.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE));
if (lb != null && name.equals(lb.getSymbolicName()) && version.equals(lb.getVersion())) {
return true;
}
for (Bundle b : bc.getBundles()) {
if (name.equals(b.getSymbolicName()) && version.equals(b.getVersion())) {
return true;
}
}
return false;
}
@Override
String getReqName(final BundleRequirement requirement)
{
final StringBuffer sb = new StringBuffer(50);
final String filter = requirement.getDirectives().get("filter");
final String idName = getFilterValue(filter, IdentityNamespace.IDENTITY_NAMESPACE);
if (idName != null) {
sb.append(idName);
appendVersionAndType(sb, requirement);
} else {
// Filter too complex to extract info from...
sb.append(filter);
}
final BundleWiring reqWiring = requirement.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, reqWiring);
return sb.toString();
}
static int whichNameSpaces(String namespace) {
int ns;
if (namespace == null) {
ns = NS_BUNDLE|NS_HOST|NS_IDENTITY|NS_PACKAGE|NS_OTHER;
} else if (BundleRevision.BUNDLE_NAMESPACE.equals(namespace)) {
ns = NS_BUNDLE;
} else if (BundleRevision.HOST_NAMESPACE.equals(namespace)) {
ns = NS_HOST;
} else if (IdentityNamespace.IDENTITY_NAMESPACE.equals(namespace)) {
ns = NS_IDENTITY;
} else if (NativeNamespace.NATIVE_NAMESPACE.equals(namespace)) {
ns = NS_NATIVE;
} else if (BundleRevision.PACKAGE_NAMESPACE.equals(namespace)) {
ns = NS_PACKAGE;
} else {
ns = NS_OTHER;
}
return ns;
}
/**
* Creates a {@link BundleCapability} from one entry of the Bundle-Capability
* header.
*
* @param gen
* the owning bundle revision.
* @param he
* the parsed bundle capability header entry.
*/
public BundleCapabilityImpl(final BundleGeneration gen, final HeaderEntry he)
{
this.gen = gen;
owner = gen;
namespace = he.getKey();
if (gen.bundle.id != 0) {
for (final String ns : Arrays
.asList(new String[] { BundleRevision.BUNDLE_NAMESPACE,
BundleRevision.HOST_NAMESPACE,
BundleRevision.PACKAGE_NAMESPACE,
ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE,
IdentityNamespace.IDENTITY_NAMESPACE,
NativeNamespace.NATIVE_NAMESPACE})) {
if (ns.equals(namespace)) {
throw new IllegalArgumentException("Capability with name-space '" + ns
+ "' must not be provided in the "
+ Constants.PROVIDE_CAPABILITY
+ " manifest header.");
}
}
}
attributes = Collections.unmodifiableMap(he.getAttributes());
directives = Collections.unmodifiableMap(he.getDirectives());
}
/**
* @see org.osgi.framework.wiring.BundleRevision#getTypes()
* @category BundleRevision
*/
public int getTypes() {
return identity == null ? 0
: IdentityNamespace.TYPE_FRAGMENT
.equals(identity.getAttributes()
.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE))
? BundleRevision.TYPE_FRAGMENT : 0;
}
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);
}
/**
* Get the name of a repository resource from the identity name space."
*/
public static String getResourceName(Resource resource)
{
final List<Capability> idCaps =
resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
if (idCaps.size() != 1) {
Activator.log.error("Found " + idCaps.size()
+ " identity capabilites expected one: " + idCaps);
return resource.toString();
}
final Capability idCap = idCaps.get(0);
return idCap.getAttributes().get(IdentityNamespace.IDENTITY_NAMESPACE)
.toString();
}
/**
* Get version of a repository resource form the identity name space.
*/
public static Version getResourceVersion(Resource resource)
{
final List<Capability> idCaps =
resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
if (idCaps.size() != 1) {
Activator.log.error("Found " + idCaps.size()
+ " identity capabilites expected one: " + idCaps);
return Version.emptyVersion;
}
final Capability idCap = idCaps.get(0);
return (Version) idCap.getAttributes()
.get(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
}
/**
* Get the type of a repository resource from the identity name space."
*
* @param resource
* The resource to get the type for.
*
* @return Type as a string, one of {@link IdentityNamespace#TYPE_BUNDLE},
* {@link IdentityNamespace#TYPE_FRAGMENT}, and
* {@link IdentityNamespace#TYPE_UNKNOWN}.
*/
public static String getResourceType(Resource resource)
{
final List<Capability> idCaps =
resource.getCapabilities(IdentityNamespace.IDENTITY_NAMESPACE);
for (final Capability idCap : idCaps) {
final String type =
(String) idCap.getAttributes()
.get(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE);
if (type != null) {
return type;
}
}
return "—";
}
/**
* Convert a resource type constant to a short string.
*
* @param resourceType
* The type to convert.
* @return A string object with one of the texts "bundle", "fragment" or
* "unknown".
*
*/
public static String resourceTypeToString(String resourceType)
{
if (IdentityNamespace.TYPE_BUNDLE.equals(resourceType)) {
return "bundle";
} else if (IdentityNamespace.TYPE_FRAGMENT.equals(resourceType)) {
return "fragment";
} else if (IdentityNamespace.TYPE_UNKNOWN.equals(resourceType)) {
return "unknown";
}
return "unknown";
}
public int cmdBundle(Dictionary<String,?> opts, Reader in, PrintWriter out,
Session session) {
final boolean verbose = (opts.get("-l") != null);
final String bsn = (String) opts.get("symbolicname");
final String ver = (String) opts.get("versionRange");
BasicRequirement requirement;
if (bsn != null) {
requirement = new BasicRequirement(IdentityNamespace.IDENTITY_NAMESPACE, bsn);
} else {
requirement = new BasicRequirement(IdentityNamespace.IDENTITY_NAMESPACE);
}
if (ver != null) {
requirement.addVersionRangeFilter(new VersionRange(ver));
}
requirement.addBundleIdentityFilter();
List<Resource> resources = new ArrayList<Resource>();
for (Capability c : getRepositoryManager().findProviders(requirement)) {
resources.add(c.getResource());
}
if (resources.isEmpty()) {
out.println("No bundles found!");
return 1;
} else {
printBundleResources(out, resources, verbose);
}
return 0;
}
/**
* Append information about provided capabilities.
*
* @param sb Output buffer.
* @param nameSpace The name space that we present capabilities for.
* @param wiring The wiring that we present capabilities for.
*/
private void appendProvidedCapabilities(final StringBuffer sb,
final String nameSpace,
final BundleWiring wiring)
{
WireFormatter wf;
if (BundleRevision.PACKAGE_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterPackage(nameSpace, wiring);
} else if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterEE(nameSpace, wiring);
} else if (BundleRevision.HOST_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterHost(nameSpace, wiring);
} else if (BundleRevision.BUNDLE_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterBundle(nameSpace, wiring);
} else if (IdentityNamespace.IDENTITY_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterID(nameSpace, wiring);
} else {
wf = new WireFormatter(nameSpace, wiring);
}
// Mapping from capability title to (string) to requesters (strings)
final Map<String,List<String>> cap2requesters
= new TreeMap<String, List<String>>();
wf.providedCapabilitiesView(cap2requesters);
appendCapabilityInfo(sb, cap2requesters);
}
/**
* Append information about required capabilities.
*
* @param sb Output buffer.
* @param nameSpace The name-space that we present requirements for.
* @param wiring The wiring that we present requirements for.
*/
private void appendRequiredCapabilities(final StringBuffer sb,
final String nameSpace,
final BundleWiring wiring)
{
WireFormatter wf;
if (BundleRevision.PACKAGE_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterPackage(nameSpace, wiring);
} else if (ExecutionEnvironmentNamespace.EXECUTION_ENVIRONMENT_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterEE(nameSpace, wiring);
} else if (BundleRevision.HOST_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterHost(nameSpace, wiring);
} else if (BundleRevision.BUNDLE_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterBundle(nameSpace, wiring);
} else if (IdentityNamespace.IDENTITY_NAMESPACE.equals(nameSpace)) {
wf = new WireFormatterID(nameSpace, wiring);
} else {
wf = new WireFormatter(nameSpace, wiring);
}
// Mapping from capability title to (string) to provider (strings)
final Map<String,List<String>> cap2providers
= new TreeMap<String, List<String>>();
wf.requiredCapabilitiesView(cap2providers);
appendCapabilityInfo(sb, cap2providers);
}
/**
* @see org.osgi.framework.wiring.BundleRevision#getSymbolicName()
* @category BundleRevision
*/
public String getSymbolicName() {
return identity == null ? null
: (String) identity.getAttributes()
.get(IdentityNamespace.IDENTITY_NAMESPACE);
}
/**
* @see org.osgi.framework.wiring.BundleRevision#getVersion()
* @category BundleRevision
*/
public Version getVersion() {
return identity == null ? Version.emptyVersion
: (Version) identity.getAttributes().get(
IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE);
}
public void addBundleIdentityFilter() {
String bf = eq(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE, IdentityNamespace.TYPE_BUNDLE);
String ff = eq(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE, IdentityNamespace.TYPE_FRAGMENT);
multiOpFilter('&', multiOp('|', bf, ff));
}
@Override
String getCapName(final BundleCapability capability)
{
// Make a modifiable clone of the attributes.
final Map<String, Object> attrs
= new TreeMap<String, Object>(capability.getAttributes());
final StringBuffer sb = new StringBuffer(50);
sb.append(attrs.remove(IdentityNamespace.IDENTITY_NAMESPACE));
final Version version =
(Version) attrs.remove(Constants.VERSION_ATTRIBUTE);
if (version != null) {
sb.append(" ");
sb.append(version);
}
final String type = (String) attrs.remove("type");
if (type != null) {
sb.append(" ");
sb.append(type);
}
if (!attrs.isEmpty()) {
sb.append(" {");
boolean first = true;
for (final Entry<String,Object> entry :attrs.entrySet()) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(entry.getKey());
sb.append('=');
String value = entry.getValue().toString();
value = Strings.replace(value, "<", "<");
value = Strings.replace(value, ">", ">");
value = WiringHTMLDisplayer.makeLink(value);
sb.append(value);
}
sb.append('}');
}
final BundleWiring capWiring = capability.getRevision().getWiring();
appendPendingRemovalOnRefresh(sb, capWiring);
return sb.toString();
}
/**
* IdentityNamespace capability
*/
public BundleCapabilityImpl(BundleGeneration bg) {
gen = bg;
owner = gen;
namespace = IdentityNamespace.IDENTITY_NAMESPACE;
Map<String,Object> attrs = new HashMap<String, Object>();
attrs.put(IdentityNamespace.IDENTITY_NAMESPACE, gen.symbolicName);
attrs.put(IdentityNamespace.CAPABILITY_TYPE_ATTRIBUTE,
gen.fragment != null ? IdentityNamespace.TYPE_FRAGMENT : IdentityNamespace.TYPE_BUNDLE);
attrs.put(IdentityNamespace.CAPABILITY_VERSION_ATTRIBUTE, gen.version);
if (gen.archive != null) {
String a = gen.archive.getAttribute(Constants.BUNDLE_COPYRIGHT);
if (a != null) {
attrs.put(IdentityNamespace.CAPABILITY_COPYRIGHT_ATTRIBUTE, a);
}
a = gen.archive.getAttribute(Constants.BUNDLE_DESCRIPTION);
if (a != null) {
attrs.put(IdentityNamespace.CAPABILITY_DESCRIPTION_ATTRIBUTE, a);
}
a = gen.archive.getAttribute(Constants.BUNDLE_DOCURL);
if (a != null) {
attrs.put(IdentityNamespace.CAPABILITY_DOCUMENTATION_ATTRIBUTE, a);
}
a = gen.archive.getAttribute("Bundle-License");
if (a != null) {
StringBuffer sb = new StringBuffer();
try {
List<HeaderEntry> lic = Util.parseManifestHeader("Bundle-License", a, true, true, false);
for (HeaderEntry he : lic) {
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(he.getKey());
}
} catch (IllegalArgumentException iae) {
gen.bundle.fwCtx.frameworkInfo(gen.bundle, iae);
sb.append(a);
}
attrs.put(IdentityNamespace.CAPABILITY_LICENSE_ATTRIBUTE, sb.toString());
}
}
attributes = Collections.unmodifiableMap(attrs);
Map<String,String> dirs = new HashMap<String, String>();
dirs.put(Constants.SINGLETON_DIRECTIVE, gen.singleton ? "true" : "false");
// TODO, should we try to find classfiers?
directives = Collections.unmodifiableMap(dirs);
}