1

i want to find all classes annotated with my custom annotation inside OSGi bundle,
actual scanning must take place inside the bundle itself, or from another bundle,
i have tried using ClassPathScanningCandidateComponentProvider, but it causes exception:

java.io.FileNotFoundException: URL [bundleresource://38.fwk29597962/my/base/package/] cannot be resolved to absolute file path because it does not reside in the file system: bundleresource://38.fwk29597962/my/base/package/

i am 99% sure, solutions like reflections or other scanning libraries didn't work either, don't remember now why

i could config component-scan like so:

<context:annotation-config />
<context:component-scan base-package="my.base.package" use-default-filters="false">
    <context:include-filter type="annotation" expression="path.to.my.annotation"/>
</context:component-scan>

and get what i need form bean factory, but that would require adding @Scope("prototype") to all my annotated classes, so spring does not create singletons by default, etc.
any better solution?

-my annotated classes does not have @Component or anything related to spring
-osgi framework(eclipse equinox 3.8) is embedded in a web application
-using spring 3.2.3 and gemini-blueprint 2.0.0.M02

sv13
  • 176
  • 3
  • 13

1 Answers1

3

If you're prepared to do the scanning in build time rather than at runtime, then bnd has a rather nice macro you can use:

MyAnnotated-Classes: ${classes;CONCRETE;ANNOTATION;org.example.MyAnnotation}

...which will generate a MyAnnotated-Classes head in the manifest that lists all of the classes in your bundle that have @MyAnnotation. Scanning this header at runtime is now trivial.

This approach is a significant optimisation over runtime classpath scanning. Also runtime scanning can be unreliable, since you need to catch the contents of Bundle-ClassPath, but avoid any imported/required classes.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • 1
    [```bundle.adapt(BundleWiring.class).listResources("/", "*.class", LISTRESOURCES_LOCAL | LISTRESOURCES_RECURSE)```](http://www.osgi.org/javadoc/r5/core/org/osgi/framework/wiring/BundleWiring.html#listResources(java.lang.String,%20java.lang.String,%20int)) can be used to scan a bundle's ```Bundle-ClassPath``` for all .class resources. – BJ Hargrave Oct 01 '13 at 13:33
  • 1
    @Neil Bartlett great to know that, but i came up with another idea, i will process annotated classes with annotation processor, also at build time, i could easily define it in maven compiler plugin, processor will generate file with what i need, since its a little bit more than just a list, and later that file will be included in the jar – sv13 Oct 01 '13 at 15:17
  • @BJ Hargrave you forgot to add its available from osgi 4.3 – sv13 Oct 01 '13 at 15:18
  • I just linked to the most recent javadoc. – BJ Hargrave Oct 01 '13 at 16:26