You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Philipp Hoenisch <ph...@gmail.com> on 2013/11/19 18:20:32 UTC

Finding classes in a Bundle

Hey guys,

I'm not sure if this belongs in here or more to the Felix mailing list,
anyway, I'll give it a try:

versions i use: 
karaf: 2.3.2
felix: 4.0.3

I'm trying to find a class which is located in one of the packages without
knowing it's name, however, I do know that it is annotated with a specific
annotation. This class is autogenerated using a maven plugin.
Anyway, what I try to do is to find this class (located in the same bundle)
and create an instance during runtime, aka right after the bundleActivator
was started

As the package name is known I tried a simple approach aka:

Thread.currentThread().getContextClassLoader().getResources(PACKAGENAME);

and then iterate over the the files.

The problem is that the ClassLoaders work a bit differently in OSGI right? 

Can anyone point me to the right direction how I can find a particular
class?

thank you in advance,
best regards,
Philipp



--
View this message in context: http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: Finding classes in a Bundle

Posted by Timothy Stewart <ti...@gmail.com>.
Oh that's nice and simple.  I think I'll try that out myself!



--
View this message in context: http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368p4030380.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: Finding classes in a Bundle

Posted by Philipp Hoenisch <ph...@gmail.com>.
Hey guys, 

thanks for your answers, 

@Timothy, i couldn't get the BundleWiring instance, maybe it's because I'm
still on an older version?
Anyway, thanks to Andi and Jean I got it working like this:

Enumeration<URL> resources = bundle.findEntries(PACKAGE, "*", true);

as easy as pie :)

cheers,
Philipp



--
View this message in context: http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368p4030379.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: Finding classes in a Bundle

Posted by Timothy Stewart <ti...@gmail.com>.
I don't know if this is "right" or "wrong", but I'm doing something similar
to what you describe.

I iterate through the classes in my bundle looking for classes with a
specific annotation, then I "register" those in a Map for later lookup.

import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.wiring.BundleWiring;

...

public MessageHandlerFactoryImpl() {
        BundleWiring wiring = (BundleWiring)
FrameworkUtil.getBundle(MessageHandlerFactoryImpl.class).adapt(BundleWiring.class);
        Collection<String> resources = wiring.listResources("/", "*.class",
BundleWiring.LISTRESOURCES_RECURSE | BundleWiring.LISTRESOURCES_LOCAL);
        for (String className : resources) {
            className = className.replace('/', '.').substring(0,
className.length() - 6);
            log.info("Checking for EventHandler in class {}", className);
            try {
                Class c = Class.forName(className);
                if (c.isAnnotationPresent(Handler.class) &&
MessageHandler.class.isAssignableFrom(c)) {
                    Handler a = (Handler) c.getAnnotation(Handler.class);
                    registerHandler(a.name(), c);
                }
            } catch (Exception ex) {
                log.error("Cannot register handler class {}.", className,
ex);
            }
        }
    }



--
View this message in context: http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368p4030371.html
Sent from the Karaf - User mailing list archive at Nabble.com.

Re: Finding classes in a Bundle

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Philipp,

I guess that you mean "programmatically" ?

For an user perspective, you have the packages:export command for that.

Programmatically, you can use the PackageAdmin service for that.

Regards
JB

On 11/19/2013 06:20 PM, Philipp Hoenisch wrote:
> Hey guys,
>
> I'm not sure if this belongs in here or more to the Felix mailing list,
> anyway, I'll give it a try:
>
> versions i use:
> karaf: 2.3.2
> felix: 4.0.3
>
> I'm trying to find a class which is located in one of the packages without
> knowing it's name, however, I do know that it is annotated with a specific
> annotation. This class is autogenerated using a maven plugin.
> Anyway, what I try to do is to find this class (located in the same bundle)
> and create an instance during runtime, aka right after the bundleActivator
> was started
>
> As the package name is known I tried a simple approach aka:
>
> Thread.currentThread().getContextClassLoader().getResources(PACKAGENAME);
>
> and then iterate over the the files.
>
> The problem is that the ClassLoaders work a bit differently in OSGI right?
>
> Can anyone point me to the right direction how I can find a particular
> class?
>
> thank you in advance,
> best regards,
> Philipp
>
>
>
> --
> View this message in context: http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Finding classes in a Bundle

Posted by Andreas Pieber <an...@gmail.com>.
Hey Philipp,

It kind of depends on your use case. One option is to e.g. use a bundle
listener to get each bundle. On the bundles themselves you've options to
load resources and classes [1]. This might be an option. But as said,
depends quite heavily on your use case.

Kind regards,
Andreas

[1] http://www.osgi.org/javadoc/r4v43/core/org/osgi/framework/Bundle.html


On Tue, Nov 19, 2013 at 6:20 PM, Philipp Hoenisch <
philipp.hoenisch@gmail.com> wrote:

> Hey guys,
>
> I'm not sure if this belongs in here or more to the Felix mailing list,
> anyway, I'll give it a try:
>
> versions i use:
> karaf: 2.3.2
> felix: 4.0.3
>
> I'm trying to find a class which is located in one of the packages without
> knowing it's name, however, I do know that it is annotated with a specific
> annotation. This class is autogenerated using a maven plugin.
> Anyway, what I try to do is to find this class (located in the same bundle)
> and create an instance during runtime, aka right after the bundleActivator
> was started
>
> As the package name is known I tried a simple approach aka:
>
> Thread.currentThread().getContextClassLoader().getResources(PACKAGENAME);
>
> and then iterate over the the files.
>
> The problem is that the ClassLoaders work a bit differently in OSGI right?
>
> Can anyone point me to the right direction how I can find a particular
> class?
>
> thank you in advance,
> best regards,
> Philipp
>
>
>
> --
> View this message in context:
> http://karaf.922171.n3.nabble.com/Finding-classes-in-a-Bundle-tp4030368.html
> Sent from the Karaf - User mailing list archive at Nabble.com.
>