title: OSGI: asking Framework to load class exported by a bundle without visiting each bundle's classloader?
body: <p>OK.  So I have a org.osgi.framework.launch.Framework which I created programmatically in the following way.</p>
  
  <pre><code>framework = ServiceLoader.load(FrameworkFactory.class).iterator().next().newFramework(getFrameworkConfig());
  framework.start();
  installBundles(BUNDLES_PATH); // installs bundles from a directory, by searching BUNDLES_PATH recursively for JARs
  </code></pre>
  
  <p>What I want to do, is have a universal loadClass method (as a method in this class) Which will scan the installed bundles, read their Export-Package: declarations, and call the correct bundle's loadClass method, based on the packageName of the class that I'm passing as a parameter.</p>
  
  <p>Is there a smart way to do this?  or is it better to just do this:</p>
  
  <pre><code>Class&lt;?&gt; c = null;
  // else try every installed bundle one-by-one
  for (Bundle bundle : framework.getBundleContext().getBundles()) {
     try {
        c = bundle.loadClass(className);
  
     } catch (ClassNotFoundException e) {
        // OK, move onto next bundle
        continue;
     }
     if (c != null)
        break;
  }
  return c;
  </code></pre>
  
  <p>I realize I could use services to just have the bundles publish their available services and have the framework query the service with getAllServiceReferences() but this is more work for the programmer, and I'm not sure I wanna go the route of Declarative services.</p>