@Test
 public void verifyPerThreadScoped() {
   ServiceHandle<KryoFactory> serviceHandle = locator.getServiceHandle(KryoFactory.class);
   ActiveDescriptor<KryoFactory> activeDescriptor = serviceHandle.getActiveDescriptor();
   Class<? extends Annotation> scope = activeDescriptor.getScopeAnnotation();
   assertThat(scope).isEqualTo(Singleton.class);
 }
  /** Tests that a third-party service that fails in dispose */
  @Test
  public void testFailingInDisposeThirdPartyService() {
    ServiceLocator locator = LocatorHelper.getServiceLocator(RecordingErrorService.class);

    AlwaysFailInDisposeActiveDescriptor thirdPartyDescriptor =
        new AlwaysFailInDisposeActiveDescriptor();

    ServiceLocatorUtilities.addOneDescriptor(locator, thirdPartyDescriptor);

    ActiveDescriptor<?> serviceDescriptor =
        locator.getBestDescriptor(
            BuilderHelper.createContractFilter(SimpleService.class.getName()));
    Assert.assertNotNull(serviceDescriptor);

    ServiceHandle<SimpleService> handle = locator.getServiceHandle(SimpleService.class);
    Assert.assertNotNull(handle);
    Assert.assertNotNull(handle.getService());

    handle.destroy();

    List<ErrorInformation> errors =
        locator.getService(RecordingErrorService.class).getAndClearErrors();

    Assert.assertEquals(1, errors.size());

    ErrorInformation ei = errors.get(0);

    Assert.assertEquals(ErrorType.SERVICE_DESTRUCTION_FAILURE, ei.getErrorType());
    Assert.assertEquals(serviceDescriptor, ei.getDescriptor());
    Assert.assertNull(ei.getInjectee());

    Throwable associatedException = ei.getAssociatedException();
    Assert.assertTrue(associatedException.getMessage().contains(ERROR_STRING));
  }
Example #3
0
  /**
   * Enables the Configuration subsystem of HK2. This call is idempotent
   *
   * @param locator The non-null service locator in which to enable the configuration subsystem
   */
  public static void enableConfigurationSystem(ServiceLocator locator) {
    ServiceHandle<ConfiguredByContext> alreadyThere =
        locator.getServiceHandle(ConfiguredByContext.class);
    if (alreadyThere != null) {
      // The assumption is that if this service is there then this is already on, don't do it again
      return;
    }

    ManagerUtilities.enableConfigurationHub(locator);

    ServiceLocatorUtilities.addClasses(locator, true, ConfiguredValidator.class);

    ServiceLocatorUtilities.addClasses(
        locator,
        true,
        ConfiguredByContext.class,
        ConfigurationValidationService.class,
        ConfiguredByInjectionResolver.class,
        ConfigurationListener.class,
        ChildInjectResolverImpl.class);

    // Creates demand, starts the thing off
    locator.getService(ConfigurationListener.class);
  }
