2023-10-26 18:11:50 +02:00
|
|
|
package org.gcube.informationsystem.types.knowledge;
|
|
|
|
|
2023-10-31 15:50:16 +01:00
|
|
|
import java.util.Calendar;
|
2023-10-27 18:11:58 +02:00
|
|
|
import java.util.Collection;
|
2023-10-31 15:50:16 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2023-10-27 18:11:58 +02:00
|
|
|
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2023-10-26 18:11:50 +02:00
|
|
|
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
|
2023-10-27 18:11:58 +02:00
|
|
|
import org.gcube.informationsystem.model.knowledge.TypesDiscoverer;
|
2023-10-26 18:11:50 +02:00
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class TypesKnowledge {
|
|
|
|
|
2023-10-27 18:11:58 +02:00
|
|
|
private static TypesKnowledge instance;
|
2023-10-26 18:11:50 +02:00
|
|
|
|
2023-10-27 18:11:58 +02:00
|
|
|
public synchronized static TypesKnowledge getInstance() {
|
|
|
|
if(instance==null) {
|
|
|
|
instance = new TypesKnowledge();
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:50:16 +01:00
|
|
|
// in millisec
|
|
|
|
public static final long DEFAULT_EXPIRING_TIMEOUT;
|
|
|
|
|
|
|
|
static {
|
|
|
|
DEFAULT_EXPIRING_TIMEOUT = TimeUnit.HOURS.toMillis(6);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:11:58 +02:00
|
|
|
protected boolean initialized;
|
2023-10-31 15:50:16 +01:00
|
|
|
public int expiringTimeout;
|
|
|
|
|
|
|
|
// in millisec used only for logging and debugging
|
|
|
|
protected Calendar creationTime;
|
|
|
|
// in millisec
|
|
|
|
protected Calendar expiringTime;
|
|
|
|
|
2023-10-27 18:11:58 +02:00
|
|
|
protected ModelKnowledge<Type, TypeInformation> modelKnowledge;
|
|
|
|
protected TypesDiscoverer<Type> typesDiscoverer;
|
|
|
|
|
|
|
|
public TypesKnowledge() {
|
|
|
|
initialized = false;
|
2023-10-31 15:50:16 +01:00
|
|
|
expiringTimeout = (int) DEFAULT_EXPIRING_TIMEOUT;
|
2023-10-27 18:11:58 +02:00
|
|
|
modelKnowledge = new ModelKnowledge<>(new TypeInformation());
|
|
|
|
}
|
|
|
|
|
2023-10-31 15:50:16 +01:00
|
|
|
public void setExpiringTimeout(int expiringTimeout) {
|
|
|
|
this.expiringTimeout = expiringTimeout;
|
|
|
|
}
|
|
|
|
|
2023-10-27 18:11:58 +02:00
|
|
|
public TypesDiscoverer<Type> getTypesDiscoverer() {
|
|
|
|
return typesDiscoverer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTypesDiscoverer(TypesDiscoverer<Type> typesDiscoverer) {
|
|
|
|
this.typesDiscoverer = typesDiscoverer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
|
|
|
|
if(!initialized) {
|
|
|
|
discover();
|
2023-10-31 15:50:16 +01:00
|
|
|
}else {
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
if(now.after(expiringTime)) {
|
|
|
|
renew();
|
|
|
|
}
|
2023-10-26 18:11:50 +02:00
|
|
|
}
|
|
|
|
return modelKnowledge;
|
|
|
|
}
|
2023-10-27 18:11:58 +02:00
|
|
|
|
|
|
|
protected synchronized void init(boolean forceReinitialization) {
|
|
|
|
if(typesDiscoverer!=null && (initialized==false || forceReinitialization)) {
|
|
|
|
initialized = false;
|
|
|
|
modelKnowledge = new ModelKnowledge<>(new TypeInformation());
|
|
|
|
AccessType[] modelTypes = AccessType.getModelTypes();
|
|
|
|
for(AccessType modelType : modelTypes) {
|
|
|
|
Collection<Type> types = typesDiscoverer.discover(modelType);
|
|
|
|
modelKnowledge.addAllType(types);
|
|
|
|
}
|
|
|
|
initialized = true;
|
2023-10-31 15:50:16 +01:00
|
|
|
this.creationTime = Calendar.getInstance();
|
|
|
|
this.expiringTime = Calendar.getInstance();
|
|
|
|
this.expiringTime.setTimeInMillis(creationTime.getTimeInMillis());
|
|
|
|
this.expiringTime.add(Calendar.MILLISECOND, -1);
|
|
|
|
this.expiringTime.add(Calendar.MILLISECOND, expiringTimeout);
|
2023-10-27 18:11:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method do nothing if TypesDiscoverer
|
|
|
|
* was not set.
|
|
|
|
* Otherwise initialized the ModelKnowledge
|
|
|
|
* if it was not already initialized.
|
|
|
|
* To enforce rediscovery use renew method.
|
|
|
|
*/
|
|
|
|
public void discover() {
|
|
|
|
init(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Force reinitialization of
|
|
|
|
*/
|
|
|
|
public void renew() {
|
|
|
|
init(true);
|
|
|
|
}
|
2023-10-26 18:11:50 +02:00
|
|
|
}
|