Skip to content
Snippets Groups Projects
Commit 0df046e4 authored by Paul-Christian Volkmer's avatar Paul-Christian Volkmer
Browse files

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.
parent bb9b4d40
No related merge requests found
......@@ -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;
......
......@@ -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;
}
}
......@@ -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" />
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment