refs #200: Create accouting-lib library

https://support.d4science.org/issues/200
Retrieving Persistence Connection Information from IS

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@115823 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-03 10:18:51 +00:00
parent 186bd0bdab
commit c4cfd8b5a4
3 changed files with 178 additions and 1 deletions

11
pom.xml
View File

@ -56,6 +56,17 @@
<scope>provided</scope>
</dependency>
<!-- Test Dependency -->
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>registry-publisher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-smartgears</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -116,19 +116,23 @@ public class PersistenceConfiguration {
}
private static String decrypt(String encrypted) throws Exception {
/*
Key key = null;
return StringEncrypter.getEncrypter().decrypt(encrypted, key);
*/
return encrypted;
}
private static PersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{
PersistenceConfiguration persistenceConfiguration = new PersistenceConfiguration();
Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints();
for(AccessPoint accessPoint : accessPoints){
persistenceConfiguration.uri = new URI(accessPoint.address());
persistenceConfiguration.username = accessPoint.name();
String encryptedPassword = accessPoint.password();
String password = decrypt(encryptedPassword);
persistenceConfiguration.password = password;
persistenceConfiguration.propertyMap = accessPoint.propertyMap();
}

View File

@ -0,0 +1,162 @@
/**
*
*/
package org.gcube.accounting.persistence;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.common.resources.gcore.Resource;
import org.gcube.common.resources.gcore.Resources;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.publisher.AdvancedScopedPublisher;
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
import org.gcube.informationsystem.publisher.ScopedPublisher;
import org.gcube.informationsystem.publisher.exception.RegistryNotFoundException;
import org.gcube.smartgears.configuration.Mode;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
*
*/
public class PersistenceConfigurationTest {
private static final Logger logger = LoggerFactory.getLogger(PersistenceConfigurationTest.class);
public static final String PROFILE_DESCRIPTION = "This ServiceEndpoint contains the parameter to connect to DB to persist log accounting.";
public static final String RUNNING_ON = "http://localhost:5984";
public static final String TEST_VERSION = "1.0.0";
public static final short[] VERSION_SLICES = new short[]{1,6,0,0};
public static final String DESCRIPTION = "CouchDB Server";
public static final String COUCHDB_CLASS_NAME = "CouchDBPersistence";
public static final String FAKE_USERNAME = "fakeusername";
public static final String FAKE_PASSWORD = "fakepassword";
public static final String TEST_SCOPE = "/gcube/devsec";
public static final String DB_NAME_PROPERTY_NAME = "dbName";
public static final String DB_NAME_PROPERTY_VALUE = "accounting";
/**
* Publish the provided resource on all Service Scopes retrieved from
* Context
* @param resource to be published
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private static void publishScopedResource(Resource resource, List<String> scopes) throws RegistryNotFoundException, Exception {
StringWriter stringWriter = new StringWriter();
Resources.marshal(resource, stringWriter);
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
try {
logger.debug("Trying to publish to {}:\n{}", scopes, stringWriter);
scopedPublisher.create(resource, scopes);
} catch (RegistryNotFoundException e) {
logger.error("The resource was not published", e);
throw e;
}
}
/**
* Remove the resource from IS
* @param resource to be unpublished
* @throws RegistryNotFoundException if the Registry is not found so the
* resource has not be published
*/
private static void unPublishScopedResource(Resource resource) throws RegistryNotFoundException, Exception {
//StringWriter stringWriter = new StringWriter();
//Resources.marshal(resource, stringWriter);
ScopedPublisher scopedPublisher = RegistryPublisherFactory.scopedPublisher();
AdvancedScopedPublisher advancedScopedPublisher = new AdvancedScopedPublisher(scopedPublisher);
String id = resource.id();
logger.debug("Trying to remove {} with ID {} from {}", resource.getClass().getSimpleName(), id, ScopeProvider.instance.get());
//scopedPublisher.remove(resource, scopes);
advancedScopedPublisher.forceRemove(resource);
logger.debug("{} with ID {} removed successfully", resource.getClass().getSimpleName(), id);
}
/**
* Create the Service Endpoint using information related to discovered
* available plugins and their own discovered capabilities
* @return the created {@link ServiceEndpoint}
*/
protected static ServiceEndpoint createServiceEndpoint(){
logger.debug("Getting Available Plugins and their own supported capabilities");
logger.debug("Creating ServiceEndpoint to publish on IS available plugins and their own supported capabilities");
ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
Profile profile = serviceEndpoint.newProfile();
profile.category(PersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY);
profile.name(PersistenceConfiguration.SERVICE_ENDPOINT_NAME);
profile.version(TEST_VERSION);
profile.description(PROFILE_DESCRIPTION);
Platform platform = profile.newPlatform();
platform.name(RUNNING_ON);
platform.version(VERSION_SLICES[0]);
platform.minorVersion(VERSION_SLICES[1]);
platform.buildVersion(VERSION_SLICES[2]);
platform.revisionVersion(VERSION_SLICES[3]);
Runtime runtime = profile.newRuntime();
runtime.hostedOn(RUNNING_ON);
runtime.status(Mode.online.toString());
AccessPoint accessPointElement = new AccessPoint();
accessPointElement.description(DESCRIPTION);
accessPointElement.credentials(FAKE_USERNAME, FAKE_PASSWORD);
Group<Property> properties = accessPointElement.properties();
Property className = new Property();
className.nameAndValue(PersistenceConfiguration.PERSISTENCE_CLASS_NAME, COUCHDB_CLASS_NAME);
properties.add(className);
Property dbName = new Property();
dbName.nameAndValue(DB_NAME_PROPERTY_NAME, DB_NAME_PROPERTY_VALUE);
properties.add(dbName);
StringWriter stringWriter = new StringWriter();
Resources.marshal(serviceEndpoint, stringWriter);
logger.debug("The created ServiceEndpoint profile is\n{}", stringWriter.toString());
return serviceEndpoint;
}
@Test
public void testPersistenceFromIS() throws RegistryNotFoundException, Exception{
List<String> scopes = new ArrayList<String>();
scopes.add(TEST_SCOPE);
ServiceEndpoint serviceEndpoint = createServiceEndpoint();
publishScopedResource(serviceEndpoint, scopes);
PersistenceConfiguration.getPersistenceConfiguration(TEST_SCOPE, COUCHDB_CLASS_NAME);
unPublishScopedResource(serviceEndpoint);
}
}