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