001/* 002 * (C) Copyright 2018 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.runtime.codec; 020 021import java.util.Map; 022 023import org.nuxeo.lib.stream.codec.AvroBinaryCodec; 024import org.nuxeo.lib.stream.codec.AvroConfluentCodec; 025import org.nuxeo.lib.stream.codec.AvroJsonCodec; 026import org.nuxeo.lib.stream.codec.AvroMessageCodec; 027import org.nuxeo.lib.stream.codec.Codec; 028import org.nuxeo.runtime.api.Framework; 029import org.nuxeo.runtime.avro.AvroService; 030 031/** 032 * Factory to generate Avro codec with different flavors 033 * 034 * @since 10.3 035 */ 036public class AvroCodecFactory implements CodecFactory { 037 038 public static final String KEY_SCHEMA_REGISTRY_URLS = "schemaRegistryUrls"; 039 040 public static final String DEFAULT_SCHEMA_REGISTRY_URLS = "http://localhost:8081"; 041 042 public static final String KEY_ENCODING = "encoding"; 043 044 public static final String DEFAULT_ENCODING = "default"; 045 046 protected String encoding; 047 048 protected String schemaRegistryUrls; 049 050 @Override 051 public void init(Map<String, String> options) { 052 this.encoding = options.getOrDefault(KEY_ENCODING, DEFAULT_ENCODING); 053 this.schemaRegistryUrls = options.getOrDefault(KEY_SCHEMA_REGISTRY_URLS, DEFAULT_SCHEMA_REGISTRY_URLS); 054 } 055 056 @Override 057 public <T> Codec<T> newCodec(Class<T> objectClass) { 058 switch (encoding) { 059 case "json": 060 return new AvroJsonCodec<>(objectClass); 061 case "binary": 062 return new AvroBinaryCodec<>(objectClass); 063 case "confluent": 064 return new AvroConfluentCodec<>(objectClass, schemaRegistryUrls); 065 case "message": 066 default: 067 return new AvroMessageCodec<>(objectClass, Framework.getService(AvroService.class).getSchemaStore()); 068 } 069 } 070}