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

    https://github.com/apache/incubator-brooklyn/pull/307#discussion_r20127664
  
    --- Diff: core/src/main/java/brooklyn/util/osgi/Osgis.java ---
    @@ -82,54 +89,195 @@
     
         private static final String EXTENSION_PROTOCOL = "system";
         private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF";
    +    private static final Set<String> SYSTEM_BUNDLES = MutableSet.of();
    +
    +    public static class BundleFinder {
    +        protected final Framework framework;
    +        protected String symbolicName;
    +        protected String version;
    +        protected String url;
    +        protected boolean urlMandatory = false;
    +        protected final List<Predicate<? super Bundle>> predicates = 
MutableList.of();
    +        
    +        protected BundleFinder(Framework framework) {
    +            this.framework = framework;
    +        }
     
    -    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())) {
    +        public BundleFinder symbolicName(String symbolicName) {
    +            this.symbolicName = symbolicName;
    +            return this;
    +        }
    +
    +        public BundleFinder version(String version) {
    +            this.version = version;
    +            return this;
    +        }
    +        
    +        public BundleFinder id(String symbolicNameOptionallyWithVersion) {
    +            if (Strings.isBlank(symbolicNameOptionallyWithVersion))
    +                return this;
    +            
    +            Maybe<String[]> partsM = 
parseOsgiIdentifier(symbolicNameOptionallyWithVersion);
    +            if (partsM.isAbsent())
    +                throw new IllegalArgumentException("Cannot parse 
symbolic-name:version string '"+symbolicNameOptionallyWithVersion+"'");
    +            String[] parts = partsM.get();
    +            
    +            symbolicName(parts[0]);
    +            if (parts.length >= 2) version(parts[1]);
    +            
    +            return this;
    +        }
    +
    +        /** Looks for a bundle matching the given URL;
    +         * unlike {@link #requiringFromUrl(String)} however, if the URL 
does not match any bundles
    +         * it will return other matching bundles <i>if</if> a {@link 
#symbolicName(String)} is specified.
    +         */
    +        public BundleFinder preferringFromUrl(String url) {
    +            this.url = url;
    +            urlMandatory = false;
    +            return this;
    +        }
    +
    +        /** Requires the bundle to have the given URL set as its location. 
*/
    +        public BundleFinder requiringFromUrl(String url) {
    +            this.url = url;
    +            urlMandatory = true;
    +            return this;
    +        }
    +
    +        /** Finds the best matching bundle. */
    +        public Maybe<Bundle> find() {
    +            return findOne(false);
    +        }
    +        
    +        /** Finds the matching bundle, requiring it to be unique. */
    +        public Maybe<Bundle> findUnique() {
    +            return findOne(true);
    +        }
    +
    +        protected Maybe<Bundle> findOne(boolean requireExactlyOne) {
    +            if (symbolicName==null && url==null)
    +                throw new IllegalStateException(this+" must be given 
either a symbolic name or a URL");
    +            
    +            List<Bundle> result = findAll();
    +            if (result.isEmpty())
    +                return Maybe.absent("No bundle matching 
"+getConstraintsDescription());
    +            if (requireExactlyOne && result.size()>1)
    +                return Maybe.absent("Multiple bundles ("+result.size()+") 
matching "+getConstraintsDescription());
    +            
    +            return Maybe.of(result.iterator().next());
    +        }
    +        
    +        /** Finds all matching bundles, in decreasing version order. */
    +        public List<Bundle> findAll() {
    --- End diff --
    
    OsgiStandaloneTest and MoreEntityTest test several combos of these methods 
(we need real bundles for the tests to be interesting, so simpler to put them 
there)


---
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