Strabon

changeset 551:e110c06e399b

Now the new connection details are saved in WEB-INF/connection.properties upon a succesful connection with the database. Fixes Ticket #16: http://bug.strabon.di.uoa.gr/ticket/16
author Babis Nikolaou <charnik@di.uoa.gr>
date Sun Sep 16 15:39:55 2012 +0300 (2012-09-16)
parents e5930301d59c
children b95784675cc7
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java	Sat Sep 15 23:52:14 2012 +0300
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/ConnectionBean.java	Sun Sep 16 15:39:55 2012 +0300
     1.3 @@ -4,7 +4,11 @@
     1.4  package eu.earthobservatory.org.StrabonEndpoint;
     1.5  
     1.6  
     1.7 +import java.io.FileInputStream;
     1.8 +import java.io.FileOutputStream;
     1.9  import java.io.IOException;
    1.10 +import java.io.InputStream;
    1.11 +import java.util.Properties;
    1.12  
    1.13  import javax.servlet.RequestDispatcher;
    1.14  import javax.servlet.ServletConfig;
    1.15 @@ -14,6 +18,8 @@
    1.16  import javax.servlet.http.HttpServletRequest;
    1.17  import javax.servlet.http.HttpServletResponse;
    1.18  
    1.19 +import org.slf4j.Logger;
    1.20 +import org.slf4j.LoggerFactory;
    1.21  import org.springframework.web.context.WebApplicationContext;
    1.22  import org.springframework.web.context.support.WebApplicationContextUtils;
    1.23  
    1.24 @@ -25,6 +31,18 @@
    1.25  
    1.26  	private static final long serialVersionUID = 2237815345608023368L;
    1.27  
    1.28 +	private static Logger logger = LoggerFactory.getLogger(eu.earthobservatory.org.StrabonEndpoint.ConnectionBean.class);
    1.29 +	
    1.30 +	/**
    1.31 +	 * The filename of the connection.properties file
    1.32 +	 */
    1.33 +	private static final String CONNECTION_PROPERTIES_FILE = "/WEB-INF/connection.properties";
    1.34 +	
    1.35 +	/**
    1.36 +	 * Lock for updating connection.properties file 
    1.37 +	 */
    1.38 +	private static Object lock;
    1.39 +	
    1.40  	/**
    1.41  	 * Wrapper over Strabon
    1.42  	 */
    1.43 @@ -45,9 +63,11 @@
    1.44  		// get the context of the application
    1.45  		WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(context);
    1.46  
    1.47 -		// the the strabon wrapper
    1.48 +		// the strabon wrapper
    1.49  		strabonWrapper = (StrabonBeanWrapper) applicationContext.getBean("strabonBean");
    1.50  
    1.51 +		// initialize lock
    1.52 +		lock = new Object();
    1.53  	}
    1.54  	
    1.55  	@Override
    1.56 @@ -61,28 +81,54 @@
    1.57  		
    1.58  		// set new connection details
    1.59  		strabonWrapper.setConnectionDetails(
    1.60 +				request.getParameter("dbname"), 
    1.61  				request.getParameter("username"), 
    1.62  				request.getParameter("password"), 
    1.63 -				request.getParameter("dbname"), 
    1.64 +				request.getParameter("port"),
    1.65  				request.getParameter("hostname"), 
    1.66 -				request.getParameter("port"), 
    1.67  				request.getParameter("dbengine"));
    1.68  		
    1.69  		// pass the query that had been issued to the dispatcher
    1.70  		request.setAttribute("query", request.getAttribute("query"));
    1.71 +
    1.72 +		if (logger.isInfoEnabled()) {
    1.73 +			logger.info("[StrabonEndpoint.ConnectionBean] Establishing connection with database using new connection details.");
    1.74 +		}
    1.75 +		
    1.76 +		// establish connection
    1.77 +		if (strabonWrapper.init()) { // successfully connected, go to query.jsp
    1.78 +			if (logger.isInfoEnabled()) {
    1.79 +				logger.info("[StrabonEndpoint.ConnectionBean] Connection with database established.");
    1.80 +				logger.info("[StrabonEndpoint.ConnectionBean] Saving new connection details in {}.", CONNECTION_PROPERTIES_FILE);
    1.81 +			}
    1.82  			
    1.83 -		// establish connection
    1.84 -		if (strabonWrapper.init()) { // successfully connected, go to QueryBean
    1.85 -			// TODO: save the new connection details in beans.xml
    1.86 +			// save the new connection details
    1.87 +			saveNewConnectionDetails(
    1.88 +									request.getParameter("dbname"),
    1.89 +									request.getParameter("username"), 
    1.90 +									request.getParameter("password"), 
    1.91 +									request.getParameter("port"), 
    1.92 +									request.getParameter("hostname"), 
    1.93 +									request.getParameter("dbengine"));
    1.94 +			
    1.95 +			if (logger.isInfoEnabled()) {
    1.96 +				logger.info("[StrabonEndpoint.ConnectionBean] New connection details succesfully saved.");
    1.97 +			}
    1.98 +			
    1.99 +			// go to query.jsp
   1.100  			dispatcher = request.getRequestDispatcher("/query.jsp");
   1.101  			
   1.102  		} else { // try again
   1.103 +			if (logger.isInfoEnabled()) {
   1.104 +				logger.info("[StrabonEndpoint.ConnectionBean] Cannot establish connection with database.");
   1.105 +			}
   1.106 +			
   1.107  			// pass the current details of the connection
   1.108 +			request.setAttribute("dbname", 	 request.getParameter("dbname"));
   1.109  			request.setAttribute("username", request.getParameter("username"));
   1.110  			request.setAttribute("password", request.getParameter("password"));
   1.111 -			request.setAttribute("dbname", request.getParameter("dbname"));
   1.112 +			request.setAttribute("port", 	 request.getParameter("port"));
   1.113  			request.setAttribute("hostname", request.getParameter("hostname"));
   1.114 -			request.setAttribute("port", request.getParameter("port"));
   1.115  			request.setAttribute("dbengine", request.getParameter("dbengine"));
   1.116  			
   1.117  			dispatcher = request.getRequestDispatcher("/connection.jsp");
   1.118 @@ -92,6 +138,33 @@
   1.119  		dispatcher.forward(request, response);
   1.120  	}
   1.121  
   1.122 +	private void saveNewConnectionDetails(String dbname, String username, String password, 
   1.123 +				 		String port, String hostname, String dbengine) throws IOException {
   1.124 +		Properties properties = new Properties();
   1.125 +		
   1.126 +		synchronized (lock) {
   1.127 +			// get connection.properties as input stream
   1.128 +			InputStream input = new FileInputStream(context.getRealPath(CONNECTION_PROPERTIES_FILE));
   1.129 +			
   1.130 +			// load the properties
   1.131 +			properties.load(input);
   1.132 +			
   1.133 +			// update them
   1.134 +			properties.setProperty("dbname", dbname);
   1.135 +			properties.setProperty("username", username);
   1.136 +			properties.setProperty("password", password);
   1.137 +			properties.setProperty("port", port);
   1.138 +			properties.setProperty("hostname", hostname);
   1.139 +			properties.setProperty("dbengine", dbengine);
   1.140 +			
   1.141 +			// store them
   1.142 +			properties.store(new FileOutputStream(context.getRealPath(CONNECTION_PROPERTIES_FILE)), null);
   1.143 +			
   1.144 +			// close the stream
   1.145 +			input.close();
   1.146 +		}
   1.147 +	}
   1.148 +
   1.149  	
   1.150  
   1.151  }
     2.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Sat Sep 15 23:52:14 2012 +0300
     2.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Sun Sep 16 15:39:55 2012 +0300
     2.3 @@ -242,18 +242,17 @@
     2.4  		return true;
     2.5  	}
     2.6  
     2.7 -	public void setConnectionDetails(String username, String password, String dbname, String hostname, String port, String dbengine) {
     2.8 +	public void setConnectionDetails(String dbname, String username, String password, String port, String hostname, String dbengine) {
     2.9 +		this.databaseName = dbname;
    2.10  		this.user = username;
    2.11  		this.password = password;
    2.12 -		this.databaseName = dbname;
    2.13 -		this.dbBackend = dbengine;
    2.14 -		this.password = password;
    2.15  		try { 
    2.16  			this.port = Integer.valueOf(port);
    2.17  		} catch (NumberFormatException e) {
    2.18  			this.port = 5432;
    2.19  		}
    2.20  		this.serverName = hostname;
    2.21 +		this.dbBackend = dbengine;
    2.22  		this.checkForLockTable = true;		
    2.23  	}
    2.24