0
votes

I am trying to run one bundle in OSGi and in one class I am using @Inject annotation. Build is succesfull but the variable indexerConfiguration is still null. Why?

    @Named
    @Singleton
    public class IndexerConfiguration
    {

        private Indexer indexer;    
        private Scanner scanner;    
        private Map<String, IndexCreator> indexers;    

        @Inject
        public IndexerConfiguration( Indexer indexer,
                                     Scanner scanner,
                                     Map<String, IndexCreator> indexers ){
            this.indexer = indexer;
            this.scanner = scanner;
            this.indexers = indexers;
        }
       public Indexer getIndexer(){
          return indexer;
       }

The second class is in the same package:

public class MyFactory implements ManagedServiceFactory {
    public static final String PID = "myPid";

    private volatile DependencyManager m_dependencyManager;
    private final Map<String, Component> m_components = new HashMap<String, Component>();

    @Inject
    private IndexerConfiguration indexerConfiguration;


    @Override
    public String getName() {
        return "myBundle";
    }

    @Override
    public void updated(String pid,
            @SuppressWarnings("rawtypes") Dictionary properties)
            throws ConfigurationException {

        if (m_components.containsKey(pid)) {
            return;
        }

        Component component = m_dependencyManager
                .createComponent()
                .setImplementation(myOwnClass.class)
                .add(m_dependencyManager.createConfigurationDependency()
                        .setPid(pid));

        indexerConfiguration.getIndexer(); //this is still null :( why?
        m_components.put(pid, component);
        m_dependencyManager.add(component);

    }

all imports are ok, build is succesfull, but why the injection doesnt work?Thanks

1
OSGi itself does not support @Inject. Are you using any framework that should support it? - Christian Schneider
...hm I though that.. no no. no framework :( thanks - Curamrda

1 Answers

1
votes

There are some ways to work with @Inject in OSGi.

One is to use pax-cdi which adapts the CDI framework OpenWebBeans or Weld for OSGi. The nice thing here is that it aims to fully support the CDI standard. The current disadvantage is that it is still a bit experimental in some corners like jpa support.

Another one is to use the blueprint-maven-plugin from aries. It supports a small subset of CDI and maps it to a blueprint context at compile time. The advantage here is that it already support jpa and that you can combine it with hand written blueprint to implement more complex cases and leverage blueprint extensions like apache cxf for webservices and apache camel for integration. The disadvantage is that it only supports a small part of CDI and is not yet well documented. It still works quite well and I have used it in a large customer project to migrate from spring/tomcat to blueprint/OSGi. I have a small example project tasklist-blueprint-cdi at github. I plan to do a tutorial about it quite soon too.

If all this is too experimental for you the proven solutions for DI and services in OSGi are blueprint and declarative services. They have their own limitations though.