120 lines
3.3 KiB
Java
120 lines
3.3 KiB
Java
package com.stephenschafer.email;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.sql.Connection;
|
|
import java.sql.SQLException;
|
|
import java.util.Properties;
|
|
|
|
import javax.naming.InitialContext;
|
|
import javax.naming.NamingException;
|
|
import javax.sql.DataSource;
|
|
|
|
public class Configuration {
|
|
public static final Configuration INSTANCE;
|
|
private String jndiName;
|
|
private DbConnectionPool pool;
|
|
private String privilegedHost;
|
|
private String defaultDomain;
|
|
private boolean loaded;
|
|
|
|
private Configuration() {
|
|
jndiName = null;
|
|
pool = null;
|
|
loaded = false;
|
|
}
|
|
|
|
public void load() throws IOException, ClassNotFoundException, SQLException {
|
|
if (loaded) {
|
|
return;
|
|
}
|
|
final Properties systemProps = System.getProperties();
|
|
final String catalinaHome = systemProps.getProperty("catalina.home");
|
|
String propertiesFileName = System.getenv("EMAIL_PROPERTIES");
|
|
if (propertiesFileName == null) {
|
|
propertiesFileName = catalinaHome + "/conf/Catalina/localhost/email.properties";
|
|
}
|
|
final File propertiesFile = new File(propertiesFileName);
|
|
final Properties properties = new Properties();
|
|
try (FileInputStream fis = new FileInputStream(propertiesFile)) {
|
|
properties.load(fis);
|
|
}
|
|
Logger.logFilename = properties.getProperty("log.filename");
|
|
Logger.log("********************************* Starting");
|
|
jndiName = properties.getProperty("db.jndi");
|
|
final String url = properties.getProperty("db.url");
|
|
final String username = properties.getProperty("db.username");
|
|
final String password = properties.getProperty("db.password");
|
|
if (jndiName == null) {
|
|
pool = new DbConnectionPool("com.mysql.jdbc.Driver", url, username, password);
|
|
}
|
|
privilegedHost = properties.getProperty("priv-host");
|
|
final String defaultDomain = properties.getProperty("default-domain");
|
|
this.defaultDomain = defaultDomain == null ? "schafer.cc" : defaultDomain;
|
|
Logger.log("initialized, jndiName = " + jndiName);
|
|
loaded = true;
|
|
}
|
|
|
|
public Connection getConnection() throws SQLException, NamingException {
|
|
final String jndiName = getJndiName();
|
|
if (jndiName != null) {
|
|
final InitialContext initialContext = new InitialContext();
|
|
final DataSource datasource = (DataSource) initialContext.lookup(jndiName);
|
|
SQLException lastException = null;
|
|
int i = 0;
|
|
while (i < 4) {
|
|
try {
|
|
return datasource.getConnection();
|
|
}
|
|
catch (final SQLException e) {
|
|
Logger.log("Failed to get connection, " + (3 - i) + " retrys left", e);
|
|
lastException = e;
|
|
try {
|
|
Thread.sleep(5000L);
|
|
}
|
|
catch (final InterruptedException ex) {
|
|
}
|
|
++i;
|
|
continue;
|
|
}
|
|
}
|
|
if (lastException != null) {
|
|
throw lastException;
|
|
}
|
|
}
|
|
return getPool().getConnection(8, false, true);
|
|
}
|
|
|
|
public String getJndiName() {
|
|
return jndiName;
|
|
}
|
|
|
|
public void setJndiName(final String jndiName) {
|
|
this.jndiName = jndiName;
|
|
}
|
|
|
|
public DbConnectionPool getPool() {
|
|
return pool;
|
|
}
|
|
|
|
public void setPool(final DbConnectionPool pool) {
|
|
this.pool = pool;
|
|
}
|
|
|
|
static {
|
|
INSTANCE = new Configuration();
|
|
}
|
|
|
|
public String getPrivilegedHost() {
|
|
return privilegedHost;
|
|
}
|
|
|
|
public String getDefaultDomain() {
|
|
return defaultDomain;
|
|
}
|
|
|
|
public void setDefaultDomain(final String defaultDomain) {
|
|
this.defaultDomain = defaultDomain;
|
|
}
|
|
}
|