fixes #275: Create CouchDB connector for persistence accounting as separated project

https://support.d4science.org/issues/275

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115445 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-06-19 15:51:11 +00:00
parent e2ee2c5103
commit f0a4588f16
9 changed files with 13 additions and 190 deletions

View File

@ -23,5 +23,10 @@
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,4 +1,5 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,2 @@
disabled=06target
eclipse.preferences.version=1

16
pom.xml
View File

@ -45,21 +45,6 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- CouchDB libraries -->
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>1.3.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.7</version>
<type>jar</type>
</dependency>
<!-- END CouchDB libraries -->
<dependency>
<groupId>junit</groupId>
@ -73,7 +58,6 @@
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -1,116 +0,0 @@
/**
*
*/
package org.gcube.accounting.persistence;
import java.io.Serializable;
import java.util.Map;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.ViewQuery;
import org.ektorp.ViewResult;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.http.StdHttpClient.Builder;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.gcube.accounting.datamodel.BasicUsageRecord;
import org.gcube.accounting.datamodel.UsageRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class CouchDBPersistence extends Persistence {
private static final Logger logger = LoggerFactory.getLogger(CouchDBPersistence.class);
protected CouchDbInstance couchDbInstance;
protected CouchDbConnector couchDbConnector;
protected final static String HOST = "HOST";
protected final static String DEFAULT_HOST = "localhost";
protected final static String PORT = "PORT";
protected final static String USERNAME = "USERNAME";
protected final static String PASSWORD = "PASSWORD";
protected String host = "localhost";
protected int port = 5984;
protected String username = "";
protected String password = "";
protected String dbName = "accounting";
protected CouchDBPersistence() throws Exception {
super();
}
protected HttpClient initHttpClient(String uri, int port, String username, String password){
Builder builder = new StdHttpClient.Builder().host(uri).port(port);
if(username!=null && username.compareTo("")!=0 &&
password!=null && password.compareTo("")!=0){
builder.username(username).password(password);
}
HttpClient httpClient = builder.build();
return httpClient;
}
protected ViewResult query(ViewQuery query){
ViewResult result = couchDbConnector.queryView(query);
return result;
}
@Override
public void close() throws Exception {
couchDbConnector.getConnection().shutdown();
}
/**
* {@inheritDoc}
*/
@Override
protected void prepareConnection() throws Exception {
logger.debug("Preparing Connection for {}", this.getClass().getSimpleName());
HttpClient httpClient = initHttpClient(host, port, username, password);
couchDbInstance = new StdCouchDbInstance(httpClient);
couchDbConnector = new StdCouchDbConnector(dbName, couchDbInstance);
couchDbConnector.createDatabaseIfNotExists(); // TODO remove this
}
protected void createItem(JsonNode node, String id) throws Exception {
if(id!=null && id.compareTo("")!=0){
couchDbConnector.create(id, node);
}else{
couchDbConnector.create(node);
}
}
/**
* {@inheritDoc}
*/
@Override
protected void reallyAccount(UsageRecord usageRecord) throws Exception {
JsonNode node = usageRecordToJsonNode(usageRecord);
createItem(node, usageRecord.getId());
}
public static JsonNode usageRecordToJsonNode(UsageRecord usageRecord) throws Exception {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.valueToTree(usageRecord.getResourceProperties());
return node;
}
public static UsageRecord jsonNodeToUsageRecord(JsonNode jsonNode) throws Exception {
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Serializable> result = mapper.convertValue(jsonNode, Map.class);
UsageRecord usageRecord = BasicUsageRecord.getUsageRecord(result);
return usageRecord;
}
}

View File

@ -1,17 +0,0 @@
/**
*
*/
package org.gcube.accounting.persistence;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class MongoDBPersistence {
protected MongoDBPersistence(){
super();
}
}

View File

@ -4,6 +4,7 @@
package org.gcube.accounting.persistence;
import java.io.File;
import java.util.ServiceLoader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -54,7 +55,6 @@ public abstract class Persistence {
protected static void init() {
setFallbackLocation(null);
try {
/*
ServiceLoader<Persistence> serviceLoader = ServiceLoader.load(Persistence.class);
for (Persistence foundPersistence : serviceLoader) {
if(foundPersistence.getClass().isInstance(FallbackPersistence.class)){
@ -66,19 +66,16 @@ public abstract class Persistence {
persistence = foundPersistence;
break;
} catch (Exception e) {
logger.debug(String.format("%s not initialized correctly. It will not be used", persistence.getClass().getSimpleName()));
logger.debug(String.format("%s not initialized correctly. It will not be used", foundPersistence.getClass().getSimpleName()));
}
}
if(persistence==null){
persistence = fallback;
}
*/
persistence = new CouchDBPersistence();
persistence.prepareConnection();
persistence.account(createTestUsageRecord());
} catch(Exception e){
logger.error("Unable to instance {}. Using fallback as default",
CouchDBPersistence.class.getSimpleName());
logger.error("Unable to instance a Persistence Implementation. Using fallback as default",
e.getCause());
persistence = fallback;
}
}

View File

@ -11,7 +11,7 @@ import org.junit.Test;
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class RawUsageRecordTest {
public class UsageRecordTest {
@Test
public void testCompareToSameObject(){

View File

@ -1,33 +0,0 @@
/**
*
*/
package org.gcube.accounting.datamodel.persistence;
import org.codehaus.jackson.JsonNode;
import org.gcube.accounting.datamodel.UsageRecord;
import org.gcube.accounting.persistence.CouchDBPersistence;
import org.gcube.accounting.persistence.Persistence;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class CouchDBPersistenceTest {
private static final Logger logger = LoggerFactory.getLogger(CouchDBPersistenceTest.class);
@Test
public void TestJsonNodeUsageRecordConversions() throws Exception {
UsageRecord usageRecord = Persistence.createTestUsageRecord();
logger.debug("UsageRecord : {}", usageRecord.toString());
JsonNode node = CouchDBPersistence.usageRecordToJsonNode(usageRecord);
logger.debug("Node : {}", node.toString());
UsageRecord ur = CouchDBPersistence.jsonNodeToUsageRecord(node);
Assert.assertEquals(0, usageRecord.compareTo(ur));
Assert.assertEquals(0, ur.compareTo(usageRecord));
}
}