Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C865B11472 for ; Mon, 30 Jun 2014 22:22:13 +0000 (UTC) Received: (qmail 55852 invoked by uid 500); 30 Jun 2014 22:22:13 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 55814 invoked by uid 500); 30 Jun 2014 22:22:13 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 55803 invoked by uid 99); 30 Jun 2014 22:22:13 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Jun 2014 22:22:13 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 30 Jun 2014 22:22:14 +0000 Received: (qmail 55568 invoked by uid 99); 30 Jun 2014 22:21:48 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 30 Jun 2014 22:21:48 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 5A04E9901EB; Mon, 30 Jun 2014 22:21:48 +0000 (UTC) From: aledsage To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Initial OSGI work Content-Type: text/plain Message-Id: <20140630222148.5A04E9901EB@tyr.zones.apache.org> Date: Mon, 30 Jun 2014 22:21:48 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org 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 getBundlesByName(Framework framework, String symbolicName, Predicate versionMatcher) { + List 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 getBundlesByName(Framework framework, String symbolicName) { + return getBundlesByName(framework, symbolicName, Predicates.alwaysTrue()); + } + + /** + * Tries to find a bundle in the given framework with name matching either `name' or `name:version'. + */ + public static Maybe getBundle(Framework framework, String symbolicNameOptionallyWithVersion) { + String[] parts = symbolicNameOptionallyWithVersion.split(":"); + Maybe result = Maybe.absent("No bundles matching "+symbolicNameOptionallyWithVersion); + if (parts.length == 2) { + result = getBundle(framework, parts[0], parts[1]); + } else if (parts.length == 1) { + List 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---