org.osgi.framework.wiring.BundleRequirement#org.osgi.resource.Resource源码实例Demo

下面列出了org.osgi.framework.wiring.BundleRequirement#org.osgi.resource.Resource 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: concierge   文件: Concierge.java
protected void filterResources(final Collection<ResolverHook> hooks,
		final Collection<Resource> resources,
		final Collection<Resource> removed) {
	final ArrayList<BundleRevision> revisions = new ArrayList<BundleRevision>();
	removed.addAll(resources);
	for (final Iterator<Resource> iter = resources.iterator(); iter
			.hasNext();) {
		final Resource res = iter.next();
		if (res instanceof BundleRevision) {
			revisions.add((BundleRevision) res);
			iter.remove();
		}
	}

	final ConciergeCollections.RemoveOnlyList<BundleRevision> filteredResources = new ConciergeCollections.RemoveOnlyList<BundleRevision>(
			revisions);

	for (final ResolverHook hook : resolver.hooks.keySet()) {
		hook.filterResolvable(filteredResources);
	}

	resources.addAll(filteredResources);
	removed.removeAll(filteredResources);
}
 
源代码2 项目: concierge   文件: Concierge.java
public synchronized Map<Resource, List<Wire>> resolve(
		final ResolveContext context) throws ResolutionException {
	if (context == null) {
		throw new IllegalArgumentException("context is null");
	}

	final MultiMap<Resource, Wire> solution = new MultiMap<Resource, Wire>();
	final ArrayList<Requirement> unresolvedRequirements = new ArrayList<Requirement>();
	final ArrayList<Resource> unresolvedResources = new ArrayList<Resource>();

	resolve0(context, solution, unresolvedRequirements,
			unresolvedResources, true);

	if (!unresolvedRequirements.isEmpty()
			|| !unresolvedResources.isEmpty()) {
		throw new ResolutionException("Could not resolve.", null,
				unresolvedRequirements);
	}

	return solution.getFlatMap();
}
 
源代码3 项目: knopflerfish.org   文件: Util.java
/**
 * Get the MIME-type of a repository resource.
 *
 * @param resource
 *          The resource to get the MIME type for.
 *
 * @return Resource MIME type or {@code null} if no MIME-type available.
 */
public static String getResourceMime(Resource resource)
{
  String res = null;
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final String mime =
      (String) attrs.get(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE);
    if (mime != null) {
      res = mime;
      break;
    }
  }
  return res;
}
 
源代码4 项目: knopflerfish.org   文件: Util.java
/**
 * Get the location to use when installing this resource.
 *
 * If available, the resource URL will be used as the location. Otherwise we
 * simply use the hash code of the resource.
 *
 * @param resource
 *          the resource to determine the installation location for.
 * @return location to use when installing this resource or {@code null} if
 *         location is available for this resource.
 */
public static String getLocation(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    if (supportedMimeTypes.contains(attrs
        .get(ContentNamespace.CAPABILITY_MIME_ATTRIBUTE))) {
      final String url =
        (String) attrs.get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
      if (url != null) {
        return url;
      }
    }
  }
  return null;
}
 
源代码5 项目: knopflerfish.org   文件: RepositoryCommandGroup.java
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));
    }
  }
}
 
源代码6 项目: knopflerfish.org   文件: RepositoryCommandGroup.java
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;
}
 
源代码7 项目: knopflerfish.org   文件: ResolveContextImpl.java
private void addToProvidersIfMatching(Resource res, List<Capability> providers, Requirement req) {
	String f = req.getDirectives().get(Namespace.REQUIREMENT_FILTER_DIRECTIVE);
	Filter filter = null;
	if(f != null) {
	  try {
	    filter = bc.createFilter(f);
	  } catch (InvalidSyntaxException e) {
	    // TODO log filter failure, skip
	    System.err.println("Failed, " + f + ". " + e);
	    return;
	  }
	}
	for(Capability c : res.getCapabilities(req.getNamespace())) {
	  if(filter != null && !filter.matches(c.getAttributes())) {
	    continue;
	  }
	  providers.add(c);
	}
}
 
源代码8 项目: knopflerfish.org   文件: ResolveContextImpl.java
@Override
public int insertHostedCapability(List<Capability> caps,
		HostedCapability hc) {
	List<Resource> resources = explicitResources;
	int index = resources.indexOf(hc.getResource());
	for (int i = 0; i < caps.size(); ++i) {
		Capability c = caps.get(i); 
		int otherIndex = resources.indexOf(c.getResource());
		if (otherIndex > index ) {
			caps.add(i, hc);
			return i;
		}
	}
	caps.add(hc);
	return caps.size()-1;
}
 
源代码9 项目: concierge   文件: Concierge.java
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);
}
 
源代码10 项目: knopflerfish.org   文件: RepositoryDisplayer.java
/**
 * If possible, get the bundle for a resource.
 *
 * @param resource
 *          the resource to search for
 * @return an installed bundle, if the resource seem to be installed
 */
