Reflections not working for plugins in 2.8/2.9

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Reflections not working for plugins in 2.8/2.9

PeterB
My plugin is using the Reflections API but the following is no longer working in Nexus 2.8 or 2.9.
        Reflections r = new Reflections("my.package");

I get errors like:
2014-08-05 12:18:56,773+0200 WARN  [Thread-8] *SYSTEM org.reflections.Reflections - could not create Vfs.Dir from url. ignoring the exception and continuing                                        
jvm 1    | org.reflections.ReflectionsException: could not create Vfs.Dir from url, no matching UrlType was found [bundle://23.0:2/]                                                                            
jvm 1    | either use fromURL(final URL url, final List<UrlType> urlTypes) or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.  

In previous versions (-2.7.x) the url started with "jar:file:" and now (2.8.x-) it starts with "bundle:". The plugin is built for 2.7 and is working in all other aspects so I wonder if anyone can shed some light of the change in Nexus causing this. Perhaps understanding this change will enlighten me?

I have started to create an UrlType of my own but didn't get all the way. I can get the jars (also with protocol "bundle" ) from the bundle but can't convert them to a File to use.

Any help would be appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Reflections not working for plugins in 2.8/2.9

Stuart McCulloch-2
Hi Peter,

Nexus 2.8 and 2.9 embed an OSGi runtime (specifically Apache/Felix) which is used to manage plugins.

This means that any resource URLs retrieved from plugins will typically be “bundle://…” which relates to the plugin's bundle in the OSGi framework.

Last time I checked the reflections library only worked with URLClassLoaders, to get it to work with Apache/Felix you would need to write your own UrlType.

Mapping the “bundle://…” URL back to a specific file is not straightforward, but could be achieved using something like:

        URLConnection con = bundleURL.openConnection();
        Method getLocalURL = con.getClass().getDeclaredMethod("getLocalURL");
        getLocalURL.setAccessible(true);
        URL localURL = ((URL) getLocalURL.invoke(con));

You may also need to strip any leading “jar:” or trailing "!.*” depending what URL you looked up, but this is hopefully a start.

Finally depending what you use the reflections library for there are alternative solutions that can work better in OSGi, such as generating the class/annotation data at build time and then loading that data dynamically at runtime.


Kind regards, Stuart

On 12 Aug 2014, at 13:39, PeterB <[hidden email]> wrote:

> My plugin is using the  Reflections API
> <https://github.com/ronmamo/reflections>   but the following is no longer
> working in Nexus 2.8 or 2.9.
>        Reflections r = new Reflections("my.package");
>
> I get errors like:
> 2014-08-05 12:18:56,773+0200 WARN  [Thread-8] *SYSTEM
> org.reflections.Reflections - could not create Vfs.Dir from url. ignoring
> the exception and continuing                                        
> jvm 1    | org.reflections.ReflectionsException: could not create Vfs.Dir
> from url, no matching UrlType was found [bundle://23.0:2/]                                                                            
> jvm 1    | either use fromURL(final URL url, final List<UrlType> urlTypes)
> or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or
> addDefaultURLTypes(UrlType urlType) with your specialized UrlType.  
>
> In previous versions (-2.7.x) the url started with "jar:file:" and now
> (2.8.x-) it starts with "bundle:". The plugin is built for 2.7 and is
> working in all other aspects so I wonder if anyone can shed some light of
> the change in Nexus causing this. Perhaps understanding this change will
> enlighten me?
>
> I have started to create an UrlType of my own but didn't get all the way. I
> can get the jars (also with protocol "bundle" ) from the bundle but can't
> convert them to a File to use.
>
> Any help would be appreciated.
>
>
>
> --
> View this message in context: http://maven.40175.n5.nabble.com/Reflections-not-working-for-plugins-in-2-8-2-9-tp5802184.html
> Sent from the Nexus Maven Repository Manager Dev List mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Reflections not working for plugins in 2.8/2.9

PeterB
Thanks! That piece of code was exactly what I lacked for my URLType implementation so now it works.

Both the reflection code and the classes it shall find is in code shared with other application. So supplying an URLType in our Nexus plugin seems like a good solution that does not affect any of the other application.

/Best Regards Peter