Renamed Project by adding -se-plugin suffix as convention

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/is-exporter-se-plugin@148403 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-05-09 13:41:59 +00:00
commit f7b3f9f230
23 changed files with 1747 additions and 0 deletions

36
.classpath Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>is-exporter-se-plugin</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

View File

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

208
pom.xml Normal file
View File

@ -0,0 +1,208 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.gcube.information-system</groupId>
<artifactId>is-exporter-se-plugin</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>IS Exporter</name>
<description>Exports GenericReosurce and ServiceEndpoint from gCoreIS to Resource Registry</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<distroDirectory>${project.basedir}/distro</distroDirectory>
<serviceClass>InformationSystem</serviceClass>
</properties>
<scm>
<connection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId}</connection>
<developerConnection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId}</developerConnection>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/${project.artifactId}</url>
</scm>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>information-system-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.vremanagement</groupId>
<artifactId>smart-executor-api</artifactId>
<version>[1.5.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.core</groupId>
<artifactId>common-scope</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources</groupId>
<artifactId>common-gcore-resources</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>authorization-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>information-system-model</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>gcube-resources</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry-publisher</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>home-library-jcr</artifactId>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>home-library</artifactId>
<version>[2.0.0-SNAPSHOT,3.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>copy-profile</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target</outputDirectory>
<resources>
<resource>
<directory>${distroDirectory}</directory>
<filtering>true</filtering>
<includes>
<include>profile.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,55 @@
package org.gcube.informationsystem.exporter;
import java.util.Map;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.informationsystem.exporter.mapper.GenericResourceExporter;
import org.gcube.informationsystem.exporter.mapper.ServiceEndpointExporter;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ISExporterPlugin extends Plugin<ISExporterPluginDeclaration> {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(ISExporterPlugin.class);
public ISExporterPlugin(ISExporterPluginDeclaration pluginDeclaration) {
super(pluginDeclaration);
logger.debug("contructor");
}
/**{@inheritDoc}*/
@Override
public void launch(Map<String, Object> inputs) throws Exception {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String scope = authorizationEntry.getContext();
logger.debug("Launching {} execution on scope {}",
ISExporterPluginDeclaration.NAME, scope);
GenericResourceExporter genericResourceExporter = new GenericResourceExporter();
genericResourceExporter.export();
ServiceEndpointExporter serviceEndpointExporter = new ServiceEndpointExporter();
serviceEndpointExporter.export();
logger.debug("{} execution finished", ISExporterPluginDeclaration.NAME);
}
/**{@inheritDoc}*/
@Override
protected void onStop() throws Exception {
logger.debug("onStop()");
Thread.currentThread().interrupt();
}
}

View File

@ -0,0 +1,69 @@
/**
*
*/
package org.gcube.informationsystem.exporter;
import java.util.HashMap;
import java.util.Map;
import org.gcube.vremanagement.executor.plugin.Plugin;
import org.gcube.vremanagement.executor.plugin.PluginDeclaration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ISExporterPluginDeclaration implements PluginDeclaration {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(ISExporterPlugin.class);
/**
* Plugin name used by the Executor to retrieve this class
*/
public static final String NAME = "ISExporter";
public static final String DESCRIPTION = "IS Exporter";
public static final String VERSION = "1.0.0";
/**{@inheritDoc}*/
@Override
public void init() {
logger.debug(String.format("%s initialized", ISExporterPlugin.class.getSimpleName()));
}
/**{@inheritDoc}*/
@Override
public String getName() {
return NAME;
}
/**{@inheritDoc}*/
@Override
public String getDescription() {
return DESCRIPTION;
}
/**{@inheritDoc}*/
@Override
public String getVersion() {
return VERSION;
}
/**{@inheritDoc}*/
@Override
public Map<String, String> getSupportedCapabilities() {
Map<String, String> discoveredCapabilities = new HashMap<String, String>();
// No capabilities to discover
return discoveredCapabilities;
}
/**{@inheritDoc}*/
@Override
public Class<? extends Plugin<? extends PluginDeclaration>> getPluginImplementation() {
return ISExporterPlugin.class;
}
}

View File

@ -0,0 +1,367 @@
package org.gcube.informationsystem.exporter.mapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.homelibrary.home.Home;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.HomeManager;
import org.gcube.common.homelibrary.home.HomeManagerFactory;
import org.gcube.common.homelibrary.home.workspace.Workspace;
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
import org.gcube.common.homelibrary.util.WorkspaceUtil;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.exporter.ISExporterPluginDeclaration;
import org.gcube.informationsystem.exporter.mapper.exception.CreateException;
import org.gcube.informationsystem.exporter.mapper.exception.UpdateException;
import org.gcube.informationsystem.impl.utils.ISMapper;
import org.gcube.informationsystem.model.ISConstants;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
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;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
public abstract class GCoreResourceMapper<GR extends org.gcube.common.resources.gcore.Resource, R extends Resource> {
/**
* Logger
*/
private static Logger logger = LoggerFactory.getLogger(GCoreResourceMapper.class);
public static final String EXPORTED = "EXPORTED";
public static final String EXPORTED_FROM_OLD_GCORE_IS = "EXPORTED_FROM_OLD_GCORE_IS";
public static final String MAPPING_ERROR = "mapping";
public static final String PUBLISHING_ERROR = "publishing";
public static final String TYPE = "type";
public static final String ID = "id";
public static final String ERROR = "error";
public static final String DATE = "date";
public static final String EXCEPTION_TYPE = "exceptionType";
public static final String CREATE = "create";
public static final String UPDATE = "update";
protected Class<GR> grClass;
protected Class<R> rClass;
public static final String UTF8 = "UTF-8";
protected ResourceRegistryPublisher resourceRegistryPublisher;
protected ResourceRegistryClient resourceRegistryClient;
public static String getCurrentContextName(){
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = null;
try {
authorizationEntry = Constants.authorizationService().get(token);
} catch (Exception e) {
return ScopeProvider.instance.get();
}
return authorizationEntry.getContext();
}
public static String getDateString(Calendar calendar){
Date date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat(ISConstants.DATETIME_PATTERN);
return format.format(date);
}
public File getFile(Class<?> grClass, String contextFullName, String dateString){
return new File(contextFullName.replace("/", "_") + "-" + grClass.getSimpleName() + "-" + dateString + "-exporter.json");
}
protected GCoreResourceMapper(Class<GR> grClass, Class<R> rClass){
this.grClass = grClass;
this.rClass = rClass;
this.resourceRegistryPublisher = ResourceRegistryPublisherFactory.create();
this.resourceRegistryClient = ResourceRegistryClientFactory.create();
}
protected List<GR> getAll() {
SimpleQuery query = ICFactory.queryFor(grClass);
DiscoveryClient<GR> client = ICFactory.clientFor(grClass);
return client.submit(query);
}
protected String getStringAsUTF8(String s) throws UnsupportedEncodingException{
byte bytes[] = s.getBytes("ISO-8859-1");
return new String(bytes, UTF8);
}
protected R create(R r) throws ResourceRegistryException {
return resourceRegistryPublisher.createResource(rClass, r);
}
protected R update(R r) throws ResourceRegistryException {
return resourceRegistryPublisher.updateResource(rClass, r);
}
protected R read(UUID uuid) throws ResourceRegistryException {
return resourceRegistryClient.getInstance(rClass, uuid);
}
protected R createOrUpdate(R r) throws ResourceRegistryException {
UUID uuid = r.getHeader().getUUID();
boolean update = false;
try {
resourceRegistryClient.exists(rClass, uuid);
update = true;
}catch (ResourceNotFoundException e) {
update = false;
} catch (ResourceAvailableInAnotherContextException e) {
// This code should never be reached because this should be fixed in map function
resourceRegistryPublisher.addResourceToContext(uuid);
try{
Thread.sleep(100);
}catch (Exception ee) {}
update= true;
}
if(update){
logger.debug("Resource with UUID {} exist. It will be updated", uuid);
try{
return update(r);
}catch (ResourceRegistryException e) {
throw new UpdateException(e);
}
}else{
logger.debug("Resource with UUID {} does not exist. It will be created", uuid);
try{
return create(r);
}catch (ResourceRegistryException e) {
throw new CreateException(e);
}
}
}
private ObjectNode addNodeToArray(ArrayNode arrayNode, ObjectMapper objectMapper, GR gr, Exception e){
ObjectNode objectNode = objectMapper.createObjectNode();
objectNode.put(TYPE, grClass.getSimpleName());
objectNode.put(ID, gr.id());
objectNode.put(ERROR, e.getMessage());
arrayNode.add(objectNode);
return objectNode;
}
protected String getUsername() throws Exception {
String token = SecurityTokenProvider.instance.get();
return Constants.authorizationService().get(token).getClientInfo().getId();
}
@SuppressWarnings("deprecation")
protected Home getHome(HomeManager manager) throws Exception{
Home home = null;
String scope = ScopeProvider.instance.get();
if(scope!=null){
home = manager.getHome(getUsername());
}else{
home = manager.getHome();
}
return home;
}
protected WorkspaceFolder getExporterFolder(Workspace ws) throws Exception {
WorkspaceFolder root = ws.getRoot();
WorkspaceFolder exporterFolder = null;
if(!ws.exists(ISExporterPluginDeclaration.NAME, root.getId())){
String folderDescription = String.format("The folder is used by %s plugin to store informations regarding failures exporting old GCore Resource to new Resource Registry", ISExporterPluginDeclaration.NAME);
exporterFolder = ws.createFolder(ISExporterPluginDeclaration.NAME, folderDescription, root.getId());
}else{
exporterFolder = (WorkspaceFolder) ws.find(ISExporterPluginDeclaration.NAME, root.getId());
}
return exporterFolder;
}
public static final String APPLICATION_JSON_MIMETYPE = "application/json";
private static final String FOLDER_DESCRIPTION = "Failures Report Folder for " + ISExporterPluginDeclaration.NAME;
protected FolderItem publishFileToWorkspace(File file) throws Exception {
HomeManagerFactory factory = HomeLibrary.getHomeManagerFactory();
HomeManager manager = factory.getHomeManager();
Home home = getHome(manager);
Workspace ws = home.getWorkspace();
WorkspaceFolder exporterFolder = getExporterFolder(ws);
FileInputStream fileInputStream = new FileInputStream(file);
FolderItem folderItem = WorkspaceUtil.createExternalFile(
exporterFolder, file.getName(),
FOLDER_DESCRIPTION,
APPLICATION_JSON_MIMETYPE,
fileInputStream);
return folderItem;
}
protected abstract R map(GR gr) throws Exception;
protected void notifyFailures(int allSize, List<GR> failed){
String contextName = getCurrentContextName();
if(failed.size()==0){
logger.debug("No needs to create an empty report besause there were no exporting failures on {}", contextName);
return;
}
logger.warn("-------------------------------------------------------");
logger.warn("{} : {} of {} ({} failures) {}s were exported as {}s", contextName,
allSize-failed.size(), allSize, failed.size(), grClass.getSimpleName(), rClass.getSimpleName());
logger.warn("-------------------------------------------------------\n\n");
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode objectNode = objectMapper.createObjectNode();
Calendar calendar = Calendar.getInstance();
String dateString = getDateString(calendar);
objectNode.put(DATE, dateString);
ObjectNode context = objectNode.putObject(contextName);
ArrayNode mappingArrayNode = context.putArray(MAPPING_ERROR);
ArrayNode publishingArrayNode = context.putArray(PUBLISHING_ERROR);
for(GR gr : failed){
logger.trace("-------------------------------------------------------");
R r = null;
try {
r = map(gr);
} catch (Exception e) {
addNodeToArray(mappingArrayNode, objectMapper, gr, e);
logger.trace("Error exporting {}. The problem was on mapping {} with UUID {}", gr, grClass.getSimpleName(), gr.id());
logger.trace("-------------------------------------------------------\n");
continue;
}
try {
createOrUpdate(r);
}catch (Exception e) {
if(e.getCause() instanceof NullPointerException){
logger.error("This MUST BE A BUG. Please Investigate");
}
if(e.getMessage().contains("com.orientechnologies.orient.server.distributed.task.ODistributedOperationException")){
logger.error("This is an OrientDB distributed Issue");
}
ObjectNode node = addNodeToArray(publishingArrayNode, objectMapper, gr, e);
if(e instanceof CreateException) {
node.put(EXCEPTION_TYPE, CREATE);
}
if(e instanceof UpdateException){
node.put(EXCEPTION_TYPE, UPDATE);
}
try {
logger.trace("Error exporting {}. The problem was on publishing {} as {}", gr, rClass.getSimpleName(), ISMapper.marshal(r));
} catch (JsonProcessingException e1) {
logger.trace("", e1);
}
}
logger.trace("-------------------------------------------------------\n");
}
try {
String json = objectMapper.writeValueAsString(objectNode);
File file = getFile(rClass, contextName, dateString);
synchronized (file) {
try(FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)){
out.println(json);
out.flush();
} catch( IOException e ){
throw e;
}
}
try {
publishFileToWorkspace(file);
file.delete();
}catch (Exception e) {
logger.error("Error while saving file {} on Workspace", file.getName(), e);
// TODO Use the Social Notification
}
} catch (Exception e) {
logger.error("Error exporting JSON error result", e);
}
logger.trace("-------------------------------------------------------\n\n\n\n\n");
}
protected R mapAndPublish(GR gr) throws Exception {
R r = map(gr);
List<? extends Facet> facets = r.getIdentificationFacets();
for(Facet f : facets){
f.setAdditionalProperty(EXPORTED, EXPORTED_FROM_OLD_GCORE_IS);
}
return createOrUpdate(r);
}
public void export() {
List<GR> all = getAll();
logger.debug("-------------------------------------------------------");
logger.debug("Going to export {} {}s as {}s",
all.size(), grClass.getSimpleName(), rClass.getSimpleName());
logger.debug("-------------------------------------------------------");
List<GR> failed = new ArrayList<>();
for(GR gr : all){
try {
Thread.sleep(300);
mapAndPublish(gr);
}catch (Exception e) {
failed.add(gr);
}
}
notifyFailures(all.size(), failed);
}
}

View File

@ -0,0 +1,134 @@
package org.gcube.informationsystem.exporter.mapper;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.common.resources.gcore.GenericResource.Profile;
import org.gcube.informationsystem.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.impl.entity.facet.SimpleFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl;
import org.gcube.informationsystem.impl.entity.resource.ConfigurationImpl;
import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.entity.facet.SimpleFacet;
import org.gcube.informationsystem.model.entity.facet.SoftwareFacet;
import org.gcube.informationsystem.model.entity.resource.Configuration;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.json.JSONObject;
import org.json.XML;
public class GenericResourceExporter extends GCoreResourceMapper<GenericResource, Configuration> {
public static final String FIXED_VERSION = "1.0.0";
public static final String FULL_BODY = "FULL_BODY";
public GenericResourceExporter(){
super(GenericResource.class, Configuration.class);
}
@Override
protected Configuration map(GenericResource gr) throws Exception {
Profile profile = gr.profile();
UUID uuid = UUID.fromString(gr.id());
boolean readFromIS = false;
Configuration configuration = null;
SoftwareFacet softwareFacet = null;
SimpleFacet simpleFacet = null;
try {
resourceRegistryClient.exists(rClass, uuid);
readFromIS = true;
}catch (ResourceNotFoundException e) {
readFromIS = false;
} catch (ResourceAvailableInAnotherContextException e) {
resourceRegistryPublisher.addResourceToContext(uuid);
Thread.sleep(100);
readFromIS = true;
}
if(readFromIS){
configuration = read(uuid);
//softwareFacet = (SoftwareFacet) configuration.getIdentificationFacets().get(0);
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfs = configuration.getConsistsOf();
for(ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfs){
Facet target = c.getTarget();
if(c instanceof IsIdentifiedBy){
if(target instanceof SoftwareFacet) {
softwareFacet = (SoftwareFacet) target;
continue;
}
}
if(c instanceof ConsistsOf){
if(target instanceof SimpleFacet){
simpleFacet = (SimpleFacet) target;
continue;
}
}
}
}else{
configuration = new ConfigurationImpl();
Header header = new HeaderImpl(uuid);
configuration.setHeader(header);
}
/* ----------------------------------------- */
if(softwareFacet==null){
softwareFacet = new SoftwareFacetImpl();
IsIdentifiedBy<Configuration, SoftwareFacet> identifiedBy =
new IsIdentifiedByImpl<Configuration, SoftwareFacet>(configuration, softwareFacet, null);
configuration.addFacet(identifiedBy);
}
softwareFacet.setGroup(profile.type());
softwareFacet.setName(profile.name());
softwareFacet.setVersion(FIXED_VERSION);
String description = profile.description();
if(description!=null && description.compareTo("")!=0){
softwareFacet.setDescription(getStringAsUTF8(description));
}
/* ----------------------------------------- */
/* ----------------------------------------- */
String xmlBody = profile.bodyAsString();
JSONObject jsonBody = XML.toJSONObject(getStringAsUTF8(xmlBody));
Map<String, Object> map = jsonBody.toMap();
if(simpleFacet==null){
simpleFacet = new SimpleFacetImpl();
configuration.addFacet(simpleFacet);
try {
SimpleFacet testSimpleFacet = new SimpleFacetImpl();
testSimpleFacet.setAdditionalProperties(map);
SimpleFacet created = resourceRegistryPublisher.createFacet(SimpleFacet.class, testSimpleFacet);
resourceRegistryPublisher.deleteFacet(created);
simpleFacet.setAdditionalProperties(map);
}catch (Exception e) {
simpleFacet.setAdditionalProperty(FULL_BODY, getStringAsUTF8(xmlBody));
}
}else{
if(simpleFacet.getAdditionalProperty(FULL_BODY)!=null){
simpleFacet.setAdditionalProperty(FULL_BODY, getStringAsUTF8(xmlBody));
}else{
simpleFacet.setAdditionalProperties(map);
}
}
/* ----------------------------------------- */
return configuration;
}
}

View File

@ -0,0 +1,303 @@
package org.gcube.informationsystem.exporter.mapper;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
import org.gcube.common.resources.gcore.common.Platform;
import org.gcube.common.resources.gcore.utils.Group;
import org.gcube.informationsystem.impl.embedded.HeaderImpl;
import org.gcube.informationsystem.impl.embedded.ValueSchemaImpl;
import org.gcube.informationsystem.impl.entity.facet.AccessPointFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.NetworkingFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.ServiceStateFacetImpl;
import org.gcube.informationsystem.impl.entity.facet.SoftwareFacetImpl;
import org.gcube.informationsystem.impl.entity.resource.EServiceImpl;
import org.gcube.informationsystem.impl.relation.ConsistsOfImpl;
import org.gcube.informationsystem.impl.relation.IsIdentifiedByImpl;
import org.gcube.informationsystem.model.embedded.Header;
import org.gcube.informationsystem.model.embedded.ValueSchema;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.Resource;
import org.gcube.informationsystem.model.entity.facet.AccessPointFacet;
import org.gcube.informationsystem.model.entity.facet.NetworkingFacet;
import org.gcube.informationsystem.model.entity.facet.ServiceStateFacet;
import org.gcube.informationsystem.model.entity.facet.SoftwareFacet;
import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.model.relation.ConsistsOf;
import org.gcube.informationsystem.model.relation.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServiceEndpointExporter extends GCoreResourceMapper<ServiceEndpoint, EService> {
private static Logger logger = LoggerFactory.getLogger(ServiceEndpointExporter.class);
public static final String FIXED_VERSION = "1.0.0";
public static final String PLATFORM = "PLATFORM";
public static final String POSITION = "POSITION";
public static final String GHN_ID = "ghnID";
public static final String USERNAME = "USERNAME";
public static final String PASSWORD = "PASSWORD";
public static final String USERNAME_PASSWORD_SCHEMA_STRING;
//public static final URI USERNAME_PASSWORD_SCHEMA;
public static final String NAME = "NAME";
public static final String ENCRYPTED = "ENCRYPTED";
public static final String VALUE = "VALUE";
public static final String PROPERTY_SCHEMA_STRING;
public static final URI PROPERTY_SCHEMA;
public static final String NOT_URI_ENDPOINT = "NOT_URI_ENDPOINT";
static {
USERNAME_PASSWORD_SCHEMA_STRING = String.format("%s:%s", USERNAME, PASSWORD);
//USERNAME_PASSWORD_SCHEMA = URI.create(USERNAME_PASSWORD_SCHEMA_STRING);
PROPERTY_SCHEMA_STRING = String.format("%s:%s:%s", NAME, ENCRYPTED, VALUE);
PROPERTY_SCHEMA = URI.create(PROPERTY_SCHEMA_STRING);
}
public ServiceEndpointExporter(){
super(ServiceEndpoint.class, EService.class);
}
@Override
protected EService map(ServiceEndpoint gr) throws Exception {
Profile profile = gr.profile();
UUID uuid = UUID.fromString(gr.id());
boolean readFromIS = false;
EService eService = null;
AccessPointFacet[] accessPointFacets = null;
SoftwareFacet softwareFacet = null;
SoftwareFacet platformSoftwareFacet = null;
ServiceStateFacet serviceStateFacet = null;
NetworkingFacet networkingFacet = null;
try {
resourceRegistryClient.exists(rClass, uuid);
readFromIS = true;
}catch (ResourceNotFoundException e) {
readFromIS = false;
}catch (ResourceAvailableInAnotherContextException e) {
resourceRegistryPublisher.addResourceToContext(uuid);
Thread.sleep(100);
readFromIS = true;
}
Group<AccessPoint> accessPoints = profile.accessPoints();
accessPointFacets = new AccessPointFacet[accessPoints.size()];
if(readFromIS){
eService = read(uuid);
//softwareFacet = (SoftwareFacet) eService.getIdentificationFacets().get(0);
List<ConsistsOf<? extends Resource, ? extends Facet>> consistsOfs = eService.getConsistsOf();
for(ConsistsOf<? extends Resource, ? extends Facet> c : consistsOfs){
Facet target = c.getTarget();
if(c instanceof IsIdentifiedBy){
if(target instanceof SoftwareFacet) {
softwareFacet = (SoftwareFacet) target;
continue;
}
} else {
if(target instanceof AccessPointFacet){
try {
Object positionObject = c.getAdditionalProperty(POSITION);
Integer position = Integer.valueOf(positionObject.toString());
if(position!=null){
accessPointFacets[position] = (AccessPointFacet) target;
}
}catch (Exception e) {
// Position is used on relation to match the AccessPoint on ServiceEndpoint
logger.error("No POSITION found", e);
}
continue;
}
if(target instanceof SoftwareFacet){
SoftwareFacet targetSoftwareFacet = (SoftwareFacet) target;
if(targetSoftwareFacet.getGroup().compareTo(PLATFORM)==0){
platformSoftwareFacet = targetSoftwareFacet;
}
continue;
}
if(target instanceof ServiceStateFacet){
serviceStateFacet = (ServiceStateFacet) target;
continue;
}
if(target instanceof NetworkingFacet){
networkingFacet = (NetworkingFacet) target;
continue;
}
}
}
}else{
eService = new EServiceImpl();
Header header = new HeaderImpl(uuid);
eService.setHeader(header);
}
/* ----------------------------------------- */
if(softwareFacet==null){
softwareFacet = new SoftwareFacetImpl();
IsIdentifiedBy<EService, SoftwareFacet> identifiedBy =
new IsIdentifiedByImpl<EService, SoftwareFacet>(eService, softwareFacet, null);
eService.addFacet(identifiedBy);
}
softwareFacet.setGroup(profile.category());
softwareFacet.setName(profile.name());
softwareFacet.setVersion(FIXED_VERSION);
String description = profile.description();
if(description!=null && description.compareTo("")!=0){
softwareFacet.setDescription(getStringAsUTF8(description));
}
/* ----------------------------------------- */
/* ----------------------------------------- */
Platform platform = profile.platform();
if(platformSoftwareFacet==null){
platformSoftwareFacet = new SoftwareFacetImpl();
eService.addFacet(platformSoftwareFacet);
}
platformSoftwareFacet.setGroup(PLATFORM);
platformSoftwareFacet.setName(platform.name());
String platformVersion = String.format("%d.%d.%d-%d",
platform.version(), platform.minorVersion(),
platform.revisionVersion(), platform.buildVersion());
softwareFacet.setVersion(platformVersion);
/* ----------------------------------------- */
/* ----------------------------------------- */
Runtime runTime = profile.runtime();
if(serviceStateFacet==null){
serviceStateFacet = new ServiceStateFacetImpl();
eService.addFacet(serviceStateFacet);
}
serviceStateFacet.setValue(runTime.status());
if(networkingFacet==null){
networkingFacet = new NetworkingFacetImpl();
eService.addFacet(networkingFacet);
}
networkingFacet.setHostName(runTime.hostedOn());
String ghnID = runTime.ghnId();
if(ghnID!=null && ghnID.compareTo("")!=0){
networkingFacet.setAdditionalProperty(GHN_ID, ghnID);
}
/* ----------------------------------------- */
/* ----------------------------------------- */
int i=0;
for(AccessPoint accessPoint : accessPoints){
if(accessPointFacets[i] == null){
accessPointFacets[i] = new AccessPointFacetImpl();
ConsistsOf<EService, AccessPointFacet> consistsOf = new ConsistsOfImpl<EService, AccessPointFacet>(eService, accessPointFacets[i], null);
consistsOf.setAdditionalProperty(POSITION, i);
eService.addFacet(consistsOf);
}
accessPointFacets[i].setEntryName(accessPoint.name());
String address = accessPoint.address();
if(address!=null && address.compareTo("")!=0){
try {
URI uri = URI.create(address);
accessPointFacets[i].setEndpoint(uri);
}catch (IllegalArgumentException e) {
accessPointFacets[i].setAdditionalProperty(NOT_URI_ENDPOINT, address);
}
}
String accessPointDescription = accessPoint.description();
if(accessPointDescription!=null && accessPointDescription.compareTo("")!=0){
accessPointFacets[i].setDescription(getStringAsUTF8(accessPointDescription));
}
/* ---------- */
ValueSchema authorization = new ValueSchemaImpl();
String value = USERNAME_PASSWORD_SCHEMA_STRING;
String schema = USERNAME_PASSWORD_SCHEMA_STRING;
boolean replaceColon = false;
try {
value = value.replace(USERNAME, accessPoint.username());
}catch (NullPointerException e) {
value = value.replace(USERNAME, "");
schema = schema.replace(USERNAME, "");
replaceColon = true;
}
try {
value = value.replace(PASSWORD, accessPoint.password());
}catch (NullPointerException e) {
value = value.replace(PASSWORD, "");
schema = schema.replace(PASSWORD, "");
replaceColon = true;
}
if(replaceColon){
value = value.replace(":", "");
schema = schema.replace(":", "");
}
if(value.compareTo("")!=0){
authorization.setValue(value);
authorization.setSchema(URI.create(schema));
accessPointFacets[i].setAuthorization(authorization);
}
/* ----- */
List<ValueSchema> properties = new ArrayList<>();
for(Property property : accessPoint.properties()){
ValueSchema valueSchema = new ValueSchemaImpl();
String propertyValue = PROPERTY_SCHEMA.toString();
propertyValue = propertyValue.replace(NAME, property.name());
propertyValue = propertyValue.replace(ENCRYPTED, Boolean.toString(property.isEncrypted()));
propertyValue = propertyValue.replace(VALUE, property.value());
valueSchema.setValue(propertyValue);
valueSchema.setSchema(PROPERTY_SCHEMA);
properties.add(valueSchema);
}
accessPointFacets[i].setProperties(properties);
i++;
}
/* ----------------------------------------- */
return eService;
}
}

View File

@ -0,0 +1,23 @@
package org.gcube.informationsystem.exporter.mapper.exception;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
public class CreateException extends ResourceRegistryException {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = 7815403701115692343L;
public CreateException(String message) {
super(message);
}
public CreateException(Throwable cause) {
super(cause);
}
public CreateException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,24 @@
package org.gcube.informationsystem.exporter.mapper.exception;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
public class UpdateException extends ResourceRegistryException {
/**
* Generated Serial Version UID
*/
private static final long serialVersionUID = -1473775004888760485L;
public UpdateException(String message) {
super(message);
}
public UpdateException(Throwable cause) {
super(cause);
}
public UpdateException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,53 @@
package org.gcube.informationsystem.exporter;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.informationsystem.exporter.mapper.GenericResourceExporterTest;
import org.gcube.informationsystem.exporter.mapper.ServiceEndpointExporterTest;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ISExporterPluginTest {
private static Logger logger = LoggerFactory.getLogger(ISExporterPluginTest.class);
@Test
public void testLaunch() throws ObjectNotFound, Exception{
String[] tokens = {
ScopedTest.GCUBE,
ScopedTest.GCUBE_DEVSEC,
ScopedTest.GCUBE_DEVSEC_DEVVRE,
ScopedTest.GCUBE_DEVNEXT,
ScopedTest.GCUBE_DEVNEXT_NEXTNEXT
};
for(String token : tokens){
logger.info("\n\n\n-------------------------------------------------------------------------");
ScopedTest.setContext(token);
ISExporterPlugin isExporterPlugin = new ISExporterPlugin(new ISExporterPluginDeclaration());
isExporterPlugin.launch(null);
logger.info("\n\n\n");
}
}
//@Test
public void delAllExported() throws ObjectNotFound, Exception{
String[] tokens = {
ScopedTest.GCUBE,
ScopedTest.GCUBE_DEVSEC,
ScopedTest.GCUBE_DEVSEC_DEVVRE,
ScopedTest.GCUBE_DEVNEXT,
ScopedTest.GCUBE_DEVNEXT_NEXTNEXT
};
for(String token : tokens){
ScopedTest.setContext(token);
GenericResourceExporterTest genericResourceExporterTest = new GenericResourceExporterTest();
genericResourceExporterTest.del();
ServiceEndpointExporterTest serviceEndpointExporterTest = new ServiceEndpointExporterTest();
serviceEndpointExporterTest.del();
}
}
}

View File

@ -0,0 +1,120 @@
/**
*
*/
package org.gcube.informationsystem.exporter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.gcube.common.authorization.client.Constants;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactorySetter;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactorySetter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ScopedTest {
private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class);
protected static final String PROPERTIES_FILENAME = "token.properties";
private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT";
public static final String GCUBE_DEVNEXT;
private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT";
public static final String GCUBE_DEVNEXT_NEXTNEXT;
public static final String GCUBE_DEVSEC_VARNAME = "GCUBE_DEVSEC";
public static final String GCUBE_DEVSEC;
public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE";
public static final String GCUBE_DEVSEC_DEVVRE;
public static final String GCUBE_VARNAME = "GCUBE";
public static final String GCUBE;
public static final String DEFAULT_TEST_SCOPE;
public static final String ALTERNATIVE_TEST_SCOPE;
protected static final String REGISTRY_PROPERTIES_FILENAME = "registry.properties";
public static final String RESOURCE_REGISTRY_URL_PROPERTY = "RESOURCE_REGISTRY_URL";
public static final String RESOURCE_REGISTRY_URL;
static {
Properties properties = new Properties();
InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME);
GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME);
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
GCUBE = properties.getProperty(GCUBE_VARNAME);
DEFAULT_TEST_SCOPE = GCUBE_DEVSEC;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVSEC_DEVVRE;
properties = new Properties();
input = ScopedTest.class.getClassLoader().getResourceAsStream(REGISTRY_PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
RESOURCE_REGISTRY_URL = properties.getProperty(RESOURCE_REGISTRY_URL_PROPERTY);
if(RESOURCE_REGISTRY_URL!=null){
ResourceRegistryPublisherFactorySetter.forceToURL(RESOURCE_REGISTRY_URL);
ResourceRegistryClientFactorySetter.forceToURL(RESOURCE_REGISTRY_URL);
}
}
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context);
return context;
}
public static void setContext(String token) throws ObjectNotFound, Exception{
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(getCurrentScope(token));
}
@BeforeClass
public static void beforeClass() throws Exception{
setContext(DEFAULT_TEST_SCOPE);
}
@AfterClass
public static void afterClass() throws Exception{
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}
}