static Bundle getBundle(Resource resource)
{
  final Bundle[] bl = bc.getBundles();

  for (int i = 0; bl != null && i < bl.length; i++) {
    if (Util.isBundleFromResource(bl[i], resource)) {
      return bl[i];
    }
  }
  return null;
}
 
源代码11 项目: knopflerfish.org   文件: RepositoryDisplayer.java
/**
 * Return the resource that the given bundle originates from is any.
 */
Resource getResource(Bundle b)
{
  for (final RepositoryNode rn : locationMap.values()) {
    final Resource resource = rn.getResource();
    final String rLoc = Util.getLocation(resource);
    if (b.getLocation().equals(rLoc)) {
      return resource;
    }
  }
  return null;
}
 
源代码12 项目: knopflerfish.org   文件: RepositoryDisplayer.java
@Override
public int compare(Resource r1, Resource r2)
{
  final String s1 = Util.getResourceName(r1).toLowerCase();
  final String s2 = Util.getResourceName(r2).toLowerCase();
  int n = 0;

  try {
    n = s1.compareTo(s2);
    if (n == 0) {
      final Version v1 = Util.getResourceVersion(r1);
      final Version v2 = Util.getResourceVersion(r2);
      n = v1.compareTo(v2);

      if (n == 0) {
        final String loc1 = Util.getLocation(r1);
        final String loc2 = Util.getLocation(r2);
        if (loc1 != null && loc2 != null) {
          n = loc1.compareTo(loc2);
        } else if (loc1 == null) {
          n = -1;
        } else {
          n = loc2 == null ? 0 : 1;
        }
      }
    }
  } catch (final Exception e) {
  }
  return n;
}
 
源代码13 项目: knopflerfish.org   文件: Util.java
/**
 * 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();
}
 
源代码14 项目: knopflerfish.org   文件: Util.java
/**
 * 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);
}
 
源代码15 项目: knopflerfish.org   文件: Util.java
/**
 * 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 "&mdash;";
}
 
源代码16 项目: knopflerfish.org   文件: Util.java
/**
 * Get the size in bytes of a repository resource.
 *
 * @param resource
 *          The resource to get the size of.
 *
 * @return Resource size in byte or {@code -1} if no size available.
 */
public static long getResourceSize(Resource resource)
{
  long res = -1;
  for (final Capability cap : resource
      .getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Long size =
      (Long) attrs.get(ContentNamespace.CAPABILITY_SIZE_ATTRIBUTE);
    if (size != null) {
      res = size.longValue();
    }
  }
  return res;
}
 
源代码17 项目: knopflerfish.org   文件: Util.java
/**
 * Get the category of a repository resource.
 *
 * The category is specified as the value of the bundle manifest entry named
 * {@link Constants#BUNDLE_CATEGORY}.
 *
 * @param resource
 *          The resource to get the category for.
 *
 * @return Resource category or {@code "[no category]"} if no category is available.
 */
public static String getResourceCategory(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("category");
    if (val != null) {
      return (String) val;
    }
  }
  return "[no category]";
}
 
源代码18 项目: knopflerfish.org   文件: Util.java
/**
 * Get the vendor of a repository resource.
 *
 * The vendor is specified as the value of the bundle manifest entry named
 * {@link Constants#BUNDLE_VENDOR}.
 *
 * @param resource
 *          The resource to get the vendor for.
 *
 * @return Resource vendor or {@code "[no vendor]"} if vendor information is
 *         not available.
 */
public static String getResourceVendor(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("vendor");
    if (val != null) {
      return (String) val;
    }
  }
  return "[no vendor]";
}
 
源代码19 项目: knopflerfish.org   文件: Util.java
/**
 * Get the icon for a repository resource.
 *
 * The icon is specified as the value of the bundle manifest entry named
 * {@code Bundle-Icon}.
 *
 * @param resource
 *          The resource to get the icon for.
 *
 * @return Resource icon string or {@code null} if icon information is
 *         not available.
 */
public static String getResourceIcon(Resource resource)
{
  for (final Capability cap : resource
      .getCapabilities(KF_EXTRAS_NAMESPACE)) {
    final Map<String, Object> attrs = cap.getAttributes();
    final Object val = attrs.get("icon");
    if (val != null) {
      return (String) val;
    }
  }
  return null;
}
 
源代码20 项目: knopflerfish.org   文件: RepositoryImpl.java
@Override
public Promise<Collection<Resource>> findProviders(RequirementExpression expression) {
  Deferred<Collection<Resource>> d = new Deferred<Collection<Resource>>();
  try {
    new ExpressionResolver(this, expression, d).start();
  } catch (Exception e) {
    d.fail(e);
  }
  return d.getPromise();
}
 