Example #4
0
  private <T> XmlRootHandle<T> unmarshallClass(
      URI uri,
      ModelImpl model,
      XmlServiceParser localParser,
      XMLStreamReader reader,
      boolean advertise,
      boolean advertiseInHub)
      throws Exception {
    long elapsedUpToJAXB = 0;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedUpToJAXB = System.currentTimeMillis();
    }

    Hk2JAXBUnmarshallerListener listener =
        new Hk2JAXBUnmarshallerListener(jaUtilities, classReflectionHelper);

    long jaxbUnmarshallElapsedTime = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      jaxbUnmarshallElapsedTime = System.currentTimeMillis();
      elapsedUpToJAXB = jaxbUnmarshallElapsedTime - elapsedUpToJAXB;
      Logger.getLogger()
          .debug("Time up to parsing " + uri + " is " + elapsedUpToJAXB + " milliseconds");
    }

    T root;
    if (localParser != null) {
      root = localParser.parseRoot(model, uri, listener);
    } else {
      root = XmlStreamImpl.parseRoot(this, model, reader, listener);
    }

    long elapsedJAXBToAdvertisement = 0;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedJAXBToAdvertisement = System.currentTimeMillis();
      jaxbUnmarshallElapsedTime = elapsedJAXBToAdvertisement - jaxbUnmarshallElapsedTime;
      Logger.getLogger()
          .debug(
              "Time parsing "
                  + uri
                  + " is "
                  + jaxbUnmarshallElapsedTime
                  + " milliseconds "
                  + ", now with "
                  + jaUtilities.getNumGenerated()
                  + " proxies generated and "
                  + jaUtilities.getNumPreGenerated()
                  + " pre generated proxies loaded");
    }

    DynamicChangeInfo changeControl =
        new DynamicChangeInfo(
            jaUtilities,
            ((advertiseInHub) ? hub : null),
            this,
            ((advertise) ? dynamicConfigurationService : null),
            serviceLocator);

    for (BaseHK2JAXBBean base : listener.getAllBeans()) {
      String instanceName = Utilities.createInstanceName(base);
      base._setInstanceName(instanceName);

      base._setDynamicChangeInfo(changeControl);

      if (DEBUG_PARSING) {
        Logger.getLogger().debug("XmlServiceDebug found bean " + base);
      }
    }
    if (DEBUG_PARSING) {
      Logger.getLogger().debug("XmlServiceDebug after parsing all beans in " + uri);
    }

    long elapsedPreAdvertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedPreAdvertisement = System.currentTimeMillis();
      elapsedJAXBToAdvertisement = elapsedPreAdvertisement - elapsedJAXBToAdvertisement;
      Logger.getLogger()
          .debug(
              "Time from parsing to PreAdvertisement "
                  + uri
                  + " is "
                  + elapsedJAXBToAdvertisement
                  + " milliseconds");
    }

    DynamicConfiguration config =
        (advertise) ? dynamicConfigurationService.createDynamicConfiguration() : null;
    WriteableBeanDatabase wdb = (advertiseInHub) ? hub.getWriteableDatabaseCopy() : null;

    LinkedList<BaseHK2JAXBBean> allBeans = listener.getAllBeans();
    List<ActiveDescriptor<?>> addedDescriptors =
        new ArrayList<ActiveDescriptor<?>>(allBeans.size());
    for (BaseHK2JAXBBean bean : allBeans) {
      ActiveDescriptor<?> added = Utilities.advertise(wdb, config, bean);
      if (added != null) {
        addedDescriptors.add(added);
      }
    }

    long elapsedHK2Advertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHK2Advertisement = System.currentTimeMillis();
      elapsedPreAdvertisement = elapsedHK2Advertisement - elapsedPreAdvertisement;
      Logger.getLogger()
          .debug(
              "Time from JAXB to PreAdvertisement "
                  + uri
                  + " is "
                  + elapsedPreAdvertisement
                  + " milliseconds");
    }

    if (config != null) {
      config.commit();
    }

    long elapsedHubAdvertisement = 0L;
    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHubAdvertisement = System.currentTimeMillis();
      elapsedHK2Advertisement = elapsedHubAdvertisement - elapsedHK2Advertisement;
      Logger.getLogger()
          .debug(
              "Time to advertise "
                  + uri
                  + " in HK2 is "
                  + elapsedHK2Advertisement
                  + " milliseconds");
    }

    if (wdb != null) {
      wdb.commit(new XmlHubCommitMessage() {});
    }

    // The following is put here so that InstanceLifecycleListeners will get invoked at this time
    // rather than later so that defaulting can be done now.  It doesn't hurt because these are
    // all constant descriptors
    for (ActiveDescriptor<?> ad : addedDescriptors) {
      serviceLocator.getServiceHandle(ad).getService();
    }

    if (JAUtilities.DEBUG_GENERATION_TIMING) {
      elapsedHubAdvertisement = System.currentTimeMillis() - elapsedHubAdvertisement;
      Logger.getLogger()
          .debug(
              "Time to advertise "
                  + uri
                  + " in Hub is "
                  + elapsedHubAdvertisement
                  + " milliseconds");
    }

    return new XmlRootHandleImpl<T>(
        this, hub, root, model, uri, advertise, advertiseInHub, changeControl);
  }