Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
rsteppac2 wrote > What I could do, I guess, is to wrap the library's factory code in my own > and set the context classloader of the current thread to the application > context class loader before delegating to the actual factory. This worked. Though instead of the application classloader I used the classloader of the factory class. Ralf -- View this message in context: http://camel.465427.n5.nabble.com/getResourceAsStream-in-OSGi-bundle-not-working-with-Camel-2-19-tp5807017p5807197.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
Hello² And you're problem is described at this issue: https://issues.apache.org/jira/browse/CAMEL-10513. Camel 2.19.0 changed the initialization time in blueprint - so that's why you have different thread/TCCL. regards Grzegorz Grzybek 2017-07-14 19:24 GMT+02:00 Grzegorz Grzybek : > Hello > > Yes. Never, ever use thread context class loader - in blueprint you can't > be sure what thread you're using. And you don't want to cause memory leaks. > > if you have bundle reference or bundlecontext, you can get any bundle and > use it to load resource. > > regards > Grzegorz Grzybek > > 2017-07-14 17:45 GMT+02:00 Claus Ibsen : > >> In pure OSGi that is not the way to load resources from the classpath. >> OSGi has its own set of APIs for that. >> >> You should load via BundleContext and Bundle >> https://osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html >> https://osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html >> >> That is also what Camel uses when its runs in OSGi and its >> ClassResolver via the OsgiClassResolver class. >> >> It may be that the current thread in the past may have worked, but >> there is no guarantee. And what have changed is hard to tell as Karaf >> and Camel upgrades are using newer version of OSGi and Blueprint etc. >> >> On Fri, Jul 14, 2017 at 5:39 PM, rsteppac2 wrote: >> > Hello Claus, >> > >> > thanks for your suggestions! >> > >> > it is 3rd party library code, so I cannot just change it. What I could >> do, I >> > guess, is to wrap the library's factory code in my own and set the >> context >> > classloader of the current thread to the application context class >> loader >> > before delegating to the actual factory. >> > >> > What I do not quite understand is why the code works consistently with >> Camel >> > versions < 2.19.0 and consistently not with >= 2.19.0. >> > And why would the classloader of the thread that builds the blueprint >> > context based on the XML in my bundle not have access to a resource >> that is >> > at the root of that very same bundle? >> > >> > >> > Thanks again. >> > Ralf >> > >> > >> > >> > -- >> > View this message in context: http://camel.465427.n5.nabble. >> com/getResourceAsStream-in-OSGi-bundle-not-working-with-Came >> l-2-19-tp5807017p5807021.html >> > Sent from the Camel - Users mailing list archive at Nabble.com. >> >> >> >> -- >> Claus Ibsen >> - >> http://davsclaus.com @davsclaus >> Camel in Action 2: https://www.manning.com/ibsen2 >> > >
Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
Hello Yes. Never, ever use thread context class loader - in blueprint you can't be sure what thread you're using. And you don't want to cause memory leaks. if you have bundle reference or bundlecontext, you can get any bundle and use it to load resource. regards Grzegorz Grzybek 2017-07-14 17:45 GMT+02:00 Claus Ibsen : > In pure OSGi that is not the way to load resources from the classpath. > OSGi has its own set of APIs for that. > > You should load via BundleContext and Bundle > https://osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html > https://osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html > > That is also what Camel uses when its runs in OSGi and its > ClassResolver via the OsgiClassResolver class. > > It may be that the current thread in the past may have worked, but > there is no guarantee. And what have changed is hard to tell as Karaf > and Camel upgrades are using newer version of OSGi and Blueprint etc. > > On Fri, Jul 14, 2017 at 5:39 PM, rsteppac2 wrote: > > Hello Claus, > > > > thanks for your suggestions! > > > > it is 3rd party library code, so I cannot just change it. What I could > do, I > > guess, is to wrap the library's factory code in my own and set the > context > > classloader of the current thread to the application context class loader > > before delegating to the actual factory. > > > > What I do not quite understand is why the code works consistently with > Camel > > versions < 2.19.0 and consistently not with >= 2.19.0. > > And why would the classloader of the thread that builds the blueprint > > context based on the XML in my bundle not have access to a resource that > is > > at the root of that very same bundle? > > > > > > Thanks again. > > Ralf > > > > > > > > -- > > View this message in context: http://camel.465427.n5.nabble. > com/getResourceAsStream-in-OSGi-bundle-not-working-with- > Camel-2-19-tp5807017p5807021.html > > Sent from the Camel - Users mailing list archive at Nabble.com. > > > > -- > Claus Ibsen > - > http://davsclaus.com @davsclaus > Camel in Action 2: https://www.manning.com/ibsen2 >
Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
In pure OSGi that is not the way to load resources from the classpath. OSGi has its own set of APIs for that. You should load via BundleContext and Bundle https://osgi.org/javadoc/r4v43/core/org/osgi/framework/BundleContext.html https://osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html That is also what Camel uses when its runs in OSGi and its ClassResolver via the OsgiClassResolver class. It may be that the current thread in the past may have worked, but there is no guarantee. And what have changed is hard to tell as Karaf and Camel upgrades are using newer version of OSGi and Blueprint etc. On Fri, Jul 14, 2017 at 5:39 PM, rsteppac2 wrote: > Hello Claus, > > thanks for your suggestions! > > it is 3rd party library code, so I cannot just change it. What I could do, I > guess, is to wrap the library's factory code in my own and set the context > classloader of the current thread to the application context class loader > before delegating to the actual factory. > > What I do not quite understand is why the code works consistently with Camel > versions < 2.19.0 and consistently not with >= 2.19.0. > And why would the classloader of the thread that builds the blueprint > context based on the XML in my bundle not have access to a resource that is > at the root of that very same bundle? > > > Thanks again. > Ralf > > > > -- > View this message in context: > http://camel.465427.n5.nabble.com/getResourceAsStream-in-OSGi-bundle-not-working-with-Camel-2-19-tp5807017p5807021.html > Sent from the Camel - Users mailing list archive at Nabble.com. -- Claus Ibsen - http://davsclaus.com @davsclaus Camel in Action 2: https://www.manning.com/ibsen2
Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
Hello Claus, thanks for your suggestions! it is 3rd party library code, so I cannot just change it. What I could do, I guess, is to wrap the library's factory code in my own and set the context classloader of the current thread to the application context class loader before delegating to the actual factory. What I do not quite understand is why the code works consistently with Camel versions < 2.19.0 and consistently not with >= 2.19.0. And why would the classloader of the thread that builds the blueprint context based on the XML in my bundle not have access to a resource that is at the root of that very same bundle? Thanks again. Ralf -- View this message in context: http://camel.465427.n5.nabble.com/getResourceAsStream-in-OSGi-bundle-not-working-with-Camel-2-19-tp5807017p5807021.html Sent from the Camel - Users mailing list archive at Nabble.com.
Re: getResourceAsStream in OSGi bundle not working with Camel 2.19
I suggest to use Camel's ClassResolver to load resources. Or you can try using the class loader from camelCamel.getApplicationContextClassLoader which should be the classloader that startup your osgi bundle. But in OSGi there is no guarantee what classloader the current thread is associated with On Fri, Jul 14, 2017 at 4:01 PM, rsteppac2 wrote: > Hello all, > > I am trying to migrate to Camel 2.19 and came across an in issue where > loading resources from an OSGi bundle stopped working. The issue is not > reproducible with versions 2.18.4 and lower. I tried Karaf 4.0.9 and 4.1.1; > the Karaf version does not appear to matter. > > In my case the resource is at the root of the bundle that has the code > trying to load it as a stream. The calling code is in a library embed in the > bundle JAR. Whether I inline the library or not does not seem to matter. The > call that now returns null instead of a stream instance is: > > Thread.currentThread().getContextClassLoader().getResourceAsStream("httl.properties"); > > The resource is loaded as part of the factory triggered via Blueprint: > > > > Am I possibly hitting https://issues.apache.org/jira/browse/CAMEL-5223? > Is there a known workaround? > > > Thanks! > Ralf > > > > -- > View this message in context: > http://camel.465427.n5.nabble.com/getResourceAsStream-in-OSGi-bundle-not-working-with-Camel-2-19-tp5807017.html > Sent from the Camel - Users mailing list archive at Nabble.com. -- Claus Ibsen - http://davsclaus.com @davsclaus Camel in Action 2: https://www.manning.com/ibsen2