源代码21 项目: knopflerfish.org   文件: RepositoryXmlParser.java
private static ParseResult parseRepository(XmlPullParser p)
    throws XmlPullParserException, IOException, Exception {
  if(!"repository".equals(p.getName())) return null;
  
  ParseResult pr = new ParseResult();
  
  for(int i = 0; i < p.getAttributeCount(); ++i) {
    if("name".equalsIgnoreCase(p.getAttributeName(i))) {
      pr.name = p.getAttributeValue(i);
      continue;
    }
    if("increment".equalsIgnoreCase(p.getAttributeName(i))) {
      pr.increment = Long.parseLong(p.getAttributeValue(i).trim());
      continue;
    }
  }
  
  for (int  e = p.getEventType();
            e != XmlPullParser.END_DOCUMENT;
            e = p.next()) {
    Resource r = null;
    if (e == XmlPullParser.START_TAG) {
      r = parseResource(p);
    }
    if (r != null) {
      pr.resources.add(r);
    }
  }
  return pr;
}
 
源代码22 项目: knopflerfish.org   文件: RepositoryXmlParser.java
private static void ensureOsgiContentUrlsAreAbsolute(URL repositoryUrl, Collection<Resource> rs) throws Exception {
  for(Resource r : rs) {
    for(Capability c : r.getCapabilities(ContentNamespace.CONTENT_NAMESPACE)) {
      String url = (String)c.getAttributes().get(ContentNamespace.CAPABILITY_URL_ATTRIBUTE);
      url = new URL(repositoryUrl, url).toExternalForm();
      c.getAttributes().put(ContentNamespace.CAPABILITY_URL_ATTRIBUTE, url);
    }
  }
}
 
源代码23 项目: knopflerfish.org   文件: RepositoryXmlParser.java
private static Resource parseResource(XmlPullParser p) throws Exception {
  if ("resource".equalsIgnoreCase(p.getName())) {
    return parseReqsAndCaps(p);
  } else {
    return null;
  }
}
 
源代码24 项目: knopflerfish.org   文件: RepositoryXmlParser.java
private static Resource parseReqsAndCaps(XmlPullParser p) throws Exception {
  if (p.isEmptyElementTag()) {
    p.next();
    return null;
  }
  ResourceImpl r = new ResourceImpl();
  int startDepth = p.getDepth();
  p.next();
  while( !(p.getEventType() == XmlPullParser.END_DOCUMENT)
      && !(p.getEventType() == XmlPullParser.END_TAG 
      && p.getDepth() == startDepth)) {
    if (p.getEventType() != XmlPullParser.START_TAG) {
      p.next();
      continue;
    } 
    if ("requirement".equalsIgnoreCase(p.getName())) {
      RequirementImpl req = parseReq(p);
      if(req != null) {
        req.d.resource = r;
        r.addReq(req);
      }
      p.next();
      continue;
    }
    if ("capability".equalsIgnoreCase(p.getName())) {
      CapabilityImpl cap = parseCap(p);
      if(cap != null) {
        cap.d.resource = r;
        r.addCap(cap);
      }
      p.next();
      continue;
    }
  }
  return r;
}
 
源代码25 项目: knopflerfish.org   文件: RepositoryXmlParser.java
public static void debug(ParseResult pr) {
  System.out.println("======= BEGIN PARSED REPOSITORY XML ======="); 
  for(Resource r : pr.resources) {
   System.out.println(r.toString()); 
  }
  System.out.println("======== END PARSED REPOSITORY XML ========");
  System.out.println("======== NAME: " + pr.name);
  System.out.println("======== INCREMENT: " + pr.increment);
  System.out.println("======== RESOURCES FOUND: " + pr.resources.size());
  System.out.flush();
}
 
源代码26 项目: knopflerfish.org   文件: ResourceImpl.java
public boolean equals(Object other) {
  if (this == other)
    return true;
  if (other == null)
    return false;
  if (!(other instanceof Resource))
    return false;
  Resource r = (Resource) other;
  return getCapabilities(null).equals(r.getCapabilities(null))
      && getRequirements(null).equals(r.getRequirements(null));
}
 
源代码27 项目: knopflerfish.org   文件: ExpressionResolver.java
public ExpressionResolver(Repository repository,
                          RequirementExpression expression,
                          Deferred<Collection<Resource>> deferred)
{
  this.repository = repository;
  this.expression = expression;
  this.deferred = deferred;
}
 
源代码28 项目: knopflerfish.org   文件: ExpressionResolver.java
@Override
public void run() {
  try {
    Collection<Resource> resolved = resolve(expression);
    if (resolved instanceof NegativeCollection) {
      RequirementBuilder rb = repository.newRequirementBuilder(ContentNamespace.CONTENT_NAMESPACE);
      Collection<Resource> rs = getResources(rb.build());
      rs.retainAll(resolved);
      resolved = rs;
    }
    deferred.resolve(resolved);
  } catch (Exception e) {
    deferred.fail(e);
  }
}
 
源代码29 项目: knopflerfish.org   文件: ExpressionResolver.java
private Collection<Resource> getResources(Requirement req)
{
  Collection<Capability> cs = repository.findProviders(Collections.singleton(req)).get(req);
  Collection<Resource> res = new ArrayList<Resource>(cs.size());
  for (Capability c : cs) {
    // TODO could resource be null?
    res.add(c.getResource());
  }
  return res;
}
 
源代码30 项目: knopflerfish.org   文件: RepositoryCommandGroup.java
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;
}