View File

@ -0,0 +1,59 @@
package org.gcube.informationsystem.exporter.mapper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import org.gcube.informationsystem.exporter.ScopedTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class GCoreResourceMapperTest extends ScopedTest {
private static Logger logger = LoggerFactory.getLogger(GenericResourceExporterTest.class);
protected File getFile(GCoreResourceMapper<?,?> grm) throws Exception {
String contextName = GCoreResourceMapper.getCurrentContextName();
String dateString = GCoreResourceMapper.getDateString(Calendar.getInstance());
File file = grm.getFile(GenericResourceExporterTest.class, contextName, dateString);
String json = "{}";
synchronized (file) {
try(FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)){
out.println(json);
out.flush();
} catch( IOException e ){
throw e;
}
}
return file;
}
//@Test
public void testWorkspace() throws Exception {
String[] tokens = {
ScopedTest.GCUBE,
ScopedTest.GCUBE_DEVSEC,
ScopedTest.GCUBE_DEVSEC_DEVVRE,
ScopedTest.GCUBE_DEVNEXT,
ScopedTest.GCUBE_DEVNEXT_NEXTNEXT
};
for(String token : tokens){
logger.info("\n\n\n-------------------------------------------------------------------------");
ScopedTest.setContext(token);
GenericResourceExporter gre = new GenericResourceExporter();
File file = getFile(gre);
gre.publishFileToWorkspace(file);
logger.info("\n\n\n");
file.delete();
}
}
}

