Search code examples
eclipsee4

Can't inject EPartService


In my bundle activator I try to inject fields 'IEventBroker' and 'EPartService'. But injected only first. Code follows:

@Inject
IEventBroker m_broker;
@Inject
EPartService m_part_service;

public void start(BundleContext context) throws Exception {

    IEclipseContext service_context = EclipseContextFactory.getServiceContext(context);
    ContextInjectionFactory.inject(this, service_context);
    boolean contains = service_context.containsKey(EPartService.class);

    // contains is always "true", but m_part_service is always "null"
    // all follows invocations returns "null" too
    //
    // service_context.get(EPartService.class); 
    // service_context.getActiveLeaf().getActive(EPartService.class);   
    // service_context.getActiveLeaf().getLocal(EPartService.class);    
    // context.getServiceReference(EPartService.class); 

    // m_broker always non-null
    m_broker.subscribe(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, new EventHandler()
        {
            @Override
            public void handleEvent(Event event)
            {
                // ... bla bla bla
            }
        });
}

In internal lists of IEclipseContext I found EPartService. Can you help me? What I did wrong?


Solution

  • Bundle activators are not injected so you can't use @Inject.

    The context returned by EclipseContextFactory.getServiceContext has very limited contents and can't be used to access things like EPartService.

    In any case the bundle activator generally isn't even run until something else in your plugin is used, so it would be too late the see the startup complete message anyway.

    So all this means you can't do what you want in the bundle activator start method.

    To get notified about the app startup complete event you can use the application LifeCycle class or define an AddOn - both these classes are injected.

    In those classes use a method like:

    @Optional
    @Inject
    public void appStartupComplete(@UIEventTopic(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE)
                                   org.osgi.service.event.Event event)