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 9DC1C17D19 for ; Tue, 27 Oct 2015 21:06:04 +0000 (UTC) Received: (qmail 96466 invoked by uid 500); 27 Oct 2015 21:05:42 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 96429 invoked by uid 500); 27 Oct 2015 21:05:42 -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 96418 invoked by uid 99); 27 Oct 2015 21:05:42 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Oct 2015 21:05:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id BD5DC1809D2 for ; Tue, 27 Oct 2015 21:05:41 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.971 X-Spam-Level: X-Spam-Status: No, score=0.971 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id H73smDRX9KiC for ; Tue, 27 Oct 2015 21:05:35 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with SMTP id F22DA20604 for ; Tue, 27 Oct 2015 21:05:33 +0000 (UTC) Received: (qmail 96283 invoked by uid 99); 27 Oct 2015 21:05:33 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 27 Oct 2015 21:05:33 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2893DE01F5; Tue, 27 Oct 2015 21:05:33 +0000 (UTC) From: CMoH To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Brooklyn OSGification - step 1 (b... Content-Type: text/plain Message-Id: <20151027210533.2893DE01F5@git1-us-west.apache.org> Date: Tue, 27 Oct 2015 21:05:33 +0000 (UTC) Github user CMoH commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/962#discussion_r43187070 --- Diff: core/src/main/java/org/apache/brooklyn/util/core/osgi/Osgis.java --- @@ -336,195 +277,56 @@ public static BundleFinder bundleFinder(Framework framework) { return bundleFinder(framework).symbolicName(symbolicName).version(Predicates.equalTo(version)).findUnique(); } - // -------- creating - - /* - * loading framework factory and starting framework based on: - * http://felix.apache.org/documentation/subprojects/apache-felix-framework/apache-felix-framework-launching-and-embedding.html + /** + * Provides an OSGI framework. + * + * When running inside an OSGi container, the container framework is returned. + * When running standalone a new Apache Felix container is created. + * + * Calling {@link #ungetFramework(Framework) } is needed in both cases, either to stop + * the embedded framework or to release the service reference. + * + * @param felixCacheDir + * @param clean + * @return + * @todo Use felixCacheDir ? */ - - public static FrameworkFactory newFrameworkFactory() { - URL url = Osgis.class.getClassLoader().getResource( - "META-INF/services/org.osgi.framework.launch.FrameworkFactory"); - if (url != null) { - try { - BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); - try { - for (String s = br.readLine(); s != null; s = br.readLine()) { - s = s.trim(); - // load the first non-empty, non-commented line - if ((s.length() > 0) && (s.charAt(0) != '#')) { - return (FrameworkFactory) Class.forName(s).newInstance(); - } - } - } finally { - if (br != null) br.close(); - } - } catch (Exception e) { - // class creation exceptions are not interesting to caller... - throw Exceptions.propagate(e); - } - } - throw new IllegalStateException("Could not find framework factory."); - } - - public static Framework newFrameworkStarted(String felixCacheDir, boolean clean, Map extraStartupConfig) { - Map cfg = MutableMap.copyOf(extraStartupConfig); - if (clean) cfg.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit"); - if (felixCacheDir!=null) cfg.put(Constants.FRAMEWORK_STORAGE, felixCacheDir); - cfg.put(Constants.FRAMEWORK_BSNVERSION, Constants.FRAMEWORK_BSNVERSION_MULTIPLE); - FrameworkFactory factory = newFrameworkFactory(); - - Stopwatch timer = Stopwatch.createStarted(); - Framework framework = factory.newFramework(cfg); - try { - framework.init(); - installBootBundles(framework); - framework.start(); - } catch (Exception e) { - // framework bundle start exceptions are not interesting to caller... - throw Exceptions.propagate(e); + public static Framework getFramework(String felixCacheDir, boolean clean) { + final Bundle frameworkBundle = FrameworkUtil.getBundle(Framework.class); + if (frameworkBundle != null) { + // already running inside an OSGi container + final BundleContext ctx = frameworkBundle.getBundleContext(); + final ServiceReference ref = ctx.getServiceReference(Framework.class); + return (Framework) ctx.getService(ref); + } else { + // not running inside OSGi container + return EmbeddedFelixFramework.newFrameworkStarted(felixCacheDir, clean, null); } - LOG.debug("System bundles are: "+SYSTEM_BUNDLES); - LOG.debug("OSGi framework started in " + Duration.of(timer)); - return framework; } - private static void installBootBundles(Framework framework) { - Stopwatch timer = Stopwatch.createStarted(); - LOG.debug("Installing OSGi boot bundles from "+Osgis.class.getClassLoader()+"..."); - Enumeration resources; - try { - resources = Osgis.class.getClassLoader().getResources(MANIFEST_PATH); - } catch (IOException e) { - throw Exceptions.propagate(e); - } - BundleContext bundleContext = framework.getBundleContext(); - Map installedBundles = getInstalledBundlesById(bundleContext); - while(resources.hasMoreElements()) { - URL url = resources.nextElement(); - ReferenceWithError installResult = installExtensionBundle(bundleContext, url, installedBundles, getVersionedId(framework)); - if (installResult.hasError() && !installResult.masksErrorIfPresent()) { - // it's reported as a critical error, so warn here - LOG.warn("Unable to install manifest from "+url+": "+installResult.getError(), installResult.getError()); - } else { - Object result = installResult.getWithoutError(); - if (result instanceof Bundle) { - String v = getVersionedId( (Bundle)result ); - SYSTEM_BUNDLES.add(v); - if (installResult.hasError()) { - LOG.debug(installResult.getError().getMessage()+(result!=null ? " ("+result+"/"+v+")" : "")); - } else { - LOG.debug("Installed "+v+" from "+url); - } - } else if (installResult.hasError()) { - LOG.debug(installResult.getError().getMessage()); - } - } - } - LOG.debug("Installed OSGi boot bundles in "+Time.makeTimeStringRounded(timer)+": "+Arrays.asList(framework.getBundleContext().getBundles())); - } - - private static Map getInstalledBundlesById(BundleContext bundleContext) { - Map installedBundles = new HashMap(); - Bundle[] bundles = bundleContext.getBundles(); - for (Bundle b : bundles) { - installedBundles.put(getVersionedId(b), b); - } - return installedBundles; - } - - /** Wraps the bundle if successful or already installed, wraps TRUE if it's the system entry, - * wraps null if the bundle is already installed from somewhere else; - * in all these cases masking an explanatory error if already installed or it's the system entry. - *

- * Returns an instance wrapping null and throwing an error if the bundle could not be installed. + /** + * Stops/ungets the OSGi framework. + * + * See {@link #getFramework(java.lang.String, boolean)} + * + * @param framework */ - private static ReferenceWithError installExtensionBundle(BundleContext bundleContext, URL manifestUrl, Map installedBundles, String frameworkVersionedId) { - //ignore http://felix.extensions:9/ system entry - if("felix.extensions".equals(manifestUrl.getHost())) - return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Skipping install of internal extension bundle from "+manifestUrl)); - - try { - Manifest manifest = readManifest(manifestUrl); - if (!isValidBundle(manifest)) - return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Resource at "+manifestUrl+" is not an OSGi bundle: no valid manifest")); - - String versionedId = getVersionedId(manifest); - URL bundleUrl = ResourceUtils.getContainerUrl(manifestUrl, MANIFEST_PATH); - - Bundle existingBundle = installedBundles.get(versionedId); - if (existingBundle != null) { - if (!bundleUrl.equals(existingBundle.getLocation()) && - //the framework bundle is always pre-installed, don't display duplicate info - !versionedId.equals(frameworkVersionedId)) { - return ReferenceWithError.newInstanceMaskingError(null, new IllegalArgumentException("Bundle "+versionedId+" (from manifest " + manifestUrl + ") is already installed, from " + existingBundle.getLocation())); - } - return ReferenceWithError.newInstanceMaskingError(existingBundle, new IllegalArgumentException("Bundle "+versionedId+" from manifest " + manifestUrl + " is already installed")); - } - - byte[] jar = buildExtensionBundle(manifest); - LOG.debug("Installing boot bundle " + bundleUrl); - //mark the bundle as extension so we can detect it later using the "system:" protocol - //(since we cannot access BundleImpl.isExtension) - Bundle newBundle = bundleContext.installBundle(EXTENSION_PROTOCOL + ":" + bundleUrl.toString(), new ByteArrayInputStream(jar)); - installedBundles.put(versionedId, newBundle); - return ReferenceWithError.newInstanceWithoutError(newBundle); - } catch (Exception e) { - Exceptions.propagateIfFatal(e); - return ReferenceWithError.newInstanceThrowingError(null, - new IllegalStateException("Problem installing extension bundle " + manifestUrl + ": "+e, e)); - } - } - - private static Manifest readManifest(URL manifestUrl) throws IOException { - Manifest manifest; - InputStream in = null; - try { - in = manifestUrl.openStream(); - manifest = new Manifest(in); - } finally { - if (in != null) { - try {in.close();} - catch (Exception e) {}; - } + public static void ungetFramework(Framework framework) { --- End diff -- The lifetime of the OSGi framework will not be controlled by Brooklyn, so it makes sense to obtain the framework similarly to other OSGi services. Those methods are get/ungetService, so I derived the names get/ungetFramework. However, when running within the brooklyn launcher their effect is start/stop, maintaining the previous semantics. --- 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. ---