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.

Reply via email to