Moved Type discovery utility in is-model
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/information-system/resource-registry@133179 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
00489f36d2
commit
b8ea84e731
|
@ -6,6 +6,7 @@ package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.impl.utils.discovery.ERDiscovery;
|
||||||
import org.gcube.informationsystem.model.embedded.Embedded;
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
||||||
import org.gcube.informationsystem.model.entity.Entity;
|
import org.gcube.informationsystem.model.entity.Entity;
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
import org.gcube.informationsystem.model.relation.Relation;
|
||||||
|
@ -106,13 +107,15 @@ public class DatabaseIntializator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createEntitiesAndRelations() throws Exception{
|
public static void createEntitiesAndRelations() throws Exception{
|
||||||
SchemaInitializator.addPackage(Embedded.class.getPackage());
|
ERDiscovery.addPackage(Embedded.class.getPackage());
|
||||||
SchemaInitializator.addPackage(Entity.class.getPackage());
|
ERDiscovery.addPackage(Entity.class.getPackage());
|
||||||
SchemaInitializator.addPackage(Relation.class.getPackage());
|
ERDiscovery.addPackage(Relation.class.getPackage());
|
||||||
for(Package p : packages){
|
ERDiscovery erDiscovery = new ERDiscovery();
|
||||||
SchemaInitializator.addPackage(p);
|
erDiscovery.discoverERTypes();
|
||||||
}
|
|
||||||
SchemaInitializator.createTypes();
|
EntityRegistrationAction entityRegistrationAction = new EntityRegistrationAction();
|
||||||
|
erDiscovery.manageDiscoveredERTypes(entityRegistrationAction);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.impl.utils.discovery.ERAction;
|
||||||
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
||||||
|
import org.gcube.informationsystem.model.entity.Entity;
|
||||||
|
import org.gcube.informationsystem.model.entity.Facet;
|
||||||
|
import org.gcube.informationsystem.model.entity.Resource;
|
||||||
|
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
||||||
|
import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
||||||
|
import org.gcube.informationsystem.model.relation.Relation;
|
||||||
|
import org.gcube.informationsystem.resourceregistry.resources.impl.SchemaManagementImpl;
|
||||||
|
import org.gcube.informationsystem.types.TypeBinder;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class EntityRegistrationAction implements ERAction {
|
||||||
|
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(EntityRegistrationAction.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <R extends Relation<? extends Entity, ? extends Entity>> void manageRelationClass(
|
||||||
|
Class<R> r) throws Exception {
|
||||||
|
try{
|
||||||
|
String json = TypeBinder.serializeType(r);
|
||||||
|
logger.trace(json);
|
||||||
|
if (ConsistsOf.class.isAssignableFrom(r)) {
|
||||||
|
new SchemaManagementImpl().registerConsistOfSchema(json);
|
||||||
|
} else if(IsRelatedTo.class.isAssignableFrom(r)){
|
||||||
|
new SchemaManagementImpl().registerRelatedToSchema(json);
|
||||||
|
} else {
|
||||||
|
new SchemaManagementImpl().registerRelationSchema(json);
|
||||||
|
}
|
||||||
|
} catch(Exception ex){
|
||||||
|
logger.error("Error creating schema for {} type {} : {}", Relation.NAME, r.getClass().getSimpleName(), ex.getMessage());
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends Entity> void manageEntityClass(Class<E> e) throws Exception {
|
||||||
|
try{
|
||||||
|
String json = TypeBinder.serializeType(e);
|
||||||
|
logger.trace(json);
|
||||||
|
if (Facet.class.isAssignableFrom(e)) {
|
||||||
|
new SchemaManagementImpl().registerFacetSchema(json);
|
||||||
|
} else if(Resource.class.isAssignableFrom(e)){
|
||||||
|
new SchemaManagementImpl().registerResourceSchema(json);
|
||||||
|
} else {
|
||||||
|
new SchemaManagementImpl().registerEntitySchema(json);
|
||||||
|
}
|
||||||
|
} catch(Exception ex){
|
||||||
|
logger.error("Error creating schema for {} type {} : {}", Entity.NAME, e.getClass().getSimpleName(), ex.getMessage());
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E extends Embedded> void manageEmbeddedClass(Class<E> e) throws Exception {
|
||||||
|
if(e==Embedded.class){
|
||||||
|
logger.trace("Discarding {} because is just a convenient interface", e);
|
||||||
|
return;
|
||||||
|
}else{
|
||||||
|
try{
|
||||||
|
String json = TypeBinder.serializeType(e);
|
||||||
|
logger.trace(json);
|
||||||
|
new SchemaManagementImpl().registerEmbeddedTypeSchema(json);
|
||||||
|
} catch(Exception ex){
|
||||||
|
logger.error("Error creating schema for {} type {} : {}", Embedded.NAME, e.getClass().getSimpleName(), ex.getMessage());
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,199 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.net.JarURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.URLConnection;
|
|
||||||
import java.net.URLDecoder;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.jar.JarEntry;
|
|
||||||
import java.util.jar.JarFile;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Got from
|
|
||||||
* http://stackoverflow.com/questions/520328/can-you-find-all-classes-in-a-package-using-reflection#answer-22462785
|
|
||||||
*
|
|
||||||
* The method first gets the current ClassLoader. It then fetches all resources
|
|
||||||
* that contain said package and iterates of these URLs. It then creates a
|
|
||||||
* URLConnection and determines what type of URL we have. It can either be a
|
|
||||||
* directory (FileURLConnection) or a directory inside a jar or zip file
|
|
||||||
* (JarURLConnection). Depending on what type of connection we have two
|
|
||||||
* different methods will be called.
|
|
||||||
*
|
|
||||||
* First lets see what happens if it is a FileURLConnection.
|
|
||||||
*
|
|
||||||
* It first checks if the passed File exists and is a directory. If that's the
|
|
||||||
* case it checks if it is a class file. If so a Class object will be created
|
|
||||||
* and put in the List. If it is not a class file but is a directory, we
|
|
||||||
* simply iterate into it and do the same thing. All other cases/files will be
|
|
||||||
* ignored.
|
|
||||||
*
|
|
||||||
* If the URLConnection is a JarURLConnection the other private helper method
|
|
||||||
* will be called. This method iterates over all Entries in the zip/jar
|
|
||||||
* archive. If one entry is a class file and is inside of the package a Class
|
|
||||||
* object will be created and stored in the ArrayList.
|
|
||||||
*
|
|
||||||
* After all resources have been parsed it (the main method) returns the
|
|
||||||
* ArrayList containing all classes in the given package, that the current
|
|
||||||
* ClassLoader knows about.
|
|
||||||
*
|
|
||||||
* If the process fails at any point a ClassNotFoundException will be thrown
|
|
||||||
* containing detailed information about the exact cause.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class ReflectionUtility {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Private helper method
|
|
||||||
*
|
|
||||||
* @param directory
|
|
||||||
* The directory to start with
|
|
||||||
* @param pckgname
|
|
||||||
* The package name to search for. Will be needed for getting the
|
|
||||||
* Class object.
|
|
||||||
* @param classes
|
|
||||||
* if a file isn't loaded but still is in the directory
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
*/
|
|
||||||
private static void checkDirectory(File directory, String pckgname,
|
|
||||||
List<Class<?>> classes) throws ClassNotFoundException {
|
|
||||||
File tmpDirectory;
|
|
||||||
|
|
||||||
if (directory.exists() && directory.isDirectory()) {
|
|
||||||
final String[] files = directory.list();
|
|
||||||
|
|
||||||
for (final String file : files) {
|
|
||||||
if (file.endsWith(".class")) {
|
|
||||||
try {
|
|
||||||
classes.add(Class.forName(pckgname + '.'
|
|
||||||
+ file.substring(0, file.length() - 6)));
|
|
||||||
} catch (final NoClassDefFoundError e) {
|
|
||||||
// do nothing. this class hasn't been found by the
|
|
||||||
// loader, and we don't care.
|
|
||||||
}
|
|
||||||
} else if ((tmpDirectory = new File(directory, file))
|
|
||||||
.isDirectory()) {
|
|
||||||
checkDirectory(tmpDirectory, pckgname + "." + file, classes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Private helper method.
|
|
||||||
*
|
|
||||||
* @param connection
|
|
||||||
* the connection to the jar
|
|
||||||
* @param pckgname
|
|
||||||
* the package name to search for
|
|
||||||
* @param classes
|
|
||||||
* the current ArrayList of all classes. This method will simply
|
|
||||||
* add new classes.
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* if a file isn't loaded but still is in the jar file
|
|
||||||
* @throws IOException
|
|
||||||
* if it can't correctly read from the jar file.
|
|
||||||
*/
|
|
||||||
private static void checkJarFile(JarURLConnection connection,
|
|
||||||
String pckgname, List<Class<?>> classes)
|
|
||||||
throws ClassNotFoundException, IOException {
|
|
||||||
final JarFile jarFile = connection.getJarFile();
|
|
||||||
final Enumeration<JarEntry> entries = jarFile.entries();
|
|
||||||
String name;
|
|
||||||
|
|
||||||
for (JarEntry jarEntry = null; entries.hasMoreElements()
|
|
||||||
&& ((jarEntry = entries.nextElement()) != null);) {
|
|
||||||
name = jarEntry.getName();
|
|
||||||
|
|
||||||
if (name.contains(".class")) {
|
|
||||||
name = name.substring(0, name.length() - 6).replace('/', '.');
|
|
||||||
|
|
||||||
if (name.contains(pckgname)) {
|
|
||||||
classes.add(Class.forName(name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<Class<?>> getClassesForPackage(Package packageObject)
|
|
||||||
throws ClassNotFoundException {
|
|
||||||
return getClassesForPackage(packageObject.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attempts to list all the classes in the specified package as determined
|
|
||||||
* by the context class loader
|
|
||||||
*
|
|
||||||
* @param pckgname
|
|
||||||
* the package name to search
|
|
||||||
* @return a list of classes that exist within that package
|
|
||||||
* @throws ClassNotFoundException
|
|
||||||
* if something went wrong
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("restriction")
|
|
||||||
public static List<Class<?>> getClassesForPackage(String pckgname)
|
|
||||||
throws ClassNotFoundException {
|
|
||||||
final List<Class<?>> classes = new ArrayList<Class<?>>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
final ClassLoader cld = Thread.currentThread()
|
|
||||||
.getContextClassLoader();
|
|
||||||
|
|
||||||
if (cld == null)
|
|
||||||
throw new ClassNotFoundException("Can't get class loader.");
|
|
||||||
|
|
||||||
final Enumeration<URL> resources = cld.getResources(pckgname
|
|
||||||
.replace('.', '/'));
|
|
||||||
URLConnection connection;
|
|
||||||
|
|
||||||
for (URL url = null; resources.hasMoreElements()
|
|
||||||
&& ((url = resources.nextElement()) != null);) {
|
|
||||||
try {
|
|
||||||
connection = url.openConnection();
|
|
||||||
|
|
||||||
if (connection instanceof JarURLConnection) {
|
|
||||||
checkJarFile((JarURLConnection) connection, pckgname,
|
|
||||||
classes);
|
|
||||||
} else if (connection instanceof sun.net.www.protocol.file.FileURLConnection) {
|
|
||||||
try {
|
|
||||||
checkDirectory(
|
|
||||||
new File(URLDecoder.decode(url.getPath(),
|
|
||||||
"UTF-8")), pckgname, classes);
|
|
||||||
} catch (final UnsupportedEncodingException ex) {
|
|
||||||
throw new ClassNotFoundException(
|
|
||||||
pckgname
|
|
||||||
+ " does not appear to be a valid package (Unsupported encoding)",
|
|
||||||
ex);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
throw new ClassNotFoundException(pckgname + " ("
|
|
||||||
+ url.getPath()
|
|
||||||
+ ") does not appear to be a valid package");
|
|
||||||
} catch (final IOException ioex) {
|
|
||||||
throw new ClassNotFoundException(
|
|
||||||
"IOException was thrown when trying to get all resources for "
|
|
||||||
+ pckgname, ioex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final NullPointerException ex) {
|
|
||||||
throw new ClassNotFoundException(
|
|
||||||
pckgname
|
|
||||||
+ " does not appear to be a valid package (Null pointer exception)",
|
|
||||||
ex);
|
|
||||||
} catch (final IOException ioex) {
|
|
||||||
throw new ClassNotFoundException(
|
|
||||||
"IOException was thrown when trying to get all resources for "
|
|
||||||
+ pckgname, ioex);
|
|
||||||
}
|
|
||||||
|
|
||||||
return classes;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,416 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeMap;
|
|
||||||
|
|
||||||
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.entity.Facet;
|
|
||||||
import org.gcube.informationsystem.model.entity.Resource;
|
|
||||||
import org.gcube.informationsystem.model.relation.ConsistsOf;
|
|
||||||
import org.gcube.informationsystem.model.relation.IsRelatedTo;
|
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.dbinitialization.Tree.Node;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.dbinitialization.Tree.NodeVisitor;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.resources.impl.SchemaManagementImpl;
|
|
||||||
import org.gcube.informationsystem.types.TypeBinder;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
public class SchemaInitializator {
|
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(SchemaInitializator.class);
|
|
||||||
|
|
||||||
private static Set<Package> packages;
|
|
||||||
|
|
||||||
private static final Comparator<Class<Entity>> entityComparator;
|
|
||||||
private static final Comparator<Class<Relation>> relationComparator;
|
|
||||||
private static final Comparator<Class<Embedded>> embeddedComparator;
|
|
||||||
|
|
||||||
|
|
||||||
private static boolean DRY_RUN;
|
|
||||||
|
|
||||||
static {
|
|
||||||
DRY_RUN = false;
|
|
||||||
packages = new HashSet<>();
|
|
||||||
|
|
||||||
entityComparator = new Comparator<Class<Entity>>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(Class<Entity> o1, Class<Entity> o2) {
|
|
||||||
//logger.trace("Comparing {} with {}", o1, o2);
|
|
||||||
return String.valueOf(o1).compareTo(String.valueOf(o2));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
relationComparator = new Comparator<Class<Relation>>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(Class<Relation> o1, Class<Relation> o2) {
|
|
||||||
//logger.trace("Comparing {} with {}", o1, o2);
|
|
||||||
return String.valueOf(o1).compareTo(String.valueOf(o2));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
embeddedComparator = new Comparator<Class<Embedded>>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(Class<Embedded> o1, Class<Embedded> o2) {
|
|
||||||
//logger.trace("Comparing {} with {}", o1, o2);
|
|
||||||
return String.valueOf(o1).compareTo(String.valueOf(o2));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static void addPackage(Package p){
|
|
||||||
packages.add(p);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
protected static void analizeVertex(Map<Class<Entity>,Node<Class<Entity>>> visitedVertex, Class<Entity> clz) {
|
|
||||||
logger.trace(" --- Analizyng Entity {}", clz.getCanonicalName());
|
|
||||||
if(visitedVertex.containsKey(clz)){
|
|
||||||
logger.trace(" --------- discarding {} because was already managed", clz);
|
|
||||||
return;
|
|
||||||
}else{
|
|
||||||
//logger.trace(" --------- Adding {} to {}", clz, );
|
|
||||||
|
|
||||||
Class<?>[] interfaces = clz.getInterfaces();
|
|
||||||
List<Class<Entity>> interfaceList = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
if(!Entity.class.isAssignableFrom(interfaceClass)){
|
|
||||||
logger.trace(" --------- discarding {} because is not a {}", interfaceClass, Entity.class.getSimpleName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<Entity> v = (Class<Entity>) interfaceClass;
|
|
||||||
|
|
||||||
if(visitedVertex.containsKey(v)){
|
|
||||||
Node<Class<Entity>> root = visitedVertex.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Entity>> node = root.addChild(clz);
|
|
||||||
visitedVertex.put(clz, node);
|
|
||||||
break;
|
|
||||||
}else{
|
|
||||||
interfaceList.add((Class<Entity>) interfaceClass);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!visitedVertex.containsKey(clz)){
|
|
||||||
for(Class<Entity> interfaceClass : interfaceList){
|
|
||||||
analizeVertex(visitedVertex, interfaceClass);
|
|
||||||
Class<Entity> v = (Class<Entity>) interfaceClass;
|
|
||||||
Node<Class<Entity>> root = visitedVertex.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Entity>> node = root.addChild(clz);
|
|
||||||
visitedVertex.put(clz, node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("{}", (Object[]) interfaces);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
protected static void analizeEmbedded(Map<Class<Embedded>,Node<Class<Embedded>>> visitedEmbedded, Class<Embedded> clz) {
|
|
||||||
logger.trace(" --- Analizyng Embedded {}", clz.getCanonicalName());
|
|
||||||
if(visitedEmbedded.containsKey(clz)){
|
|
||||||
logger.trace(" --------- discarding {} because was already managed", clz);
|
|
||||||
return;
|
|
||||||
}else{
|
|
||||||
//logger.trace(" --------- Adding {} to {}", clz, );
|
|
||||||
|
|
||||||
Class<?>[] interfaces = clz.getInterfaces();
|
|
||||||
List<Class<Embedded>> interfaceList = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
if(!Embedded.class.isAssignableFrom(interfaceClass)){
|
|
||||||
logger.trace(" --------- discarding {} because is not a {}", interfaceClass, Embedded.class.getSimpleName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Class<Embedded> v = (Class<Embedded>) interfaceClass;
|
|
||||||
|
|
||||||
if(visitedEmbedded.containsKey(v)){
|
|
||||||
Node<Class<Embedded>> root = visitedEmbedded.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Embedded>> node = root.addChild(clz);
|
|
||||||
visitedEmbedded.put(clz, node);
|
|
||||||
break;
|
|
||||||
}else{
|
|
||||||
interfaceList.add((Class<Embedded>) interfaceClass);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!visitedEmbedded.containsKey(clz)){
|
|
||||||
for(Class<Embedded> interfaceClass : interfaceList){
|
|
||||||
analizeEmbedded(visitedEmbedded, interfaceClass);
|
|
||||||
Class<Embedded> v = (Class<Embedded>) interfaceClass;
|
|
||||||
Node<Class<Embedded>> root = visitedEmbedded.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Embedded>> node = root.addChild(clz);
|
|
||||||
visitedEmbedded.put(clz, node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("{}", (Object[]) interfaces);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked"})
|
|
||||||
protected static void analizeEdge(Map<Class<Relation>,Node<Class<Relation>>> visitedEdge, Class<Relation> clz) {
|
|
||||||
logger.trace(" --- Analizyng Relation {}", clz.getCanonicalName());
|
|
||||||
if(visitedEdge.containsKey(clz)){
|
|
||||||
logger.trace(" --------- discarding {} because was already managed", clz);
|
|
||||||
return;
|
|
||||||
}else{
|
|
||||||
|
|
||||||
Class<?>[] interfaces = clz.getInterfaces();
|
|
||||||
List<Class<Relation>> interfaceList = new ArrayList<>();
|
|
||||||
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
if(!Relation.class.isAssignableFrom(interfaceClass)){
|
|
||||||
logger.trace(" --------- discarding {} because is not a {}", interfaceClass, Relation.class.getSimpleName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Class<Relation> v = (Class<Relation>) interfaceClass;
|
|
||||||
|
|
||||||
if(visitedEdge.containsKey(v)){
|
|
||||||
Node<Class<Relation>> root = visitedEdge.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Relation>> node = root.addChild(clz);
|
|
||||||
visitedEdge.put(clz, node);
|
|
||||||
break;
|
|
||||||
}else{
|
|
||||||
interfaceList.add((Class<Relation>) interfaceClass);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!visitedEdge.containsKey(clz)){
|
|
||||||
for(Class<Relation> interfaceClass : interfaceList){
|
|
||||||
analizeEdge(visitedEdge, interfaceClass);
|
|
||||||
Class<Relation> v = (Class<Relation>) interfaceClass;
|
|
||||||
Node<Class<Relation>> root = visitedEdge.get(v);
|
|
||||||
logger.trace(" --------- Adding {} to {}", clz, root);
|
|
||||||
Node<Class<Relation>> node = root.addChild(clz);
|
|
||||||
visitedEdge.put(clz, node);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("{}", (Object[]) interfaces);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected static <T> NodeVisitor<Class<T>> printNodeVisitor(Class<T> t) {
|
|
||||||
return new NodeVisitor<Class<T>>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean visit(final Node<Class<T>> node) {
|
|
||||||
final StringBuilder sb = new StringBuilder();
|
|
||||||
Node<Class<T>> curr = node;
|
|
||||||
do {
|
|
||||||
if (sb.length() > 0) {
|
|
||||||
sb.insert(0, " > ");
|
|
||||||
}
|
|
||||||
sb.insert(0, String.valueOf(curr.getValue()));
|
|
||||||
curr = curr.getParent();
|
|
||||||
} while (curr != null);
|
|
||||||
logger.debug(sb.toString());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static <T> void createEmbedded(final Node<Class<T>> node){
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<Embedded> clz = (Class<Embedded>) node.getValue();
|
|
||||||
|
|
||||||
if(clz==Embedded.class){
|
|
||||||
logger.trace("Discarding {} because is just a convenient interface", clz);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(ValueSchema.class.isAssignableFrom(clz)){
|
|
||||||
logger.trace("Discarding {} because was programmatically already created", clz);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try{
|
|
||||||
String json = TypeBinder.serializeType(clz);
|
|
||||||
logger.trace(json);
|
|
||||||
if(!DRY_RUN){
|
|
||||||
new SchemaManagementImpl().registerEmbeddedTypeSchema(json);
|
|
||||||
}
|
|
||||||
} catch(Exception e){
|
|
||||||
logger.error("error serializing schema", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static <T> void createVertex(final Node<Class<T>> node){
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<Entity> clz = (Class<Entity>) node.getValue();
|
|
||||||
|
|
||||||
try{
|
|
||||||
String json = TypeBinder.serializeType(clz);
|
|
||||||
logger.trace(json);
|
|
||||||
if(!DRY_RUN){
|
|
||||||
if (Facet.class.isAssignableFrom(clz)) {
|
|
||||||
new SchemaManagementImpl().registerFacetSchema(json);
|
|
||||||
} else if(Resource.class.isAssignableFrom(clz)){
|
|
||||||
new SchemaManagementImpl().registerResourceSchema(json);
|
|
||||||
} else {
|
|
||||||
new SchemaManagementImpl().registerEntitySchema(json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(Exception e){
|
|
||||||
logger.error("error serializing schema", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static <T> void createEdge(final Node<Class<T>> node){
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<Relation> clz = (Class<Relation>) node.getValue();
|
|
||||||
|
|
||||||
try{
|
|
||||||
String json = TypeBinder.serializeType(clz);
|
|
||||||
logger.trace(json);
|
|
||||||
if(!DRY_RUN){
|
|
||||||
if (ConsistsOf.class.isAssignableFrom(clz)) {
|
|
||||||
new SchemaManagementImpl().registerConsistOfSchema(json);
|
|
||||||
} else if(IsRelatedTo.class.isAssignableFrom(clz)){
|
|
||||||
new SchemaManagementImpl().registerRelatedToSchema(json);
|
|
||||||
} else {
|
|
||||||
new SchemaManagementImpl().registerRelationSchema(json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(Exception e){
|
|
||||||
logger.error("error serializing schema", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static <T> NodeVisitor<Class<T>> getNodeVisitor(Class<T> t) {
|
|
||||||
return new NodeVisitor<Class<T>>() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean visit(final Node<Class<T>> node) {
|
|
||||||
Class<T> clz = node.getValue();
|
|
||||||
if(Embedded.class.isAssignableFrom(clz)) {
|
|
||||||
createEmbedded(node);
|
|
||||||
}else if (Entity.class.isAssignableFrom(clz)) {
|
|
||||||
createVertex(node);
|
|
||||||
}else if (Relation.class.isAssignableFrom(clz)) {
|
|
||||||
createEdge(node);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked"})
|
|
||||||
public static void createTypes() throws Exception{
|
|
||||||
|
|
||||||
Tree<Class<Embedded>> embeddeds = new Tree<>(embeddedComparator);
|
|
||||||
Tree<Class<Entity>> vertexes = new Tree<>(entityComparator);
|
|
||||||
Tree<Class<Relation>> edges = new Tree<>(relationComparator);
|
|
||||||
|
|
||||||
|
|
||||||
Map<Class<Embedded>, Node<Class<Embedded>>> addedEmbedded = new TreeMap<>(embeddedComparator);
|
|
||||||
Map<Class<Entity>, Node<Class<Entity>>> addedVertex = new TreeMap<>(entityComparator);
|
|
||||||
Map<Class<Relation>, Node<Class<Relation>>> addedEdge = new TreeMap<>(relationComparator);
|
|
||||||
|
|
||||||
|
|
||||||
Node<Class<Embedded>> rootEmbedded = embeddeds.getRootElement();
|
|
||||||
rootEmbedded.setValue(Embedded.class);
|
|
||||||
addedEmbedded.put(Embedded.class, rootEmbedded);
|
|
||||||
|
|
||||||
Node<Class<Entity>> rootVertex = vertexes.getRootElement();
|
|
||||||
rootVertex.setValue(Entity.class);
|
|
||||||
addedVertex.put(Entity.class, rootVertex);
|
|
||||||
|
|
||||||
Node<Class<Relation>> rootEdge = edges.getRootElement();
|
|
||||||
rootEdge.setValue(Relation.class);
|
|
||||||
addedEdge.put(Relation.class, rootEdge);
|
|
||||||
|
|
||||||
|
|
||||||
for (Package p : packages) {
|
|
||||||
logger.trace("Analyzing {}", p);
|
|
||||||
try {
|
|
||||||
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(p);
|
|
||||||
for (Class<?> clz : classes) {
|
|
||||||
//logger.trace("Analyzing {}", clz);
|
|
||||||
if(!clz.isInterface()){
|
|
||||||
logger.trace("Discarding {} that is not an interface", clz);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Embedded.class.isAssignableFrom(clz)) {
|
|
||||||
analizeEmbedded(addedEmbedded, (Class<Embedded>) clz);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Entity.class.isAssignableFrom(clz)) {
|
|
||||||
analizeVertex(addedVertex, (Class<Entity>) clz);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Relation.class.isAssignableFrom(clz)) {
|
|
||||||
analizeEdge(addedEdge, (Class<Relation>) clz);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
logger.error("Error discovering classes inside package {}",
|
|
||||||
p.getName(), e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
try{
|
|
||||||
String json = TypeBinder.serializeType(ValueSchema.class);
|
|
||||||
logger.trace(json);
|
|
||||||
if(!DRY_RUN){
|
|
||||||
new SchemaManagementImpl().registerEmbeddedTypeSchema(json);
|
|
||||||
}
|
|
||||||
} catch(Exception e){
|
|
||||||
logger.error("error serializing schema", e);
|
|
||||||
}
|
|
||||||
createEmbedded(addedEmbedded.get(ValueSchema.class));
|
|
||||||
|
|
||||||
NodeVisitor<Class<Embedded>> embeddedNodeVisitor = getNodeVisitor(Embedded.class);
|
|
||||||
embeddeds.visitNodes(embeddedNodeVisitor);
|
|
||||||
|
|
||||||
NodeVisitor<Class<Entity>> vertexNodeVisitor = getNodeVisitor(Entity.class);
|
|
||||||
vertexes.visitNodes(vertexNodeVisitor);
|
|
||||||
|
|
||||||
NodeVisitor<Class<Relation>> edgeNodeVisitor = getNodeVisitor(Relation.class);
|
|
||||||
edges.visitNodes(edgeNodeVisitor);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,137 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.SortedSet;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*/
|
|
||||||
class Tree<T> {
|
|
||||||
|
|
||||||
private final Node<T> rootElement;
|
|
||||||
|
|
||||||
public void visitNodes(final NodeVisitor<T> visitor) {
|
|
||||||
doVisit(rootElement, visitor);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T> boolean doVisit(final Node<T> node,
|
|
||||||
final NodeVisitor<T> visitor) {
|
|
||||||
boolean result = visitor.visit(node);
|
|
||||||
if (result) {
|
|
||||||
for (final Node<T> subNode : node.children) {
|
|
||||||
if (!doVisit(subNode, visitor)) {
|
|
||||||
result = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface NodeVisitor<T> {
|
|
||||||
|
|
||||||
boolean visit(Node<T> node);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node<T> getRootElement() {
|
|
||||||
return rootElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final class NodeComparator<T> implements Comparator<Node<T>> {
|
|
||||||
|
|
||||||
private final Comparator<T> wrapped;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(final Node<T> o1, final Node<T> o2) {
|
|
||||||
return wrapped.compare(o1.value, o2.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public NodeComparator(final Comparator<T> wrappedComparator) {
|
|
||||||
this.wrapped = wrappedComparator;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Node<T> {
|
|
||||||
|
|
||||||
private final SortedSet<Node<T>> children;
|
|
||||||
|
|
||||||
private final Node<T> parent;
|
|
||||||
|
|
||||||
private T value;
|
|
||||||
|
|
||||||
private final Comparator<?> comparator;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Node(final T value, final Node<T> parent, final Comparator<?> comparator) {
|
|
||||||
this.value = value;
|
|
||||||
this.parent = parent;
|
|
||||||
this.comparator = comparator;
|
|
||||||
children = new TreeSet<Node<T>>(new NodeComparator<T>(
|
|
||||||
(Comparator<T>) comparator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Node<T>> getChildren() {
|
|
||||||
return new ArrayList<Node<T>>(children);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node<T> getParent() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setValue(final T value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Node<T> addChild(final T value) {
|
|
||||||
final Node<T> node = new Node<T>(value, this, comparator);
|
|
||||||
return children.add(node) ? node : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString(){
|
|
||||||
return String.valueOf(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
private static final Comparator NATURAL_ORDER = new Comparator() {
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
|
||||||
public int compare(final Object o1, final Object o2) {
|
|
||||||
return ((Comparable) o1).compareTo(o2);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Comparator<?> comparator;
|
|
||||||
|
|
||||||
public Tree() {
|
|
||||||
this(null, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tree(final Comparator<? super T> comparator) {
|
|
||||||
this(comparator, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tree(final Comparator<? super T> comparator, final T rootValue) {
|
|
||||||
this.comparator = comparator == null ? NATURAL_ORDER : comparator;
|
|
||||||
this.rootElement = new Node<T>(rootValue, null, this.comparator);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Tree(final T rootValue) {
|
|
||||||
this(null, rootValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -3,13 +3,10 @@
|
||||||
*/
|
*/
|
||||||
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
package org.gcube.informationsystem.resourceregistry.dbinitialization;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.impl.utils.discovery.ERDiscovery;
|
||||||
import org.gcube.informationsystem.model.embedded.Embedded;
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
||||||
import org.gcube.informationsystem.model.entity.Entity;
|
import org.gcube.informationsystem.model.entity.Entity;
|
||||||
import org.gcube.informationsystem.model.entity.facet.AccessPointInterfaceFacet;
|
|
||||||
import org.gcube.informationsystem.model.entity.resource.Actor;
|
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
import org.gcube.informationsystem.model.relation.Relation;
|
||||||
import org.gcube.informationsystem.model.relation.consistsof.HasContact;
|
|
||||||
import org.gcube.informationsystem.model.relation.isrelatedto.BelongsTo;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -20,14 +17,16 @@ public class ContextCreation {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws Exception{
|
public void test() throws Exception{
|
||||||
SchemaInitializator.addPackage(Embedded.class.getPackage());
|
ERDiscovery.addPackage(Embedded.class.getPackage());
|
||||||
SchemaInitializator.addPackage(Entity.class.getPackage());
|
ERDiscovery.addPackage(Entity.class.getPackage());
|
||||||
SchemaInitializator.addPackage(Relation.class.getPackage());
|
ERDiscovery.addPackage(Relation.class.getPackage());
|
||||||
SchemaInitializator.addPackage(AccessPointInterfaceFacet.class.getPackage());
|
|
||||||
SchemaInitializator.addPackage(Actor.class.getPackage());
|
ERDiscovery erDiscovery = new ERDiscovery();
|
||||||
SchemaInitializator.addPackage(HasContact.class.getPackage());
|
erDiscovery.discoverERTypes();
|
||||||
SchemaInitializator.addPackage(BelongsTo.class.getPackage());
|
|
||||||
SchemaInitializator.createTypes();
|
|
||||||
|
//EntityRegistrationAction erEntityRegistrationAction = new EntityRegistrationAction();
|
||||||
|
//erDiscovery.manageDiscoveredERTypes(erEntityRegistrationAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
</appender>
|
</appender>
|
||||||
|
|
||||||
|
|
||||||
<logger name="org.gcube" level="TRACE" />
|
<logger name="org.gcube" level="DEBUG" />
|
||||||
|
|
||||||
<root level="WARN">
|
<root level="WARN">
|
||||||
<appender-ref ref="STDOUT" />
|
<appender-ref ref="STDOUT" />
|
||||||
|
|
Loading…
Reference in New Issue