View File

@ -0,0 +1,111 @@
package org.gcube.informationsystem.exporter.mapper;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.informationsystem.exporter.ScopedTest;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.resource.Configuration;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
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.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GenericResourceExporterTest extends ScopedTest{
private static Logger logger = LoggerFactory.getLogger(GenericResourceExporterTest.class);
@Test
public void export(){
GenericResourceExporter gre = new GenericResourceExporter();
gre.export();
}
public void del() throws ResourceRegistryException{
ResourceRegistryClient client = ResourceRegistryClientFactory.create();
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create();
@SuppressWarnings("unchecked")
List<Configuration> configurations = (List<Configuration>) client.getInstances(Configuration.NAME, false);
List<Configuration> failed = new ArrayList<>();
logger.debug("Going to delete {} {}s",
configurations.size(), Configuration.NAME);
int excluded = 0;
for(Configuration configuration : configurations){
try {
Facet facet = configuration.getIdentificationFacets().get(0);
String string = (String) facet.getAdditionalProperty(GCoreResourceMapper.EXPORTED);
publisher.deleteResource(configuration);
if(string!=null && string.compareTo(GCoreResourceMapper.EXPORTED_FROM_OLD_GCORE_IS)==0){
publisher.deleteResource(configuration);
}else{
excluded++;
}
}catch (Exception e) {
failed.add(configuration);
}
}
logger.debug("{} of {} ({} failures) {}s were deleted. {} {}s were excluded because there was not exported from gCore IS.",
configurations.size()-failed.size()-excluded, configurations.size(), failed.size(), Configuration.NAME, excluded, Configuration.NAME);
}
//@Test
public void investigateSingleResource() throws Exception {
ScopedTest.setContext(ScopedTest.GCUBE);
UUID uuid = UUID.fromString("");
//ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create();
//publisher.deleteResource(uuid);
//ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
SimpleQuery query = ICFactory.queryFor(GenericResource.class)
.addCondition(String.format("$resource/ID/text() eq '%1s'", uuid.toString()));
DiscoveryClient<GenericResource> client = ICFactory.clientFor(GenericResource.class);
List<GenericResource> seList = client.submit(query);
GenericResourceExporter see = new GenericResourceExporter();
see.mapAndPublish(seList.get(0));
}
//@Test
public void addToAllScopes() throws Exception {
UUID uuid = UUID.fromString("");
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create();
String[] tokens = {
ScopedTest.GCUBE,
ScopedTest.GCUBE_DEVSEC,
ScopedTest.GCUBE_DEVSEC_DEVVRE,
ScopedTest.GCUBE_DEVNEXT,
ScopedTest.GCUBE_DEVNEXT_NEXTNEXT
};
for(String token : tokens){
ScopedTest.setContext(token);
publisher.addResourceToContext(uuid);
}
}
}

