2016-11-03 17:27:14 +01:00
|
|
|
package org.gcube.dataaccess.algorithms.drmalgorithms;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
|
|
|
|
import org.gcube.dataaccess.databases.access.DatabasesDiscoverer;
|
|
|
|
import org.gcube.dataaccess.databases.resources.DBResource;
|
|
|
|
import org.gcube.dataaccess.databases.resources.DBResource.AccessPoint;
|
|
|
|
import org.gcube.dataanalysis.ecoengine.datatypes.PrimitiveType;
|
|
|
|
import org.gcube.dataanalysis.ecoengine.datatypes.StatisticalType;
|
|
|
|
import org.gcube.dataanalysis.ecoengine.datatypes.enumtypes.PrimitiveTypes;
|
2017-02-28 17:16:34 +01:00
|
|
|
import org.gcube.dataanalysis.ecoengine.interfaces.StandardLocalInfraAlgorithm;
|
2016-11-03 17:27:14 +01:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryException;
|
|
|
|
import org.gcube.resources.discovery.client.api.InvalidResultException;
|
|
|
|
|
|
|
|
/** Class that allows to retrieve information about the chosen resource */
|
2017-02-28 17:16:34 +01:00
|
|
|
public class ListDBInfo extends StandardLocalInfraAlgorithm {
|
2016-11-04 13:11:00 +01:00
|
|
|
private static final String PLATFORM_VERSION = "Platform Version ";
|
|
|
|
private static final String DIALECT_NAME = "Dialect Name ";
|
|
|
|
private static final String DATABASE_NAME = "Database Name ";
|
|
|
|
private static final String DRIVER_NAME = "Driver Name ";
|
|
|
|
private static final String URL_NAME = "URL ";
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
private LinkedHashMap<String, StatisticalType> map = new LinkedHashMap<String, StatisticalType>();
|
|
|
|
|
|
|
|
// database's parameters specified by the user
|
|
|
|
private String resourceName = null;
|
|
|
|
|
|
|
|
// list that contains information about the resource
|
|
|
|
private List<AccessPoint> ap = new ArrayList<AccessPoint>();
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
// variable that keeps track of database platform version
|
|
|
|
private String platformVersion = "";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() throws Exception {
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug("In ListDBInfo->Initialization");
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
String scope = config.getGcubeScope();
|
2017-02-28 17:16:34 +01:00
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->scope set by config object: " + scope);
|
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getDescription() {
|
|
|
|
// add a simple description for the algorithm
|
|
|
|
return "Algorithm that allows to view information about one chosen resource of Database Type in the Infrastructure";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-28 17:16:34 +01:00
|
|
|
protected void process() throws Exception, IOException,
|
|
|
|
IllegalStateException, DiscoveryException, InvalidResultException {
|
2016-11-03 17:27:14 +01:00
|
|
|
AnalysisLogger.getLogger().debug("In ListDBInfo->Processing");
|
2017-02-28 17:16:34 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
// retrieve information
|
|
|
|
List<AccessPoint> apInfo = retrieveInfo();
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
2017-02-28 17:16:34 +01:00
|
|
|
"access point dimension: " + apInfo.size());
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
for (int i = 0; i < apInfo.size(); i++) {
|
|
|
|
String name = DATABASE_NAME + (i + 1);
|
|
|
|
PrimitiveType DBName = new PrimitiveType(
|
|
|
|
String.class.getName(),
|
|
|
|
apInfo.get(i).getDatabaseName(), PrimitiveTypes.STRING,
|
|
|
|
name, name);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
map.put(name, DBName);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->Database Name: "
|
|
|
|
+ apInfo.get(i).getDatabaseName());
|
|
|
|
String urlId = URL_NAME + (i + 1);
|
|
|
|
PrimitiveType url = new PrimitiveType(String.class.getName(),
|
|
|
|
apInfo.get(i).address(), PrimitiveTypes.STRING, urlId,
|
|
|
|
urlId);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
map.put(urlId, url);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->URL: " + apInfo.get(i).address());
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
String driverId = DRIVER_NAME + (i + 1);
|
|
|
|
PrimitiveType driver = new PrimitiveType(
|
|
|
|
String.class.getName(), apInfo.get(i).getDriver(),
|
|
|
|
PrimitiveTypes.STRING, driverId, driverId);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
map.put(driverId, driver);
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->Driver Name: "
|
|
|
|
+ apInfo.get(i).getDriver());
|
2016-11-03 17:27:14 +01:00
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
String dialectId = DIALECT_NAME + (i + 1);
|
|
|
|
PrimitiveType dialect = new PrimitiveType(
|
|
|
|
String.class.getName(), apInfo.get(i).getDialect(),
|
|
|
|
PrimitiveTypes.STRING, dialectId, dialectId);
|
|
|
|
|
|
|
|
map.put(dialectId, dialect);
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->Dialect Name: "
|
|
|
|
+ apInfo.get(i).getDialect());
|
|
|
|
|
|
|
|
String platformId = PLATFORM_VERSION + (i + 1);
|
|
|
|
PrimitiveType platformVersionValue = new PrimitiveType(
|
|
|
|
String.class.getName(), platformVersion,
|
|
|
|
PrimitiveTypes.STRING, platformId, platformId);
|
|
|
|
|
|
|
|
map.put(platformId, platformVersionValue);
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->Platform Version: " + platformVersion);
|
|
|
|
|
|
|
|
}
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
}
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
catch (IllegalStateException e) {
|
2017-02-28 17:16:34 +01:00
|
|
|
// e.printStackTrace();
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo-> ERROR " + e.getMessage());
|
2016-11-03 17:27:14 +01:00
|
|
|
throw e;
|
|
|
|
|
|
|
|
} catch (DiscoveryException e1) {
|
2017-02-28 17:16:34 +01:00
|
|
|
// e1.printStackTrace();
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo-> ERROR " + e1.getMessage());
|
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
throw e1;
|
|
|
|
|
|
|
|
} catch (InvalidResultException e2) {
|
2017-02-28 17:16:34 +01:00
|
|
|
// e2.printStackTrace();
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo-> ERROR " + e2.getMessage());
|
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
throw e2;
|
|
|
|
|
|
|
|
}
|
2017-02-28 17:16:34 +01:00
|
|
|
// catch(IOException e3){
|
|
|
|
// // e3.printStackTrace();
|
|
|
|
//
|
|
|
|
// AnalysisLogger.getLogger().debug("In ListDBInfo-> Exception " +
|
|
|
|
// e3.getMessage());
|
|
|
|
//
|
|
|
|
// throw e3;
|
|
|
|
// }
|
|
|
|
|
|
|
|
catch (Exception e4) {
|
|
|
|
|
|
|
|
// e4.printStackTrace();
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo-> Exception " + e4.getMessage());
|
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
throw e4;
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
}
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void setInputParameters() {
|
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
// AnalysisLogger.getLogger().debug("In ListDBInfo->setting inputs");
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
// resource name specified by the user
|
|
|
|
addStringInput("ResourceName", "The name of the resource", "");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void shutdown() {
|
|
|
|
AnalysisLogger.getLogger().debug("In ListDBInfo->Shutdown");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StatisticalType getOutput() {
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug("In ListDBInfo->retrieving outputs");
|
|
|
|
|
|
|
|
// generate a primitive type for the collection
|
|
|
|
PrimitiveType output = new PrimitiveType(LinkedHashMap.class.getName(),
|
2017-02-28 17:16:34 +01:00
|
|
|
map, PrimitiveTypes.MAP, "ResultsMap" + UUID.randomUUID(),
|
|
|
|
"Results Map");
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
private List<AccessPoint> retrieveInfo() throws Exception,
|
|
|
|
IllegalStateException, DiscoveryException, InvalidResultException {
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
resourceName = getInputParameter("ResourceName");
|
|
|
|
|
|
|
|
if (resourceName != null) {
|
|
|
|
resourceName = getInputParameter("ResourceName").trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((resourceName == null) || (resourceName.equals(""))) {
|
|
|
|
throw new Exception("Warning: insert the resource name");
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve information about the chosen resource
|
|
|
|
DatabasesDiscoverer discovery = new DatabasesDiscoverer();
|
|
|
|
|
|
|
|
List<DBResource> resources = discovery.discover();
|
|
|
|
|
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->number of database resources: "
|
|
|
|
+ resources.size());
|
|
|
|
|
|
|
|
check: for (int i = 0; i < resources.size(); i++) {
|
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
if (resources.get(i).getResourceName().toLowerCase()
|
|
|
|
.equals(resourceName.toLowerCase())) {
|
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
platformVersion = resources.get(i).getPlatformVersion();
|
|
|
|
|
|
|
|
// ap = resources.get(i).getAccessPoints();
|
|
|
|
|
|
|
|
normalizeDBInfo(resources.get(i));
|
|
|
|
|
|
|
|
ap = resources.get(i).getAccessPoints();
|
|
|
|
|
|
|
|
break check;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return ap;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void normalizeDBInfo(DBResource resource) throws Exception {
|
2017-02-28 17:16:34 +01:00
|
|
|
|
|
|
|
try {
|
2016-11-03 17:27:14 +01:00
|
|
|
int ap = resource.getAccessPoints().size();
|
|
|
|
|
|
|
|
for (int i = 0; i < ap; i++) {
|
|
|
|
resource.normalize(i);
|
|
|
|
}
|
2017-02-28 17:16:34 +01:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
2016-11-03 17:27:14 +01:00
|
|
|
AnalysisLogger.getLogger().debug(
|
|
|
|
"In ListDBInfo->: Error in normalization process"
|
|
|
|
+ e.getMessage());
|
2017-02-28 17:16:34 +01:00
|
|
|
|
2016-11-03 17:27:14 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2017-02-28 17:16:34 +01:00
|
|
|
// int ap = resource.getAccessPoints().size();
|
|
|
|
//
|
|
|
|
// for (int i = 0; i < ap; i++) {
|
|
|
|
//
|
|
|
|
// try {
|
|
|
|
// resource.normalize(i);
|
|
|
|
// }
|
|
|
|
// // catch (IOException e) {
|
|
|
|
// catch (IOException e) {
|
|
|
|
// // TODO Auto-generated catch block
|
|
|
|
// // e.printStackTrace();
|
|
|
|
// AnalysisLogger.getLogger().debug(
|
|
|
|
// "In ListDBInfo->: Error in normalization process"
|
|
|
|
// + e.getMessage());
|
|
|
|
// throw e;
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
2016-11-03 17:27:14 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|