All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cz.o2.proxima.scheme.avro.AvroSerializerFactory Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017-2024 O2 Czech Republic, a.s.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.o2.proxima.scheme.avro;

import com.google.auto.service.AutoService;
import cz.o2.proxima.core.scheme.SerializationException;
import cz.o2.proxima.core.scheme.ValueSerializer;
import cz.o2.proxima.core.scheme.ValueSerializerFactory;
import cz.o2.proxima.core.util.Classpath;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.Schema;
import org.apache.avro.specific.SpecificRecord;

/** Avro serializer factory for manipulate with SpecificRecords */
@Slf4j
@AutoService(ValueSerializerFactory.class)
public class AvroSerializerFactory implements ValueSerializerFactory {

  private static final long serialVersionUID = 1L;

  private final Map> serializersCache = new ConcurrentHashMap<>();

  @Override
  public String getAcceptableScheme() {
    return "avro";
  }

  @SuppressWarnings("unchecked")
  @Override
  public  ValueSerializer getValueSerializer(URI specifier) {
    return (ValueSerializer)
        serializersCache.computeIfAbsent(specifier, AvroSerializerFactory::createSerializer);
  }

  private static  ValueSerializer createSerializer(URI uri) {
    return new ValueSerializer() {

      private static final long serialVersionUID = 1L;

      final String avroClassName = uri.getSchemeSpecificPart();

      transient M defaultInstance = null;

      transient AvroSerializer avroSerializer = null;

      @Override
      public Optional deserialize(byte[] input) {
        if (avroSerializer == null) {
          avroSerializer = new AvroSerializer<>(getAvroSchemaForClass(avroClassName));
        }
        try {
          return Optional.of(avroSerializer.deserialize(input));
        } catch (IOException ex) {
          log.warn("Unable to deserialize avro payload", ex);
          return Optional.empty();
        }
      }

      @Override
      public byte[] serialize(M value) {
        if (avroSerializer == null) {
          avroSerializer = new AvroSerializer<>(getAvroSchemaForClass(avroClassName));
        }
        try {
          return avroSerializer.serialize(value);
        } catch (IOException ex) {
          throw new SerializationException("Unable to serialize avro object", ex);
        }
      }

      @SuppressWarnings("unchecked")
      @Override
      public M getDefault() {
        if (defaultInstance == null) {
          defaultInstance =
              Classpath.newInstance(
                  (Class) Classpath.findClass(avroClassName, SpecificRecord.class));
        }
        return defaultInstance;
      }

      private Schema getAvroSchemaForClass(String avroClassName) {
        try {
          Class avroClass =
              Classpath.findClass(avroClassName, SpecificRecord.class);
          Method method = avroClass.getMethod("getSchema");
          return (Schema) method.invoke(Classpath.newInstance(avroClass));
        } catch (IllegalAccessException
            | IllegalArgumentException
            | SecurityException
            | InvocationTargetException
            | NoSuchMethodException ex) {

          throw new IllegalArgumentException("Cannot get schema from class " + avroClassName, ex);
        }
      }

      @Override
      public boolean isUsable() {
        try {
          return deserialize(serialize(getDefault())).isPresent();
        } catch (Exception ex) {
          log.warn(
              "Exception during (de)serialization of default value for "
                  + "class {}. Please consider making all fields optional, otherwise "
                  + "you might encounter unexpected behavior.",
              avroClassName,
              ex);
        }
        try {
          return getDefault() != null;
        } catch (Exception ex) {
          log.warn("Error getting default value for {}", avroClassName, ex);
          return false;
        }
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy