Exemplo n.º 1
0
    @SuppressWarnings("unchecked")
    private <V> V initializeConfigurationInstance(
        ServiceComposite serviceComposite,
        UnitOfWork uow,
        ServiceDescriptor serviceModel,
        String identity)
        throws InstantiationException {
      Module module = api.moduleOf(serviceComposite);
      Usecase usecase = UsecaseBuilder.newUsecase("Configuration:" + me.identity().get());
      UnitOfWork buildUow = module.newUnitOfWork(usecase);

      EntityBuilder<V> configBuilder =
          buildUow.newEntityBuilder(serviceModel.<V>configurationType(), identity);

      // Check for defaults
      String s = identity + ".properties";
      Class<?> type = first(api.serviceDescriptorFor(serviceComposite).types());
      // Load defaults from classpath root if available
      if (type.getResource(s) == null && type.getResource("/" + s) != null) {
        s = "/" + s;
      }
      InputStream asStream = type.getResourceAsStream(s);
      if (asStream != null) {
        try {
          PropertyMapper.map(asStream, (Composite) configBuilder.instance());
        } catch (IOException e1) {
          InstantiationException exception =
              new InstantiationException("Could not read underlying Properties file.");
          exception.initCause(e1);
          throw exception;
        }
      }

      try {
        configBuilder.newInstance();
        buildUow.complete();

        // Try again
        return (V) findConfigurationInstanceFor(serviceComposite, uow);
      } catch (Exception e1) {
        InstantiationException ex =
            new InstantiationException(
                "Could not instantiate configuration, and no Properties file was found ("
                    + s
                    + ")");
        ex.initCause(e1);
        throw ex;
      }
    }
Exemplo n.º 2
0
    @SuppressWarnings("unchecked")
    public <V> V findConfigurationInstanceFor(ServiceComposite serviceComposite, UnitOfWork uow)
        throws InstantiationException {
      ServiceDescriptor serviceModel = api.serviceDescriptorFor(serviceComposite);

      String identity = serviceComposite.identity().get();
      V configuration;
      try {
        configuration = uow.get(serviceModel.<V>configurationType(), identity);
        uow.pause();
      } catch (NoSuchEntityException | EntityTypeNotFoundException e) {
        return (V) initializeConfigurationInstance(serviceComposite, uow, serviceModel, identity);
      }
      return configuration;
    }