In OSGi, each bundle has its own class loader and these class loaders are connected in a mesh by exported/imported packages.
So if you do what you do in your example, the Reflections class is only going
to see the class loader of the Reflections class which does not import your
domain classes. In this case, you even seem to be missing the Google Guava
classes.
To make this code work you need to provide the OSGi details to your Reflections
class. In general you can add a class loader to such an object or set the
Thread Context class loader. However, the class loader you add must be quite
smart, it must understand OSGi class loading in detail and forward to the
correct Bundle Class Loader.
I am not sure what you want to achieve but in general you want to think in
bundles before you scan since bundles can come and go so you must not only
create some structures you should also be ready to destroy them when the bundle
is stopped. So normally you create a _Bundle Tracker_ object that gets
triggered when a bundle gets activated/deactivated. On activation you can turn
the Bundle object into a class loader using
bundle.adapt(BundleWiring.class).getClassLoader(). You then do the scanning and
create services that represent the result.
If you are going to get this to work it would be highly appreciated if you
recorded your experiences and donated them as an OSGi enRoute app note.
Kind regards,
Peter Kriens
> On 22 aug. 2016, at 20:38, Randy Leonard <[email protected]> wrote:
>
> I am using org.apache.servicemix.bundles.reflections to scan classes for
> annotations within an OSGi bundle that built using enRoute tools.
>
> To this end, the following line of code is generating an error:
>
> Reflections ref = new Reflections("com.simple.services");
>
> The stack trace is as follows:
>
> java.lang.NoClassDefFoundError: com/google/common/base/Predicate
> at
> com.simple.db.xyz.provider.XyzDaoImpl.findWebServiceClients(XyzDaoImpl.java:25)
> at
> com.simple.db.xyz.provider.command.SoapListCommand.list(SoapListCommand.java:20)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:498)
> at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
> at
> org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
> at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
> at
> org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
> at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
> at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
> at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
> at
> org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
> at org.apache.felix.gogo.shell.Console.run(Console.java:62)
> at org.apache.felix.gogo.shell.Shell.console(Shell.java:203)
> at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:128)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:498)
> at org.apache.felix.gogo.runtime.Reflective.invoke(Reflective.java:137)
> at
> org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82)
> at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:480)
> at
> org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:406)
> at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108)
> at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:182)
> at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:119)
> at
> org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:94)
> at org.apache.felix.gogo.shell.Activator.run(Activator.java:75)
> at java.lang.Thread.run(Thread.java:745)
> Caused by: java.lang.ClassNotFoundException: com.google.common.base.Predicate
> cannot be found by org.apache.servicemix.bundles.reflections_0.9.10.3
> at
> org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:439)
> at
> org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:352)
> at
> org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:344)
> at
> org.eclipse.osgi.internal.loader.ModuleClassLoader.loadClass(ModuleClassLoader.java:160)
> at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
> ... 31 more
>
>
> My question is if there is a working example of using
> org.apache.servicemix.bundles.reflections within enRoute.
>
> Randy Leonard
> [email protected] <mailto:[email protected]>
> www.leonard.info <http://www.leonard.info/>
> +1 720-270-2628
>
>
>
> _______________________________________________
> OSGi Developer Mail List
> [email protected]
> https://mail.osgi.org/mailman/listinfo/osgi-dev
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________ OSGi Developer Mail List [email protected] https://mail.osgi.org/mailman/listinfo/osgi-dev
