Github user aledsage commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/31#discussion_r14379322
  
    --- Diff: core/src/main/java/brooklyn/util/osgi/Osgis.java ---
    @@ -0,0 +1,151 @@
    +package brooklyn.util.osgi;
    +
    +import java.io.BufferedReader;
    +import java.io.InputStream;
    +import java.io.InputStreamReader;
    +import java.net.URL;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.felix.framework.FrameworkFactory;
    +import org.osgi.framework.Bundle;
    +import org.osgi.framework.BundleException;
    +import org.osgi.framework.Constants;
    +import org.osgi.framework.Version;
    +import org.osgi.framework.launch.Framework;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import brooklyn.util.ResourceUtils;
    +import brooklyn.util.collections.MutableList;
    +import brooklyn.util.collections.MutableMap;
    +import brooklyn.util.exceptions.Exceptions;
    +import brooklyn.util.guava.Maybe;
    +
    +import com.google.common.annotations.Beta;
    +import com.google.common.base.Joiner;
    +import com.google.common.base.Predicate;
    +import com.google.common.base.Predicates;
    +
    +/** 
    + * utilities for working with osgi.
    + * osgi support is in early days (June 2014) so this class is beta, 
subject to change,
    + * particularly in how framework is started and bundles installed.
    + * 
    + * @since 0.7.0  */
    +@Beta
    +public class Osgis {
    +
    +    private static final Logger LOG = LoggerFactory.getLogger(Osgis.class);
    +
    +    public static List<Bundle> getBundlesByName(Framework framework, 
String symbolicName, Predicate<Version> versionMatcher) {
    +        List<Bundle> result = MutableList.of();
    +        for (Bundle b: framework.getBundleContext().getBundles()) {
    +            if (symbolicName.equals(b.getSymbolicName()) && 
versionMatcher.apply(b.getVersion())) {
    +                result.add(b);
    +            }
    +        }
    +        return result;
    +    }
    +
    +    public static List<Bundle> getBundlesByName(Framework framework, 
String symbolicName) {
    +        return getBundlesByName(framework, symbolicName, 
Predicates.<Version>alwaysTrue());
    +    }
    +
    +    /**
    +     * Tries to find a bundle in the given framework with name matching 
either `name' or `name:version'.
    +     */
    +    public static Maybe<Bundle> getBundle(Framework framework, String 
symbolicNameOptionallyWithVersion) {
    +        String[] parts = symbolicNameOptionallyWithVersion.split(":");
    +        Maybe<Bundle> result = Maybe.absent("No bundles matching 
"+symbolicNameOptionallyWithVersion);
    +        if (parts.length == 2) {
    +            result = getBundle(framework, parts[0], parts[1]);
    +        } else if (parts.length == 1) {
    +            List<Bundle> matches = getBundlesByName(framework, 
symbolicNameOptionallyWithVersion);
    +            if (!matches.isEmpty()) {
    +                result = Maybe.of(matches.iterator().next());
    --- End diff --
    
    Perhaps add a TODO about getting the latest stable version, rather than 
just first in list?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to