public static void main(String[] args) {
   Service service = ServiceLocator.getService("Service1");
   service.execute();
   service = ServiceLocator.getService("Service2");
   service.execute();
   service = ServiceLocator.getService("Service1");
   service.execute();
   service = ServiceLocator.getService("Service2");
   service.execute();
 }
  @Test
  public void testDoesNotUseTCCL() {
    Thread.currentThread()
        .setContextClassLoader(
            new ClassLoader() {
              @Override
              public Enumeration<URL> getResources(String name) throws IOException {
                throw new AssertionError();
              }
            });

    ServiceLocator serviceLocator = new ServiceLocator();
    serviceLocator.getService(TestService.class);
  }
  @Test
  public void testClassHierarchies() {
    ServiceLocator provider = new ServiceLocator();
    final Service service = new ChildTestService();
    provider.addService(service);
    assertThat(provider.getService(FooProvider.class), sameInstance(service));
    final Service fancyCacheProvider = new FancyCacheProvider();
    provider.addService(fancyCacheProvider);

    final Collection<CacheProvider> servicesOfType =
        provider.getServicesOfType(CacheProvider.class);
    assertThat(servicesOfType, is(not(empty())));
    assertThat(servicesOfType.iterator().next(), sameInstance(fancyCacheProvider));
  }
Пример #4
0
  /* package */ @SuppressWarnings({"unchecked"})
  void register() {
    ServiceLocator locator = getServiceLocator();

    ActiveDescriptor<?> myselfReified = locator.reifyDescriptor(this);

    DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
    DynamicConfiguration dc = dcs.createDynamicConfiguration();

    //        habitat.add(this);
    HK2Loader loader = this.model.classLoaderHolder;

    Set<Type> ctrs = new HashSet<Type>();
    ctrs.add(myselfReified.getImplementationClass());

    if (ConfigBean.class.isAssignableFrom(this.getClass())) {
      ctrs.add(ConfigBean.class);
    }

    DomDescriptor<Dom> domDesc =
        new DomDescriptor<Dom>(
            this, ctrs, Singleton.class, getImplementation(), new HashSet<Annotation>());
    domDesc.setLoader(loader);
    domDescriptor = dc.addActiveDescriptor(domDesc, false);

    String key = getKey();
    for (String contract : model.contracts) {
      ActiveDescriptor<Dom> alias = new AliasDescriptor<Dom>(locator, domDescriptor, contract, key);
      dc.addActiveDescriptor(alias, false);
    }
    if (key != null) {
      ActiveDescriptor<Dom> alias =
          new AliasDescriptor<Dom>(locator, domDescriptor, model.targetTypeName, key);
      dc.addActiveDescriptor(alias, false);
    }

    dc.commit();

    serviceHandle = getHabitat().getServiceHandle(domDescriptor);
  }
 @Test
 public void testCanOverrideDefaultServiceFromServiceLoader() {
   ServiceLocator locator = new ServiceLocator(new ExtendedTestService());
   TestService testService = locator.getService(TestService.class);
   assertThat(testService, instanceOf(ExtendedTestService.class));
 }