package com.github.aureliano.verbum_domini.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.github.aureliano.verbum_domini.AppConfiguration;
import com.github.aureliano.verbum_domini.exception.VerbumDominiException;

public class ConnectionSingleton {

	private static ConnectionSingleton instance;
	
	private Connection connection;
	
	private ConnectionSingleton() {
		super();
	}
	
	public static ConnectionSingleton instance() {
		if (instance == null) {
			instance = new ConnectionSingleton();
		}
		
		return instance;
	}
	
	public Connection getConnection() {
		return this.connection;
	}
	
	public void startUp() {
		if (!this.isInitialized()) {
			this.createConnection();
		}
	}
	
	public void shutDown() {
		try {
			this.connection.close();
		} catch (SQLException ex) {
			throw new VerbumDominiException(ex);
		}
	}
	
	public boolean isInitialized() {
		return (this.connection != null);
	}
	
	private void createConnection() {
		AppConfiguration config = AppConfiguration.instance();
		String driver = config.getProperty("jdbc.driver_class");
		if (driver == null) {
			this.throwUnsetPropertyException("jdbc.driver_class");
		}
		
		String url = config.getProperty("jdbc.connection.url");
		if (url == null) {
			this.throwUnsetPropertyException("jdbc.connection.url");
		}
		
		String user = config.getProperty("jdbc.connection.user");
		String password = config.getProperty("jdbc.connection.password");
		
		try {
			Class.forName(driver);
			this.connection = DriverManager.getConnection(url, user, password);
		} catch (Exception ex) {
			
		}
	}
	
	private void throwUnsetPropertyException(String property) {
		throw new VerbumDominiException("Property '" + property + "' not set in app-configuration.properties file.");
	}
}
@@ -1489,28 +1489,298 @@
 %0A%09%09%09
-Class.forName(driver
+System.out.println(%22Loading driver class %22 + driver);%0A%09%09%09Class.forName(driver);%0A%09%09%09%0A%09%09%09System.out.println(%22Establishing connection to %22 + url);%0A%09%09%09System.out.println(%22User: %22 + user);%0A%09%09%09System.out.println(%22Password: %22 + ((password == null) ? %22not provided%22 : password.replaceAll(%22.%22, %22*%22))
 );%0A%09
@@ -1876,19 +1876,55 @@
 n ex) %7B%0A
-
 %09%09%09
+throw new VerbumDominiException(ex);
 %0A%09%09%7D%0A%09%7D%0A

Add verbosity to connection establishment.