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@115811 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
ff86cc3400
commit
186bd0bdab
10
pom.xml
10
pom.xml
|
@ -40,6 +40,16 @@
|
||||||
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.resources.discovery</groupId>
|
||||||
|
<artifactId>ic-client</artifactId>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.core</groupId>
|
||||||
|
<artifactId>common-encryption</artifactId>
|
||||||
|
<version>[1.0.2-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
|
|
@ -27,7 +27,7 @@ public class FallbackPersistence extends Persistence {
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void prepareConnection() {
|
public void prepareConnection(PersistenceConfiguration configuration) {
|
||||||
// Nothing TO DO
|
// Nothing TO DO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,8 +63,11 @@ public abstract class Persistence {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
logger.debug(String.format("Testing %s", foundPersistence.getClass().getSimpleName()));
|
String foundPersistenceClassName = foundPersistence.getClass().getSimpleName();
|
||||||
foundPersistence.prepareConnection();
|
logger.debug("Testing {}", foundPersistenceClassName);
|
||||||
|
String scope = null; // TODO
|
||||||
|
PersistenceConfiguration configuration = PersistenceConfiguration.getPersistenceConfiguration(scope, foundPersistenceClassName);
|
||||||
|
foundPersistence.prepareConnection(configuration);
|
||||||
/*
|
/*
|
||||||
* Uncomment the following line of code if you want to try
|
* Uncomment the following line of code if you want to try
|
||||||
* to create a test UsageRecord before setting the
|
* to create a test UsageRecord before setting the
|
||||||
|
@ -114,9 +117,10 @@ public abstract class Persistence {
|
||||||
* Prepare the connection to persistence.
|
* Prepare the connection to persistence.
|
||||||
* This method must be used by implementation class to open
|
* This method must be used by implementation class to open
|
||||||
* the connection with the persistence storage, DB, file etc.
|
* the connection with the persistence storage, DB, file etc.
|
||||||
|
* @param configuration The configuration to create the connection
|
||||||
* @throws Exception if fails
|
* @throws Exception if fails
|
||||||
*/
|
*/
|
||||||
protected abstract void prepareConnection() throws Exception;
|
protected abstract void prepareConnection(PersistenceConfiguration configuration) throws Exception;
|
||||||
|
|
||||||
/* *
|
/* *
|
||||||
* Prepare the connection and try to write a test record on default
|
* Prepare the connection and try to write a test record on default
|
||||||
|
|
|
@ -0,0 +1,151 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.accounting.persistence;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.security.Key;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.gcube.common.encryption.StringEncrypter;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
||||||
|
import org.gcube.common.resources.gcore.utils.Group;
|
||||||
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||||
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR) http://www.lucafrosini.com/
|
||||||
|
*/
|
||||||
|
public class PersistenceConfiguration {
|
||||||
|
|
||||||
|
protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting";
|
||||||
|
protected final static String SERVICE_ENDPOINT_NAME = "Persistence";
|
||||||
|
|
||||||
|
protected static final String PERSISTENCE_CLASS_NAME = "persistenceClassName";
|
||||||
|
|
||||||
|
protected URI uri;
|
||||||
|
protected String username;
|
||||||
|
protected String password;
|
||||||
|
|
||||||
|
protected Map<String, Property> propertyMap;
|
||||||
|
|
||||||
|
public PersistenceConfiguration(){
|
||||||
|
propertyMap = new HashMap<String, Property>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public PersistenceConfiguration(URI uri, String username, String password){
|
||||||
|
this.uri = uri;
|
||||||
|
this.username = username;
|
||||||
|
this.password = password;
|
||||||
|
this.propertyMap = new HashMap<String, Property>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the uri
|
||||||
|
*/
|
||||||
|
public URI getUri() {
|
||||||
|
return uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param uri the uri to set
|
||||||
|
*/
|
||||||
|
public void setUri(URI uri) {
|
||||||
|
this.uri = uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the username
|
||||||
|
*/
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param username the username to set
|
||||||
|
*/
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the password
|
||||||
|
*/
|
||||||
|
public String getPassword() {
|
||||||
|
return password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param password the password to set
|
||||||
|
*/
|
||||||
|
public void setPassword(String password) {
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the propertyMap
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public String getProperty(String propertyKey) throws Exception {
|
||||||
|
Property propertyValue = propertyMap.get(propertyKey);
|
||||||
|
String value = propertyValue.value();
|
||||||
|
if(propertyValue.isEncrypted()){
|
||||||
|
value = decrypt(value);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ServiceEndpoint getServiceEndpoint(String scope, String persistenceClassName){
|
||||||
|
ScopeProvider.instance.set(scope);
|
||||||
|
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
|
||||||
|
query.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", SERVICE_ENDPOINT_CATEGORY));
|
||||||
|
query.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME));
|
||||||
|
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Name/text() eq '%s'", PERSISTENCE_CLASS_NAME));
|
||||||
|
query.addCondition(String.format("$resource/Profile/AccessPoint/Properties/Property/Value/text() eq '%s'", persistenceClassName));
|
||||||
|
query.setResult("$resource");
|
||||||
|
|
||||||
|
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
|
||||||
|
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
||||||
|
return serviceEndpoints.get(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String decrypt(String encrypted) throws Exception {
|
||||||
|
Key key = null;
|
||||||
|
return StringEncrypter.getEncrypter().decrypt(encrypted, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
return persistenceConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param scope
|
||||||
|
* @param persistenceClassName
|
||||||
|
* @return
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected static PersistenceConfiguration getPersistenceConfiguration(String scope, String persistenceClassName) throws Exception {
|
||||||
|
ServiceEndpoint serviceEndpoint = getServiceEndpoint(scope, persistenceClassName);
|
||||||
|
return createPersistenceConfiguration(serviceEndpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue