001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.conf.Settings; 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 private Settings getSettings() { 058 // The datasource selects the tenant database. Generated schema names 059 // describe the model and must not qualify runtime SQL. 060 return new Settings().withRenderSchema(false); 061 } 062 063 public DSLContext getDslContext() { 064 var dslContent = DSL.using(ds, SQLDialect.MYSQL, getSettings()); 065 066 if(PersistenceDebugFlags.LogConnections) 067 log.info("getDslContext() returning " + dslContent); 068 069 return dslContent; 070 } 071 072 public DSLContext getNTDslContext() { 073 var dslContent = DSL.using(ntds, SQLDialect.MYSQL, getSettings()); 074 075 if(PersistenceDebugFlags.LogConnections) 076 log.info("getNTDslContext() returning " + dslContent); 077 078 return dslContent; 079 } 080 081}