package it.polimi.hegira.adapters.cassandra;

import org.apache.log4j.Logger;

import it.polimi.hegira.exceptions.ConnectException;
import it.polimi.hegira.queue.TaskQueue;
import it.polimi.hegira.utils.ConfigurationManagerCassandra;
import it.polimi.hegira.utils.Constants;
import it.polimi.hegira.utils.PropertiesManager;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.AuthenticationException;
import com.datastax.driver.core.exceptions.NoHostAvailableException;

/**
 * This class implements the singleton pattern.
 * 
 * new implementation: a new session is created and returned for each thread
 * 
 * old implementation: (In this way all threads are forced to share the same session.
 * This is a recomended practice when dealing with a single keyspace.)
 * 
 * @author Andrea Celli
 *
 */
public class SessionManager {

	private static Logger log = Logger.getLogger(SessionManager.class);
	
	//the instance of the manager
	private static SessionManager manager;
	//the session shared among differend threads
	//private static Session session;
	
	private String server;
	private String username;
	private String password;
	private String keyspace;
	
	private SessionManager(){
		//credentials
		this.server=PropertiesManager.getCredentials(Constants.CASSANDRA_SERVER);
		this.username=PropertiesManager.getCredentials(Constants.CASSANDRA_USERNAME);
		this.password=PropertiesManager.getCredentials(Constants.CASSANDRA_PASSWORD);
		this.keyspace=ConfigurationManagerCassandra.getConfigurationProperties(Constants.KEYSPACE);
		//build the session
		/*	try{
				Cluster.Builder clusterBuilder=Cluster.builder()
						.addContactPoint(server)
						.withCredentials(username, password);
				Cluster cluster=clusterBuilder.build();
				this.session=cluster.connect(keyspace);
				return;
			}catch(NoHostAvailableException | 
					AuthenticationException |
					IllegalStateException ex){
				log.error(Thread.currentThread().getName() + " - Not able to connect to Cassandra ",ex);
				throw new ConnectException(ex);
			}*/
	}
	
	/**
	 * returns the unique instance of the session manager.
	 * creates the instance if it does not already exist.
	 * @return SessionManager
	 * @Throws ConnectException (raised if the session manager is not able to connect to Cassandra)
	 */
	public static SessionManager getSessionManager() throws ConnectException{
		if(manager==null){
			manager=new SessionManager();
		}
		return manager;
	}
	
	/**
	 * creates and returns a new session
	 * 
	 * @return session
	 */
	public Session getSession() throws ConnectException{
		Session session;
		try{
			Cluster.Builder clusterBuilder=Cluster.builder()
					.addContactPoint(server)
					.withCredentials(username, password);
			Cluster cluster=clusterBuilder.build();
			session=cluster.connect(keyspace);
			log.debug("new session created for thread: "+Thread.currentThread().getName());
			return session;
		}catch(NoHostAvailableException | 
				AuthenticationException |
				IllegalStateException ex){
			log.error(Thread.currentThread().getName() + " - Not able to connect to Cassandra ",ex);
			throw new ConnectException(ex);
	}
 }
}

@@ -597,114 +597,8 @@
 %0A * 
-%0A * new implementation: a new session is created and returned for each thread%0A * %0A * old implementation: (
 In t
@@ -981,18 +981,16 @@
 hreads%0A%09
-//
 private 
@@ -1011,24 +1011,42 @@
  session;%0A%09%0A
+%09/*%0A%09 * version 2%0A
 %09private Str
@@ -1134,16 +1134,18 @@
 eyspace;
+*/
 %0A%09%0A%09priv
@@ -1164,16 +1164,40 @@
 anager()
+ throws ConnectException
 %7B%0A%09%09//cr
@@ -1208,21 +1208,23 @@
 tials%0A%09%09
-this.
+String 
 server=P
@@ -1286,21 +1286,23 @@
 VER);%0A%09%09
-this.
+String 
 username
@@ -1368,21 +1368,23 @@
 AME);%0A%09%09
-this.
+String 
 password
@@ -1450,21 +1450,23 @@
 ORD);%0A%09%09
-this.
+String 
 keyspace
@@ -1568,18 +1568,16 @@
 ssion%0A%09%09
-/*
 %09try%7B%0A%09%09
@@ -2036,18 +2036,16 @@
 x);%0A%09%09%09%7D
-*/
 %0A%09%7D%0A%09%0A%09/
@@ -2456,33 +2456,22 @@
 %09 * 
-creates and returns a new
+get the unique
  ses
@@ -2534,16 +2534,19 @@
 ssion() 
+%7B//
 throws C
@@ -2562,16 +2562,41 @@
 eption%7B%0A
+%09%09/*%0A%09%09 *version 2 %0A%09%09 *%0A
 %09%09Sessio
@@ -3148,14 +3148,33 @@
 (ex);%0A%09%7D
+*/%0A%09return session;
 %0A %7D%0A%7D%0A

rollback to the previous version of the Session Manager (unique session)