001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
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
017package com.echothree.util.server.kafka;
018
019import fish.payara.cloud.connectors.kafka.api.KafkaConnectionFactory;
020import javax.naming.InitialContext;
021import javax.naming.NamingException;
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025public class KafkaConnectionFactoryResource {
026
027    private final Log log = LogFactory.getLog(KafkaConnectionFactoryResource.class);
028
029    private static final String KCF = "java:/KafkaConnectionFactory";
030
031    private static final KafkaConnectionFactoryResource instance = new KafkaConnectionFactoryResource();
032
033    private final KafkaConnectionFactory kafkaConnectionFactory;
034
035    @SuppressWarnings("BanJNDI")
036    protected KafkaConnectionFactoryResource() {
037        KafkaConnectionFactory kafkaConnectionFactory;
038
039        // This provides a soft failure vs. the hard failure from using @Resource.
040        try {
041            kafkaConnectionFactory = InitialContext.doLookup(KCF);
042            log.info("Found " + KCF + ", KafkaConnectionFactory available");
043        } catch (NamingException ne) {
044            kafkaConnectionFactory = null;
045            log.error("Unable to locate " + KCF + ", KafkaConnectionFactory not available");
046        }
047
048        this.kafkaConnectionFactory = kafkaConnectionFactory;
049    }
050
051    public static KafkaConnectionFactoryResource getInstance() {
052        return instance;
053    }
054
055    public KafkaConnectionFactory getKafkaConnectionFactory() {
056        return kafkaConnectionFactory;
057    }
058
059}