Search in sources :

Example 1 with ReflectData

use of org.apache.avro.reflect.ReflectData in project camel by apache.

the class AvroComponent method applyToConfiguration.

/**
     * Applies endpoint parameters to configuration & resolves protocol and other required configuration properties.
     */
private void applyToConfiguration(AvroConfiguration config, URI endpointUri, Map<String, Object> parameters) throws Exception {
    config.parseURI(endpointUri, parameters, this);
    setProperties(config, parameters);
    if (config.getProtocol() == null && config.getProtocolClassName() != null) {
        Class<?> protocolClass = getCamelContext().getClassResolver().resolveClass(config.getProtocolClassName());
        if (protocolClass != null) {
            try {
                Field f = protocolClass.getField("PROTOCOL");
                if (f != null) {
                    Protocol protocol = (Protocol) f.get(null);
                    config.setProtocol(protocol);
                }
            } catch (NoSuchFieldException e) {
                ReflectData reflectData = ReflectData.get();
                config.setProtocol(reflectData.getProtocol(protocolClass));
                config.setReflectionProtocol(true);
            }
        }
    }
    if (config.getProtocol() == null) {
        throw new IllegalArgumentException("Avro configuration does not contain protocol");
    }
    if (config.getMessageName() != null && !config.getProtocol().getMessages().containsKey(config.getMessageName())) {
        throw new IllegalArgumentException("Message " + config.getMessageName() + " is not defined in protocol");
    }
    if (config.isSingleParameter()) {
        Map<String, Protocol.Message> messageMap = config.getProtocol().getMessages();
        Iterable<Protocol.Message> messagesToCheck = config.getMessageName() == null ? messageMap.values() : Collections.singleton(messageMap.get(config.getMessageName()));
        for (Protocol.Message message : messagesToCheck) {
            if (message.getRequest().getFields().size() != 1) {
                throw new IllegalArgumentException("Single parameter option can't be used with message " + message.getName() + " because it has " + message.getRequest().getFields().size() + " parameters defined");
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ReflectData(org.apache.avro.reflect.ReflectData) Protocol(org.apache.avro.Protocol)

Aggregations

Field (java.lang.reflect.Field)1 Protocol (org.apache.avro.Protocol)1 ReflectData (org.apache.avro.reflect.ReflectData)1