Ejemplo n.º 1
0
    private Codec<?> buildCustomCodec(Element e) {
      String classAttr = e.getAttribute(ATTR_CODEC);
      Class<?> customCodecClass;
      try {
        customCodecClass = Class.forName(classAttr);
        if (CustomCodec.class.isAssignableFrom(customCodecClass)) {
          CustomCodec customCodec = (CustomCodec) customCodecClass.newInstance();

          Map<String, String> params = new HashMap<>();
          List<Element> paramElements = getSubElements(e);
          for (Element paramElement : paramElements) {
            params.put(paramElement.getAttribute(ATTR_KEY), paramElement.getAttribute(ATTR_VALUE));
          }
          if (customCodec instanceof Configurable) {
            ((Configurable) customCodec).configure(params);
          }

          return new CustomCodecAdapter(customCodec, getOptionalInteger(e, ATTR_LENGTH));
        } else {
          throw new ConfigException(format("Invalid custom class %s", classAttr));
        }
      } catch (ClassNotFoundException
          | SecurityException
          | InstantiationException
          | IllegalAccessException
          | IllegalArgumentException ex) {
        throw new ConfigException(ex.getMessage(), ex);
      }
    }