diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java index 44ed3da..fe21e0d 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/contexts/entities/ContextManagement.java @@ -1,9 +1,7 @@ package org.gcube.informationsystem.resourceregistry.contexts.entities; -import java.io.IOException; import java.util.HashMap; import java.util.Iterator; -import java.util.List; import java.util.Map; import java.util.UUID; @@ -19,7 +17,6 @@ import org.gcube.informationsystem.contexts.reference.relations.IsParentOf; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.relations.Relation; import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; -import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; import org.gcube.informationsystem.resourceregistry.api.exceptions.AlreadyPresentException; import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException; import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException; @@ -63,26 +60,9 @@ public class ContextManagement extends EntityElementManagement renew() throws ResourceRegistryException { - String contextsJsonString = allFromServer(false); - List contexts = null; - try { - contexts = ElementMapper.unmarshalList(contextsJsonString); - } catch (IOException e) { - logger.error("Unable to read contexts from DB", e); - } - return contexts; - } - }; - public ContextManagement() { super(AccessType.CONTEXT); init(); - ContextCache contextCache = ContextCache.getInstance(); - contextCache.setContextCacheRenewal(contextCacheRenewal); } public ContextManagement(ODatabaseDocument oDatabaseDocument) throws ResourceRegistryException { diff --git a/src/main/java/org/gcube/informationsystem/resourceregistry/dbinitialization/DatabaseEnvironment.java b/src/main/java/org/gcube/informationsystem/resourceregistry/dbinitialization/DatabaseEnvironment.java index 40e7031..9f15cf0 100644 --- a/src/main/java/org/gcube/informationsystem/resourceregistry/dbinitialization/DatabaseEnvironment.java +++ b/src/main/java/org/gcube/informationsystem/resourceregistry/dbinitialization/DatabaseEnvironment.java @@ -1,6 +1,7 @@ package org.gcube.informationsystem.resourceregistry.dbinitialization; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.security.Key; @@ -17,13 +18,18 @@ import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.entities.EntityElement; import org.gcube.informationsystem.base.reference.properties.PropertyElement; import org.gcube.informationsystem.base.reference.relations.RelationElement; +import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.model.reference.properties.Header; import org.gcube.informationsystem.model.reference.properties.Property; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCache; +import org.gcube.informationsystem.resourceregistry.api.contexts.ContextCacheRenewal; +import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; +import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement; import org.gcube.informationsystem.resourceregistry.contexts.security.AdminSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.ContextSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.QueryTemplatesSecurityContext; -import org.gcube.informationsystem.resourceregistry.contexts.security.TypeSecurityContext; import org.gcube.informationsystem.resourceregistry.contexts.security.SecurityContext.PermissionMode; +import org.gcube.informationsystem.resourceregistry.contexts.security.TypeSecurityContext; import org.gcube.informationsystem.resourceregistry.instances.base.ElementManagement; import org.gcube.informationsystem.resourceregistry.types.properties.PropertyTypeDefinitionManagement; import org.gcube.informationsystem.types.TypeMapper; @@ -37,6 +43,7 @@ import org.gcube.informationsystem.types.reference.properties.PropertyType; import org.gcube.informationsystem.types.reference.relations.ConsistsOfType; import org.gcube.informationsystem.types.reference.relations.IsRelatedToType; import org.gcube.informationsystem.types.reference.relations.RelationType; +import org.gcube.informationsystem.utils.ElementMapper; import org.gcube.informationsystem.utils.discovery.ElementSpecilizationDiscovery; import org.gcube.informationsystem.utils.discovery.RegistrationProvider; import org.gcube.informationsystem.utils.discovery.SchemaAction; @@ -253,6 +260,8 @@ public class DatabaseEnvironment { KEY = initDbKey(properties); + initContextCacheRenewal(); + } protected static Key initDbKey(Properties properties) { @@ -338,6 +347,26 @@ public class DatabaseEnvironment { } } + public static void initContextCacheRenewal() { + ContextCacheRenewal contextCacheRenewal = new ContextCacheRenewal() { + + @Override + public List renew() throws ResourceRegistryException { + ContextManagement contextManagement = new ContextManagement(); + String contextsJsonString = contextManagement.allFromServer(false); + List contexts = null; + try { + contexts = ElementMapper.unmarshalList(contextsJsonString); + } catch (IOException e) { + logger.error("Unable to read contexts from DB", e); + } + return contexts; + } + }; + ContextCache.getInstance().setContextCacheRenewal(contextCacheRenewal); + } + + public static Key getDatabaseKey() { return KEY; } diff --git a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java index 8be35d9..85a1892 100644 --- a/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java +++ b/src/test/java/org/gcube/informationsystem/resourceregistry/instances/multicontext/AddToContextTest.java @@ -303,4 +303,17 @@ public class AddToContextTest extends MultiContextTest { } } + +// @Test +// public void testAddToContext() throws Exception { +// ContextCache contextCache = ContextCache.getInstance(); +// HostingNode hostingNode = new HostingNodeImpl(); +// Header header = new HeaderImpl(UUID.fromString("a87bb07e-5320-4fd8-a48d-bf3cc55756c4")); +// hostingNode.setHeader(header); +// ResourceManagement resourceManagement = ERManagementTest.getResourceManagement(hostingNode); +// resourceManagement.setDryRun(false); +// UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName("/gcube/devsec").getUUID(); +// resourceManagement.addToContext(contextUUID); +// } + }