accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/dao/DatabaseParameterRetriever....

109 lines
3.8 KiB
Java

package org.gcube.dataharvest.dao;
import java.security.Key;
import java.util.List;
import java.util.Properties;
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.utils.Group;
import org.gcube.dataharvest.AccountingDataHarvesterPlugin;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class DatabaseParameterRetriever {
private static Logger logger = LoggerFactory.getLogger(DatabaseParameterRetriever.class);
public static final String LOCAL_DB = "LOCAL_DB";
public static final String SERVICE_ENDPOINT_CATEGORY = "Database";
public static final String SERVICE_ENDPOINT_NAME = "AccountingDashboard";
public static final String DB_URI = "DB_URI";
public static final String DB_USERNAME = "DB_USERNAME";
public static final String DB_PASSWORD = "DB_PASSWORD";
public DatabaseParameterRetriever() {
}
private static String decrypt(String encrypted, Key... key) throws Exception {
return StringEncrypter.getEncrypter().decrypt(encrypted);
}
protected void checkParameter(String parameter, String parameterName, boolean localDB) throws DaoException {
if(parameter == null || parameter.isEmpty()) {
throw new DaoException("DB " + parameterName + " cannot be null nor empty. Please check your "
+ (localDB ? "local configuration." : "ServiceEndpoint"));
}
}
public DatabaseConnectionData retrieveDatabaseInfo() throws DaoException {
Properties properties = AccountingDataHarvesterPlugin.getProperties().get();
boolean localDB = Boolean.parseBoolean(properties.getProperty(LOCAL_DB, "true"));
String uri = "";
String username = "";
String password = "";
if(localDB) {
logger.debug("Using configuration from local config");
uri = properties.getProperty(DB_URI);
username = properties.getProperty(DB_USERNAME);
password = properties.getProperty(DB_PASSWORD);
} else {
try {
String className = this.getClass().getSimpleName();
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/Interface/Endpoint/@EntryName eq '%s'",
className));
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
if(serviceEndpoints.size()==0) {
throw new DaoException("No endpoints found to get database connection.");
}
if(serviceEndpoints.size() > 1) {
throw new DaoException("More than one endpoint found to get database connection. Not sure which one use.");
}
Group<AccessPoint> accessPoints = serviceEndpoints.get(0).profile().accessPoints();
for(AccessPoint accessPoint : accessPoints) {
if(accessPoint.name().compareTo(className) == 0) {
uri = accessPoint.address();
username = accessPoint.username();
String encryptedPassword = accessPoint.password();
password = decrypt(encryptedPassword);
}
}
} catch(Exception e) {
throw new DaoException("Error retrieving database connection", e);
}
}
checkParameter(uri, "URI", localDB);
checkParameter(username, "Username", localDB);
checkParameter(password, "Password", localDB);
return new DatabaseConnectionData(uri, username, password);
}
}