package hirondelle.web4j.config;
import hirondelle.web4j.database.ConnectionSource;
import hirondelle.web4j.database.DAOException;
import hirondelle.web4j.util.Util;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionSrc implements ConnectionSource {
public static final String DEFAULT_CONN_STRING = "DefaultDbConnectionString";
public static final String DEFAULT = "DEFAULT";
public final void init(Map<String, String> aConfig){
fDefaultDbConnString = aConfig.get(DEFAULT_CONN_STRING);
ensureAllSettingsPresent();
fMapNameToConnectionString = new LinkedHashMap<String, String>();
fMapNameToConnectionString.put(DEFAULT, fDefaultDbConnString);
fLogger.config(
"Connection strings : " + Util.logOnePerLine(fMapNameToConnectionString)
);
}
public final Set<String> getDatabaseNames(){
return Collections.unmodifiableSet(fMapNameToConnectionString.keySet());
}
public final Connection getConnection() throws DAOException {
return getConnectionByName(DEFAULT);
}
public final Connection getConnection(String aDatabaseName) throws DAOException {
return getConnectionByName(aDatabaseName);
}
protected Connection getConnectionByName(String aDbName) throws DAOException {
Connection result = null;
String dbConnString = getConnectionString(aDbName);
if( ! Util.textHasContent(dbConnString) ){
throw new IllegalArgumentException(
"Unknown database name : " + Util.quote(aDbName)
);
}
try {
Context initialContext = new InitialContext();
DataSource datasource = (DataSource)initialContext.lookup(dbConnString);
if ( datasource == null ){
fLogger.severe("Datasource is null for : " + dbConnString);
}
result = datasource.getConnection();
}
catch (NamingException ex){
throw new DAOException(
"Config error with JNDI and datasource, for db " + Util.quote(dbConnString), ex
);
}
catch (SQLException ex ){
throw new DAOException(
"Cannot get JNDI connection from datasource, for db " + Util.quote(dbConnString),
ex
);
}
return result;
}
protected String getConnectionString(String aDbName){
return fMapNameToConnectionString.get(aDbName);
}
private static Map<String, String> fMapNameToConnectionString;
private static String fDefaultDbConnString;
private static final Logger fLogger = Util.getLogger(ConnectionSrc.class);
private static void ensureAllSettingsPresent(){
if( ! Util.textHasContent(fDefaultDbConnString) ) {
logError(DEFAULT_CONN_STRING);
}
}
private static void logError(String aSettingName){
fLogger.severe("Web.xml missing init-param setting for " + aSettingName);
}
}