001/* 002 * (C) Copyright 2006-2018 Nuxeo (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 * 017 * Contributors: 018 * anechaev 019 */ 020package org.nuxeo.runtime.kafka; 021 022import java.util.Properties; 023import java.util.Set; 024import java.util.stream.Collectors; 025 026import org.apache.kafka.clients.consumer.ConsumerConfig; 027import org.apache.kafka.clients.producer.ProducerConfig; 028import org.nuxeo.lib.stream.log.kafka.KafkaUtils; 029import org.nuxeo.runtime.model.DefaultComponent; 030import org.nuxeo.runtime.model.Descriptor; 031 032public class KafkaConfigServiceImpl extends DefaultComponent implements KafkaConfigService { 033 034 public static final String XP_KAFKA_CONFIG = "kafkaConfig"; 035 036 public static final int APPLICATION_STARTED_ORDER = -600; 037 038 protected static final String DEFAULT_BOOTSTRAP_SERVERS = "DEFAULT_TEST"; 039 040 protected static final long START_STAMP = System.currentTimeMillis(); 041 042 @Override 043 public int getApplicationStartedOrder() { 044 // since there is no dependencies, let's start before main nuxeo core services 045 return APPLICATION_STARTED_ORDER; 046 } 047 048 @Override 049 public Set<String> listConfigNames() { 050 return getDescriptors(XP_KAFKA_CONFIG).stream() 051 .map(Descriptor::getId) 052 .collect(Collectors.toSet()); 053 } 054 055 @Override 056 public Properties getProducerProperties(String configName) { 057 Properties ret = getDescriptor(configName).producerProperties.properties; 058 if (DEFAULT_BOOTSTRAP_SERVERS.equals(ret.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG))) { 059 ret.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaUtils.getBootstrapServers()); 060 } 061 return ret; 062 } 063 064 @Override 065 public Properties getConsumerProperties(String configName) { 066 Properties ret = getDescriptor(configName).consumerProperties.properties; 067 if (DEFAULT_BOOTSTRAP_SERVERS.equals(ret.get(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG))) { 068 ret.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaUtils.getBootstrapServers()); 069 } 070 return ret; 071 } 072 073 @Override 074 public String getTopicPrefix(String configName) { 075 KafkaConfigDescriptor config = getDescriptor(configName); 076 String ret = config.topicPrefix == null ? "" : config.topicPrefix; 077 if (config.randomPrefix) { 078 ret += START_STAMP + "-"; 079 } 080 return ret; 081 } 082 083 @Override 084 public Properties getAdminProperties(String configName) { 085 Properties ret = getDescriptor(configName).adminProperties.properties; 086 if (DEFAULT_BOOTSTRAP_SERVERS.equals(ret.get(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG))) { 087 ret.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, KafkaUtils.getBootstrapServers()); 088 } 089 return ret; 090 } 091 092 protected KafkaConfigDescriptor getDescriptor(String configName) { 093 KafkaConfigDescriptor descriptor = getDescriptor(XP_KAFKA_CONFIG, configName); 094 if (descriptor == null) { 095 throw new IllegalArgumentException("Unknown configuration name: " + configName); 096 } 097 return descriptor; 098 } 099 100}