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 final void init(Map<String, String> aConfig){
fDefaultDbConnString = aConfig.get(DEFAULT_CONN_STRING);
fAccessControlDbConnectionString =
aConfig.get(ACCESS_CONTROL_CONN_STRING)
;
fTranslationDbConnectionString = aConfig.get(TRANSLATION_CONN_STRING);
ensureAllSettingsPresent();
fMapNameToConnectionString = new LinkedHashMap<String, String>();
fMapNameToConnectionString.put(DEFAULT, fDefaultDbConnString);
fMapNameToConnectionString.put(ACCESS_CONTROL, fAccessControlDbConnectionString);
fMapNameToConnectionString.put(TRANSLATION, fTranslationDbConnectionString);
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);
}
public static final String DEFAULT = "DEFAULT";
public static final String ACCESS_CONTROL = "ACCESS_CONTROL";
public static final String TRANSLATION = "TRANSLATION";
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();
if ( initialContext == null ) {
fLogger.severe(
"DataSource problem. InitialContext is null. Db : " + Util.quote(dbConnString)
);
}
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 final String DEFAULT_CONN_STRING = "DefaultDbConnectionString";
private static String fDefaultDbConnString;
private static final String ACCESS_CONTROL_CONN_STRING =
"AccessControlDbConnectionString"
;
private static String fAccessControlDbConnectionString;
private static final String TRANSLATION_CONN_STRING = "TranslationDbConnectionString";
private static String fTranslationDbConnectionString;
private static final Logger fLogger = Util.getLogger(ConnectionSrc.class);
private static void ensureAllSettingsPresent(){
if( ! Util.textHasContent(fDefaultDbConnString) ) {
logError(DEFAULT_CONN_STRING);
}
if( ! Util.textHasContent(fTranslationDbConnectionString) ) {
logError(TRANSLATION_CONN_STRING);
}
if ( ! Util.textHasContent(fAccessControlDbConnectionString) ) {
logError(ACCESS_CONTROL_CONN_STRING);
}
}
private static void logError(String aSettingName){
fLogger.severe("Web.xml missing init-param setting for " + aSettingName);
}
}