/*
 *  Copyright 2014 Waratek Ltd.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.waratek.spiracle.sql.util;

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

import javax.servlet.ServletContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.waratek.spiracle.sql.servlet.util.ParameterNullFix;

public class ConnectionUtil {
	private static final Logger logger = Logger.getLogger(ParameterNullFix.class);

	public static Connection getConnection(ServletContext application, String connectionType) throws SQLException {
		Connection con = null;
		logger.info("Connection Type: " + connectionType);
		if(connectionType.equals("c3p0")) {
			ComboPooledDataSource ds = (ComboPooledDataSource)application.getAttribute("connectionPool");
			con = ds.getConnection();
		} else if(connectionType.equals("java")) {
			con = (Connection)application.getAttribute("connection");
		} else if(connectionType.equals("spring")) {
			FileSystemXmlApplicationContext context = (FileSystemXmlApplicationContext)application.getAttribute("springContext");
			DriverManagerDataSource dmds = (DriverManagerDataSource)context.getBean("dataSource");
			con = dmds.getConnection();
		} else if(connectionType.equals("jndi")) {
			DataSource ds = (DataSource) application.getAttribute("jndiConnectionPool");
			con = ds.getConnection();
		}

		logger.info("Returning connection: " + con.toString());
		return con;
	}
}

@@ -1086,17 +1086,21 @@
 nUtil %7B%0A
-%09
+%0A    
 private 
@@ -1171,17 +1171,20 @@
 lass);%0A%0A
-%09
+    
 public s
@@ -1287,18 +1287,24 @@
 ption %7B%0A
-%09%09
+        
 Connecti
@@ -1318,18 +1318,24 @@
 = null;%0A
-%09%09
+        
 logger.i
@@ -1381,12 +1381,19 @@
 e);%0A
-%09%09
+        
 if
+ 
 (con
@@ -1415,22 +1415,46 @@
 als(
-%22c3p0%22)) %7B%0A%09%09%09
+Constants.C3P0_ORACLE)) %7B%0A            
 Comb
@@ -1499,16 +1499,17 @@
 aSource)
+ 
 applicat
@@ -1529,30 +1529,281 @@
 ute(
-%22connectionPool%22);%0A%09%09%09
+Constants.ORACLE_CONNECTION_POOL);%0A            con = ds.getConnection();%0A        %7D else if (connectionType.equals(Constants.C3P0_MYSQL)) %7B%0A            ComboPooledDataSource ds = (ComboPooledDataSource) application.getAttribute(Constants.MYSQL_CONNECTION_POOL);%0A            
 con 
@@ -1816,34 +1816,40 @@
 etConnection();%0A
-%09%09
+        
 %7D else if(connec
@@ -1833,32 +1833,33 @@
        %7D else if
+ 
 (connectionType.
@@ -1869,67 +1869,180 @@
 als(
-%22java%22)) %7B%0A%09%09%09con = (Connection)application.getAttribute(%22c
+Constants.C3P0_MSSQL)) %7B%0A            ComboPooledDataSource ds = (ComboPooledDataSource) application.getAttribute(Constants.MSSQL_CONNECTION_POOL);%0A            con = ds.getC
 onne
@@ -2046,22 +2046,28 @@
 nnection
-%22);%0A%09%09
+();%0A        
 %7D else i
@@ -2059,32 +2059,33 @@
        %7D else if
+ 
 (connectionType.
@@ -2104,19 +2104,28 @@
 ng%22)) %7B%0A
-%09%09%09
+            
 FileSyst
@@ -2191,16 +2191,17 @@
 Context)
+ 
 applicat
@@ -2235,19 +2235,28 @@
 text%22);%0A
-%09%09%09
+            
 DriverMa
@@ -2303,16 +2303,17 @@
 aSource)
+ 
 context.
@@ -2331,27 +2331,36 @@
 taSource%22);%0A
-%09%09%09
+            
 con = dmds.g
@@ -2375,18 +2375,24 @@
 tion();%0A
-%09%09
+        
 %7D else i
@@ -2392,16 +2392,17 @@
  else if
+ 
 (connect
@@ -2427,19 +2427,28 @@
 di%22)) %7B%0A
-%09%09%09
+            
 DataSour
@@ -2520,11 +2520,20 @@
 %22);%0A
-%09%09%09
+            
 con 
@@ -2558,15 +2558,27 @@
 ();%0A
-%09%09%7D%0A%0A%09%09
+        %7D%0A%0A        
 logg
@@ -2633,10 +2633,16 @@
 ));%0A
-%09%09
+        
 retu
@@ -2653,9 +2653,12 @@
 on;%0A
-%09
+    
 %7D%0A%7D%0A

Update connection return logic to reflect new connection pools and removal of unmanaged java JDBC connections.