View File

@ -0,0 +1,86 @@
package org.gcube.informationsystem.exporter.mapper;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.informationsystem.exporter.ScopedTest;
import org.gcube.informationsystem.model.entity.Facet;
import org.gcube.informationsystem.model.entity.resource.EService;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisher;
import org.gcube.informationsystem.resourceregistry.publisher.ResourceRegistryPublisherFactory;
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.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ServiceEndpointExporterTest extends ScopedTest{
private static Logger logger = LoggerFactory.getLogger(ServiceEndpointExporterTest.class);
@Test
public void export(){
ServiceEndpointExporter see = new ServiceEndpointExporter();
see.export();
}
public void del() throws ResourceRegistryException{
ResourceRegistryClient client = ResourceRegistryClientFactory.create();
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create();
@SuppressWarnings("unchecked")
List<EService> eServices = (List<EService>) client.getInstances(EService.NAME, false);
List<EService> failed = new ArrayList<>();
logger.debug("Going to delete {} {}s",
eServices.size(), EService.NAME);
int excluded = 0;
for(EService eService : eServices){
try {
Facet facet = eService.getIdentificationFacets().get(0);
String string = (String) facet.getAdditionalProperty(GCoreResourceMapper.EXPORTED);
if(string!=null && string.compareTo(GCoreResourceMapper.EXPORTED_FROM_OLD_GCORE_IS)==0){
publisher.deleteResource(eService);
}else{
excluded++;
}
}catch (Exception e) {
failed.add(eService);
}
}
logger.debug("{} of {} ({} failures) {}s were deleted. {} {}s were excluded because there was not exported from gCore IS.",
eServices.size()-failed.size()-excluded, eServices.size(), failed.size(), EService.NAME, excluded, EService.NAME);
}
@Test
public void investigateSingleResource() throws Exception {
UUID uuid = UUID.fromString("66a674a2-8319-43b4-86c1-bd092bc7c490");
ResourceRegistryPublisher publisher = ResourceRegistryPublisherFactory.create();
publisher.addResourceToContext(uuid);
//ResourceRegistryClient resourceRegistryClient = ResourceRegistryClientFactory.create();
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format("$resource/ID/text() eq '%1s'", uuid.toString()));
DiscoveryClient<ServiceEndpoint> client = ICFactory.clientFor(ServiceEndpoint.class);
List<ServiceEndpoint> seList = client.submit(query);
ServiceEndpointExporter see = new ServiceEndpointExporter();
see.notifyFailures(seList.size(), seList);
}
}

View File

@ -0,0 +1,9 @@
package org.gcube.informationsystem.resourceregistry.client;
public class ResourceRegistryClientFactorySetter {
public static void forceToURL(String url) {
ResourceRegistryClientFactory.forceToURL(url);
}
}

View File

@ -0,0 +1,9 @@
package org.gcube.informationsystem.resourceregistry.publisher;
public class ResourceRegistryPublisherFactorySetter {
public static void forceToURL(String url) {
ResourceRegistryPublisherFactory.forceToURL(url);
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
</encoder>
</appender>
<!--
<appender name="EXPORTER_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>exporter_error.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n
</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>exporter_error.%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
-->
<logger name="org.gcube" level="ERROR" />
<logger name="org.gcube.informationsystem" level="DEBUG" />
<logger name="org.gcube.informationsystem.exporter" level="TRACE" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
<!--
<logger name="org.gcube.informationsystem.exporter" level="INFO">
<appender-ref ref="EXPORTER_FILE" />
</logger>
-->
</configuration>

View File

@ -0,0 +1 @@
RESOURCE_REGISTRY_URL=http://pc-frosini.isti.cnr.it:8080/resource-registry