brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ahgittin <...@git.apache.org>
Subject [GitHub] incubator-brooklyn pull request: Brooklyn OSGification - step 1 (b...
Date Tue, 27 Oct 2015 09:12:42 GMT
Github user ahgittin commented on a diff in the pull request:

    https://github.com/apache/incubator-brooklyn/pull/962#discussion_r43097168
  
    --- 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<Object,Object> 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<URL> resources;
    -        try {
    -            resources = Osgis.class.getClassLoader().getResources(MANIFEST_PATH);
    -        } catch (IOException e) {
    -            throw Exceptions.propagate(e);
    -        }
    -        BundleContext bundleContext = framework.getBundleContext();
    -        Map<String, Bundle> 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<String, Bundle> getInstalledBundlesById(BundleContext bundleContext)
{
    -        Map<String, Bundle> installedBundles = new HashMap<String, Bundle>();
    -        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 <i>masking</i> an explanatory error if already
installed or it's the system entry.
    -     * <p>
    -     * Returns an instance wrapping null and <i>throwing</i> 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<String, Bundle> 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 --
    
    is `unget` a technical term?  deserves javadoc, or make it private.


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

Mime
View raw message