[Dev] Dependency Inject not working to get IEclipseContext Object

2015-11-03 Thread Nuwan Pallewela
Hi Devs,

I'm trying to get a IEclipseContext object to get IEventBroker. DI method
to get IEventBroker instance always gives a null. Alternative method to get
IEventBroker is through IEclipseContext instance. DI method to get it also
not working. Is there any way to get a instance of IEclipseContext object?

Thanks,
Nuwan

-- 
--

*Nuwan Chamara Pallewela*


*Software Engineer*

*WSO2, Inc. *http://wso2.com
*lean . enterprise . middleware*

Email   *nuw...@wso2.com *
Mobile  *+94719079739@*
___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


Re: [Dev] Dependency Inject not working to get IEclipseContext Object

2015-11-03 Thread Nuwan Pallewela
Hi,

I have tried the above method and got an exception of
"org.eclipse.e4.core.di.InjectionException:
Unable to process "EventBroker.logger": no actual value was found for the
argument "Logger".".
As discussed off line with kavith, this was occurred because of
ServiceContext does not give the logger for IEventBroker.

Alternative way to get IEventBroker instance and subscribe without using DI
was found using PlatformUI and shown in following code.

debuggerEventBroker = (IEventBroker)
PlatformUI.getWorkbench().getService(IEventBroker.class);
debuggerEventBroker.subscribe(ESBDebuggerConstants.ESBDEBUGTARGET_EVENT_TOPIC,
this);

And we can listen and handle events by implementing
org.osgi.service.event.EventHandler interface in our subscriber class.

Thanks,
Nuwan


On Tue, Nov 3, 2015 at 9:52 PM, Kavith Lokuhewage  wrote:

> Hi Nuwan,
>
> You can observe a reference to IEclipseContext using the bundleContext
> object. You can get a reference to bundleContext via bundle activator or
> via FrameworkUtil class.
>
> See following snippet.
>
> BundleContext bundleContext =
> FrameworkUtil.getBundle(SomeClassInYourBundle.class).getBundleContext();
> IEclipseContext eclipseContext =
> EclipseContextFactory.getServiceContext(bundleContext);
>
> BTW, how did you try to get a IEventBroker reference via Dependency
> Injection?
> If you are getting null for injected references always, I suspect that DI
> engine does not manage your object.
>
> FYI, Eclipse E4 DI engine only manages classes in Application Model or
> framework components by default (eg. commands, handlers, activators,
> declarative services, etc.). If your class is not a framework component and
> you manually instantiate it in your code using *new *operator, DI engine
> is not aware of that. In such kind of a situation, you need to manually
> inject the required references using ContextInjectionFactory. See following
> snippet.
>
> YourClass object = new YourClass();
> ContextInjectionFactory.inject(object, eclipseContext);
>
> For the ContextInjectionFactory.inject method, you need to pass the DI
> context (to tell DI engine from which context to obtain references which
> are needed to inject - this should be the global eclipse context in most
> cases) and your object. You can obtain the global eclipseContext using
> first snippet.
>
> For example, you can use below code to get a reference to IEventBroker in
> a custom class which is not a framework component.
>
> public YourClass{
>
> @Inject
> IEventBroker eventBroker;
>
> public YourClass(){}
>
> }
>
> Then, inject reference while instantiating.
>
> BundleContext bundleContext =
> FrameworkUtil.getBundle(YourClass.class).getBundleContext();
> IEclipseContext eclipseContext =
> EclipseContextFactory.getServiceContext(bundleContext);
> YourClass object = new YourClass();
> ContextInjectionFactory.inject(object, eclipseContext);
>
> HTH.
> Thanks,
>
> On Tue, Nov 3, 2015 at 9:07 PM, Nuwan Pallewela  wrote:
>
>> Hi Devs,
>>
>> I'm trying to get a IEclipseContext object to get IEventBroker. DI method
>> to get IEventBroker instance always gives a null. Alternative method to get
>> IEventBroker is through IEclipseContext instance. DI method to get it also
>> not working. Is there any way to get a instance of IEclipseContext object?
>>
>> Thanks,
>> Nuwan
>>
>> --
>> --
>>
>> *Nuwan Chamara Pallewela*
>>
>>
>> *Software Engineer*
>>
>> *WSO2, Inc. *http://wso2.com
>> *lean . enterprise . middleware*
>>
>> Email   *nuw...@wso2.com *
>> Mobile  *+94719079739 <%2B94719079739>@*
>>
>>
>>
>> ___
>> Dev mailing list
>> Dev@wso2.org
>> http://wso2.org/cgi-bin/mailman/listinfo/dev
>>
>>
>
>
> --
> *Kavith Lokuhewage*
> Software Engineer
> WSO2 Inc. - http://wso2.com
> lean . enterprise . middleware
> Mobile - +9477-9-145-123 | +9471-455-6-401
> Linkedin 
> Twitter 
>



