Reorganized utilities and their usage
This commit is contained in:
parent
4b18898fd0
commit
010e62f4fa
|
@ -52,7 +52,7 @@ public final class ContextImpl extends EntityElementImpl implements Context {
|
|||
this();
|
||||
this.name = name;
|
||||
if(uuid == null){
|
||||
uuid = UUIDManager.getInstance().generateValidRandomUUID();
|
||||
uuid = UUIDManager.getInstance().generateValidUUID();
|
||||
}
|
||||
this.uuid = uuid;
|
||||
this.metadata = new MetadataImpl();
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class JsonUtility {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(JsonUtility.class);
|
||||
|
||||
public static JsonNode getJsonNode(String json) throws JsonProcessingException, IOException {
|
||||
if(json==null || json.compareTo("")==0){
|
||||
return null;
|
||||
}
|
||||
logger.trace("Trying to get Jsonnode from {}", json);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode jsonNode = mapper.readTree(json);
|
||||
return jsonNode;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class TypeUtility {
|
||||
|
||||
public static String getTypeName(JsonNode jsonNode){
|
||||
return jsonNode.get(Element.CLASS_PROPERTY).asText();
|
||||
}
|
||||
|
||||
public static String getTypeName(String json) throws JsonProcessingException, IOException{
|
||||
JsonNode jsonNode = JsonUtility.getJsonNode(json);
|
||||
return getTypeName(jsonNode);
|
||||
}
|
||||
|
||||
public static String getTypeName(Element element){
|
||||
return getTypeName(element.getClass());
|
||||
}
|
||||
|
||||
public static <E extends Element> String getTypeName(Class<E> clz){
|
||||
return TypeMapper.getType(clz);
|
||||
}
|
||||
|
||||
}
|
|
@ -56,18 +56,26 @@ public final class UUIDManager {
|
|||
return new TreeSet<>(RESERVED_UUID);
|
||||
}
|
||||
|
||||
public UUID generateValidRandomUUID() {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
return validateUUID(uuid);
|
||||
public UUID generateValidUUID() {
|
||||
return generateValidUUID(null);
|
||||
}
|
||||
|
||||
public UUID validateUUID(UUID uuid) {
|
||||
UUID ret = uuid;
|
||||
while(RESERVED_UUID.contains(ret)) {
|
||||
ret = UUID.randomUUID();
|
||||
public UUID generateValidUUID(UUID uuid) {
|
||||
uuid = uuid==null ? UUID.randomUUID() : uuid;
|
||||
while(RESERVED_UUID.contains(uuid)) {
|
||||
uuid = UUID.randomUUID();
|
||||
}
|
||||
return ret;
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public UUID validateUUID(UUID uuid) throws Exception {
|
||||
if(RESERVED_UUID.contains(uuid)) {
|
||||
throw new Exception(uuid.toString() + " UUID is reserved. All the following UUID are reserved " + getAllReservedUUIDAsStrings().toString());
|
||||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class UUIDUtility {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(UUIDUtility.class);
|
||||
|
||||
public static UUID getUUID(JsonNode jsonNode){
|
||||
if(jsonNode.has(IdentifiableElement.UUID_PROPERTY)) {
|
||||
return UUID.fromString(jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static UUID getUUID(String json) throws JsonProcessingException, IOException {
|
||||
logger.trace("Trying to get UUID of {} ", json);
|
||||
JsonNode jsonNode = JsonUtility.getJsonNode(json);
|
||||
return getUUID(jsonNode);
|
||||
}
|
||||
|
||||
public static String getUUIDAsString(JsonNode jsonNode){
|
||||
return getUUID(jsonNode).toString();
|
||||
}
|
||||
|
||||
public static String getUUIDAsString(String json) throws JsonProcessingException, IOException{
|
||||
return getUUID(json).toString();
|
||||
}
|
||||
|
||||
public static UUID fromString(String uuidString) throws Exception {
|
||||
UUID uuid = UUID.fromString(uuidString);
|
||||
UUIDManager.getInstance().validateUUID(uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.utils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.informationsystem.base.reference.IdentifiableElement;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Utility {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(Utility.class);
|
||||
|
||||
public static UUID getUUIDFromJsonNode(JsonNode jsonNode){
|
||||
UUID uuid = UUID.fromString(jsonNode.get(IdentifiableElement.UUID_PROPERTY).asText());
|
||||
logger.trace("UUID got from {} is : {} ", jsonNode.toString(), uuid);
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public static UUID getUUIDFromJSONString(String json) throws JsonProcessingException, IOException {
|
||||
logger.trace("Trying to get UUID of {} ", json);
|
||||
JsonNode jsonNode = getJSONNode(json);
|
||||
return getUUIDFromJsonNode(jsonNode);
|
||||
}
|
||||
|
||||
public static JsonNode getJSONNode(String json) throws JsonProcessingException, IOException {
|
||||
if(json==null || json.compareTo("")==0){
|
||||
return null;
|
||||
}
|
||||
logger.trace("Trying to get Jsonnode from {}", json);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode jsonNode = mapper.readTree(json);
|
||||
return jsonNode;
|
||||
}
|
||||
|
||||
}
|
|
@ -42,18 +42,18 @@ public class UUIDManagerTest {
|
|||
|
||||
Set<UUID> allReservedUUID = uuidManager.getAllReservedUUID();
|
||||
|
||||
UUID uuid = uuidManager.generateValidRandomUUID();
|
||||
UUID uuid = uuidManager.generateValidUUID();
|
||||
|
||||
Assert.assertTrue(!allReservedUUID.contains(uuid));
|
||||
|
||||
uuid = UUID.randomUUID();
|
||||
|
||||
uuid = uuidManager.validateUUID(uuid);
|
||||
uuid = uuidManager.generateValidUUID(uuid);
|
||||
|
||||
Assert.assertTrue(!allReservedUUID.contains(uuid));
|
||||
|
||||
for(UUID reservedUUID : allReservedUUID) {
|
||||
uuid = uuidManager.validateUUID(reservedUUID);
|
||||
uuid = uuidManager.generateValidUUID(reservedUUID);
|
||||
Assert.assertTrue(reservedUUID.compareTo(uuid)!=0);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue