// Path: src/fi/mamk/osa/database/sql/DatabaseConfiguration.java
// public class DatabaseConfiguration {
//     
//     private String dbDriverName = null;
//     private String dbUser = null;
//     private String dbPassword = null;
//     private String dbURI = null;
//     
//     private int dbPoolMinSize = 0;
//     private int dbPoolMaxSize = 0;
//     private int dbTimeout = 0;
// 
//     private static final Logger logger = Logger.getLogger( DatabaseConfiguration.class  );
// 
//     /**
//      * Default constructor
//      */
//     public DatabaseConfiguration() {
//         
//     }
//     
//     /**
//      * Constructor
//      * @param xmlFile
//      */
//     public DatabaseConfiguration(String xmlFile) {
//         SAXBuilder builder = new SAXBuilder();
// 
//         try {
// 
//             InputStream is = new FileInputStream( xmlFile );
// 
//             Document doc = builder.build ( is );
//             Element root = doc.getRootElement();
// 
//             dbDriverName = root.getChild("dbDriverName").getTextTrim();
//             dbUser = root.getChild("dbUser").getTextTrim();
//             dbPassword = root.getChild("dbPassword").getTextTrim();
//             dbURI = root.getChild("dbURI").getTextTrim();
//             dbPoolMinSize = Integer.parseInt( root.getChild("dbPoolMinSize").getTextTrim() );
//             dbPoolMaxSize = Integer.parseInt( root.getChild("dbPoolMaxSize").getTextTrim() );
//             dbTimeout = Integer.parseInt( root.getChild("dbTimeout").getTextTrim() );
//             logger.info("XXXXXXXXXXXXXX "+dbUser+"\n");
// 
//         }   catch ( Exception ex ) {
//             logger.error( "Could not read configuration file: ", ex );
//         }
// 
//     }
// 
//     public String getDbDriverName() {
//         return dbDriverName;
//     }
// 
//     public String getDbUser() {
//         return dbUser;
//     }
// 
//     public String getDbPassword() {
//         return dbPassword;
//     }
// 
//     public String getDbURI() {
//         return dbURI;
//     }
// 
//     public int getDbPoolMinSize() {
//         return dbPoolMinSize;
//     }
// 
//     public int getDbPoolMaxSize() {
//         return dbPoolMaxSize;
//     }
// 
//     public int getDbTimeout() {
//         return dbTimeout;
//     }
// 
//     /**
//      * Function: setDbDriverName
//      * @param dbDriverName the dbDriverName to set
//      */
//     public void setDbDriverName(String dbDriverName) {
//         this.dbDriverName = dbDriverName;
//     }
// 
//     /**
//      * @param dbUser the dbUser to set
//      */
//     public void setDbUser(String dbUser) {
//         this.dbUser = dbUser;
//     }
// 
//     /**
//      * @param dbPassword the dbPassword to set
//      */
//     public void setDbPassword(String dbPassword) {
//         this.dbPassword = dbPassword;
//     }
// 
//     /**
//      * @param dbURI the dbURI to set
//      */
//     public void setDbURI(String dbURI) {
//         this.dbURI = dbURI;
//     }
// 
//     /**
//      * @param dbPoolMinSize the dbPoolMinSize to set
//      */
//     public void setDbPoolMinSize(int dbPoolMinSize) {
//         this.dbPoolMinSize = dbPoolMinSize;
//     }
// 
//     /**
//      * @param dbPoolMaxSize the dbPoolMaxSize to set
//      */
//     public void setDbPoolMaxSize(int dbPoolMaxSize) {
//         this.dbPoolMaxSize = dbPoolMaxSize;
//     }
// 
//     /**
//      * @param dbTimeout the dbTimeout to set
//      */
//     public void setDbTimeout(int dbTimeout) {
//         this.dbTimeout = dbTimeout;
//     }
// 
//     @Override
//     public String toString() {
//         
//         ReflectionToStringBuilder tsb = 
//             new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
//         return tsb.toString();
//     }
// }
// 
// Path: src/fi/mamk/osa/database/sql/SQLManager.java
// public abstract class SQLManager extends DatabaseManager {
//     
//     private static final Logger logger = Logger.getLogger(SQLManager.class);
//     
//     public static DataSource dataSource = null;
//     public static MiniConnectionPoolManager poolManager = null;
//   
//     /**
//      * Get db connection
//      * @return
//      * @throws SQLException
//      */
//     public static Connection getConnection() throws SQLException {
//         if (dataSource != null) {
//             return dataSource.getConnection();
//         }
//         if (poolManager != null) {
//             return poolManager.getConnection();
//         }
//         return null;
//     }
//     
//     /** 
//      * Checks the database status.
//      * 
//      * @return true if database is working properly. 
//      *         false if any of needed databases is offline
//      */
//     public abstract boolean testDatabase();    
// }


import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.dbcp.*;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Date;
import org.apache.log4j.Logger;
import fi.mamk.osa.database.sql.DatabaseConfiguration;
import fi.mamk.osa.database.sql.SQLManager;
        PoolableConnectionFactory poolableConnectionFactory =
                new PoolableConnectionFactory(
            connectionFactory,connectionPool,null,null,false,true);

        PoolingDataSource dataSource = 
            new PoolingDataSource(connectionPool);

        return dataSource;
    }

    public static void printDriverStats() throws Exception {
        ObjectPool connectionPool = ConnectionManager._pool;
        logger.info("NumActive: " + connectionPool.getNumActive());
        logger.info("NumIdle: " + connectionPool.getNumIdle());
    }

    /**
    *  getNumLockedProcesses - gets the 
    *  number of currently locked processes on the MySQL db
    *
    *  @return Number of locked processes
    */
    public int getNumLockedProcesses()
    {
        int num_locked_connections = 0;
        Connection con = null; 
        PreparedStatement p_stmt = null;  ResultSet rs = null;
        try
        {
            con = ConnectionManager.ds.getConnection();
// Path: src/fi/mamk/osa/database/sql/DatabaseConfiguration.java
// public class DatabaseConfiguration {
//     
//     private String dbDriverName = null;
//     private String dbUser = null;
//     private String dbPassword = null;
//     private String dbURI = null;
//     
//     private int dbPoolMinSize = 0;
//     private int dbPoolMaxSize = 0;
//     private int dbTimeout = 0;
// 
//     private static final Logger logger = Logger.getLogger( DatabaseConfiguration.class  );
// 
//     /**
//      * Default constructor
//      */
//     public DatabaseConfiguration() {
//         
//     }
//     
//     /**
//      * Constructor
//      * @param xmlFile
//      */
//     public DatabaseConfiguration(String xmlFile) {
//         SAXBuilder builder = new SAXBuilder();
// 
//         try {
// 
//             InputStream is = new FileInputStream( xmlFile );
// 
//             Document doc = builder.build ( is );
//             Element root = doc.getRootElement();
// 
//             dbDriverName = root.getChild("dbDriverName").getTextTrim();
//             dbUser = root.getChild("dbUser").getTextTrim();
//             dbPassword = root.getChild("dbPassword").getTextTrim();
//             dbURI = root.getChild("dbURI").getTextTrim();
//             dbPoolMinSize = Integer.parseInt( root.getChild("dbPoolMinSize").getTextTrim() );
//             dbPoolMaxSize = Integer.parseInt( root.getChild("dbPoolMaxSize").getTextTrim() );
//             dbTimeout = Integer.parseInt( root.getChild("dbTimeout").getTextTrim() );
//             logger.info("XXXXXXXXXXXXXX "+dbUser+"\n");
// 
//         }   catch ( Exception ex ) {
//             logger.error( "Could not read configuration file: ", ex );
//         }
// 
//     }
// 
//     public String getDbDriverName() {
//         return dbDriverName;
//     }
// 
//     public String getDbUser() {
//         return dbUser;
//     }
// 
//     public String getDbPassword() {
//         return dbPassword;
//     }
// 
//     public String getDbURI() {
//         return dbURI;
//     }
// 
//     public int getDbPoolMinSize() {
//         return dbPoolMinSize;
//     }
// 
//     public int getDbPoolMaxSize() {
//         return dbPoolMaxSize;
//     }
// 
//     public int getDbTimeout() {
//         return dbTimeout;
//     }
// 
//     /**
//      * Function: setDbDriverName
//      * @param dbDriverName the dbDriverName to set
//      */
//     public void setDbDriverName(String dbDriverName) {
//         this.dbDriverName = dbDriverName;
//     }
// 
//     /**
//      * @param dbUser the dbUser to set
//      */
//     public void setDbUser(String dbUser) {
//         this.dbUser = dbUser;
//     }
// 
//     /**
//      * @param dbPassword the dbPassword to set
//      */
//     public void setDbPassword(String dbPassword) {
//         this.dbPassword = dbPassword;
//     }
// 
//     /**
//      * @param dbURI the dbURI to set
//      */
//     public void setDbURI(String dbURI) {
//         this.dbURI = dbURI;
//     }
// 
//     /**
//      * @param dbPoolMinSize the dbPoolMinSize to set
//      */
//     public void setDbPoolMinSize(int dbPoolMinSize) {
//         this.dbPoolMinSize = dbPoolMinSize;
//     }
// 
//     /**
//      * @param dbPoolMaxSize the dbPoolMaxSize to set
//      */
//     public void setDbPoolMaxSize(int dbPoolMaxSize) {
//         this.dbPoolMaxSize = dbPoolMaxSize;
//     }
// 
//     /**
//      * @param dbTimeout the dbTimeout to set
//      */
//     public void setDbTimeout(int dbTimeout) {
//         this.dbTimeout = dbTimeout;
//     }
// 
//     @Override
//     public String toString() {
//         
//         ReflectionToStringBuilder tsb = 
//             new ReflectionToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
//         return tsb.toString();
//     }
// }
// 
// Path: src/fi/mamk/osa/database/sql/SQLManager.java
// public abstract class SQLManager extends DatabaseManager {
//     
//     private static final Logger logger = Logger.getLogger(SQLManager.class);
//     
//     public static DataSource dataSource = null;
//     public static MiniConnectionPoolManager poolManager = null;
//   
//     /**
//      * Get db connection
//      * @return
//      * @throws SQLException
//      */
//     public static Connection getConnection() throws SQLException {
//         if (dataSource != null) {
//             return dataSource.getConnection();
//         }
//         if (poolManager != null) {
//             return poolManager.getConnection();
//         }
//         return null;
//     }
//     
//     /** 
//      * Checks the database status.
//      * 
//      * @return true if database is working properly. 
//      *         false if any of needed databases is offline
//      */
//     public abstract boolean testDatabase();    
// }


// Path: src/fi/mamk/osa/database/sql/mysql/ConnectionManager.java
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.dbcp.*;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Date;
import org.apache.log4j.Logger;
import fi.mamk.osa.database.sql.DatabaseConfiguration;
import fi.mamk.osa.database.sql.SQLManager;

        PoolableConnectionFactory poolableConnectionFactory =
                new PoolableConnectionFactory(
            connectionFactory,connectionPool,null,null,false,true);

        PoolingDataSource dataSource = 
            new PoolingDataSource(connectionPool);

        return dataSource;
    }

    public static void printDriverStats() throws Exception {
        ObjectPool connectionPool = ConnectionManager._pool;
        logger.info("NumActive: " + connectionPool.getNumActive());
        logger.info("NumIdle: " + connectionPool.getNumIdle());
    }

    /**
    *  getNumLockedProcesses - gets the 
    *  number of currently locked processes on the MySQL db
    *
    *  @return Number of locked processes
    */
    public int getNumLockedProcesses()
    {
        int num_locked_connections = 0;
        Connection con = null; 
        PreparedStatement p_stmt = null;  ResultSet rs = null;
        try
        {
            con = ConnectionManager.ds.getConnection();
            p_stmt = con.prepareStatement("SHOW PROCESSLIST");