0

I'm trying to make a reusable component that creates a ServiceLocator from the services defined in the inhabitants file. I need to determine if the ServiceLocator has services apart from the built in ones. If it doesn't, maybe log some warning to the user. Something like

ServiceLocator locator =  ServiceLocatorUtilities.createAndPopulateServiceLocator();
List<?> services = locator.getAllServices(BuilderHelper.allFilter());
if (services.isEmpty()) {
    LOGGER.log(Level.WARNING, 
            "No services. Make sure inhabitants generator is working correctly.");
}

This of course doesn't work because the services still contains all the built in services. I could create a Filter like

public class NonBuiltInFilter implements Filter {

    private static final Set<String> builtInClasses = new HashSet<>(
            Arrays.asList("org.jvnet.hk2.internal.ServiceLocatorImpl",
                          "org.jvnet.hk2.internal.ThreeThirtyResolver",
                          "org.jvnet.hk2.internal.DynamicConfigurationServiceImpl",
                          "org.jvnet.hk2.internal.DefaultClassAnalyzer",
                          "org.jvnet.hk2.internal.ServiceLocatorRuntimeImpl",
                          "org.jvnet.hk2.internal.InstantiationServiceImpl")
    );

    @Override
    public boolean matches(Descriptor d) {
        return !builtInClasses.contains(d.getImplementation());
    } 
}
...
List<?> services = locator.getAllServices(new NonBuiltInFilter());

This works now but it is not a great solution, as those classes could change at any time.

So I'm just wondering if there is any standard way to check if the ServiceLocator has any services, aside from the built in ones. Or some other work-around that is not as fragile as the Filter above.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • The only thing that I think of that might be more satisfying would be to base your filter on the package org.jvnet.hk2.internal rather than the specific ones you mention here. We could start adding some sort of meta-data to indicate that something is a "system" service, but the definition of some system services would be a little tricky. Like do services provided by hk2 but not in an empty ServiceLocator count as system services? – jwells131313 Dec 23 '15 at 23:27
  • Actually, another thing that we could do is have BuilderHelper provide a "SystemService" filter that would be guaranteed to contain all the initial services in it rather than have you write it. That would probably work even better – jwells131313 Dec 23 '15 at 23:30
  • @jwells131313 Yes I like the second solution. Feature request? :-) – Paul Samsotha Dec 24 '15 at 00:26
  • 1
    already coded up, just need it reviewed and there isn't anyone around until the holidays are over... – jwells131313 Dec 24 '15 at 03:10

1 Answers1

1

In the latest version of hk2 in the hk2-locator module there is an API that returns the filter you are looking for:

Hk2LocatorUtilities

This has to be in the implementation module because another implementation may have a different set of initial services, so it isn't a general feature. Given that, there are no other implementations of hk2 that I know of, so it's still pretty general!

jwells131313
  • 2,364
  • 1
  • 16
  • 26