From 0df046e40f5695fe014b842eed6aeaad8abf04f4 Mon Sep 17 00:00:00 2001
From: Paul-Christian Volkmer <paul-christian.volkmer@mni.thm.de>
Date: Tue, 11 Sep 2012 12:07:06 +0200
Subject: [PATCH] Feature #3849: Use configs from properties file

Properties from properties file are available in all beans now.
CouchDB connection in SessionService will be created using these settings.
If a connection fails, the next request will have a new try instead of
throwing an exception.
---
 .../de/thm/arsnova/SessionController.java     |  2 +-
 .../thm/arsnova/services/SessionService.java  | 61 ++++++++++++++++---
 .../webapp/WEB-INF/spring/spring-main.xml     |  5 +-
 3 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/SessionController.java b/src/main/java/de/thm/arsnova/SessionController.java
index 57572481..89bab167 100644
--- a/src/main/java/de/thm/arsnova/SessionController.java
+++ b/src/main/java/de/thm/arsnova/SessionController.java
@@ -38,7 +38,7 @@ public class SessionController {
 	@Autowired
 	ISessionService sessionService;
 	
-	@RequestMapping("/session/{sessionkey}")
+	@RequestMapping(value="/session/{sessionkey}", method=RequestMethod.GET)
 	public Session getSession(@PathVariable String sessionkey, HttpServletResponse response) {
 		Session session = sessionService.getSession(sessionkey);
 		if (session != null) return session;
diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java
index ff789791..e40e0d34 100644
--- a/src/main/java/de/thm/arsnova/services/SessionService.java
+++ b/src/main/java/de/thm/arsnova/services/SessionService.java
@@ -27,6 +27,7 @@ import net.sf.json.JSONObject;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
 import org.springframework.security.core.userdetails.User;
@@ -45,18 +46,34 @@ import de.thm.arsnova.entities.Session;
 @Service
 public class SessionService implements ISessionService {
 
-	private final com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session(
-			"localhost", 5984);
-	private final Database database = session.getDatabase("arsnova");
+	private String databaseHost;
+	private int databasePort;
+	private String databaseName;
+	
+	private Database database;
+	
+	public static final Logger logger = LoggerFactory.getLogger(SessionService.class);
 
-	public static final Logger logger = LoggerFactory
-			.getLogger(SessionService.class);
+	@Value("#{props['couchdb.host']}")
+	public final void setDatabaseHost(String databaseHost) {
+		this.databaseHost = databaseHost;
+	}
+	
+	@Value("#{props['couchdb.port']}")
+	public final void setDatabasePort(String databasePort) {
+		this.databasePort = Integer.parseInt(databasePort);
+	}
+	
+	@Value("#{props['couchdb.name']}")
+	public final void setDatabaseName(String databaseName) {
+		this.databaseName = databaseName;
+	}
 
 	@Override
 	public Session getSession(String keyword) {
 		View view = new View("session/by_keyword");
 		view.setKey(URLEncoder.encode("\"" + keyword + "\""));
-		ViewResults results = database.view(view);
+		ViewResults results = this.getDatabase().view(view);
 
 		if (results.getJSONArray("rows").optJSONObject(0) == null)
 			return null;
@@ -103,7 +120,7 @@ public class SessionService implements ISessionService {
 		view.setGroup(true);
 		view.setStartKey(URLEncoder.encode("[\"" + sessionId + "\"]"));
 		view.setEndKey(URLEncoder.encode("[\"" + sessionId + "\",{}]"));
-		ViewResults results = database.view(view);
+		ViewResults results = this.getDatabase().view(view);
 
 		logger.info("Feedback: {}", results.getJSONArray("rows"));
 
@@ -173,7 +190,7 @@ public class SessionService implements ISessionService {
 		}
 		
 		try {
-			database.saveDocument(feedback);
+			this.getDatabase().saveDocument(feedback);
 		} catch (IOException e) {
 			return false;
 		}
@@ -185,7 +202,7 @@ public class SessionService implements ISessionService {
 	@Transactional(isolation=Isolation.READ_COMMITTED)
 	public boolean sessionKeyAvailable(String keyword) {
 		View view = new View("session/by_keyword");
-		ViewResults results = database.view(view);
+		ViewResults results = this.getDatabase().view(view);
 		
 		return ! results.containsKey(keyword);
 	}
@@ -193,7 +210,7 @@ public class SessionService implements ISessionService {
 	private String getSessionId(String keyword) {
 		View view = new View("session/by_keyword");
 		view.setKey(URLEncoder.encode("\"" + keyword + "\""));
-		ViewResults results = database.view(view);
+		ViewResults results = this.getDatabase().view(view);
 
 		if (results.getJSONArray("rows").optJSONObject(0) == null)
 			return null;
@@ -224,4 +241,28 @@ public class SessionService implements ISessionService {
 		if (this.sessionKeyAvailable(keyword)) return keyword;
 		return generateKeyword();
 	}
+	
+	private Database getDatabase() {
+		if (database == null) {
+			try {
+				com.fourspaces.couchdb.Session session = new com.fourspaces.couchdb.Session(
+						databaseHost,
+						databasePort
+					);
+					
+				database = session.getDatabase(databaseName);
+			} catch (Exception e) {
+				logger.error(
+					"Cannot connect to CouchDB database '"
+					+ databaseName
+					+"' on host '"
+					+ databaseHost
+					+ "' using port "
+					+ databasePort
+				);
+			}
+		}
+		
+		return database;
+	}
 }
diff --git a/src/main/webapp/WEB-INF/spring/spring-main.xml b/src/main/webapp/WEB-INF/spring/spring-main.xml
index cdb1ae9a..c647ffb2 100644
--- a/src/main/webapp/WEB-INF/spring/spring-main.xml
+++ b/src/main/webapp/WEB-INF/spring/spring-main.xml
@@ -3,7 +3,9 @@
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 	xmlns:context="http://www.springframework.org/schema/context"
 	xmlns:p="http://www.springframework.org/schema/p"
+	xmlns:util="http://www.springframework.org/schema/util"
 	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
+		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
 		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
 
 	<bean id="propertyPlaceholderConfigurer"
@@ -12,12 +14,13 @@
 	    p:ignoreResourceNotFound="true">
 	    <property name="locations" >
 			<list>
-                <value>config.properties.example</value>
                 <value>config.properties</value>
             </list>
 		</property>
 	</bean>
 	
+	<util:properties id="props" location="config.properties"/>
+    
 	<import resource="spring-security.xml" />
 	
 	<context:component-scan base-package="de.thm.arsnova" />
-- 
GitLab