Improving code

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/accounting/accounting-lib@117094 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2015-07-08 14:41:05 +00:00
parent d15590926e
commit 51cc4bff5a
3 changed files with 29 additions and 30 deletions

View File

@ -23,8 +23,8 @@ import org.gcube.resources.discovery.icclient.ICFactory;
*/ */
public class AccountingPersistenceConfiguration { public class AccountingPersistenceConfiguration {
protected final static String SERVICE_ENDPOINT_CATEGORY = "Accounting"; protected final String SERVICE_ENDPOINT_CATEGORY = "Accounting";
protected final static String SERVICE_ENDPOINT_NAME = "Persistence"; protected final String SERVICE_ENDPOINT_NAME = "Persistence";
protected static final String PERSISTENCE_CLASS_NAME = "persistenceClassName"; protected static final String PERSISTENCE_CLASS_NAME = "persistenceClassName";
@ -34,15 +34,25 @@ public class AccountingPersistenceConfiguration {
protected Map<String, Property> propertyMap; protected Map<String, Property> propertyMap;
protected void init(){
this.propertyMap = new HashMap<String, Property>();
}
public AccountingPersistenceConfiguration(){ public AccountingPersistenceConfiguration(){
propertyMap = new HashMap<String, Property>(); init();
} }
public AccountingPersistenceConfiguration(URI uri, String username, String password){ public AccountingPersistenceConfiguration(URI uri, String username, String password){
init();
this.uri = uri; this.uri = uri;
this.username = username; this.username = username;
this.password = password; this.password = password;
this.propertyMap = new HashMap<String, Property>(); }
public AccountingPersistenceConfiguration(String persistenceClassName) throws Exception {
init();
ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName);
setValues(serviceEndpoint);
} }
/** /**
@ -100,7 +110,7 @@ public class AccountingPersistenceConfiguration {
return value; return value;
} }
private static ServiceEndpoint getServiceEndpoint(String persistenceClassName){ protected ServiceEndpoint getServiceEndpoint(String persistenceClassName){
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class); 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/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/Name/text() eq '%s'", SERVICE_ENDPOINT_NAME));
@ -117,32 +127,18 @@ public class AccountingPersistenceConfiguration {
return StringEncrypter.getEncrypter().decrypt(encrypted); return StringEncrypter.getEncrypter().decrypt(encrypted);
} }
protected static AccountingPersistenceConfiguration createPersistenceConfiguration(ServiceEndpoint serviceEndpoint) throws Exception{ protected void setValues(ServiceEndpoint serviceEndpoint) throws Exception{
AccountingPersistenceConfiguration persistenceConfiguration = new AccountingPersistenceConfiguration();
Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints(); Group<AccessPoint> accessPoints = serviceEndpoint.profile().accessPoints();
for(AccessPoint accessPoint : accessPoints){ for(AccessPoint accessPoint : accessPoints){
persistenceConfiguration.uri = new URI(accessPoint.address()); this.uri = new URI(accessPoint.address());
persistenceConfiguration.username = accessPoint.username(); this.username = accessPoint.username();
String encryptedPassword = accessPoint.password(); String encryptedPassword = accessPoint.password();
String password = decrypt(encryptedPassword); String password = decrypt(encryptedPassword);
persistenceConfiguration.password = password; this.password = password;
persistenceConfiguration.propertyMap = accessPoint.propertyMap(); this.propertyMap = accessPoint.propertyMap();
} }
return persistenceConfiguration;
}
/**
*
* @param scope
* @param persistenceClassName
* @return
* @throws Exception
*/
public static AccountingPersistenceConfiguration getPersistenceConfiguration(String persistenceClassName) throws Exception {
ServiceEndpoint serviceEndpoint = getServiceEndpoint(persistenceClassName);
return createPersistenceConfiguration(serviceEndpoint);
} }
} }

View File

@ -87,7 +87,7 @@ public abstract class AccountingPersistenceFactory {
try { try {
String foundPersistenceClassName = foundPersistence.getClass().getSimpleName(); String foundPersistenceClassName = foundPersistence.getClass().getSimpleName();
logger.debug("Testing {}", foundPersistenceClassName); logger.debug("Testing {}", foundPersistenceClassName);
AccountingPersistenceConfiguration configuration = AccountingPersistenceConfiguration.getPersistenceConfiguration(foundPersistenceClassName); AccountingPersistenceConfiguration configuration = new AccountingPersistenceConfiguration(foundPersistenceClassName);
foundPersistence.prepareConnection(configuration); 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

View File

@ -117,8 +117,9 @@ public class PersistenceConfigurationTest {
logger.debug("Creating ServiceEndpoint to publish on IS 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(); ServiceEndpoint serviceEndpoint = new ServiceEndpoint();
Profile profile = serviceEndpoint.newProfile(); Profile profile = serviceEndpoint.newProfile();
profile.category(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY); AccountingPersistenceConfiguration accountingPersistenceConfiguration = new AccountingPersistenceConfiguration();
profile.name(AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME); profile.category(accountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY);
profile.name(accountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME);
profile.version(TEST_VERSION); profile.version(TEST_VERSION);
profile.description(PROFILE_DESCRIPTION); profile.description(PROFILE_DESCRIPTION);
@ -165,9 +166,11 @@ public class PersistenceConfigurationTest {
protected void clean(){ protected void clean(){
ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE); ScopeProvider.instance.set(GCUBE_DEVNEXT_SCOPE);
AccountingPersistenceConfiguration accountingPersistenceConfiguration = new AccountingPersistenceConfiguration();
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class) SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/Profile/Category/text() eq '%s'", AccountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY)) .addCondition(String.format("$resource/Profile/Category/text() eq '%s'", accountingPersistenceConfiguration.SERVICE_ENDPOINT_CATEGORY))
.addCondition(String.format("$resource/Profile/Name/text() eq '%s'", AccountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME)) .addCondition(String.format("$resource/Profile/Name/text() eq '%s'", accountingPersistenceConfiguration.SERVICE_ENDPOINT_NAME))
.addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON)) .addCondition(String.format("$resource/Profile/RunTime/HostedOn/text() eq '%s'", RUNNING_ON))
.setResult("$resource"); .setResult("$resource");
@ -198,7 +201,7 @@ public class PersistenceConfigurationTest {
} }
try { try {
AccountingPersistenceConfiguration persitenceConfiguration = AccountingPersistenceConfiguration.getPersistenceConfiguration(COUCHDB_CLASS_NAME); AccountingPersistenceConfiguration persitenceConfiguration = new AccountingPersistenceConfiguration(COUCHDB_CLASS_NAME);
if(createResource){ if(createResource){
Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON))); Assert.assertTrue(persitenceConfiguration.getUri().toURL().equals(new URL(RUNNING_ON)));
Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0); Assert.assertTrue(persitenceConfiguration.getUsername().compareTo(FAKE_USERNAME)==0);