예제 #1
0
    @Override
    void complete(final DynamicConfiguration configuration, final HK2Loader defaultLoader) {
      if (this.loader == null) {
        this.loader = defaultLoader;
      }

      ActiveDescriptorBuilder builder =
          BuilderHelper.activeLink(service).named(name).andLoadWith(this.loader);

      if (scope != null) {
        builder.in(scope);
      }

      if (ranked != null) {
        builder.ofRank(ranked);
      }

      for (String key : metadata.keySet()) {
        for (String value : metadata.get(key)) {
          builder.has(key, value);
        }
      }

      for (Annotation annotation : qualifiers) {
        builder.qualifiedBy(annotation);
      }

      for (Type contract : contracts) {
        builder.to(contract);
      }

      configuration.bind(builder.build());
    }
예제 #2
0
  /** Tests that adding the whole annotation adds the class and string */
  @Test
  public void testDescWithAnnotationGivenDirectly() {
    AbstractActiveDescriptor<?> desc =
        BuilderHelper.activeLink(ServiceA.class)
            .in(ServiceLocatorUtilities.getSingletonAnnotation())
            .build();

    Assert.assertEquals(
        desc.getScopeAsAnnotation(), ServiceLocatorUtilities.getSingletonAnnotation());
    Assert.assertEquals(desc.getScopeAnnotation(), Singleton.class);
    Assert.assertEquals(desc.getScope(), Singleton.class.getName());
  }
예제 #3
0
    @Override
    void complete(DynamicConfiguration configuration, HK2Loader defaultLoader) {
      if (this.loader == null) {
        this.loader = defaultLoader;
      }

      ActiveDescriptorBuilder factoryDescriptorBuilder =
          BuilderHelper.activeLink(factoryClass).named(name).andLoadWith(this.loader);
      if (factoryScope != null) {
        factoryDescriptorBuilder.in(factoryScope);
      }

      ActiveDescriptorBuilder descriptorBuilder =
          BuilderHelper.activeLink(factoryClass).named(name).andLoadWith(this.loader);
      if (scope != null) {
        descriptorBuilder.in(scope);
      }

      if (ranked != null) {
        //                factoryContractDescriptor.ofRank(factoryRank);
        descriptorBuilder.ofRank(ranked);
      }

      for (Annotation qualifier : qualifiers) {
        factoryDescriptorBuilder.qualifiedBy(qualifier);
        descriptorBuilder.qualifiedBy(qualifier);
      }

      for (Type contract : contracts) {
        factoryDescriptorBuilder.to(new ParameterizedTypeImpl(Factory.class, contract));
        descriptorBuilder.to(contract);
      }

      configuration.bind(
          new FactoryDescriptorsImpl(
              factoryDescriptorBuilder.build(), descriptorBuilder.buildFactory()));
    }
예제 #4
0
  /** Ensures we can add a scope to a service with no scope at all */
  @Test
  public void testGiveClassWithNoScopeAScope() {
    ServiceLocator locator = LocatorHelper.create();
    ServiceLocatorUtilities.enableLookupExceptions(locator);

    Descriptor desc =
        BuilderHelper.activeLink(NoScopeService.class)
            .to(NoScopeService.class)
            .in(ServiceLocatorUtilities.getSingletonAnnotation())
            .build();

    ServiceLocatorUtilities.addOneDescriptor(locator, desc);

    NoScopeService one = locator.getService(NoScopeService.class);
    NoScopeService two = locator.getService(NoScopeService.class);

    Assert.assertNotNull(one);
    Assert.assertEquals(one, two);
  }
예제 #5
0
  /**
   * Ensures that you can change from one hard-coded scope on the descriptor to a different scope
   */
  @Test
  public void testSwitchFromExplicitScopeToGhostedScope() {
    ServiceLocator locator = LocatorHelper.create();
    ServiceLocatorUtilities.enableLookupExceptions(locator);
    ServiceLocatorUtilities.addClasses(locator, GhostedContext.class);

    Descriptor desc =
        BuilderHelper.activeLink(SingletonScopedService.class)
            .to(SingletonScopedService.class)
            .in(new GhostedScopeImpl(0))
            .build();

    ServiceLocatorUtilities.addOneDescriptor(locator, desc);

    SingletonScopedService one = locator.getService(SingletonScopedService.class);
    SingletonScopedService two = locator.getService(SingletonScopedService.class);

    Assert.assertNotNull(one);
    Assert.assertNotSame(one, two);
  }
예제 #6
0
  /** Tests a simple and mostly empty descriptor */
  @Test
  public void testOnlyImpl() {
    AbstractActiveDescriptor<?> desc = BuilderHelper.activeLink(ServiceA.class).build();

    Assert.assertSame(ServiceA.class, desc.getImplementationClass());
    Assert.assertSame(ServiceA.class.getName(), desc.getImplementation());

    Assert.assertNull(desc.getName());

    Assert.assertEquals(PerLookup.class, desc.getScopeAnnotation());
    Assert.assertEquals(PerLookup.class.getName(), desc.getScope());

    Assert.assertTrue(desc.getAdvertisedContracts().isEmpty());
    Assert.assertTrue(desc.getContractTypes().isEmpty());

    Assert.assertTrue(desc.getQualifiers().isEmpty());
    Assert.assertTrue(desc.getQualifierAnnotations().isEmpty());

    Assert.assertNull(desc.getLoader());
    Assert.assertSame(DescriptorType.CLASS, desc.getDescriptorType());
    Assert.assertTrue(desc.getInjectees().isEmpty());

    Assert.assertFalse(desc.isReified());
  }
