dev@hk2.java.net

Re: HK2 on OSGi

From: Tang Yong <tangyong_at_cn.fujitsu.com>
Date: Sat, 18 Jan 2014 11:45:55 +0900

 From log, service locator is null in descriptor, so, here, I am
assuming the following two possible issues:

1. osgi-adaptor has a bug
2. the logic of obtaining the ServiceLocator maybe has some issue

because this is your inner project and using ProSyst implementation,
for better locating the reason, besides john's suggestion, I suggest that

1) making a small sample(same reproduce logic as your inner project)
using *felix* rathan than ProSyst. Pl. using felix 4.0.2

2) seeing 1)'s result and whether can reproduce the issue. If being
reproducing, pl. file a bug into hk2 jira and upload your small sample
in order that others including me can investigate the issue better.

Thanks
Tang

John Wells wrote:
> You might also want to do this:
>
> https://hk2.java.net/2.2.0/apidocs/org/glassfish/hk2/utilities/ServiceLocatorUtilities.html#enableLookupExceptions%28org.glassfish.hk2.api.ServiceLocator%29
>
>
> to ensure that any reification errors are thrown up to the getService.
> That should give us more information on why you are failing to get your
> services (hopefully)...
>
> The reason that certain lookup exceptions are not enabled by default is
> a long and bloody debate where "secure-by-default" sort of "won" over
> useability (IMO)...
>
> John Wells
> john.wells_at_oracle.comNOSPAM
>
> On 1/17/2014 7:04 AM, Martin Soch wrote:
>> Hello Tang,
>>
>> thanks for the tip. However the osgi-adapter is started (bundle with
>> ID 25). Attaching a run-log-file so you see what is going on there.
>>
>> Sahoo also mentioned (in separate email) that creating ServiceLocator
>> every time getEventService(name) is called might cause returning null.
>> However it looks like false alarm too.
>>
>> Any other ideas/tips ?
>>
>> Thanks
>> Martin
>>
>> On 17.1.2014 10:58, Tang Yong wrote:
>>> also cc sahoo, a point: hk2-osgi-adapter bundle must be started.
>>>
>>> Thanks
>>> Tang
>>>
>>> Martin Soch wrote:
>>>> Hello HK2 developers,
>>>>
>>>> I am evaluating HK2 and I am experiencing troubles with named services
>>>> when running in OSGi environment.
>>>> I would like to dynamically create named services with the following
>>>> code snippet:
>>>>
>>>> public EventService getEventService(String name) {
>>>> EventService es = locator.getService(EventService.class, name);
>>>> if (es == null) {
>>>> // there is no service in the registry with given name ->
>>>> create it
>>>> DynamicConfigurationService dcs =
>>>> locator.getService(DynamicConfigurationService.class);
>>>> DynamicConfiguration config =
>>>> dcs.createDynamicConfiguration();
>>>> Descriptor d = createEventServiceDescriptor(name);
>>>> config.bind(d);
>>>> config.commit();
>>>> es = locator.getService(EventService.class, name);
>>>> }
>>>> return es;
>>>> }
>>>>
>>>> private Descriptor createEventServiceDescriptor(String name) {
>>>> return BuilderHelper.link(EventServiceImpl.class).
>>>> to(EventService.class).
>>>> named(name).
>>>> in(Singleton.class).
>>>> build();
>>>> }
>>>>
>>>> "EventService" is the interface/contract and the "EventServiceImpl" is
>>>> the implementation of that interface.
>>>> The getEventService(String) works fine when running as standalone Java
>>>> application but in OSGi environment it always returns "null".
>>>>
>>>> Under OSGi I am using hk2-osgi-adapter to obtain the ServiceLocator
>>>> with the following code:
>>>>
>>>> Main main = (Main) bundleContext.getService((ServiceReference)
>>>> bundleContext.getServiceReference(Main.class));
>>>> ModulesRegistry registry = (ModulesRegistry)
>>>> bundleContext.getService((ServiceReference)
>>>> bundleContext.getServiceReference(ModulesRegistry.class));
>>>> StartupContext ctx = new StartupContext();
>>>> ServiceLocator l = main.createServiceLocator(registry, ctx, null,
>>>> null);
>>>>
>>>> where ModulesRegistry means com.sun.enterprise.module.ModulesRegistry,
>>>> Main means com.sun.enterprise.module.bootstrap.Main and bundleContext
>>>> is instance of org.osgi.framework.BundleContext.
>>>>
>>>> Do you have any idea why I am always getting null under OSGi ?
>>>> I am using ProSyst implementation of OSGi and HK2-2.2.0-b27 from Maven
>>>> repo.
>>>>
>>>> Thanks
>>>> Martin
>>>>
>>>
>
>

-- 
----------------------
Tang Yong
Senior Engineer
GlassFish Committer (OSGi & OSGi-JavaEE)
OSGi Alliance Supporter
Blog: http://osgizone.typepad.com/tangyong/
Nanjing Fujitsu NanDa Software Tec CO.,LTD
http://www.fujitsu.com/cn/fnst
Tel: +86-25-86630566-8310
Fax: +86-25-83317685              
----------------------