package edu.ucsb.education;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;

import edu.ucsb.education.DbengConfiguration.Database;

/**
 * Servlet implementation class Dbeng
 */
public class Dbeng extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private DbengConfiguration conf;
	private HashMap<String, Connection> connections;
	private LinkedList<String> driverNames;

	public void init() {

		connections = new HashMap<String, Connection>();
		driverNames = new LinkedList<String>();

		Gson gson = new Gson();
		JsonReader reader = null;
		try {
			reader = new JsonReader(new InputStreamReader(getServletContext()
					.getResourceAsStream("/WEB-INF/dbeng.conf.js"), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		conf = gson.fromJson(reader, DbengConfiguration.class);

		for (Database db : conf.getDatabases()) {
			if (!driverNames.contains(db.getDriver())) {
				String driver = db.getDriver();
				driverNames.add(driver);
				try {
					Class.forName(driver);
				} catch (ClassNotFoundException e) {
					log("Could not load driver `" + driver + "`");
					e.printStackTrace();
				}
			}
			try {
				connections.put(
						db.getName(),
						DriverManager.getConnection(db.getUrl(),
								db.getUsername(), db.getPassword()));
				log(connections.toString());
			} catch (SQLException e) {
				log("SQLException using driver `" + db.getDriver() + "`, url `"
						+ db.getUrl() + "`, username `" + db.getUsername()
						+ "`, password filtered");
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		/*
		 * parse requests from JSON There can be multiple request objects, each
		 * with exactly one server and one SQL statement defined
		 */

		// Check connection status and re-establish if necessary
		// REMEMBER to set content type to UTF-8 BEFORE creating PrintWriter
	}
}

@@ -830,20 +830,16 @@
 %3E driver
-Name
 s;%0A%0A%09pub
@@ -916,20 +916,16 @@
 %09%09driver
-Name
 s = new 
@@ -950,313 +950,21 @@
 ();%0A
-%0A%09%09Gson gson = new Gson();%0A%09%09JsonReader reader = null;%0A%09%09try %7B%0A%09%09%09reader = new JsonReader(new InputStreamReader(getServletContext()%0A%09%09%09%09%09.getResourceAsStream(%22/WEB-INF/dbeng.conf.js%22), %22UTF-8%22));%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09conf = gson.fromJson(reader, Dbeng
+%09%09conf = load
 Conf
@@ -976,14 +976,9 @@
 tion
-.class
+(
 );%0A%0A
@@ -1028,88 +1028,75 @@
 %0A%09%09%09
-if (!driverNames.contains(db.getDriver())) %7B%0A%09%09%09%09String driver = db.getD
+String driver = db.getDriver();%0A%09%09%09if (!drivers.contains(d
 river
-();
+)) %7B
 %0A%09%09%09
@@ -1102,20 +1102,16 @@
 %09%09driver
-Name
 s.add(dr
@@ -1460,16 +1460,30 @@
 ring());
+ // TODO DEBUG
 %0A%09%09%09%7D ca
@@ -1701,16 +1701,540 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09private DbengConfiguration loadConfiguration() %7B%0A%09%09// Maybe rewrite as getConfiguration, and check the file mtime before%0A%09%09// reading it, then read it and reload configuration if it's changed.%0A%09%09Gson gson = new Gson();%0A%09%09JsonReader reader = null;%0A%09%09try %7B%0A%09%09%09reader = new JsonReader(new InputStreamReader(getServletContext()%0A%09%09%09%09%09.getResourceAsStream(%22/WEB-INF/dbeng.conf.js%22), %22UTF-8%22));%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%09return gson.fromJson(reader, DbengConfiguration.class);%0A%09%7D%0A%0A
 %09/**%0A%09 *

Break loading config into method