예제 #7
0
  /** Tests that we can change the value of a field in a scope with a ghost added annotation */
  @Test
  public void testModifyExistingScopeWithDifferentValue() {
    ServiceLocator locator = LocatorHelper.create();
    ServiceLocatorUtilities.addClasses(locator, GhostedContext.class);

    Descriptor desc =
        BuilderHelper.activeLink(GhostedServiceWithValue.class)
            .to(GhostedServiceWithValue.class)
            .in(new GhostedScopeImpl(0))
            .build();

    ServiceLocatorUtilities.addOneDescriptor(locator, desc);

    GhostedServiceWithValue ghosted = locator.getService(GhostedServiceWithValue.class);
    Assert.assertNotNull(ghosted);

    ActiveDescriptor<?> gDesck = ghosted.getDescriptor();

    Annotation anno = gDesck.getScopeAsAnnotation();
    Assert.assertNotNull(anno);

    GhostedScope gs = (GhostedScope) anno;
    Assert.assertEquals(0, gs.value());
  }
예제 #8
0
    @Override
    void complete(DynamicConfiguration configuration, HK2Loader defaultLoader) {
      if (this.loader == null) {
        this.loader = defaultLoader;
      }

      AbstractActiveDescriptor factoryContractDescriptor =
          BuilderHelper.createConstantDescriptor(factory);
      factoryContractDescriptor.addContractType(factory.getClass());
      factoryContractDescriptor.setName(name);
      factoryContractDescriptor.setLoader(this.loader);

      ActiveDescriptorBuilder descriptorBuilder =
          BuilderHelper.activeLink(factory.getClass()).named(name).andLoadWith(this.loader);
      if (scope != null) {
        descriptorBuilder.in(scope);
      }

      if (ranked != null) {
        descriptorBuilder.ofRank(ranked);
      }

      for (Annotation qualifier : qualifiers) {
        factoryContractDescriptor.addQualifierAnnotation(qualifier);
        descriptorBuilder.qualifiedBy(qualifier);
      }

      for (Type contract : contracts) {
        factoryContractDescriptor.addContractType(
            new ParameterizedTypeImpl(Factory.class, contract));
        descriptorBuilder.to(contract);
      }

      configuration.bind(
          new FactoryDescriptorsImpl(factoryContractDescriptor, descriptorBuilder.buildFactory()));
    }
예제 #9
0
  @Test
  public void testDescWithFields() {
    SimpleQualifier1 sq1 = new SimpleQualifier1Impl();

    AbstractActiveDescriptor<?> desc =
        BuilderHelper.activeLink(ServiceA.class)
            .to(SimpleInterface1.class)
            .in(Singleton.class)
            .qualifiedBy(sq1)
            .named(NAME)
            .has(NAME, NAME)
            .ofRank(1)
            .proxy()
            .proxyForSameScope(false)
            .localOnly()
            .andLoadWith(
                new HK2Loader() {

                  @Override
                  public Class<?> loadClass(String className) throws MultiException {
                    throw new AssertionError("not called");
                  }
                })
            .build();

    Assert.assertSame(ServiceA.class, desc.getImplementationClass());
    Assert.assertSame(ServiceA.class.getName(), desc.getImplementation());

    Assert.assertSame(NAME, desc.getName());

    Assert.assertEquals(Singleton.class, desc.getScopeAnnotation());
    Assert.assertEquals(Singleton.class.getName(), desc.getScope());

    Assert.assertEquals(Boolean.TRUE, desc.isProxiable());
    Assert.assertEquals(Boolean.FALSE, desc.isProxyForSameScope());
    Assert.assertEquals(DescriptorVisibility.LOCAL, desc.getDescriptorVisibility());

    testSetOfOne(desc.getAdvertisedContracts(), SimpleInterface1.class.getName());
    testSetOfOne(desc.getContractTypes(), SimpleInterface1.class);

    boolean foundSQ1 = false;
    boolean foundName = false;
    for (Annotation anno : desc.getQualifierAnnotations()) {
      if (anno.annotationType().equals(SimpleQualifier1.class)) {
        foundSQ1 = true;
      } else if (anno.annotationType().equals(Named.class)) {
        String annoName = ((Named) anno).value();
        Assert.assertSame(annoName, NAME);
        foundName = true;
      } else {
        Assert.fail("Unknown annotation found " + anno);
      }
    }
    Assert.assertTrue(foundName);
    Assert.assertTrue(foundSQ1);

    foundSQ1 = false;
    foundName = false;
    for (String anno : desc.getQualifiers()) {
      if (anno.equals(SimpleQualifier1.class.getName())) {
        foundSQ1 = true;
      } else if (anno.equals(Named.class.getName())) {
        foundName = true;
      } else {
        Assert.fail("Unknown annotation found " + anno);
      }
    }
    Assert.assertTrue(foundName);
    Assert.assertTrue(foundSQ1);

    Assert.assertNotNull(desc.getLoader());
    Assert.assertSame(DescriptorType.CLASS, desc.getDescriptorType());
    Assert.assertTrue(desc.getInjectees().isEmpty());

    Assert.assertFalse(desc.isReified());
  }