-- 
--

*Nuwan Chamara Pallewela*


*Software Engineer*

*WSO2, Inc. *http://wso2.com
*lean . enterprise . middleware*

Email   *nuw...@wso2.com *
Mobile  *+94719079739@*
___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev


Re: [Dev] Dependency Inject not working to get IEclipseContext Object

2015-11-03 Thread Kavith Lokuhewage
Hi Nuwan,

You can observe a reference to IEclipseContext using the bundleContext
object. You can get a reference to bundleContext via bundle activator or
via FrameworkUtil class.

See following snippet.

BundleContext bundleContext =
FrameworkUtil.getBundle(SomeClassInYourBundle.class).getBundleContext();
IEclipseContext eclipseContext =
EclipseContextFactory.getServiceContext(bundleContext);

BTW, how did you try to get a IEventBroker reference via Dependency
Injection?
If you are getting null for injected references always, I suspect that DI
engine does not manage your object.

FYI, Eclipse E4 DI engine only manages classes in Application Model or
framework components by default (eg. commands, handlers, activators,
declarative services, etc.). If your class is not a framework component and
you manually instantiate it in your code using *new *operator, DI engine is
not aware of that. In such kind of a situation, you need to manually inject
the required references using ContextInjectionFactory. See following
snippet.

YourClass object = new YourClass();
ContextInjectionFactory.inject(object, eclipseContext);

For the ContextInjectionFactory.inject method, you need to pass the DI
context (to tell DI engine from which context to obtain references which
are needed to inject - this should be the global eclipse context in most
cases) and your object. You can obtain the global eclipseContext using
first snippet.

For example, you can use below code to get a reference to IEventBroker in a
custom class which is not a framework component.

public YourClass{

@Inject
IEventBroker eventBroker;

public YourClass(){}

}

Then, inject reference while instantiating.

BundleContext bundleContext =
FrameworkUtil.getBundle(YourClass.class).getBundleContext();
IEclipseContext eclipseContext =
EclipseContextFactory.getServiceContext(bundleContext);
YourClass object = new YourClass();
ContextInjectionFactory.inject(object, eclipseContext);

HTH.
Thanks,

On Tue, Nov 3, 2015 at 9:07 PM, Nuwan Pallewela  wrote:

> Hi Devs,
>
> I'm trying to get a IEclipseContext object to get IEventBroker. DI method
> to get IEventBroker instance always gives a null. Alternative method to get
> IEventBroker is through IEclipseContext instance. DI method to get it also
> not working. Is there any way to get a instance of IEclipseContext object?
>
> Thanks,
> Nuwan
>
> --
> --
>
> *Nuwan Chamara Pallewela*
>
>
> *Software Engineer*
>
> *WSO2, Inc. *http://wso2.com
> *lean . enterprise . middleware*
>
> Email   *nuw...@wso2.com *
> Mobile  *+94719079739 <%2B94719079739>@*
>
>
>
> ___
> Dev mailing list
> Dev@wso2.org
> http://wso2.org/cgi-bin/mailman/listinfo/dev
>
>


-- 
*Kavith Lokuhewage*
Software Engineer
WSO2 Inc. - http://wso2.com
lean . enterprise . middleware
Mobile - +9477-9-145-123 | +9471-455-6-401
Linkedin   Twitter

___
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev