Exemplo n.º 1
0
  /**
   * Registers all annotations specifying in {@link ComponentServiceContext}
   *
   * @param serviceHandler the {@link ServiceHandler}
   * @param annotation the {@link ComponentServiceContext} annotation.
   * @param getProvider if <b>true</b>, the suitable validator will be gotten from the given {@link
   *     Provider}, if <b>false</b>, the instance will be instantiated basing on <code>
   *     default constructor</code> specifying in corresponding object class.
   * @param provider the specified {@link Provider} container, if <code>getInject argument</code> is
   *     <b>true</b> and provider is null, one runtime-exception will be thrown.
   */
  @SuppressWarnings("unchecked")
  private static void registerAnnotationInCSC(
      ServiceHandler serviceHandler,
      ComponentServiceContext annotation,
      boolean getProvider,
      Provider provider) {

    if (getProvider == true && provider == null) {
      throw new JGentleRuntimeException("Provider must not be null while getProvider is true.");
    }
    Class<Annotation>[] annoList = (Class<Annotation>[]) annotation.annotations();
    Class<?>[] validators = annotation.validators();
    if (annoList.length != 0) {
      if (annoList.length < validators.length) {
        throw new JGentleRuntimeException(
            "invalid validators configuration in annotation: " + annotation);
      }
      for (int i = 0; i < annoList.length; i++) {
        if (i < validators.length) {
          AnnotationValidator<Annotation> validator;
          if (getProvider == false)
            validator =
                (AnnotationValidator<Annotation>) ReflectUtils.createInstance(validators[i]);
          else {
            validator = (AnnotationValidator<Annotation>) provider.getBean(validators[i]);
          }
          serviceHandler.registerAnnotation(annoList[i], validator);
        } else {
          serviceHandler.registerAnnotation(annoList[i]);
        }
      }
    }
  }
Exemplo n.º 2
0
  /**
   * Builds the context.
   *
   * @param serviceHandler the aoh
   * @param serviceProvider the service provider
   * @param configurations the configurations
   * @return the context
   */
  protected static Context buildContext(
      ServiceHandler serviceHandler, boolean serviceProvider, Configurable... configurations) {

    List<Map<String, Object>> OLArray = new ArrayList<Map<String, Object>>();
    ArrayList<Configurable> cfgInstanceList = new ArrayList<Configurable>();
    for (Configurable config : configurations) {
      try {
        config.setDefinitionManager(serviceHandler.getDefinitionManager());
        // Thực thi các xử lý được chỉ định bởi ATC
        config.initAnnotationConfig();
        cfgInstanceList.add(config);
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
      OLArray.add(config.getOptionsList());
    }
    Context result = null;
    if (!serviceProvider) {
      result = new ProviderCoreCreator(serviceHandler, OLArray);
    } else {
      result = new ServiceProviderImpl(serviceHandler, OLArray);
    }
    ((Provider) result).setConfigInstances(cfgInstanceList);
    return result;
  }