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

    https://github.com/apache/incubator-brooklyn/pull/90#discussion_r15419018
  
    --- Diff: core/src/main/java/brooklyn/util/osgi/Osgis.java ---
    @@ -157,138 +160,214 @@ public static Framework newFrameworkStarted(String 
felixCacheDir, boolean clean,
             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_SYSTEMPACKAGES_EXTRA, 
getBrooklynBootBundles());
             FrameworkFactory factory = newFrameworkFactory();
    -        
    +
    +        long frameworkInitStart = System.currentTimeMillis();
             Framework framework = factory.newFramework(cfg);
             try {
                 framework.init();
    -            // nothing needs auto-loading, currently (and this needs a new 
dependency)
    -            // AutoProcessor.process(configProps, 
m_fwk.getBundleContext());
    +            installBootBundles(framework);
                 framework.start();
             } catch (Exception e) {
                 // framework bundle start exceptions are not interesting to 
caller...
                 throw Exceptions.propagate(e);
             }
    +        long frameworkInitEnd = System.currentTimeMillis();
    +        double frameworkInitSeconds = ((double)frameworkInitEnd - 
frameworkInitStart) / 1000;
    +        LOG.info("OSGi framework started in " + 
Strings.makeRealString(frameworkInitSeconds, -1, 2, -1) + " seconds.");
    +
             return framework;
         }
     
    -    private static String getBrooklynBootBundles() {
    +    private static void installBootBundles(Framework framework) {
             Enumeration<URL> resources;
             try {
    -            resources = 
Osgis.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
    +            resources = 
Osgis.class.getClassLoader().getResources(MANIFEST_PATH);
             } catch (IOException e) {
                 throw Exceptions.propagate(e);
             }
    -        
    -        Collection<String> exportPackages = new ArrayList<String>();
    +        BundleContext bundleContext = framework.getBundleContext();
    +        Map<String, Bundle> installedBundles = 
getInstalledBundles(bundleContext);
             while(resources.hasMoreElements()) {
                 URL url = resources.nextElement();
    -            exportPackages.addAll(getBundleExportedPackages(url));
    +            installExtensionBundle(bundleContext, url, installedBundles, 
getVersionedId(framework));
             }
    +    }
     
    -        Iterator<String> brooklynPackages = 
Iterators.filter(exportPackages.iterator(), new Predicate<String>() {
    -            @Override
    -            public boolean apply(String input) {
    -                return input.startsWith(BROOKLYN_PACKAGE_PREFIX);
    -            }
    -        });
    -        
    -        String bootBundles = Joiner.on(",").join(brooklynPackages);
    -        LOG.debug("Found the following boot OSGi packages: " + 
bootBundles);
    -        return bootBundles;
    +    private static Map<String, Bundle> getInstalledBundles(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;
         }
     
    -    private static Collection<String> getBundleExportedPackages(URL 
manifestUrl) {
    +    private static void 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;
    +
             try {
    -            ManifestHelper helper = 
ManifestHelper.forManifest(manifestUrl);
    -            return helper.getExportedPackages();
    +            Manifest manifest = readManifest(manifestUrl);
    +            if (!isValidBundle(manifest)) return;
    +            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)) {
    +                    LOG.info("Ignoring duplicate " + versionedId + " from 
manifest " + manifestUrl + ", already loaded from " + 
existingBundle.getLocation());
    +                }
    +                return;
    +            }
    +            
    +            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);
             } catch (IOException e) {
                 LOG.warn("Unable to load manifest from " + manifestUrl + ", 
ignoring.", e);
             } catch (BundleException e) {
                 LOG.warn("Unable to load manifest from " + manifestUrl + ", 
ignoring.", e);
             }
    -        return Collections.emptyList();
         }
     
    +    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) {};
    +            }
    +        }
    +        return manifest;
    +    }
    +
    +    private static byte[] buildExtensionBundle(Manifest manifest) throws 
IOException {
    +        Attributes atts = manifest.getMainAttributes();
    +
    +        //the following properties are invalid in extension bundles
    +        atts.remove(new Attributes.Name(Constants.IMPORT_PACKAGE));
    +        atts.remove(new Attributes.Name(Constants.REQUIRE_BUNDLE));
    +        atts.remove(new Attributes.Name(Constants.BUNDLE_NATIVECODE));
    +        atts.remove(new Attributes.Name(Constants.DYNAMICIMPORT_PACKAGE));
    +        atts.remove(new Attributes.Name(Constants.BUNDLE_ACTIVATOR));
    +        
    +        //mark as extension bundle
    +        atts.putValue(Constants.FRAGMENT_HOST, "system.bundle; 
extension:=framework");
    +
    +        //create the jar containing the manifest
    +        ByteArrayOutputStream jar = new ByteArrayOutputStream();
    +        JarOutputStream out = new JarOutputStream(jar, manifest);
    +        out.close();
    +        return jar.toByteArray();
    +    }
    +
    +    private static boolean isValidBundle(Manifest manifest) {
    +        Attributes atts = manifest.getMainAttributes();
    +        return atts.containsKey(new 
Attributes.Name(Constants.BUNDLE_MANIFESTVERSION));
    +    }
    +
    +    private static String getVersionedId(Bundle b) {
    +        return b.getSymbolicName() + ":" + b.getVersion();
    +    }
    +
    +    private static String getVersionedId(Manifest manifest) {
    +        Attributes atts = manifest.getMainAttributes();
    +        return atts.getValue(Constants.BUNDLE_SYMBOLICNAME) + ":" +
    +            atts.getValue(Constants.BUNDLE_VERSION);
    +    }
     
         /**
          * Installs a bundle from the given URL, doing a check if already 
installed, and
          * using the {@link ResourceUtils} loader for this project (brooklyn 
core)
          */
         public static Bundle install(Framework framework, String url) throws 
BundleException {
    -        Bundle bundle = framework.getBundleContext().getBundle(url);
    -        if (bundle != null) {
    -            return bundle;
    +        boolean isLocal = isLocalUrl(url);
    +        String localUrl = url;
    +        if (!isLocal) {
    +            localUrl = cacheFile(url);
             }
     
    -        // use our URL resolution so we get classpath items
    -        LOG.debug("Installing bundle into {} from url: {}", framework, 
url);
    -        InputStream stream = 
ResourceUtils.create(Osgis.class).getResourceFromUrl(url);
    -        return framework.getBundleContext().installBundle(url, stream);
    -    }
    -
    -    public static class ManifestHelper {
    --- End diff --
    
    Is there potential value in keeping this around (and the tests), at least 
for a bit until we're sure we don't need it?  My personal preference is for 
that, rather than to aggressively delete, because I find myself forgetting 
where something was done, especially if it's in a deletion, and losing time 
rewriting.  But others feel differently.  If you're pretty sure we'll never 
need this in any case, of course we should kill it now...



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