Reorganizing code

This commit is contained in:
Luca Frosini 2022-12-06 18:29:31 +01:00
parent f8c9033f3c
commit 0fd2f53a5d
6 changed files with 201 additions and 51 deletions

View File

@ -0,0 +1,83 @@
package org.gcube.gcat;
import java.io.IOException;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.rest.RequestFilter;
import org.gcube.smartgears.ApplicationManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GCatInitializator implements ApplicationManager {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(GCatInitializator.class);
/**
* {@inheritDoc}
* The method discover the plugins available on classpath and their own
* supported capabilities and publish a ServiceEndpoint with the
* discovered information.
* Furthermore create/connect to DB
*/
@Override
public void onInit() {
RequestFilter requestFilter = new RequestFilter();
try {
requestFilter.filter(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
String context = SecretManagerProvider.instance.get().getContext();
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat is Starting on context {}\n"
+ "-------------------------------------------------------",
context);
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat Started Successfully on context {}\n"
+ "-------------------------------------------------------",
context);
}
/**
* {@inheritDoc}
* This function is invoked before the service will stop and unpublish the
* resource from the IS to maintain the infrastructure integrity.
* Furthermore close the connection to DB.
*/
@Override
public void onShutdown(){
RequestFilter requestFilter = new RequestFilter();
try {
requestFilter.filter(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
String context = SecretManagerProvider.instance.get().getContext();
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat is Stopping on context {}\n"
+ "-------------------------------------------------------",
context);
logger.trace(
"\n-------------------------------------------------------\n"
+ "GCat Stopped Successfully on context {}\n"
+ "-------------------------------------------------------",
context);
}
}

View File

@ -5,6 +5,7 @@ import java.util.Map;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider; import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
import org.gcube.gcat.configuration.isproxies.GCoreISConfigurationProxy; import org.gcube.gcat.configuration.isproxies.GCoreISConfigurationProxy;
import org.gcube.gcat.configuration.isproxies.ISConfigurationProxyFactory;
import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxy; import org.gcube.gcat.configuration.isproxies.FacetBasedISConfigurationProxy;
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration; import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.gcat.persistence.ckan.CKANUserCache; import org.gcube.gcat.persistence.ckan.CKANUserCache;
@ -21,7 +22,7 @@ public class CatalogueConfigurationFactory {
} }
private static ServiceCatalogueConfiguration load(String context) { private static ServiceCatalogueConfiguration load(String context) {
FacetBasedISConfigurationProxy restISConfigurationProxy = new FacetBasedISConfigurationProxy(context); FacetBasedISConfigurationProxy restISConfigurationProxy = ISConfigurationProxyFactory.getInstance(context);
ServiceCatalogueConfiguration serviceCatalogueConfiguration = restISConfigurationProxy.getCatalogueConfiguration(); ServiceCatalogueConfiguration serviceCatalogueConfiguration = restISConfigurationProxy.getCatalogueConfiguration();
if(serviceCatalogueConfiguration == null) { if(serviceCatalogueConfiguration == null) {
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
@ -31,16 +32,18 @@ public class CatalogueConfigurationFactory {
} }
private static void purgeFromIS(String context) { private static void purgeFromIS(String context) {
FacetBasedISConfigurationProxy restISConfigurationProxy = new FacetBasedISConfigurationProxy(context); FacetBasedISConfigurationProxy restISConfigurationProxy = ISConfigurationProxyFactory.getInstance(context);
restISConfigurationProxy.delete(); restISConfigurationProxy.delete();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
gCoreISConfigurationProxy.delete(); gCoreISConfigurationProxy.delete();
} }
private static void createOrUpdateOnIS(String context, ServiceCatalogueConfiguration catalogueConfiguration) throws Exception { private static void createOrUpdateOnIS(String context, ServiceCatalogueConfiguration catalogueConfiguration) throws Exception {
FacetBasedISConfigurationProxy restISConfigurationProxy = new FacetBasedISConfigurationProxy(context,catalogueConfiguration); FacetBasedISConfigurationProxy restISConfigurationProxy = ISConfigurationProxyFactory.getInstance(context);
restISConfigurationProxy.setCatalogueConfiguration(catalogueConfiguration);
restISConfigurationProxy.createOrUpdateOnIS(); restISConfigurationProxy.createOrUpdateOnIS();
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context, catalogueConfiguration); GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
gCoreISConfigurationProxy.setCatalogueConfiguration(catalogueConfiguration);
gCoreISConfigurationProxy.createOrUpdateOnIS(); gCoreISConfigurationProxy.createOrUpdateOnIS();
} }

View File

@ -22,6 +22,7 @@ import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
import org.gcube.informationsystem.model.impl.relations.ConsistsOfImpl; import org.gcube.informationsystem.model.impl.relations.ConsistsOfImpl;
import org.gcube.informationsystem.model.reference.entities.Entity; import org.gcube.informationsystem.model.reference.entities.Entity;
import org.gcube.informationsystem.model.reference.properties.Encrypted; import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.model.reference.properties.Header;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf; import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory; import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
@ -119,14 +120,20 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
objectMapper = new ObjectMapper(); objectMapper = new ObjectMapper();
resourceRegistryClient = ResourceRegistryClientFactory.create(); resourceRegistryClient = ResourceRegistryClientFactory.create();
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
} }
public FacetBasedISConfigurationProxy(String context, ServiceCatalogueConfiguration catalogueConfiguration) { // public FacetBasedISConfigurationProxy(String context, ServiceCatalogueConfiguration catalogueConfiguration) {
super(context, catalogueConfiguration); // super(context, catalogueConfiguration);
objectMapper = new ObjectMapper(); // objectMapper = new ObjectMapper();
resourceRegistryClient = ResourceRegistryClientFactory.create(); // resourceRegistryClient = ResourceRegistryClientFactory.create();
resourceRegistryPublisher = ResourceRegistryPublisherFactory.create(); // resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
// }
public VirtualService getVirtualService() {
if(virtualService==null) {
virtualService = queryVirtualService();
}
return virtualService;
} }
public File getJsonQueryFromFile(String filename) throws Exception { public File getJsonQueryFromFile(String filename) throws Exception {
@ -175,9 +182,13 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
return getUniqueEntity(entities, jsonQueryAsString); return getUniqueEntity(entities, jsonQueryAsString);
} }
protected VirtualService queryVirtualService() throws Exception { protected VirtualService queryVirtualService() throws InternalServerErrorException {
VirtualService virtualService = (VirtualService) queryEntity(GET_CATALOGUE_VIRTUAL_SERVICE_FILENAME); try {
return virtualService; VirtualService virtualService = (VirtualService) queryEntity(GET_CATALOGUE_VIRTUAL_SERVICE_FILENAME);
return virtualService;
}catch (Exception e) {
throw new InternalServerErrorException(e);
}
} }
protected Configuration queryGcatConfiguration() throws Exception { protected Configuration queryGcatConfiguration() throws Exception {
@ -205,12 +216,12 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
return accessPointFacet; return accessPointFacet;
} }
protected ServiceCatalogueConfiguration setGcatConfiguration(ServiceCatalogueConfiguration catalogueConfiguration) throws Exception { protected ServiceCatalogueConfiguration setConfigurationInfoFromSimpleFacet(SimpleFacet configurationSimpleFacet, ServiceCatalogueConfiguration catalogueConfiguration) throws Exception {
SimpleFacet configurationSimpleFacet = getISResource(); Header header = configurationSimpleFacet.getHeader();
UUID uuid = configurationSimpleFacet.getHeader().getUUID(); if(header!=null && header.getUUID()!=null) {
if(uuid!=null) { catalogueConfiguration.setID(header.getUUID().toString());
catalogueConfiguration.setID(uuid.toString());
} }
catalogueConfiguration.setModerationEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.MODERATION_ENABLED_KEY)); catalogueConfiguration.setModerationEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.MODERATION_ENABLED_KEY));
catalogueConfiguration.setNotificationToUsersEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY)); catalogueConfiguration.setNotificationToUsersEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY));
catalogueConfiguration.setSocialPostEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY)); catalogueConfiguration.setSocialPostEnabled((boolean) configurationSimpleFacet.getAdditionalProperty(CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY));
@ -244,7 +255,7 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
} }
if(forceUpdate) { if(forceUpdate) {
updateOnIS(configurationSimpleFacet); updateOnIS();
} }
return catalogueConfiguration; return catalogueConfiguration;
@ -280,14 +291,34 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
return catalogueConfiguration; return catalogueConfiguration;
} }
protected SimpleFacet instatiateSimpleFacetUsingDefaultValues(ServiceCatalogueConfiguration catalogueConfiguration) { protected SimpleFacet getSimpleFacetFromConfiguration(ServiceCatalogueConfiguration catalogueConfiguration) {
SimpleFacet simpleFacet = new SimpleFacetImpl(); SimpleFacet simpleFacet = new SimpleFacetImpl();
if(catalogueConfiguration.getID()!=null) {
UUID uuid = null;
try {
uuid = UUID.fromString(catalogueConfiguration.getID());
simpleFacet.getHeader().setUUID(uuid);
}catch (Exception e) {
}
}
simpleFacet.setAdditionalProperty(CatalogueConfiguration.MODERATION_ENABLED_KEY, catalogueConfiguration.isModerationEnabled()); simpleFacet.setAdditionalProperty(CatalogueConfiguration.MODERATION_ENABLED_KEY, catalogueConfiguration.isModerationEnabled());
simpleFacet.setAdditionalProperty(CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY, catalogueConfiguration.isNotificationToUsersEnabled()); simpleFacet.setAdditionalProperty(CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY, catalogueConfiguration.isNotificationToUsersEnabled());
simpleFacet.setAdditionalProperty(CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY, catalogueConfiguration.isSocialPostEnabled()); simpleFacet.setAdditionalProperty(CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY, catalogueConfiguration.isSocialPostEnabled());
simpleFacet.setAdditionalProperty(CatalogueConfiguration.DEFAULT_ORGANIZATION_KEY, catalogueConfiguration.getDefaultOrganization()); simpleFacet.setAdditionalProperty(CatalogueConfiguration.DEFAULT_ORGANIZATION_KEY, catalogueConfiguration.getDefaultOrganization());
simpleFacet.setAdditionalProperty(CatalogueConfiguration.SUPPORTED_ORGANIZATIONS_KEY, catalogueConfiguration.getSupportedOrganizations()); simpleFacet.setAdditionalProperty(CatalogueConfiguration.SUPPORTED_ORGANIZATIONS_KEY, catalogueConfiguration.getSupportedOrganizations());
Map<String,Object> additionalProperties = new HashMap<>(catalogueConfiguration.getAdditionalProperties());
for(String key : additionalProperties.keySet()) {
if(!CatalogueConfiguration.KNOWN_PROPERTIES.contains(key)) {
Object value = additionalProperties.get(key);
simpleFacet.setAdditionalProperty(key, value);
}
}
return simpleFacet; return simpleFacet;
} }
@ -295,13 +326,13 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
protected ServiceCatalogueConfiguration readFromIS() { protected ServiceCatalogueConfiguration readFromIS() {
try { try {
catalogueConfiguration = new FacetBasedISServiceCatalogueConfiguration(context, this); catalogueConfiguration = new FacetBasedISServiceCatalogueConfiguration(context, this);
virtualService = queryVirtualService();
configurationSimpleFacet = getISResource(); configurationSimpleFacet = getISResource();
if(configurationSimpleFacet==null) { if(configurationSimpleFacet==null) {
configurationSimpleFacet = instatiateSimpleFacetUsingDefaultValues(catalogueConfiguration); configurationSimpleFacet = getSimpleFacetFromConfiguration(catalogueConfiguration);
}else { }else {
catalogueConfiguration = setGcatConfiguration(catalogueConfiguration); catalogueConfiguration = setConfigurationInfoFromSimpleFacet(configurationSimpleFacet, catalogueConfiguration);
} }
catalogueConfiguration = setCkanServiceInfo(catalogueConfiguration); catalogueConfiguration = setCkanServiceInfo(catalogueConfiguration);
@ -337,7 +368,7 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
ConsistsOf<Configuration, SimpleFacet> co = new ConsistsOfImpl<>(gcatConfiguration, configurationSimpleFacet); ConsistsOf<Configuration, SimpleFacet> co = new ConsistsOfImpl<>(gcatConfiguration, configurationSimpleFacet);
co = resourceRegistryPublisher.create(co); co = resourceRegistryPublisher.create(co);
configurationSimpleFacet = co.getTarget(); configurationSimpleFacet = co.getTarget();
setGcatConfiguration(catalogueConfiguration); setConfigurationInfoFromSimpleFacet(configurationSimpleFacet, catalogueConfiguration);
} }
return catalogueConfiguration; return catalogueConfiguration;
} }
@ -355,13 +386,15 @@ public class FacetBasedISConfigurationProxy extends ISConfigurationProxy<SimpleF
} }
@Override @Override
protected ServiceCatalogueConfiguration updateOnIS(SimpleFacet configurationSimpleFacet) throws Exception { protected ServiceCatalogueConfiguration updateOnIS() throws Exception {
UUID uuid = configurationSimpleFacet.getHeader().getUUID(); Header header = configurationSimpleFacet.getHeader();
if(uuid!=null) { if(header!=null && header.getUUID()!=null) {
this.configurationSimpleFacet = resourceRegistryPublisher.update(configurationSimpleFacet); configurationSimpleFacet = getSimpleFacetFromConfiguration(catalogueConfiguration);
setGcatConfiguration(catalogueConfiguration); configurationSimpleFacet = resourceRegistryPublisher.update(configurationSimpleFacet);
setConfigurationInfoFromSimpleFacet(configurationSimpleFacet, catalogueConfiguration);
} }
return catalogueConfiguration; return catalogueConfiguration;
} }
} }

View File

@ -91,24 +91,13 @@ public class GCoreISConfigurationProxy extends ISConfigurationProxy<ServiceEndpo
private final static String OLD_NAME = "CKanDataCatalogue"; private final static String OLD_NAME = "CKanDataCatalogue";
protected ObjectMapper mapper; protected ObjectMapper mapper;
protected ServiceEndpoint serviceEndpoint;
public GCoreISConfigurationProxy(String context) { public GCoreISConfigurationProxy(String context) {
super(context); super(context);
this.mapper = new ObjectMapper(); this.mapper = new ObjectMapper();
} }
public GCoreISConfigurationProxy(String context, ServiceCatalogueConfiguration catalogueConfiguration) {
super(context, catalogueConfiguration);
this.mapper = new ObjectMapper();
}
public ServiceCatalogueConfiguration getCatalogueConfiguration() throws WebApplicationException {
if (catalogueConfiguration == null) {
catalogueConfiguration = readFromIS();
}
return catalogueConfiguration;
}
protected AccessPoint getAccessPoint(Profile profile) { protected AccessPoint getAccessPoint(Profile profile) {
Group<AccessPoint> accessPoints = profile.accessPoints(); Group<AccessPoint> accessPoints = profile.accessPoints();
Iterator<AccessPoint> accessPointIterator = accessPoints.iterator(); Iterator<AccessPoint> accessPointIterator = accessPoints.iterator();
@ -169,13 +158,15 @@ public class GCoreISConfigurationProxy extends ISConfigurationProxy<ServiceEndpo
@Override @Override
protected ServiceEndpoint getISResource() { protected ServiceEndpoint getISResource() {
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME); if(serviceEndpoint==null) {
if (serviceEndpoints==null || serviceEndpoints.size() == 0) { List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME);
logger.error("There is no {} having Category {} and Name {} in this context.", if (serviceEndpoints==null || serviceEndpoints.size() == 0) {
ServiceEndpoint.class.getSimpleName(), GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME); logger.error("There is no {} having Category {} and Name {} in this context.",
return null; ServiceEndpoint.class.getSimpleName(), GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME);
return null;
}
serviceEndpoint = serviceEndpoints.get(0);
} }
ServiceEndpoint serviceEndpoint = serviceEndpoints.get(0);
return serviceEndpoint; return serviceEndpoint;
} }
@ -671,8 +662,9 @@ public class GCoreISConfigurationProxy extends ISConfigurationProxy<ServiceEndpo
} }
@Override @Override
protected ServiceCatalogueConfiguration updateOnIS(ServiceEndpoint serviceEndpoint) throws Exception { protected ServiceCatalogueConfiguration updateOnIS() throws Exception {
RegistryPublisher registryPublisher = RegistryPublisherFactory.create(); RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
ServiceEndpoint serviceEndpoint = getISResource();
String id = serviceEndpoint.id(); String id = serviceEndpoint.id();
catalogueConfiguration.setID(id); catalogueConfiguration.setID(id);
serviceEndpoint = createServiceEndpoint(serviceEndpoint); serviceEndpoint = createServiceEndpoint(serviceEndpoint);

View File

@ -32,11 +32,15 @@ public abstract class ISConfigurationProxy<ISResource extends Object> {
return catalogueConfiguration; return catalogueConfiguration;
} }
public void setCatalogueConfiguration(ServiceCatalogueConfiguration catalogueConfiguration) {
this.catalogueConfiguration = catalogueConfiguration;
}
public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception { public ServiceCatalogueConfiguration createOrUpdateOnIS() throws Exception {
ISResource isResource = getISResource(); ISResource isResource = getISResource();
if(isResource!=null) { if(isResource!=null) {
// It's an update // It's an update
catalogueConfiguration = updateOnIS(isResource); catalogueConfiguration = updateOnIS();
}else { }else {
// It's a create // It's a create
catalogueConfiguration = createOnIS(); catalogueConfiguration = createOnIS();
@ -51,7 +55,7 @@ public abstract class ISConfigurationProxy<ISResource extends Object> {
Version version = new Version(applicationConfiguration.version()); Version version = new Version(applicationConfiguration.version());
return version; return version;
}catch (Exception e) { }catch (Exception e) {
return new Version("2.2.0"); return new Version("2.4.2");
} }
} }
@ -61,7 +65,7 @@ public abstract class ISConfigurationProxy<ISResource extends Object> {
protected abstract ServiceCatalogueConfiguration readFromIS(); protected abstract ServiceCatalogueConfiguration readFromIS();
protected abstract ServiceCatalogueConfiguration updateOnIS(ISResource isResource) throws Exception; protected abstract ServiceCatalogueConfiguration updateOnIS() throws Exception;
public abstract void delete(); public abstract void delete();

View File

@ -0,0 +1,35 @@
package org.gcube.gcat.configuration.isproxies;
import java.util.HashMap;
import java.util.Map;
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
public class ISConfigurationProxyFactory {
private static final Map<String, FacetBasedISConfigurationProxy> isConfigurationProxies;
static {
isConfigurationProxies = new HashMap<>();
}
private static FacetBasedISConfigurationProxy find(String context) {
FacetBasedISConfigurationProxy isConfigurationProxy = new FacetBasedISConfigurationProxy(context);
return isConfigurationProxy;
}
public synchronized static FacetBasedISConfigurationProxy getInstance(String context) {
FacetBasedISConfigurationProxy isConfigurationProxy = isConfigurationProxies.get(context);
if(isConfigurationProxy == null) {
isConfigurationProxy = find(context);
isConfigurationProxies.put(context, isConfigurationProxy);
}
return isConfigurationProxy;
}
public static FacetBasedISConfigurationProxy getInstance() {
String context = SecretManagerProvider.instance.get().getContext();
return getInstance(context);
}
}