Run in osgi environment

119 views
Skip to first unread message

erdal.k...@gmail.com

unread,
Jan 6, 2013, 4:32:59 PM1/6/13
to google-code...@googlegroups.com
I cannot get reflections run in an osgi environment.
Tries this:

Reflections reflections = new Reflections("my.package");
  Set<Class<? extends MyAbstractTestCase>> subTypesOf = reflections
  .getSubTypesOf( MyAbstractTestCase .class);

If I run the code from a simple main method, it works.
If I try the same code in an osgi environment, it does not work (tried with setting a class loader and the like, but still no success).

The idea is to load all junit test classes that are located in a specific osgi bundle/plugin.

Ron m

unread,
Jan 7, 2013, 4:31:50 AM1/7/13
to google-code...@googlegroups.com
make sure reflections.configuration.getUrls() contain the url of MyAbstractTestClass and the urls of the relevant implementations you wish to find

try new Reflections("my.package", ClasspathHelper.forClassLoader()) to scan all urls available


erdal.k...@gmail.com

unread,
Jan 7, 2013, 3:59:01 PM1/7/13
to google-code...@googlegroups.com
Unfortunately, that did not help.
Note: An osgi bundle is like a war file: it may be deployed as a pure jar with embedded libs (also as jars) on its bundle-classpath or it may be deployed 'deflated', i.e. unpacked with the embedded jars on its bundle-classpath.
If the osgi application is run from the IDE (e.g. eclipse), then the compiled classes are in the bin folder.

I had a look at the ClasspathHelper.forWebInfLib/forWebInfClasses but could not determine how to provide the urls to get it set up.

Do you think that would/should still work?

mamo

unread,
Jan 7, 2013, 4:37:39 PM1/7/13
to google-code...@googlegroups.com, erdal.k...@gmail.com
Vfs.addDefaultURLTypes(new UrlType() {
    public boolean matches(URL url) throws Exception {
        return url.getProtocol().startsWith("bundle");
    }

    public Dir createDir(URL url) throws Exception {
        try {
            return fromURL((URL) ClasspathHelper.contextClassLoader().
                    loadClass("org.eclipse.core.runtime.FileLocator").getMethod("resolve", URL.class).invoke(null, url));
        }
        catch (Throwable ex) {
            ex.printStackTrace();
            return null;
        }
    }
});
Reflections reflections = new Reflections("my.pack");

erdal.k...@gmail.com

unread,
Jan 7, 2013, 4:48:14 PM1/7/13
to google-code...@googlegroups.com, erdal.k...@gmail.com
Where do I find fromUrl() createDir()?
I get compile errors as that method cannot be resolve and I am not sure where you copied that snippet from.
Is there an osgi extension available for reflections?

erdal.k...@gmail.com

unread,
Jan 7, 2013, 4:54:23 PM1/7/13
to google-code...@googlegroups.com, erdal.k...@gmail.com
Sorry, found it:

static {

Vfs.addDefaultURLTypes(new UrlType() {
public boolean matches(URL url) throws Exception {
return url.getProtocol().startsWith("bundle");
}

public Dir createDir(URL url) throws Exception {
try {
return Vfs.fromURL((URL) ClasspathHelper
.contextClassLoader()
.loadClass("org.eclipse.core.runtime.FileLocator")

.getMethod("resolve", URL.class).invoke(null, url));
} catch (Throwable ex) {
ex.printStackTrace();
return null;
}
}
});
}

I added that code to my test suite and now it works!
Thanks a lot!

I think it might be good to have native support for osgi as well, so, we should not care about the proper settings, no?

vaishali....@gmail.com

unread,
Jan 15, 2014, 3:16:47 PM1/15/14
to google-code...@googlegroups.com, erdal.k...@gmail.com

Do you mean wherever within our code we need to instantiate Reflections("my package"), we should put the snippet static {...that you have in post below...} before that and then Rerflections should work in OSGI environment?

Erdal Karaca

unread,
Jan 15, 2014, 3:38:47 PM1/15/14
to google-code...@googlegroups.com
Just try and report back, I think that was the solution when I applied it a year ago in my code :-)

--
You received this message because you are subscribed to the Google Groups "google-code-reflections" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-code-refle...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Erdal Karaca

unread,
Jan 15, 2014, 3:40:06 PM1/15/14
to google-code...@googlegroups.com

BTW that should only work with Eclipse Equinox as OSGi impl.


2014/1/15 Erdal Karaca <erdal.k...@gmail.com>

Vaishali Mithbaokar

unread,
Jan 16, 2014, 9:47:19 AM1/16/14
to google-code...@googlegroups.com

Ah ok. I see that what you tried an year back of adding the search for "bundle" URLs in static code, is folded in the Reflections's latest 0.9.9-RC1 .
But it still doesnt work for me, because the Refelctions assumes classLoader is URLClassLoader? Whereas in my set up I have only DefaultClassLoader (bundle class loader), it cant find classLoader.getResources(resourceName);
Did you have to do any trick for that? Or as you said  Eclipse Equinox takes care of that?


2014/1/15 Erdal Karaca <erdal.k...@gmail.com>
To unsubscribe from this group and stop receiving emails from it, send an email to google-code-reflections+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages