2017-11-08 16:12:38 +01:00
|
|
|
package org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration;
|
|
|
|
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashMap;
|
2018-07-18 11:21:17 +02:00
|
|
|
import java.util.Iterator;
|
2023-01-18 17:56:48 +01:00
|
|
|
import java.util.List;
|
2017-11-08 16:12:38 +01:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.clients.configuration.ConfigurationImpl.CONFIGURATIONS;
|
|
|
|
import org.gcube.dataanalysis.dataminer.poolmanager.clients.configurations.AbstractConfiguration;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
2023-01-18 17:56:48 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
public class ClientConfigurationCache
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
private Logger logger;
|
|
|
|
private SVNRepository svnRepository;
|
2017-11-08 16:12:38 +01:00
|
|
|
private Map<String, AbstractConfiguration> configurations;
|
2018-07-18 11:21:17 +02:00
|
|
|
private long svnRepositoryTimeout;
|
|
|
|
private Map<String, Long> configurationsTimeouts;
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2023-01-18 17:56:48 +01:00
|
|
|
private final long duration = 120000; //2 minutes
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
ClientConfigurationCache ()
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
this.logger = LoggerFactory.getLogger(ClientConfigurationCache.class);
|
|
|
|
this.svnRepository = null;
|
2017-11-08 16:12:38 +01:00
|
|
|
this.configurations = new HashMap<>();
|
2018-07-18 11:21:17 +02:00
|
|
|
this.svnRepositoryTimeout = 0;
|
2017-11-08 16:12:38 +01:00
|
|
|
this.configurationsTimeouts = new HashMap<>();
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
public AbstractConfiguration getConfiguration (CONFIGURATIONS configuration)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
Long time = this.configurationsTimeouts.get(configuration.toString());
|
|
|
|
long currentTime = new Date().getTime();
|
|
|
|
|
|
|
|
if (time == null || currentTime > time+this.duration)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
this.configurations.put(configuration.toString(), getConfiguration (configuration.getType()));
|
|
|
|
this.configurationsTimeouts.put(configuration.toString(), currentTime);
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
return this.configurations.get(configuration.toString());
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
public SVNRepository getSVNRepository ()
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
long currentTime = new Date().getTime();
|
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
if (this.svnRepositoryTimeout == 0 || currentTime > this.svnRepositoryTimeout+this.duration)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
this.svnRepository = queryForRepository();
|
|
|
|
this.svnRepositoryTimeout = currentTime;
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
return this.svnRepository;
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
private SVNRepository queryForRepository()
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
SimpleQuery query = queryFor(GenericResource.class);
|
|
|
|
query.addCondition("$resource/Profile/SecondaryType/text() eq 'DMPMConfigurator'").setResult("$resource");
|
|
|
|
DiscoveryClient<GenericResource> client = clientFor(GenericResource.class);
|
|
|
|
List<GenericResource> ds = client.submit(query);
|
2018-07-18 11:21:17 +02:00
|
|
|
Iterator<GenericResource> resourcesIterator = ds.iterator();
|
|
|
|
SVNRepository response = null;
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
while (resourcesIterator.hasNext() && response == null)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
GenericResource resource = resourcesIterator.next();
|
|
|
|
String repositoryURL = resource.profile().body().getElementsByTagName(SVNRepository.REPOSITORY_URL).item(0).getTextContent();
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
if (repositoryURL != null)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
|
|
|
|
String repositoryPath = resource.profile().body().getElementsByTagName(SVNRepository.REPOSITORY_PATH).item(0).getTextContent();
|
|
|
|
String repositoryUsername = null;
|
|
|
|
String repositoryPassword = null;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
repositoryUsername = resource.profile().body().getElementsByTagName(SVNRepository.REPOSITORY_USERNAME).item(0).getTextContent();
|
|
|
|
repositoryPassword = resource.profile().body().getElementsByTagName(SVNRepository.REPOSITORY_PASSWORD).item(0).getTextContent();
|
|
|
|
|
|
|
|
if (repositoryUsername != null && repositoryUsername.trim() == "") repositoryUsername = null;
|
|
|
|
|
|
|
|
if (repositoryPassword != null && repositoryPassword.trim() == "") repositoryPassword = null;
|
|
|
|
|
2024-01-29 12:49:28 +01:00
|
|
|
logger.debug("Repository username "+repositoryUsername);
|
|
|
|
logger.debug("Repository password "+repositoryPassword);
|
2018-07-18 11:21:17 +02:00
|
|
|
|
2023-01-18 17:56:48 +01:00
|
|
|
} catch (Exception e)
|
|
|
|
{
|
2018-07-18 11:21:17 +02:00
|
|
|
this.logger.debug("SVN Username and password not present");
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2018-07-18 11:21:17 +02:00
|
|
|
|
2024-01-29 12:49:28 +01:00
|
|
|
logger.debug("SVN Repository URL: "+repositoryURL);
|
|
|
|
logger.debug("SVN Repository path: "+repositoryPath);
|
2018-07-18 11:21:17 +02:00
|
|
|
|
|
|
|
response = new SVNRepository(repositoryURL, repositoryPath,repositoryUsername, repositoryPassword);
|
|
|
|
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2018-07-18 11:21:17 +02:00
|
|
|
|
|
|
|
return response;
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
|
|
|
private AbstractConfiguration getConfiguration (AbstractConfiguration type)
|
2023-01-18 17:56:48 +01:00
|
|
|
{
|
2017-11-08 16:12:38 +01:00
|
|
|
SimpleQuery query = queryFor(GenericResource.class);
|
|
|
|
query.addCondition("$resource/Profile/SecondaryType/text() eq 'DMPMConfigurator'").setResult(type.getXMLModel());
|
|
|
|
DiscoveryClient<? extends AbstractConfiguration> client = clientFor(type.getClass());
|
|
|
|
List<? extends AbstractConfiguration> configurations = client.submit(query);
|
|
|
|
if (configurations != null && !configurations.isEmpty()) return configurations.get(0);
|
|
|
|
else return null;
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|
2017-11-08 16:12:38 +01:00
|
|
|
|
2023-01-18 17:56:48 +01:00
|
|
|
}
|