Since ckan can run on a different machine wrt its database, two queries are made against the IS

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/ckan-util-library@129051 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-06-10 12:15:38 +00:00
parent 2bc573d6c5
commit 213d328070
5 changed files with 182 additions and 71 deletions

View File

@ -20,7 +20,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Retrieve ckan running instance information in the infrastructure.
* Retrieve ckan running instance information in the infrastructure (for both its database and data catalogue url)
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class CKanRunningCluster {
@ -29,29 +29,37 @@ public class CKanRunningCluster {
private static final Logger logger = LoggerFactory.getLogger(CKanRunningCluster.class);
//properties
private final static String RUNTIME_RESOURCE_NAME = "CKanDatabase";
private final static String PLATFORM_NAME = "postgres";
private final static String RUNTIME_DB_RESOURCE_NAME = "CKanDatabase";
private final static String PLATFORM_DB_NAME = "postgres";
private final static String RUNTIME_CATALOGUE_RESOURCE_NAME = "CKanDatabase";
private final static String PLATFORM_CATALOGUE_NAME = "Tomcat";
// data catalogue url
// retrieved data
private List<String> hosts = new ArrayList<String>();
private List<Integer> ports = new ArrayList<Integer>();
private String dbName;
private String dbUser;
private String dbPassword;
private List<String> datacatalogueUrls = new ArrayList<String>();
private List<String> hostsDB = new ArrayList<String>();
private List<Integer> portsDB = new ArrayList<Integer>();
private String nameDB;
private String userDB;
private String passwordDB;
public CKanRunningCluster(String infrastructure) throws Exception{
public CKanRunningCluster(String scope) throws Exception{
logger.debug("Retrieving ckan database service end point information.");
try {
List<ServiceEndpoint> resources = getConfigurationFromIS(infrastructure);
List<ServiceEndpoint> resources = getConfigurationFromISFORDB(scope);
if (resources.size() > 1) {
logger.error("Too many Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" in this scope");
logger.error("Too many Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" in this scope");
throw new TooManyRunningClustersException("There exist more than 1 Runtime Resource in this scope having name "
+ RUNTIME_RESOURCE_NAME + " and Platform " + PLATFORM_NAME + ". Only one allowed per infrasrtucture.");
+ RUNTIME_DB_RESOURCE_NAME + " and Platform " + PLATFORM_DB_NAME + ". Only one allowed per infrasrtucture.");
}
else if (resources.size() == 0){
logger.error("There is no Runtime Resource having name " + RUNTIME_RESOURCE_NAME +" and Platform " + PLATFORM_NAME + " in this scope.");
logger.error("There is no Runtime Resource having name " + RUNTIME_DB_RESOURCE_NAME +" and Platform " + PLATFORM_DB_NAME + " in this scope.");
throw new NoCKanRuntimeResourceException();
}
else {
@ -68,18 +76,64 @@ public class CKanRunningCluster {
.next();
// add this host
hosts.add(accessPoint.address().split(":")[0]);
hostsDB.add(accessPoint.address().split(":")[0]);
// save the port
int port = Integer.parseInt(accessPoint.address().split(":")[1]);
ports.add(port);
portsDB.add(port);
// save the name of the cluster (this should be unique)
dbName = accessPoint.name();
nameDB = accessPoint.name();
// save user and password
dbPassword = accessPoint.password();
dbUser = accessPoint.username();
passwordDB = accessPoint.password();
userDB = accessPoint.username();
break;
}
}
}catch(Exception e ){
logger.error(e.toString());
throw new ServiceEndPointException();
}
}
} catch (Exception e) {
logger.error(e.toString());
throw e;
}
logger.debug("Retrieving ckan data catalogue service end point information.");
try {
List<ServiceEndpoint> resources = getConfigurationFromISFORCatalogueUrl(scope);
if (resources.size() > 1) {
logger.error("Too many Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" in this scope");
throw new TooManyRunningClustersException("There exist more than 1 Runtime Resource in this scope having name "
+ RUNTIME_CATALOGUE_RESOURCE_NAME + " and Platform " + PLATFORM_CATALOGUE_NAME + ". Only one allowed per infrasrtucture.");
}
else if (resources.size() == 0){
logger.error("There is no Runtime Resource having name " + RUNTIME_CATALOGUE_RESOURCE_NAME +" and Platform " + PLATFORM_CATALOGUE_NAME + " in this scope.");
throw new NoCKanRuntimeResourceException();
}
else {
try{
logger.debug(resources.toString());
for (ServiceEndpoint res : resources) {
Iterator<AccessPoint> accessPointIterator = res.profile().accessPoints().iterator();
while (accessPointIterator.hasNext()) {
ServiceEndpoint.AccessPoint accessPoint = (ServiceEndpoint.AccessPoint) accessPointIterator
.next();
// add this host
datacatalogueUrls.add(accessPoint.address());
break;
@ -99,48 +153,84 @@ public class CKanRunningCluster {
}
/**
* Retrieve endpoints information from IS
* @return list of endpoints for ckan
* Retrieve endpoints information from IS for DB
* @return list of endpoints for ckan database
* @throws Exception
*/
private List<ServiceEndpoint> getConfigurationFromIS(String infrastructure) throws Exception{
private List<ServiceEndpoint> getConfigurationFromISFORDB(String scope) throws Exception{
String scope = "";
if(infrastructure != null && !infrastructure.isEmpty())
scope += infrastructure;
String evaluatedScope = "";
if(scope != null && !scope.isEmpty())
evaluatedScope += scope;
else{
PortalContext context = PortalContext.getConfiguration();
scope += context.getInfrastructureName();
evaluatedScope += context.getInfrastructureName();
}
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(scope);
ScopeProvider.instance.set(evaluatedScope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Platform/Name/text() eq '"+ PLATFORM_NAME +"'");
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_DB_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Platform/Name/text() eq '"+ PLATFORM_DB_NAME +"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
ScopeProvider.instance.set(currScope);
return toReturn;
}
}
/**
* Retrieve endpoints information from IS for DataCatalogue URL
* @return list of endpoints for ckan data catalogue
* @throws Exception
*/
private List<ServiceEndpoint> getConfigurationFromISFORCatalogueUrl(String scope) throws Exception{
String evaluatedScope = "";
if(scope != null && !scope.isEmpty())
evaluatedScope += scope;
else{
PortalContext context = PortalContext.getConfiguration();
evaluatedScope += context.getInfrastructureName();
}
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set(evaluatedScope);
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Name/text() eq '"+ RUNTIME_CATALOGUE_RESOURCE_NAME +"'");
query.addCondition("$resource/Profile/Platform/Name/text() eq '"+ PLATFORM_CATALOGUE_NAME +"'");
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> toReturn = client.submit(query);
ScopeProvider.instance.set(currScope);
return toReturn;
}
/**
* Retrieve data catalogue url
*/
public List<String> getDataCatalogueUrl() {
return datacatalogueUrls;
}
/**
* Get the hosts for such resource.
* @return
*/
public List<String> getHosts() {
return hosts;
public List<String> getDatabaseHosts() {
return hostsDB;
}
/**
* Get the ports for such resource.
* @return
*/
public List<Integer> getPorts() {
return ports;
public List<Integer> getDatabasePorts() {
return portsDB;
}
/**
@ -148,7 +238,7 @@ public class CKanRunningCluster {
* @return
*/
public String getDataBaseName() {
return dbName;
return nameDB;
}
/**
@ -156,7 +246,7 @@ public class CKanRunningCluster {
* @return
*/
public String getDataBaseUser() {
return dbUser;
return userDB;
}
/**
@ -164,6 +254,6 @@ public class CKanRunningCluster {
* @return
*/
public String getDataBasePassword() {
return dbPassword;
return passwordDB;
}
}

View File

@ -1,7 +1,6 @@
package org.gcube.datacatalogue.ckanutillibrary;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -21,7 +20,7 @@ public class CKanUtilsFactory {
/**
* map <scope, util class for this scope>
*/
private static Map<String, CKanUtilsImpl> instanceForScopes;
private static ConcurrentHashMap<String, CKanUtilsImpl> instanceForScopes;
/**
* this object singleton instance
@ -32,9 +31,9 @@ public class CKanUtilsFactory {
* private constructor
*/
private CKanUtilsFactory(){
logger.debug("Instanciating factory");
instanceForScopes = new HashMap<String, CKanUtilsImpl>();
instanceForScopes = new ConcurrentHashMap<String, CKanUtilsImpl>();
}
@ -53,28 +52,25 @@ public class CKanUtilsFactory {
* @throws Exception
*/
public CKanUtilsImpl getCkanUtilsForScope(String scope) throws Exception{
logger.debug("Requested catalogue utils for scope " + scope);
synchronized (instanceForScopes) {
if(instanceForScopes.containsKey(scope)){
if(instanceForScopes.containsKey(scope)){
logger.debug("Catalogue utils already cached, returning object");
return instanceForScopes.get(scope);
}
else{
logger.debug("Catalogue utils already cached, returning object");
return instanceForScopes.get(scope);
logger.debug("Instanciating utils for this scope");
CKanUtilsImpl utilsForScope = new CKanUtilsImpl("/gcube");
// save into the map
instanceForScopes.put(scope, utilsForScope);
return utilsForScope;
}
else{
logger.debug("Instanciating utils for this scope");
CKanUtilsImpl utilsForScope = new CKanUtilsImpl("/gcube");
// save into the map
instanceForScopes.put(scope, utilsForScope);
return utilsForScope;
}
}
}
}

View File

@ -34,6 +34,7 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
private String CKAN_DB_NAME;
private String CKAN_DB_USER;
private String CKAN_DB_PASSWORD;
private String CKAN_DB_URL;
private Integer CKAN_DB_PORT;
// Connection to the db
@ -42,16 +43,17 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
public CKanUtilsImpl(String scope) throws Exception{
CKanRunningCluster runningInstance = new CKanRunningCluster(scope);
CKAN_CATALOGUE_URL = runningInstance.getHosts().get(0);
CKAN_DB_URL = runningInstance.getDatabaseHosts().get(0);
CKAN_DB_NAME = runningInstance.getDataBaseName();
CKAN_DB_USER = runningInstance.getDataBaseUser();
CKAN_DB_PASSWORD = StringEncrypter.getEncrypter().decrypt(runningInstance.getDataBasePassword(), scope);
CKAN_DB_PORT = runningInstance.getPorts().get(0);
CKAN_DB_PORT = runningInstance.getDatabasePorts().get(0);
CKAN_CATALOGUE_URL = runningInstance.getDataCatalogueUrl().get(0);
// create db connection
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://" + CKAN_CATALOGUE_URL + ":" + CKAN_DB_PORT + "/" + CKAN_DB_NAME, CKAN_DB_USER, CKAN_DB_PASSWORD);
"jdbc:postgresql://" + CKAN_DB_URL + ":" + CKAN_DB_PORT + "/" + CKAN_DB_NAME, CKAN_DB_USER, CKAN_DB_PASSWORD);
}
@ -260,9 +262,14 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
connection.close();
}
@Override
public String getCKANDBUrl() {
return CKAN_DB_URL;
}
@Override
public String getCatalogueUrl() {
return "https://" + CKAN_CATALOGUE_URL;
return CKAN_CATALOGUE_URL;
}
@Override
@ -319,4 +326,5 @@ public class CKanUtilsImpl implements CKanUtilsInterface{
return result;
}
}

View File

@ -57,6 +57,11 @@ public interface CKanUtilsInterface {
*/
public String getCatalogueUrl();
/**
* Return the url of the database
*/
public String getCKANDBUrl();
/**
* Get the list of licenses' titles.
* @return the list of licenses' titles

View File

@ -1,16 +1,15 @@
package org.gcube.datacatalogue.ckanutillibrary;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import org.gcube.datacatalogue.ckanutillibrary.models.CKanUserWrapper;
import org.gcube.datacatalogue.ckanutillibrary.models.ROLES_IN_ORGANIZATION;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.CheckedCkanClient;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanOrganization;
public class TestCKanLib {
@ -89,13 +88,26 @@ public class TestCKanLib {
}
//@Test
public void createOrganization() throws Exception{
String callUrl = "https://ckan-d-d4s.d4science.org/ckan-connector/gcube/service/organization/costorg?gcube-token=";
URL url = new URL(callUrl);
HttpsURLConnection connection = (HttpsURLConnection)url.openConnection();
connection.setRequestMethod("PUT");
public void deleteUserDataset() throws Exception{
String url = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube").getCatalogueUrl();
String token = CKanUtilsFactory.getInstance().getCkanUtilsForScope("/gcube").getApiKeyFromUser("costantino_perciante");
CheckedCkanClient client = new CheckedCkanClient(url, token);
List<String> datasetNames = client.getDatasetList();
System.out.println("datasetNames is " + datasetNames);
for (String name : datasetNames) {
CkanDataset dataset = new CkanDataset(name);
if(dataset.getAuthor().equals("costantino_perciante")){
//client.deleteDataset(name);
System.out.println("Name is " + name);
}
}
System.out.println(connection.getResponseCode() + " " + connection.getResponseMessage());
}
}