001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.persistence;
018
019import com.echothree.util.common.exception.PersistenceDatabaseException;
020import javax.naming.InitialContext;
021import javax.naming.NamingException;
022import javax.sql.DataSource;
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.jooq.DSLContext;
026import org.jooq.SQLDialect;
027import org.jooq.impl.DSL;
028
029public class DslContextFactory {
030    
031    private static final Log log = LogFactory.getLog(DslContextFactory.class);
032    
033    private static final String DS = "java:/EchoThreeDS";
034    private static final String NTDS = "java:/EchoThreeNTDS";
035    
036    private static final DslContextFactory instance = new DslContextFactory();
037    
038    private final DataSource ds;
039    private final DataSource ntds;
040
041    @SuppressWarnings("BanJNDI")
042    protected DslContextFactory() {
043        try {
044            var jndiContext = new InitialContext();
045            ds = (DataSource)jndiContext.lookup(DS);
046            ntds = (DataSource)jndiContext.lookup(NTDS);
047        } catch (NamingException ne) {
048            throw new PersistenceDatabaseException(ne);
049        }
050    }
051    
052    public static DslContextFactory getInstance() {
053        return instance;
054    }
055    
056    public DSLContext getDslContext() {
057        var dslContent = DSL.using(ds, SQLDialect.MYSQL);
058        
059        if(PersistenceDebugFlags.LogConnections)
060            log.info("getDslContext() returning " + dslContent);
061
062        return dslContent;
063    }
064    
065    public DSLContext getNTDslContext() {
066        var dslContent = DSL.using(ntds, SQLDialect.MYSQL);
067        
068        if(PersistenceDebugFlags.LogConnections)
069            log.info("getNTDslContext() returning " + dslContent);
070
071        return dslContent;
072    }
073    
074}