Exemplo n.º 1
0
  private final void setTypeComparatorFactory(
      TypeComparatorFactory<?> factory, String classNameKey, String parametersPrefix) {
    // sanity check the factory type
    InstantiationUtil.checkForInstantiation(factory.getClass());

    // store the type
    this.config.setString(classNameKey, factory.getClass().getName());
    // store the parameters
    final DelegatingConfiguration parameters =
        new DelegatingConfiguration(this.config, parametersPrefix);
    factory.writeParametersToConfig(parameters);
  }
Exemplo n.º 2
0
  private final <T> TypeComparatorFactory<T> getTypeComparatorFactory(
      String classNameKey, String parametersPrefix, ClassLoader cl) {
    // check the class name
    final String className = this.config.getString(classNameKey, null);
    if (className == null) {
      return null;
    }

    // instantiate the class
    @SuppressWarnings("unchecked")
    final Class<TypeComparatorFactory<T>> superClass =
        (Class<TypeComparatorFactory<T>>) (Class<?>) TypeComparatorFactory.class;
    final TypeComparatorFactory<T> factory;
    try {
      Class<? extends TypeComparatorFactory<T>> clazz =
          Class.forName(className, true, cl).asSubclass(superClass);
      factory = InstantiationUtil.instantiate(clazz, superClass);
    } catch (ClassNotFoundException cnfex) {
      throw new RuntimeException(
          "The class '"
              + className
              + "', noted in the configuration as "
              + "comparator factory, could not be found. It is not part of the user code's class loader resources.");
    } catch (ClassCastException ccex) {
      throw new CorruptConfigurationException(
          "The class noted in the configuration as the comparator factory "
              + "is no subclass of TypeComparatorFactory.");
    }

    // parameterize the comparator factory
    final Configuration parameters = new DelegatingConfiguration(this.config, parametersPrefix);
    try {
      factory.readParametersFromConfig(parameters, cl);
    } catch (ClassNotFoundException cnfex) {
      throw new RuntimeException(
          "The type serializer factory could not load its parameters from the "
              + "configuration due to missing classes.",
          cnfex);
    }

    return factory;
  }