Initial import.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/resource-registry-database-creator@157994 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Luca Frosini 2017-10-30 13:31:46 +00:00
parent 5e32a895a4
commit 2394ece203
7 changed files with 465 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.8">
<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>resource-registry-database-creator</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>

126
pom.xml Normal file
View File

@ -0,0 +1,126 @@
<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>resource-registry-database-creator</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Resource Registry Database Creator</name>
<description>Create Database and all Types inside it</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.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.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>parthenos-entities</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test Dependency -->
<dependency>
<groupId>org.gcube.information-system</groupId>
<artifactId>resource-registry</artifactId>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
<scope>test</scope>
</dependency>
<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-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>servicearchive</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,125 @@
/**
*
*/
package org.gcube.informationsystem.model.discovery;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.gcube.informationsystem.impl.utils.discovery.ReflectionUtility;
import org.gcube.informationsystem.model.ISManageable;
import org.gcube.informationsystem.model.annotations.ISProperty;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ISMDiscovery<ISM extends ISManageable> {
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
protected final Graph<Class<ISM>, DefaultEdge> graph;
public Graph<Class<ISM>, DefaultEdge> getGraph() {
return graph;
}
protected final Class<ISM> root;
protected final Set<Class<ISM>> visited;
public ISMDiscovery(Class<ISM> root) {
this.root = root;
this.graph = new DefaultDirectedGraph<>(DefaultEdge.class);
this.visited = new HashSet<>();
this.graph.addVertex(root);
this.visited.add(root);
}
protected void addISM(Class<ISM> clz, Class<ISM> parent) {
graph.addEdge(parent, clz);
visited.add(clz);
logger.debug("Adding {} as children of {}", clz, parent);
}
protected void analizeISM(Class<ISM> clz) {
logger.trace(" --- Analizyng {}", clz.getCanonicalName());
if (visited.contains(clz)) {
logger.trace(" --------- discarding {} because was already managed", clz);
return;
} else {
graph.addVertex(clz);
Class<?>[] interfaces = clz.getInterfaces();
for (Class<?> interfaceClass : interfaces) {
if (!root.isAssignableFrom(interfaceClass)) {
logger.trace(" --------- discarding {} because is not a {}", interfaceClass,
root.getClass().getSimpleName());
continue;
}
@SuppressWarnings("unchecked")
Class<ISM> parent = (Class<ISM>) interfaceClass;
if (!visited.contains(parent)) {
analizeISM(parent);
}
addISM(clz, parent);
}
if(root==Embedded.class){
for (Method m : clz.getDeclaredMethods()){
m.setAccessible(true);
if(m.isAnnotationPresent(ISProperty.class)){
if(root.isAssignableFrom(m.getReturnType())){
@SuppressWarnings("unchecked")
Class<ISM> type = (Class<ISM>) m.getReturnType();
analizeISM(type);
addISM(clz, type);
}
}
}
}
logger.trace("{}", (Object[]) interfaces);
}
}
public void discover() throws Exception {
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(root.getPackage());
for (Class<?> clz : classes) {
logger.trace("Analyzing {}", clz);
if (!clz.isInterface()) {
logger.trace("Discarding {} that is not an interface", clz);
continue;
}
if (root.isAssignableFrom(clz)) {
@SuppressWarnings("unchecked")
Class<ISM> ism = (Class<ISM>) clz;
analizeISM(ism);
}
}
}
}

View File

@ -0,0 +1,43 @@
package org.gcube.informationsystem.model.discovery;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ISMDiscoveryTest {
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
@Test
public void testEmbeddedDiscovery() throws Exception{
ISMDiscovery<Embedded> embeddedDiscovery = new ISMDiscovery<>(Embedded.class);
embeddedDiscovery.discover();
}
@Test
public void testEntityDiscovery() throws Exception{
ISMDiscovery<Entity> entityDiscovery = new ISMDiscovery<>(Entity.class);
entityDiscovery.discover();
BreadthFirstIterator<Class<Entity>, DefaultEdge> breadthFirstIterator = new BreadthFirstIterator<Class<Entity>, DefaultEdge>(entityDiscovery.getGraph());
while(breadthFirstIterator.hasNext()){
Class<Entity> entity = breadthFirstIterator.next();
logger.info("Going to create : {}", entity);
}
}
@Test
public void testRelationDiscovery() throws Exception{
@SuppressWarnings("rawtypes")
ISMDiscovery<Relation> relationDiscovery = new ISMDiscovery<>(Relation.class);
relationDiscovery.discover();
}
}

View File

@ -0,0 +1,93 @@
package org.gcube.informationsystem.resourceregistry;
import java.util.ArrayList;
import java.util.List;
import org.gcube.informationsystem.impl.utils.discovery.SchemaAction;
import org.gcube.informationsystem.model.ISManageable;
import org.gcube.informationsystem.model.discovery.ISMDiscovery;
import org.gcube.informationsystem.model.embedded.Embedded;
import org.gcube.informationsystem.model.embedded.ValueSchema;
import org.gcube.informationsystem.model.entity.Entity;
import org.gcube.informationsystem.model.relation.Relation;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.schema.SchemaNotFoundException;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseIntializator;
import org.gcube.informationsystem.resourceregistry.dbinitialization.SchemaActionImpl;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.traverse.BreadthFirstIterator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DataBaseCreator {
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
@SuppressWarnings({ "unchecked" })
@Test
public <ISM extends ISManageable> void createDatabase() throws Exception{
DatabaseIntializator.initGraphDB();
List<Class<ISM>> isManageables= new ArrayList<>();
isManageables.add((Class<ISM>) Embedded.class);
isManageables.add((Class<ISM>) Entity.class);
isManageables.add((Class<ISM>) Relation.class);
SchemaAction schemaAction = new SchemaActionImpl();
for(Class<ISM> ism : isManageables){
ISMDiscovery<ISM> ismDiscovery = new ISMDiscovery<>(ism);
ismDiscovery.discover();
BreadthFirstIterator<Class<ISM>, DefaultEdge> breadthFirstIterator = new BreadthFirstIterator<>(ismDiscovery.getGraph());
List<Class<ISM>> toRetry = new ArrayList<>();
while(breadthFirstIterator.hasNext()){
Class<ISM> clz = breadthFirstIterator.next();
manageClass(schemaAction, ism, clz, toRetry);
List<Class<ISM>> nested = new ArrayList<>();
for(Class<ISM> c : toRetry){
manageClass(schemaAction, ism, c, nested);
}
toRetry = nested;
if(clz==Embedded.class){
schemaAction.manageEmbeddedClass(ValueSchema.class);
}
}
for(Class<ISM> c : toRetry){
manageClass(schemaAction, ism, c, null);
}
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
protected <ISM extends ISManageable> void manageClass(SchemaAction schemaAction, Class<ISM> ism, Class<ISM> clz, List<Class<ISM>> toRetry) throws Exception{
try {
if(ism==Embedded.class){
schemaAction.manageEmbeddedClass((Class<Embedded>) clz);
}
if(ism==Entity.class){
schemaAction.manageEntityClass((Class<Entity>) clz);
}
if(ism==Relation.class){
schemaAction.manageRelationClass((Class<Relation>) clz);
}
} catch (SchemaNotFoundException e) {
if(toRetry!=null){
toRetry.add(clz);
}
} catch (SchemaAlreadyPresentException e) {
logger.error("{}", e.getMessage());
}catch (Exception e) {
throw e;
}
}
}

View File

@ -0,0 +1,19 @@
<?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>
<logger name="org.gcube" level="INFO" />
<logger name="org.gcube.informationsystem" level="DEBUG" />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>