简体   繁体   中英

How to inject a declarative service my JvmModelInferrer

I have an xtext project which contains MyDslJvmModelInferrer. In this inferrer I would like to use my declarative service, which is defined like described in this tutorial , and process the parsed model within the service.

How can I get access to the declarative service within my ModelInferrer?

Thank's in advance.

You can add a instance binding and then use the default api code as described in the tutorial

def void configureMyService(Binder binder) {
    val BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); 
    val ServiceReference<?> serviceReference = bundleContext.getServiceReference(IMyService.getName());
    val IMyService service = bundleContext.getService(serviceReference) as IMyService; 
    binder.bind(IMyService).toInstance(service)
}

then a normal @Inject IMyService myService should work

update: i dont know when the osgi services will be available so you may have to do this lazy

in the module

import com.google.inject.Provider
...
def Provider<IMyService> provideIMyService() {
    return new MyServiceProvider
}

create the provider class

import com.google.inject.Provider
class MyServiceProvider implements Provider<IMyService> {

    override get() {
        val BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); 
        val ServiceReference<?> serviceReference = bundleContext.getServiceReference(IMyService.getName());
        val IMyService service = bundleContext.getService(serviceReference) as IMyService; 
        return service
    }

}

and use it import com.google.inject.Provider ... @Inject Provider myServiceProvider ... // inside method val myService = myServiceProvider.get

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM